Massive/EditSet.tsx

156 lines
3.9 KiB
TypeScript
Raw Normal View History

2022-07-03 01:50:01 +00:00
import React, {useEffect, useRef, useState} from 'react';
2022-07-04 04:03:48 +00:00
import {StyleSheet, Text, View} from 'react-native';
import {Button, Modal, Portal, TextInput} from 'react-native-paper';
2022-07-03 01:50:01 +00:00
import {getDb} from './db';
2022-07-04 04:03:48 +00:00
import Set from './set';
2022-07-03 01:50:01 +00:00
export default function EditSet({
id,
onSave,
show,
setShow,
setId,
2022-07-04 11:09:41 +00:00
onRemove,
2022-07-03 01:50:01 +00:00
}: {
id?: number;
setId: (id?: number) => void;
onSave: () => void;
show: boolean;
setShow: (visible: boolean) => void;
2022-07-04 11:09:41 +00:00
onRemove: () => void;
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-04 04:03:48 +00:00
const weightRef = useRef<any>(null);
const repsRef = useRef<any>(null);
const unitRef = useRef<any>(null);
2022-07-03 01:50:01 +00:00
useEffect(() => {
if (!id) return;
getDb().then(async db => {
const [result] = await db.executeSql(`SELECT * FROM sets WHERE id = ?`, [
id,
]);
if (!result.rows.item(0)) throw new Error("Can't find specified Set.");
const set: Set = result.rows.item(0);
setName(set.name);
setReps(set.reps.toString());
setWeight(set.weight.toString());
setUnit(set.unit);
});
}, [id]);
const save = async () => {
if (!name || !reps || !weight) return;
const db = await getDb();
if (!id)
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 = ?`,
[name, reps, weight, unit, id],
);
setShow(false);
onSave();
};
const remove = async () => {
if (!id) return;
const db = await getDb();
await db.executeSql(`DELETE FROM sets WHERE id = ?`, [id]);
setShow(false);
2022-07-04 11:09:41 +00:00
onRemove();
2022-07-03 01:50:01 +00:00
};
return (
2022-07-04 04:03:48 +00:00
<>
<Portal>
<Modal
visible={show}
contentContainerStyle={styles.modal}
onDismiss={() => setShow(false)}>
2022-07-03 01:50:01 +00:00
<Text style={styles.title}>Add a set</Text>
<TextInput
2022-07-04 04:03:48 +00:00
style={styles.text}
2022-07-03 01:50:01 +00:00
autoFocus
2022-07-04 04:03:48 +00:00
label="Name *"
2022-07-03 01:50:01 +00:00
value={name}
onChangeText={setName}
onSubmitEditing={() => repsRef.current?.focus()}
/>
<TextInput
2022-07-04 04:03:48 +00:00
style={styles.text}
label="Reps *"
2022-07-03 01:50:01 +00:00
keyboardType="numeric"
value={reps}
onChangeText={setReps}
ref={repsRef}
2022-07-04 04:03:48 +00:00
onSubmitEditing={() => weightRef.current?.focus()}
2022-07-03 01:50:01 +00:00
/>
<TextInput
2022-07-04 04:03:48 +00:00
style={styles.text}
label="Weight *"
keyboardType="numeric"
value={weight}
onChangeText={setWeight}
onSubmitEditing={save}
ref={weightRef}
/>
<TextInput
style={styles.text}
label="Unit (kg)"
2022-07-03 01:50:01 +00:00
value={unit}
onChangeText={setUnit}
ref={unitRef}
onSubmitEditing={save}
/>
<View style={styles.bottom}>
<Button mode="contained" icon="save" onPress={save}>
Save
</Button>
<Button icon="close" onPress={() => setShow(false)}>
Cancel
</Button>
<Button icon="trash" onPress={remove} disabled={!id}>
Delete
</Button>
</View>
2022-07-04 04:03:48 +00:00
</Modal>
</Portal>
2022-07-03 01:50:01 +00:00
<Button
2022-07-04 04:03:48 +00:00
icon="add"
2022-07-03 01:50:01 +00:00
mode="contained"
onPress={() => {
setId(undefined);
setShow(true);
}}>
Add
</Button>
2022-07-04 04:03:48 +00:00
</>
2022-07-03 01:50:01 +00:00
);
}
const styles = StyleSheet.create({
2022-07-04 04:03:48 +00:00
modal: {
height: '100%',
backgroundColor: 'black',
padding: 20,
},
text: {
marginBottom: 10,
},
2022-07-03 01:50:01 +00:00
bottom: {
flexDirection: 'row',
},
title: {
fontSize: 20,
2022-07-04 04:03:48 +00:00
marginBottom: 10,
2022-07-03 01:50:01 +00:00
},
});