Skip to content

Commit

Permalink
'Delete note feature' added
Browse files Browse the repository at this point in the history
  • Loading branch information
Drougnov committed Sep 18, 2022
1 parent 68fccbf commit 31e7ce9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
26 changes: 16 additions & 10 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,31 @@ export default function App(){
function createNewNote() {
const newNote = {
id: nanoid(),
body: "# Type your note's title here"
body: `# Enter title here \n\n`
}
setNotes(prevNotes => [newNote, ...prevNotes])
setCurrentNoteId(newNote.id)
}
}

function updateNote(text) {
setNotes(oldNotes => {
let arr = [];
for(let i=0; i<oldNotes.length; i++){
if(oldNotes[i].id === currentNoteId){
arr.unshift({...oldNotes[i], body:text});
}else{
arr.push({...oldNotes[i], body:text})
let arr = []
for(let i= 0; i < oldNotes.length; i++) {
if(oldNotes[i].id === currentNoteId) {
arr.unshift({ ...oldNotes[i], body: text })
} else {
arr.push(oldNotes[i])
}
}
}
return arr;
return arr;
})
}

function deleteNote(event, noteId) {
event.stopPropagation()
setNotes(oldNotes => oldNotes.filter(note => note.id !== noteId))
}

function findCurrentNote() {
return notes.find(note => {
return note.id === currentNoteId
Expand All @@ -56,6 +61,7 @@ export default function App(){
setCurrentNoteId={setCurrentNoteId}
currentNote={findCurrentNote()}
newNote={createNewNote}
deleteNote={deleteNote}
/>
{currentNoteId && notes.length > 0 &&
<Editor
Expand Down
3 changes: 2 additions & 1 deletion src/Components/Sidebar.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
export default function Sidebar(props){
const noteList = props.notes.map((note, index)=> (
const noteList = props.notes.map((note)=> (
<li key={note.id}
className={`title ${
note.id === props.currentNote.id ? "selected-note" : ""
}`}
onClick={() =>props.setCurrentNoteId(note.id)}
>
<span className="text-snippet">{note.body.split('\n')[0]}</span>
<button className="delete-btn" onClick={(event) => props.deleteNote(event, note.id)}><i class="fa-solid fa-trash"></i></button>
</li>
))
return(
Expand Down

0 comments on commit 31e7ce9

Please sign in to comment.