aboutsummaryrefslogtreecommitdiffstats
path: root/helpers.jsx
diff options
context:
space:
mode:
authorGravatar piotrruss <mail@pruss.it> 2022-05-22 13:49:12 +0100
committerGravatar piotrruss <mail@pruss.it> 2022-05-22 13:49:12 +0100
commit994bc43d488eefc0ee39f39dd7fae5515322b17b (patch)
tree64872194dbcc1112f1850dd94fd4c3185c1a6d6f /helpers.jsx
parent853aefca82243a574e3fd8d5e5c7270355ba0cdb (diff)
downloadnotes_mobile-994bc43d488eefc0ee39f39dd7fae5515322b17b.tar.gz
notes_mobile-994bc43d488eefc0ee39f39dd7fae5515322b17b.tar.bz2
notes_mobile-994bc43d488eefc0ee39f39dd7fae5515322b17b.zip
move api & helpers to utils
Diffstat (limited to 'helpers.jsx')
-rw-r--r--helpers.jsx124
1 files changed, 0 insertions, 124 deletions
diff --git a/helpers.jsx b/helpers.jsx
deleted file mode 100644
index 9e130dc..0000000
--- a/helpers.jsx
+++ /dev/null
@@ -1,124 +0,0 @@
-import AsyncStorage from '@react-native-async-storage/async-storage'
-import { Alert } from 'react-native';
-import { login, getNote, editNote, createNote, removeNote } from './api'
-
-export const handleLogin = async ({ email, password, setSession, showError }) => {
- try {
- const response = await login({ email, password })
- const cookies = response.headers?.map?.['set-cookie']
- const data = await response.json()
- if (data?.isLoggedIn) {
- await AsyncStorage.setItem('session', JSON.stringify({ ...data, cookies }))
- setSession({ ...data, cookies })
- }
- } catch(e) {
- showError('Error while logging in')
- }
-}
-
-export const handleLogout = ({ session, setSession, showError }) => {
- const logout = async () => {
- try {
- await AsyncStorage.clear();
- setSession(null)
- } catch(e) {
- showError('Error while logging out')
- }
- }
-
- Alert.alert(
- 'Are you sure?',
- `Do you want to log out user ${session.email}?`,
- [
- {
- text: 'Logout',
- onPress: logout,
- style: 'destructive',
- },
- {
- text: 'Cancel',
- onPress: () => {},
- style: 'cancel',
- },
- ],
- );
-}
-
-export const handleGetNote = async ({ note, setContent, setEdit, session, showError }) => {
- try {
- const response = await getNote({ note, session })
- const { content } = await response.json()
- setContent(content)
- } catch(e) {
- showError('Error while fetching note')
- setEdit(null)
- }
-}
-
-export const handleEditNote = async ({ note, title, content, setSaving, setEdit, session, showError }) => {
- try {
- setSaving(true)
- await editNote({ note, title, content, session })
- setSaving(false)
- setEdit(null)
- } catch(e) {
- showError('Error while saving note')
- setSaving(false)
- setEdit(null)
- }
-}
-
-export const handleCreateNote = async ({ title, content, setSaving, setEdit, session, showError }) => {
- try {
- setSaving(true)
- await createNote({ title, content, session })
- setSaving(false)
- setEdit(null)
- } catch(e) {
- showError('Error while saving note')
- setSaving(false)
- setEdit(null)
- }
-}
-
-export const handleRemove = ({ note, session, fetchNotes, setLoading, showError }) => {
- const deleteNote = async () => {
- setLoading(true)
- try {
- await removeNote({ note, session })
- fetchNotes()
- } catch(e) {
- console.log(e)
- showError('Error while removing note')
- setLoading(false)
- }
- }
-
- Alert.alert(
- 'Are you sure?',
- `Note "${note.title}" will be permanently removed`,
- [
- {
- text: 'Remove',
- onPress: deleteNote,
- style: 'destructive',
- },
- {
- text: 'Cancel',
- onPress: () => {},
- style: 'cancel',
- },
- ],
- );
-};
-
-export const SORT = [
- '▼ Updated',
- '▲ Updated',
- '▼ Created',
- '▲ Created',
- '▼ Title',
- '▲ Title',
-]
-
-