2022-07-06 04:27:36 +00:00
|
|
|
import {format} from 'date-fns';
|
2022-07-07 02:18:38 +00:00
|
|
|
import React, {useContext, useEffect, useRef, useState} from 'react';
|
|
|
|
import {StyleSheet, Text} from 'react-native';
|
|
|
|
import {Button, Dialog, Portal, TextInput} from 'react-native-paper';
|
|
|
|
import {DatabaseContext} from './App';
|
|
|
|
import Set from './set';
|
2022-07-03 01:50:01 +00:00
|
|
|
|
|
|
|
export default function EditSet({
|
|
|
|
onSave,
|
|
|
|
show,
|
|
|
|
setShow,
|
2022-07-07 02:18:38 +00:00
|
|
|
set,
|
2022-07-03 01:50:01 +00:00
|
|
|
}: {
|
|
|
|
onSave: () => void;
|
|
|
|
show: boolean;
|
|
|
|
setShow: (visible: boolean) => void;
|
2022-07-07 02:18:38 +00:00
|
|
|
set?: Set;
|
2022-07-03 01:50:01 +00:00
|
|
|
}) {
|
|
|
|
const [name, setName] = useState('');
|
|
|
|
const [reps, setReps] = useState('');
|
|
|
|
const [weight, setWeight] = useState('');
|
|
|
|
const [unit, setUnit] = useState('');
|
2022-07-06 04:27:36 +00:00
|
|
|
const [created, setCreated] = useState(new Date(new Date().toUTCString()));
|
2022-07-04 04:03:48 +00:00
|
|
|
const weightRef = useRef<any>(null);
|
|
|
|
const repsRef = useRef<any>(null);
|
|
|
|
const unitRef = useRef<any>(null);
|
2022-07-07 02:18:38 +00:00
|
|
|
const db = useContext(DatabaseContext);
|
|
|
|
|
|
|
|
const refresh = async () => {
|
|
|
|
if (!set) return setCreated(new Date(new Date().toUTCString()));
|
|
|
|
setName(set.name);
|
|
|
|
setReps(set.reps.toString());
|
|
|
|
setWeight(set.weight.toString());
|
|
|
|
setUnit(set.unit);
|
|
|
|
setCreated(new Date(set.created));
|
|
|
|
};
|
2022-07-03 01:50:01 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
2022-07-07 02:18:38 +00:00
|
|
|
refresh();
|
|
|
|
}, [set]);
|
2022-07-03 01:50:01 +00:00
|
|
|
|
|
|
|
const save = async () => {
|
|
|
|
if (!name || !reps || !weight) return;
|
2022-07-07 03:13:44 +00:00
|
|
|
if (!set?.id)
|
2022-07-03 01:50:01 +00:00
|
|
|
await db.executeSql(
|
|
|
|
`INSERT INTO sets(name, reps, weight, created, unit) VALUES (?,?,?,?,?)`,
|
|
|
|
[name, reps, weight, new Date().toISOString(), unit || 'kg'],
|
|
|
|
);
|
|
|
|
else
|
|
|
|
await db.executeSql(
|
|
|
|
`UPDATE sets SET name = ?, reps = ?, weight = ?, unit = ? WHERE id = ?`,
|
2022-07-07 02:18:38 +00:00
|
|
|
[name, reps, weight, unit, set.id],
|
2022-07-03 01:50:01 +00:00
|
|
|
);
|
|
|
|
setShow(false);
|
|
|
|
onSave();
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
2022-07-05 00:43:23 +00:00
|
|
|
<Portal>
|
2022-07-06 09:03:56 +00:00
|
|
|
<Dialog visible={show} onDismiss={() => setShow(false)}>
|
2022-07-07 02:18:38 +00:00
|
|
|
<Dialog.Title>{set?.id ? `Edit "${name}"` : 'Add a set'}</Dialog.Title>
|
2022-07-06 09:03:56 +00:00
|
|
|
<Dialog.Content>
|
|
|
|
<TextInput
|
|
|
|
style={styles.text}
|
|
|
|
autoFocus
|
|
|
|
label="Name *"
|
|
|
|
value={name}
|
|
|
|
onChangeText={setName}
|
|
|
|
onSubmitEditing={() => repsRef.current?.focus()}
|
2022-07-07 00:45:45 +00:00
|
|
|
autoCorrect={false}
|
2022-07-06 09:03:56 +00:00
|
|
|
/>
|
|
|
|
<TextInput
|
|
|
|
style={styles.text}
|
|
|
|
label="Reps *"
|
|
|
|
keyboardType="numeric"
|
|
|
|
value={reps}
|
|
|
|
onChangeText={setReps}
|
|
|
|
ref={repsRef}
|
|
|
|
onSubmitEditing={() => weightRef.current?.focus()}
|
|
|
|
/>
|
|
|
|
<TextInput
|
|
|
|
style={styles.text}
|
|
|
|
label="Weight *"
|
|
|
|
keyboardType="numeric"
|
|
|
|
value={weight}
|
|
|
|
onChangeText={setWeight}
|
|
|
|
onSubmitEditing={save}
|
|
|
|
ref={weightRef}
|
|
|
|
/>
|
|
|
|
<TextInput
|
|
|
|
style={styles.text}
|
|
|
|
label="Unit (kg)"
|
|
|
|
value={unit}
|
|
|
|
onChangeText={setUnit}
|
|
|
|
ref={unitRef}
|
|
|
|
onSubmitEditing={save}
|
|
|
|
/>
|
|
|
|
<Text style={styles.text}>{format(created, 'PPPP p')}</Text>
|
|
|
|
</Dialog.Content>
|
|
|
|
<Dialog.Actions>
|
2022-07-05 00:43:23 +00:00
|
|
|
<Button icon="close" onPress={() => setShow(false)}>
|
|
|
|
Cancel
|
|
|
|
</Button>
|
2022-07-06 09:03:56 +00:00
|
|
|
<Button mode="contained" icon="save" onPress={save}>
|
|
|
|
Save
|
|
|
|
</Button>
|
|
|
|
</Dialog.Actions>
|
|
|
|
</Dialog>
|
2022-07-05 00:43:23 +00:00
|
|
|
</Portal>
|
2022-07-03 01:50:01 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
2022-07-04 04:03:48 +00:00
|
|
|
text: {
|
|
|
|
marginBottom: 10,
|
|
|
|
},
|
2022-07-03 01:50:01 +00:00
|
|
|
title: {
|
|
|
|
fontSize: 20,
|
2022-07-04 04:03:48 +00:00
|
|
|
marginBottom: 10,
|
2022-07-03 01:50:01 +00:00
|
|
|
},
|
|
|
|
});
|