From 64c4d83bc28efe414c541cd27885afee4904e166 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Haakon=20Helmen=20Rus=C3=A5s?= Date: Sun, 19 Nov 2023 18:28:32 +0100 Subject: [PATCH] Feature/local storage (#75) * chore: adding async storage package * feat: adding store and retrieve functions * remove log statement * feat: adding local storage * fix: adding types to local storage * fix: move getLocalState to api dir - remove useLocalStorage hook - use new fetching logic * fix: refactor marks form to seperate component * fix: typo corrected by wife * styles: general style changes * styles: better table styling --- .../siktemerker/components/MarksForm.tsx | 93 +++++++++++++++ .../{ => components}/MarksTable.tsx | 42 +++++-- app/(tabs)/siktemerker/index.tsx | 110 ++++-------------- components/common/Input.tsx | 4 +- package-lock.json | 31 +++++ package.json | 3 +- utils/LocalStorage/getLocalStorage.ts | 20 ++++ utils/LocalStorage/storeLocal.ts | 18 +++ utils/index.ts | 2 + 9 files changed, 222 insertions(+), 101 deletions(-) create mode 100644 app/(tabs)/siktemerker/components/MarksForm.tsx rename app/(tabs)/siktemerker/{ => components}/MarksTable.tsx (55%) create mode 100644 utils/LocalStorage/getLocalStorage.ts create mode 100644 utils/LocalStorage/storeLocal.ts diff --git a/app/(tabs)/siktemerker/components/MarksForm.tsx b/app/(tabs)/siktemerker/components/MarksForm.tsx new file mode 100644 index 0000000..bd4f231 --- /dev/null +++ b/app/(tabs)/siktemerker/components/MarksForm.tsx @@ -0,0 +1,93 @@ +import React, { FC } from 'react'; +import { Keyboard, StyleSheet, View } from 'react-native'; + +import { Button, Input } from '../../../../components/common'; +import { MarkValue } from '../../../../types'; +import { formatNumber } from '../../../../utils'; +import { useCalcForm } from '../useCalcForm'; + +interface MarksFormProps { + status: string; + sendMarks: (newMark: MarkValue) => Promise; +} + +const MarksForm: FC = ({ sendMarks, status }) => { + const [{ aimError, aimValue, distanceError, distanceValue }, dispatch] = useCalcForm(); + + function handleDistanceChange(value: string) { + dispatch({ type: 'SET_DISTANCE_VALUE', payload: value }); + } + + function handleAimChange(value: string) { + dispatch({ type: 'SET_AIM_VALUE', payload: value }); + } + + async function handleAddMark() { + if (!aimValue) { + dispatch({ type: 'SET_AIM_ERROR', payload: true }); + } + if (!distanceValue) { + dispatch({ type: 'SET_DISTANCE_ERROR', payload: true }); + } + if (aimValue && distanceValue) { + const newEntry: MarkValue = { aim: parseFloat(aimValue), distance: parseFloat(distanceValue) }; + + await sendMarks(newEntry); + dispatch({ type: 'SET_AIM_VALUE', payload: '' }); + dispatch({ type: 'SET_DISTANCE_VALUE', payload: '' }); + Keyboard.dismiss(); + } + } + + return ( + + + dispatch({ type: 'SET_DISTANCE_ERROR', payload: false })} + placeholderText="F.eks. 20" + keyboardType="numeric" + error={distanceError} + errorMessage="Fyll inn" + value={distanceValue} + onChangeText={(value) => handleDistanceChange(formatNumber(value))} + /> + + + dispatch({ type: 'SET_AIM_ERROR', payload: false })} + placeholderText="F.eks. 2.3" + keyboardType="numeric" + value={aimValue} + error={aimError} + errorMessage="Fyll inn" + onChangeText={(value) => handleAimChange(formatNumber(value))} + /> + +