Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

worked on delete functional #61

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 38 additions & 14 deletions src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,19 @@ function App() {

const [loading, setLoading] = useState(true);

const [deletedItems,setDeletedItems] = useState([]);
const [addedItems,setAddedItems] = useState([]);

/*Checks the localstorage to see if the dark mode was enabled during last visit*/
useEffect(() => {
if(!localStorage.getItem('deletedItemsList')){
let a = [];
localStorage.setItem('deletedItemsList',JSON.stringify(a));
}
if(!localStorage.getItem('addedItemsList')){
let a = [];
localStorage.setItem('addedItemsList',JSON.stringify(a));
}
if(!localStorage.getItem("darkmode")){
return
}
Expand All @@ -55,47 +66,60 @@ function App() {
// read operaton
const getData = () => {
setLoading(true);
// getting deleted items data
let dellist = localStorage.getItem('deletedItemsList');
dellist = JSON.parse(dellist);
setDeletedItems(dellist);
// getting added items data
let addlist = localStorage.getItem("addedItemsList");
console.log(addlist);
addlist = JSON.parse(addlist);
setAddedItems(addlist);
axios
.get("https://6315b6ef33e540a6d38296a9.mockapi.io/notepad-app")
.then((res) => {
setLoading(false);
console.log(res.data);
setNotes(res.data);
setNotes(res.data.filter(it=>!dellist.includes(it.id)));
});
};

useEffect(() => {
getData();
}, []);

const addNote = (text,writer) => {
const addNote = async (text,writer) => {
const date = new Date();
const newNote = {
let newNote = {
id: ID,
color1: randomColor1,
color2: randomColor2,
text: text,
writer:writer,
date: date.toLocaleDateString(),
};
const newNotes = [...notes, newNote];
setNotes(newNotes);
// console.log(newNotes);

//create operation
axios.post(
newNote.id = await axios.post(
"https://6315b6ef33e540a6d38296a9.mockapi.io/notepad-app",
newNote
);
).then(res => {return res.data.id});
let newAddedlist = addedItems;
newAddedlist.push(newNote.id);
setAddedItems(newAddedlist);
localStorage.setItem('addedItemsList',JSON.stringify(newAddedlist));
const newNotes = [...notes, newNote];
setNotes(newNotes);
};

const deleteNote = (id) => {
const newNotes = notes.filter((note) => note.id !== id);
//delete operation
if (id == ID){
axios.delete(
`https://6315b6ef33e540a6d38296a9.mockapi.io/notepad-app/${id}`);
setNotes(newNotes);
if (addedItems.includes(id)){
let delist = deletedItems;
delist.push(id);
setDeletedItems(delist);
delist = JSON.stringify(delist);
localStorage.setItem('deletedItemsList',delist);
setNotes(newNotes);
}
else
toast("📋 This is not YourQutoes", {
Expand Down