diff options
author | 2022-05-18 23:02:58 +0100 | |
---|---|---|
committer | 2022-05-18 23:02:58 +0100 | |
commit | d77d7c440333ded46eeb8d28e22ec5517f3b15b8 (patch) | |
tree | 34bc54ef125433b722184777f33da90e1badf745 /components/Edit.jsx | |
parent | 9173d98070a0463b352625c2e50d20543f7c12fb (diff) | |
download | notes_mobile-d77d7c440333ded46eeb8d28e22ec5517f3b15b8.tar.gz notes_mobile-d77d7c440333ded46eeb8d28e22ec5517f3b15b8.tar.bz2 notes_mobile-d77d7c440333ded46eeb8d28e22ec5517f3b15b8.zip |
small fixes
Diffstat (limited to 'components/Edit.jsx')
-rw-r--r-- | components/Edit.jsx | 127 |
1 files changed, 127 insertions, 0 deletions
diff --git a/components/Edit.jsx b/components/Edit.jsx new file mode 100644 index 0000000..d2ec6b3 --- /dev/null +++ b/components/Edit.jsx @@ -0,0 +1,127 @@ +import { StyleSheet, Text, TextInput , View } from 'react-native'; +import { useState, useEffect } from 'react' +import Menu from './Menu' + +const Edit = ({ edit, setEdit, session, setSession, showError }) => { + const [saving, setSaving] = useState() + const [title, setTitle] = useState(edit ? edit.title : '') + const [content, setContent] = useState() + + const fetchNote = async () => { + try { + const response = await fetch(`https://apps.pruss.it/api/notes/${edit.noteId}`, { + method: 'GET', headers: { 'Cookie': session.cookies }, + }) + const { content } = await response.json() + setContent(content) + } catch(e) { + showError('Error while fetching note') + setEdit(null) + } + } + + const saveNote = async () => { + try { + setSaving(true) + await fetch(`https://apps.pruss.it/api/notes/${edit._id}`, { + method: 'PUT', + headers: { 'Content-Type': 'plain/text; charset=utf-8', 'Cookie': session.cookies }, + body: JSON.stringify({ title, noteId: edit.noteId, content }) + }) + setSaving(false) + setEdit(null) + } catch(e) { + showError('Error while saving note') + setSaving(false) + setEdit(null) + } + } + + createNote = async () => { + try { + setSaving(true) + await fetch(`https://apps.pruss.it/api/notes`, { + method: 'POST', + headers: { 'Content-Type': 'plain/text; charset=utf-8', 'Cookie': session.cookies }, + body: JSON.stringify({ title, content }) + }) + setSaving(false) + setEdit(null) + } catch(e) { + showError('Error while saving note') + setSaving(false) + setEdit(null) + } + } + + useEffect(() => { + if (edit?._id) { + fetchNote() + } else { + setContent('') + } + }, []) + + return ( + <> + <Menu + session={session} + setSession={setSession} + showError={showError} + setEdit={setEdit} + saveNote={edit._id ? saveNote : createNote} + /> + { + content === undefined || saving + ? <Text style={styles.text}>{saving ? 'Saving...' : 'Loading...'}</Text> + : ( + <View style={styles.container}> + <TextInput + placeholder="Title" + placeholderTextColor="#BBB" + style={styles.title} + value={title} + onChange={e => setTitle(e.nativeEvent.text)} + /> + <TextInput + placeholder="Content" + placeholderTextColor="#BBB" + multiline={true} + textAlignVertical="top" + style={styles.content} + value={content} + onChange={e => setContent(e.nativeEvent.text)} + /> + </View> + ) + } + </> + ) +} + +const styles = StyleSheet.create({ + container: { + flex: 1, + padding: 15, + marginBottom: 50, + }, + title: { + color: 'white', + borderBottomColor: 'white', + borderBottomWidth: 1, + paddingBottom: 15, + marginBottom: 10, + fontWeight: 'bold', + }, + content: { + color: 'white', + flexGrow: 1, + paddingVertical: 10, + }, + text: { + padding: 15, + color: 'white', + } +}); + +export default Edit |