Massive/SetForm.tsx

160 lines
4.7 KiB
TypeScript
Raw Normal View History

import {useCallback, useRef, useState} from 'react';
2022-10-13 03:32:12 +00:00
import {TextInput, View} from 'react-native';
2022-10-01 02:35:52 +00:00
import DocumentPicker from 'react-native-document-picker';
2022-10-05 10:38:52 +00:00
import {Button, Card, TouchableRipple} from 'react-native-paper';
2022-10-01 02:35:52 +00:00
import ConfirmDialog from './ConfirmDialog';
import {MARGIN} from './constants';
import {setRepo} from './db';
import GymSet from './gym-set';
2022-08-26 01:54:51 +00:00
import MassiveInput from './MassiveInput';
2022-10-14 04:27:19 +00:00
import {useSnackbar} from './MassiveSnack';
import {useSettings} from './use-settings';
2022-07-20 03:48:48 +00:00
export default function SetForm({
save,
set,
}: {
set: GymSet;
save: (set: GymSet) => void;
2022-07-20 03:48:48 +00:00
}) {
const [name, setName] = useState(set.name);
const [reps, setReps] = useState(set.reps.toString());
const [weight, setWeight] = useState(set.weight.toString());
2022-10-02 04:59:08 +00:00
const [newImage, setNewImage] = useState(set.image);
2022-07-20 03:48:48 +00:00
const [unit, setUnit] = useState(set.unit);
2022-10-01 02:35:52 +00:00
const [showRemove, setShowRemove] = useState(false);
2022-08-07 03:09:45 +00:00
const [selection, setSelection] = useState({
start: 0,
end: set.reps.toString().length,
});
2022-10-01 02:35:52 +00:00
const [removeImage, setRemoveImage] = useState(false);
2022-10-14 04:27:19 +00:00
const {toast} = useSnackbar();
const {settings} = useSettings();
const weightRef = useRef<TextInput>(null);
const repsRef = useRef<TextInput>(null);
const unitRef = useRef<TextInput>(null);
2022-09-01 01:59:06 +00:00
const handleSubmit = async () => {
2022-10-02 04:59:08 +00:00
console.log(`${SetForm.name}.handleSubmit:`, {set, uri: newImage, name});
2022-08-23 03:58:44 +00:00
if (!name) return;
2022-10-02 04:59:08 +00:00
let image = newImage;
if (!newImage && !removeImage)
image = await setRepo.findOne({where: {name}}).then(s => s.image);
2022-10-01 02:35:52 +00:00
console.log(`${SetForm.name}.handleSubmit:`, {image});
2022-07-20 03:48:48 +00:00
save({
name,
reps: Number(reps),
weight: Number(weight),
id: set.id,
unit,
2022-10-01 02:35:52 +00:00
image,
minutes: Number(set.minutes ?? 3),
seconds: Number(set.seconds ?? 30),
sets: set.sets ?? 3,
2022-07-20 03:48:48 +00:00
});
};
const handleName = (value: string) => {
setName(value.replace(/,|'/g, ''));
if (value.match(/,|'/))
toast('Commas and single quotes would break CSV exports', 6000);
};
const handleUnit = (value: string) => {
setUnit(value.replace(/,|'/g, ''));
if (value.match(/,|'/))
toast('Commas and single quotes would break CSV exports', 6000);
};
2022-10-01 02:35:52 +00:00
const changeImage = useCallback(async () => {
const {fileCopyUri} = await DocumentPicker.pickSingle({
type: 'image/*',
copyTo: 'documentDirectory',
});
2022-10-02 04:59:08 +00:00
if (fileCopyUri) setNewImage(fileCopyUri);
2022-10-01 02:35:52 +00:00
}, []);
const handleRemove = useCallback(async () => {
2022-10-02 04:59:08 +00:00
setNewImage('');
2022-10-01 02:35:52 +00:00
setRemoveImage(true);
setShowRemove(false);
}, []);
2022-07-20 03:48:48 +00:00
return (
<>
2022-10-13 03:32:12 +00:00
<View style={{flex: 1}}>
2022-08-26 01:54:51 +00:00
<MassiveInput
2022-10-13 03:32:12 +00:00
label="Name"
value={name}
onChangeText={handleName}
autoCorrect={false}
autoFocus={!name}
onSubmitEditing={() => repsRef.current?.focus()}
/>
2022-10-13 03:32:12 +00:00
<MassiveInput
label="Reps"
keyboardType="numeric"
value={reps}
onChangeText={setReps}
onSubmitEditing={() => weightRef.current?.focus()}
selection={selection}
onSelectionChange={e => setSelection(e.nativeEvent.selection)}
autoFocus={!!name}
innerRef={repsRef}
/>
<MassiveInput
label="Weight"
keyboardType="numeric"
value={weight}
onChangeText={setWeight}
onSubmitEditing={handleSubmit}
innerRef={weightRef}
/>
{!!settings.showUnit && (
<MassiveInput
autoCapitalize="none"
label="Unit"
value={unit}
onChangeText={handleUnit}
innerRef={unitRef}
/>
)}
2022-10-14 05:20:30 +00:00
{typeof set.id === 'number' && !!settings.showDate && (
<MassiveInput label="Created" disabled value={set.created} />
)}
2022-10-13 03:32:12 +00:00
{!!settings.images && newImage && (
<TouchableRipple
style={{marginBottom: MARGIN}}
onPress={changeImage}
onLongPress={() => setShowRemove(true)}>
<Card.Cover source={{uri: newImage}} />
</TouchableRipple>
)}
{!!settings.images && !newImage && (
<Button
style={{marginBottom: MARGIN}}
onPress={changeImage}
icon="add-photo-alternate">
Image
</Button>
)}
</View>
2022-08-23 03:58:44 +00:00
<Button
disabled={!name}
mode="contained"
icon="save"
onPress={handleSubmit}>
2022-07-20 03:48:48 +00:00
Save
</Button>
2022-10-01 02:35:52 +00:00
<ConfirmDialog
title="Remove image"
onOk={handleRemove}
show={showRemove}
setShow={setShowRemove}>
Are you sure you want to remove the image?
</ConfirmDialog>
2022-07-20 03:48:48 +00:00
</>
);
}