1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
|
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, setLoading, showError }) => {
setLoading(true)
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) {
setLoading(false)
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',
]
|