Massive/EditPlan.tsx

130 lines
3.8 KiB
TypeScript
Raw Normal View History

2022-07-06 05:40:53 +00:00
import React, {useEffect, useState} from 'react';
import {StyleSheet, Text, View} from 'react-native';
import {Button, Dialog, Modal, Portal, TextInput} from 'react-native-paper';
2022-07-06 05:40:53 +00:00
import DayMenu from './DayMenu';
2022-07-06 06:26:43 +00:00
import WorkoutMenu from './WorkoutMenu';
2022-07-06 05:40:53 +00:00
import {getDb} from './db';
import {Plan} from './plan';
export default function EditPlan({
id,
onSave,
show,
setShow,
clearId,
}: {
id?: number;
clearId: () => void;
onSave: () => void;
show: boolean;
setShow: (visible: boolean) => void;
}) {
const [days, setDays] = useState('');
const [workouts, setWorkouts] = useState('');
2022-07-06 06:26:43 +00:00
const [names, setNames] = useState<string[]>([]);
2022-07-06 05:40:53 +00:00
useEffect(() => {
getDb().then(async db => {
2022-07-06 06:26:43 +00:00
const [namesResult] = await db.executeSql(
'SELECT DISTINCT name FROM sets',
);
if (!namesResult.rows.length) return;
setNames(namesResult.rows.raw().map(({name}) => name));
if (!id) return;
2022-07-06 05:40:53 +00:00
const [result] = await db.executeSql(`SELECT * FROM plans WHERE id = ?`, [
id,
]);
if (!result.rows.item(0)) throw new Error("Can't find specified Set.");
const set: Plan = result.rows.item(0);
setDays(set.days);
setWorkouts(set.workouts);
});
}, [id]);
const save = async () => {
if (!days || !workouts) return;
const db = await getDb();
if (!id)
await db.executeSql(`INSERT INTO plans(days, workouts) VALUES (?, ?)`, [
days,
workouts,
]);
else
await db.executeSql(
`UPDATE plans SET days = ?, workouts = ? WHERE id = ?`,
[days, workouts, id],
);
setShow(false);
onSave();
};
const selectDay = (day: string, index: number) => {
const newDays = days.split(',');
newDays[index] = day;
setDays(newDays.join(','));
};
2022-07-06 06:26:43 +00:00
const selectWorkout = (workout: string, index: number) => {
const newWorkouts = workouts.split(',');
newWorkouts[index] = workout;
setWorkouts(newWorkouts.join(','));
};
const removeWorkout = (index: number) => {
const newWorkouts = workouts.split(',');
delete newWorkouts[index];
setWorkouts(newWorkouts.filter(day => day).join(','));
};
2022-07-06 05:40:53 +00:00
const removeDay = (index: number) => {
const newDays = days.split(',');
delete newDays[index];
setDays(newDays.filter(day => day).join(','));
};
return (
<Portal>
<Dialog visible={show} onDismiss={() => setShow(false)}>
<Dialog.Title>{id ? `Edit "${days}"` : 'Add a plan'}</Dialog.Title>
2022-07-06 10:02:43 +00:00
<Dialog.Content style={{alignItems: 'flex-end'}}>
2022-07-06 05:40:53 +00:00
{days.split(',').map((day, index) => (
<DayMenu
index={index}
onDelete={() => removeDay(index)}
onSelect={option => selectDay(option, index)}
onAdd={() => setDays(days + ',Monday')}
selected={day}
key={index}
/>
))}
2022-07-06 10:02:43 +00:00
<Button icon="add" onPress={() => setDays(days + ',Monday')}>
Add day
</Button>
2022-07-06 06:26:43 +00:00
{workouts.split(',').map((workout, index) => (
<WorkoutMenu
index={index}
selected={workout}
onAdd={() => setWorkouts(workouts + ',')}
onSelect={option => selectWorkout(option, index)}
onDelete={() => removeWorkout(index)}
names={names}
key={index}
/>
))}
2022-07-06 10:02:43 +00:00
<Button icon="add" onPress={() => setWorkouts(workouts + ',')}>
Add workout
</Button>
</Dialog.Content>
<Dialog.Actions>
2022-07-06 05:40:53 +00:00
<Button icon="close" onPress={() => setShow(false)}>
Cancel
</Button>
2022-07-06 10:02:43 +00:00
<Button mode="contained" icon="save" onPress={save}>
Save
</Button>
</Dialog.Actions>
</Dialog>
2022-07-06 05:40:53 +00:00
</Portal>
);
}