|
| 1 | +import { useState } from 'react'; |
| 2 | +import NoteContainer from './components/Notes/NoteContainer'; |
| 3 | +import SideBar from './components/SideBar/SideBar'; |
| 4 | + |
| 5 | +const DUMMY_NOTES = [ |
| 6 | + { |
| 7 | + id: 'n3', |
| 8 | + text: 'Hey there, Add your note by clicking the plus icon and choosing the theme color', |
| 9 | + time: 'Jul 28, 3:50 PM', |
| 10 | + color: '#FBA1A1' |
| 11 | + }, |
| 12 | + { |
| 13 | + id: 'n2', |
| 14 | + text: 'You can edit your note by clicking the pencil icon', |
| 15 | + time: 'Jul 25, 8:00 PM', |
| 16 | + color: '#C4DFAA' |
| 17 | + }, |
| 18 | + { |
| 19 | + id: 'n1', |
| 20 | + text: 'You can delete your note too by clicking the delete icon. \n\nYes..Yes you can delete these instruction notes too', |
| 21 | + time: 'Jul 18, 10:55 AM', |
| 22 | + color: '#F5F0BB' |
| 23 | + } |
| 24 | +]; |
| 25 | + |
1 | 26 | const App = () => {
|
| 27 | + |
| 28 | + const [notes, setNotes] = useState(DUMMY_NOTES); |
| 29 | + |
| 30 | + const addNoteHandler = (themeColor) => { |
| 31 | + const note = { |
| 32 | + id: Date.now() + "" + Math.floor(Math.random()*78), |
| 33 | + text: '', |
| 34 | + time: new Date().toLocaleDateString('en-US', { hourCycle: 'h12', minute: '2-digit', day: '2-digit', month: 'short', hour: '2-digit'}), |
| 35 | + color: themeColor |
| 36 | + } |
| 37 | + setNotes( (prevNotes)=>{ |
| 38 | + return [note, ...prevNotes]; |
| 39 | + }); |
| 40 | + } |
| 41 | + |
| 42 | + const deleteNoteHandler = (noteId) => { |
| 43 | + const prevNotes = [...notes]; |
| 44 | + let indexToDelete = -1; |
| 45 | + for(let i=0; i<prevNotes.length; i++){ |
| 46 | + if( prevNotes[i]['id'] == noteId ) indexToDelete=i; |
| 47 | + } |
| 48 | + if(indexToDelete < 0) return; |
| 49 | + else prevNotes.splice(indexToDelete, 1); |
| 50 | + setNotes(prevNotes); |
| 51 | + } |
2 | 52 |
|
3 |
| - return <div>Hello World</div>; |
| 53 | + return( |
| 54 | + <div className='app'> |
| 55 | + <SideBar addTheme ={ addNoteHandler } /> |
| 56 | + <NoteContainer notes={notes} deleteNote = { deleteNoteHandler } /> |
| 57 | + </div> |
| 58 | + ); |
4 | 59 |
|
5 | 60 | }
|
6 | 61 |
|
|
0 commit comments