Created
January 28, 2020 06:13
-
-
Save jelorivera08/393c98f71cd534f965e6a34f4451075e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useState } from 'react'; | |
import createNoteMutation from './mutations/createNote'; | |
import deleteNoteMutation from './mutations/deleteNote'; | |
import updateNoteMutation from './mutations/updateNote'; | |
const MainPage = ({ notes }) => { | |
const [newNote, setNewNote] = useState(''); | |
const [noteContentBeingUpdated, setNoteContentBeingUpdated] = useState(''); | |
const [noteIdBeingUpdated, setNoteIdBeingUpdated] = useState(''); | |
return ( | |
<div> | |
<header>Notes</header> | |
<ul> | |
{notes.map(v => { | |
const isBeingUpdated = noteIdBeingUpdated === v._id; | |
return ( | |
<div key={v._id}> | |
{isBeingUpdated ? ( | |
<li> | |
<input | |
autoFocus | |
value={noteContentBeingUpdated} | |
onChange={e => setNoteContentBeingUpdated(e.target.value)} | |
/> | |
</li> | |
) : ( | |
<li>{v.content}</li> | |
)} | |
<div style={{ display: 'flex' }}> | |
<button | |
onClick={() => { | |
if (isBeingUpdated) { | |
updateNoteMutation(v._id, noteContentBeingUpdated); | |
setNoteIdBeingUpdated(''); | |
setNoteContentBeingUpdated(''); | |
} else { | |
setNoteIdBeingUpdated(v._id); | |
setNoteContentBeingUpdated(v.content); | |
} | |
}} | |
> | |
update | |
</button> | |
<button onClick={() => deleteNoteMutation(v._id)}> | |
delete | |
</button> | |
</div> | |
</div> | |
); | |
})} | |
</ul> | |
<footer> | |
<input | |
value={newNote} | |
onChange={e => setNewNote(e.target.value)} | |
placeholder='Add a note here' | |
></input> | |
<button | |
onClick={() => { | |
if (newNote) { | |
createNoteMutation(newNote); | |
setNewNote(''); | |
} | |
}} | |
> | |
create note | |
</button> | |
</footer> | |
</div> | |
); | |
}; | |
export default MainPage; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment