From 994bc43d488eefc0ee39f39dd7fae5515322b17b Mon Sep 17 00:00:00 2001 From: piotrruss Date: Sun, 22 May 2022 13:49:12 +0100 Subject: move api & helpers to utils --- App.js | 51 --------------------- App.jsx | 51 +++++++++++++++++++++ api.js | 31 ------------- components/Edit.jsx | 4 +- components/List.jsx | 4 +- components/Login.jsx | 2 +- components/Menu.jsx | 2 +- helpers.jsx | 124 --------------------------------------------------- utils/api.js | 31 +++++++++++++ utils/helpers.jsx | 124 +++++++++++++++++++++++++++++++++++++++++++++++++++ 10 files changed, 212 insertions(+), 212 deletions(-) delete mode 100644 App.js create mode 100644 App.jsx delete mode 100644 api.js delete mode 100644 helpers.jsx create mode 100644 utils/api.js create mode 100644 utils/helpers.jsx diff --git a/App.js b/App.js deleted file mode 100644 index 2d40b0e..0000000 --- a/App.js +++ /dev/null @@ -1,51 +0,0 @@ -import { StatusBar } from 'expo-status-bar' -import { StyleSheet, SafeAreaView, Text } from 'react-native' -import { useState, useEffect } from 'react' -import AsyncStorage from '@react-native-async-storage/async-storage' -import Main from './components/Main' -import Login from './components/Login' - -export default function App() { - const [session, setSession] = useState() - const [error, setError] = useState(null) - - const showError = (e) => { - setError(e) - setTimeout(() => { setError(null) }, 2000) - } - - useEffect(() => { - AsyncStorage.getItem('session') - .then(session => setSession(JSON.parse(session))) - .catch(() => setSession(null)) - }, []) - - if (error || session === undefined) return ( - - {error - ? {error} - : Loading...} - - ) - - return ( - - { - session === null - ? - :
- } - - - ) -} - -const styles = StyleSheet.create({ - container: { - flex: 1, - backgroundColor: '#000', - }, - error: { - color: 'red', - } -}) diff --git a/App.jsx b/App.jsx new file mode 100644 index 0000000..2d40b0e --- /dev/null +++ b/App.jsx @@ -0,0 +1,51 @@ +import { StatusBar } from 'expo-status-bar' +import { StyleSheet, SafeAreaView, Text } from 'react-native' +import { useState, useEffect } from 'react' +import AsyncStorage from '@react-native-async-storage/async-storage' +import Main from './components/Main' +import Login from './components/Login' + +export default function App() { + const [session, setSession] = useState() + const [error, setError] = useState(null) + + const showError = (e) => { + setError(e) + setTimeout(() => { setError(null) }, 2000) + } + + useEffect(() => { + AsyncStorage.getItem('session') + .then(session => setSession(JSON.parse(session))) + .catch(() => setSession(null)) + }, []) + + if (error || session === undefined) return ( + + {error + ? {error} + : Loading...} + + ) + + return ( + + { + session === null + ? + :
+ } + + + ) +} + +const styles = StyleSheet.create({ + container: { + flex: 1, + backgroundColor: '#000', + }, + error: { + color: 'red', + } +}) diff --git a/api.js b/api.js deleted file mode 100644 index 3441967..0000000 --- a/api.js +++ /dev/null @@ -1,31 +0,0 @@ -const API = 'https://apps.pruss.it/api' - -export const login = ({ email, password }) => fetch(`${API}/login`, { - method: 'POST', - headers: { 'Content-Type': 'plain/text; charset=utf-8' }, - body: JSON.stringify({ email, password }) -}) - -export const getList = ({ session }) => fetch(`${API}/notes`, { - 'Cookie': session.cookies -}) - -export const getNote = ({ note, session }) => fetch(`${API}/notes/${note.noteId}`, { - method: 'GET', headers: { 'Cookie': session.cookies }, -}) - -export const createNote = ({ title, content, session }) => fetch(`${API}/notes`, { - method: 'POST', - headers: { 'Content-Type': 'plain/text; charset=utf-8', 'Cookie': session.cookies }, - body: JSON.stringify({ title, content }) -}) - -export const editNote = ({ note, title, content, session }) => fetch(`${API}/notes/${note._id}`, { - method: 'PUT', - headers: { 'Content-Type': 'plain/text; charset=utf-8', 'Cookie': session.cookies }, - body: JSON.stringify({ title, noteId: note.noteId, content }) -}) - -export const removeNote = ({ note, session }) => fetch(`${API}/notes/${note._id}`, { - method: 'DELETE', headers: { 'Cookie': session.cookies }, -}) diff --git a/components/Edit.jsx b/components/Edit.jsx index 7818637..b15b7fc 100644 --- a/components/Edit.jsx +++ b/components/Edit.jsx @@ -1,6 +1,6 @@ -import { StyleSheet, Text, TextInput , View } from 'react-native'; +import { StyleSheet, Text, TextInput , View } from 'react-native' import { useState, useEffect } from 'react' -import { handleGetNote, handleCreateNote, handleEditNote } from '../helpers'; +import { handleGetNote, handleCreateNote, handleEditNote } from '../utils/helpers' import Menu from './Menu' const Edit = ({ edit: note, setEdit, session, setSession, showError }) => { diff --git a/components/List.jsx b/components/List.jsx index 8003187..3e4e11c 100644 --- a/components/List.jsx +++ b/components/List.jsx @@ -1,11 +1,11 @@ import { StyleSheet, Text } from 'react-native' import SwipeableFlatList from 'react-native-swipeable-list' import { useState, useEffect } from 'react' -import { getList } from '../api' +import { getList } from '../utils/api' import Note from './Note' import Menu from './Menu' import RemoveNoteButton from './RemoveNoteButton' -import { handleRemove } from "../helpers"; +import { handleRemove } from "../utils/helpers"; const List = ({ session, setSession, showError, setEdit }) => { const [list, setList] = useState() diff --git a/components/Login.jsx b/components/Login.jsx index 8b42913..db4b15b 100644 --- a/components/Login.jsx +++ b/components/Login.jsx @@ -1,6 +1,6 @@ import { StyleSheet, Text, TextInput, Pressable, View } from 'react-native' import { useState } from 'react' -import { handleLogin } from '../helpers' +import { handleLogin } from '../utils/helpers' const Login = ({ setSession, showError }) => { const [email, setEmail] = useState(''); diff --git a/components/Menu.jsx b/components/Menu.jsx index f380c62..f9c1522 100644 --- a/components/Menu.jsx +++ b/components/Menu.jsx @@ -1,5 +1,5 @@ import { StyleSheet, Text, View } from 'react-native'; -import { handleLogout, SORT } from '../helpers' +import { handleLogout, SORT } from '../utils/helpers' const Menu = ({ session, setSession, setEdit, showError, saveNote, sort, setSort }) => { const changeSort = () => sort < SORT.length-1 ? setSort(sort+1) : setSort(0) 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', -] - - diff --git a/utils/api.js b/utils/api.js new file mode 100644 index 0000000..3441967 --- /dev/null +++ b/utils/api.js @@ -0,0 +1,31 @@ +const API = 'https://apps.pruss.it/api' + +export const login = ({ email, password }) => fetch(`${API}/login`, { + method: 'POST', + headers: { 'Content-Type': 'plain/text; charset=utf-8' }, + body: JSON.stringify({ email, password }) +}) + +export const getList = ({ session }) => fetch(`${API}/notes`, { + 'Cookie': session.cookies +}) + +export const getNote = ({ note, session }) => fetch(`${API}/notes/${note.noteId}`, { + method: 'GET', headers: { 'Cookie': session.cookies }, +}) + +export const createNote = ({ title, content, session }) => fetch(`${API}/notes`, { + method: 'POST', + headers: { 'Content-Type': 'plain/text; charset=utf-8', 'Cookie': session.cookies }, + body: JSON.stringify({ title, content }) +}) + +export const editNote = ({ note, title, content, session }) => fetch(`${API}/notes/${note._id}`, { + method: 'PUT', + headers: { 'Content-Type': 'plain/text; charset=utf-8', 'Cookie': session.cookies }, + body: JSON.stringify({ title, noteId: note.noteId, content }) +}) + +export const removeNote = ({ note, session }) => fetch(`${API}/notes/${note._id}`, { + method: 'DELETE', headers: { 'Cookie': session.cookies }, +}) diff --git a/utils/helpers.jsx b/utils/helpers.jsx new file mode 100644 index 0000000..9e130dc --- /dev/null +++ b/utils/helpers.jsx @@ -0,0 +1,124 @@ +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', +] + + -- cgit v1.2.3