69 | );
70 | };
71 |
72 | export default TaskItem;
73 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Getting Started with Create React App
2 |
3 | This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).
4 |
5 | ## Available Scripts
6 |
7 | In the project directory, you can run:
8 |
9 | ### `npm start`
10 |
11 | Runs the app in the development mode.\
12 | Open [http://localhost:3000](http://localhost:3000) to view it in your browser.
13 |
14 | The page will reload when you make changes.\
15 | You may also see any lint errors in the console.
16 |
17 | ### `npm test`
18 |
19 | Launches the test runner in the interactive watch mode.\
20 | See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.
21 |
22 | ### `npm run build`
23 |
24 | Builds the app for production to the `build` folder.\
25 | It correctly bundles React in production mode and optimizes the build for the best performance.
26 |
27 | The build is minified and the filenames include the hashes.\
28 | Your app is ready to be deployed!
29 |
30 | See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.
31 |
32 | ### `npm run eject`
33 |
34 | **Note: this is a one-way operation. Once you `eject`, you can't go back!**
35 |
36 | If you aren't satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.
37 |
38 | Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you're on your own.
39 |
40 | You don't have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn't feel obligated to use this feature. However we understand that this tool wouldn't be useful if you couldn't customize it when you are ready for it.
41 |
42 | ## Learn More
43 |
44 | You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).
45 |
46 | To learn React, check out the [React documentation](https://reactjs.org/).
47 |
48 | ### Code Splitting
49 |
50 | This section has moved here: [https://facebook.github.io/create-react-app/docs/code-splitting](https://facebook.github.io/create-react-app/docs/code-splitting)
51 |
52 | ### Analyzing the Bundle Size
53 |
54 | This section has moved here: [https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size](https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size)
55 |
56 | ### Making a Progressive Web App
57 |
58 | This section has moved here: [https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app](https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app)
59 |
60 | ### Advanced Configuration
61 |
62 | This section has moved here: [https://facebook.github.io/create-react-app/docs/advanced-configuration](https://facebook.github.io/create-react-app/docs/advanced-configuration)
63 |
64 | ### Deployment
65 |
66 | This section has moved here: [https://facebook.github.io/create-react-app/docs/deployment](https://facebook.github.io/create-react-app/docs/deployment)
67 |
68 | ### `npm run build` fails to minify
69 |
70 | This section has moved here: [https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify](https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify)
71 |
--------------------------------------------------------------------------------
/src/App.js:
--------------------------------------------------------------------------------
1 | import Header from "./components/Header";
2 | import Footer from "./components/Footer";
3 | import AddTask from "./components/AddTask";
4 | import TaskList from "./components/TaskList";
5 | import { useEffect, useState } from "react";
6 | import { data } from "autoprefixer";
7 | import { createContext } from "react";
8 |
9 | export const DeleteContext = createContext();
10 | export const EditContext = createContext();
11 |
12 | const App = () => {
13 | const [tasks, setTasks] = useState([]);
14 | const [loading, setLoading] = useState(true);
15 | const [error, setError] = useState("");
16 |
17 | const [editedText, setEditedText] = useState("");
18 | const [toggleEditeMode, setToggleEditeMode] = useState(true);
19 |
20 | //getting data from the server
21 | useEffect(() => {
22 | fetchingData();
23 | }, []);
24 |
25 | //fetching data from server
26 | const fetchingData = async () => {
27 | try {
28 | const res = await fetch(
29 | "https://dramatic-quintessential-deerstalker.glitch.me/tasks"
30 | );
31 | if (!res.ok) throw new Error("somethign went wrong");
32 | const data = await res.json();
33 | setTasks(data);
34 | setLoading(false);
35 | } catch (error) {
36 | setError(error);
37 | }
38 | };
39 |
40 | //deleting data
41 | const handleDelete = (id) => {
42 | deleteData(id);
43 | //set updated tasks
44 | setTasks(tasks.filter((task) => id !== task.id));
45 | };
46 |
47 | //delete data
48 | const deleteData = async (id) => {
49 | await fetch(
50 | `https://dramatic-quintessential-deerstalker.glitch.me/tasks/${id}`,
51 | {
52 | method: "DELETE",
53 | headers: {
54 | "content-type": "aplication/json",
55 | },
56 | }
57 | );
58 | };
59 |
60 | //data editing
61 | const handleEdit = (id) => {
62 | const [editableTarget] = tasks.filter((task) => id === task.id);
63 |
64 | editableTarget.isEditable = true;
65 | setEditedText(editableTarget.text);
66 |
67 | setTasks([...tasks]);
68 | setToggleEditeMode(false);
69 |
70 | //re-arrange
71 | tasks
72 | .filter((task) => task.id !== id)
73 | .map((editableEl) => (editableEl.isEditable = false));
74 | };
75 |
76 | //editing task form handler
77 | const handleEditeSubmit = (e, id) => {
78 | e.preventDefault();
79 | setToggleEditeMode(!toggleEditeMode);
80 |
81 | //update data
82 | const updateData = {
83 | text: editedText,
84 | id: id,
85 | };
86 |
87 | //put request
88 | puttingRequest(id, updateData);
89 |
90 | //real time update
91 | const [editableTarget] = tasks.filter((task) => id === task.id);
92 | editableTarget.isEditable = false;
93 | editableTarget.text = updateData.text;
94 | };
95 |
96 | const puttingRequest = async (id, newData) => {
97 | await fetch(
98 | `https://dramatic-quintessential-deerstalker.glitch.me/tasks/${id}`,
99 | {
100 | method: "PUT",
101 | headers: {
102 | "content-type": "application/json",
103 | },
104 | body: JSON.stringify(newData),
105 | }
106 | );
107 | };
108 |
109 | return (
110 |