Skip to content

Farok-ahmed/react-crud-app

Repository files navigation

React CRUD Application Documentation

Overview

This application is a simple CRUD (Create, Read, Update, Delete) task manager built with React. It allows users to add, view, edit, and delete tasks. Each task has an id, name, email, and phone number associated with it.

Components

The application is composed of three main components:

  1. CrudManager
  2. CrudForm
  3. CrudList

CrudManager Component

File: CrudManager.jsx

This is the main component that orchestrates the entire application.

import React, { useState } from 'react';
import CrudForm from "./CrudForm.jsx";
import CrudList from "./CrudList.jsx";
import { toast } from "react-toastify";
import uuid4 from "uuid4";

const CrudManager = () => {
    const [crudTasks, setCrudTasks] = useState([]);
    const [crudEditing, setCrudEditing] = useState(null);

    // ... (function definitions)

    return (
        <div className="bg-slate-900 h-screen flex flex-col items-center pt-32">
            <h1 className="text-white text-2xl font-extrabold capitalize py-8">
                Welcome to my Crud App
            </h1>
            <div className="w-[1200px] bg-slate-500">
                <CrudForm 
                    onSubmit={crudEditing ? crudUpdate : addCrud} 
                    initialData={crudEditing}
                />
                <CrudList 
                    crudTasks={crudTasks} 
                    CrudDelete={CrudDelete} 
                    crudEdit={crudEdit}
                />
            </div>
        </div>
    );
};

State:

  • crudTasks: An array that holds all the tasks.
  • crudEditing: Holds the task currently being edited, or null if no task is being edited.

Functions:

  • addCrud(crudTask): Adds a new task to the crudTasks array.
const addCrud = (crudTask) => {
    setCrudTasks([...crudTasks, { ...crudTask, id: uuid4() }]);
    toast.success("Successfully Added Data");
};
  • crudUpdate(crudUpdate): Updates an existing task in the crudTasks array.
const crudUpdate = (crudUpdate) => {
    setCrudTasks(crudTasks.map((task) => 
        task.id === crudUpdate.id ? crudUpdate : task
    ));
    setCrudEditing(null);
    toast.info("Successfully Update Data");
};
  • CrudDelete(id): Removes a task from the crudTasks array based on its id.
const CrudDelete = (id) => {
    setCrudTasks(crudTasks.filter((task) => task.id !== id));
    toast.error("Successfully Delete Data");
};
  • crudEdit(task): Sets the crudEditing state to the task being edited.
const crudEdit = (task) => {
    setCrudEditing(task);
};

CrudForm Component

File: CrudForm.jsx

This component renders a form for adding new tasks or editing existing ones.

Key props:

  • onSubmit: Function to call when the form is submitted (either addCrud or crudUpdate).
  • initialData: Data of the task being edited (if any).

CrudList Component

File: CrudList.jsx

This component renders the list of all tasks and provides options to edit or delete each task.

Key props:

  • crudTasks: Array of all tasks to display.
  • CrudDelete: Function to call when deleting a task.
  • crudEdit: Function to call when editing a task.

Workflow

  1. When the application loads, the CrudManager component initializes with an empty crudTasks array.
  2. The CrudForm allows users to input new task details (name, email, phone).
  3. When a new task is submitted, the addCrud function is called, which updates the crudTasks state.
  4. The CrudList component re-renders to display the new task.
  5. To edit a task, the user clicks an edit button, which calls crudEdit and populates the form with the task's current data.
  6. When the edit form is submitted, crudUpdate is called to modify the existing task.
  7. To delete a task, the user clicks a delete button, which calls CrudDelete to remove the task from the array.

Styling

The application uses Tailwind CSS for styling. Key classes:

  • bg-slate-900: Dark background for the main container
  • text-white: White text color
  • w-[1200px]: Sets a fixed width for the task container

Dependencies

  • React
  • uuid4 (for generating unique IDs)
  • react-toastify (for toast notifications)
  • Tailwind CSS (for styling)

Potential Improvements

  1. Implement data persistence using local storage or a backend API.
  2. Add sorting and filtering functionality to the task list.
  3. Implement pagination or infinite scrolling for large numbers of tasks.
  4. Enhance form validation and error handling.
  5. Add unit and integration tests for better code reliability.
  6. Implement a search functionality to find specific tasks quickly.
  7. Add a confirmation dialog before deleting tasks to prevent accidental deletions.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published