Massive/EditPlan.tsx

165 lines
4.6 KiB
TypeScript
Raw Normal View History

2022-07-11 00:28:30 +00:00
import {
NavigationProp,
2022-07-11 00:28:30 +00:00
RouteProp,
useNavigation,
useRoute,
} from "@react-navigation/native";
import { useCallback, useEffect, useState } from "react";
import { ScrollView, StyleSheet, View } from "react-native";
import { Button, IconButton, Text } from "react-native-paper";
import { MARGIN, PADDING } from "./constants";
import { planRepo, setRepo } from "./db";
import { defaultSet } from "./gym-set";
import StackHeader from "./StackHeader";
import Switch from "./Switch";
import { DAYS } from "./time";
2023-08-21 12:25:29 +00:00
import AppInput from "./AppInput";
import { StackParams } from "./AppStack";
import { DrawerParams } from "./drawer-param-list";
2022-07-06 05:40:53 +00:00
2022-07-11 00:28:30 +00:00
export default function EditPlan() {
const { params } = useRoute<RouteProp<StackParams, "EditPlan">>();
const { plan } = params;
2023-08-21 12:25:29 +00:00
const [title, setTitle] = useState<string>(plan?.title);
2022-09-18 06:45:05 +00:00
const [days, setDays] = useState<string[]>(
plan.days ? plan.days.split(",") : []
);
2022-07-11 00:28:30 +00:00
const [workouts, setWorkouts] = useState<string[]>(
plan.workouts ? plan.workouts.split(",") : []
);
const [names, setNames] = useState<string[]>([]);
const { navigate: drawerNavigate } =
useNavigation<NavigationProp<DrawerParams>>();
const { navigate: stackNavigate } =
useNavigation<NavigationProp<StackParams>>();
2022-07-11 00:28:30 +00:00
2022-07-06 05:40:53 +00:00
useEffect(() => {
setRepo
.createQueryBuilder()
.select("name")
.distinct(true)
.orderBy("name")
.getRawMany()
2023-06-27 03:16:59 +00:00
.then((values) => {
console.log(EditPlan.name, { values });
setNames(values.map((value) => value.name));
});
}, []);
2022-07-06 05:40:53 +00:00
2022-07-09 23:51:52 +00:00
const save = useCallback(async () => {
console.log(`${EditPlan.name}.save`, { days, workouts, plan });
if (!days || !workouts) return;
const newWorkouts = workouts.filter((workout) => workout).join(",");
const newDays = days.filter((day) => day).join(",");
2023-08-21 12:25:29 +00:00
await planRepo.save({
title: title,
days: newDays,
workouts: newWorkouts,
id: plan.id,
});
}, [title, days, workouts, plan]);
2022-07-06 05:40:53 +00:00
2022-07-09 23:51:52 +00:00
const toggleWorkout = useCallback(
(on: boolean, name: string) => {
if (on) {
setWorkouts([...workouts, name]);
2022-07-09 23:51:52 +00:00
} else {
setWorkouts(workouts.filter((workout) => workout !== name));
2022-07-09 23:51:52 +00:00
}
},
[setWorkouts, workouts]
);
2022-07-06 06:26:43 +00:00
2022-07-09 23:51:52 +00:00
const toggleDay = useCallback(
(on: boolean, day: string) => {
if (on) {
setDays([...days, day]);
2022-07-09 23:51:52 +00:00
} else {
setDays(days.filter((d) => d !== day));
2022-07-09 23:51:52 +00:00
}
},
[setDays, days]
);
2022-07-06 05:40:53 +00:00
return (
<>
2023-01-08 05:05:59 +00:00
<StackHeader
title={typeof plan.id === "number" ? "Edit plan" : "Add plan"}
2023-06-27 03:16:59 +00:00
>
{typeof plan.id === "number" && (
<IconButton
onPress={async () => {
await save();
const newPlan = await planRepo.findOne({
where: { id: plan.id },
});
let first = await setRepo.findOne({
where: { name: workouts[0] },
order: { created: "desc" },
});
if (!first) first = { ...defaultSet, name: workouts[0] };
delete first.id;
stackNavigate("StartPlan", { plan: newPlan, first });
}}
2023-10-19 05:28:56 +00:00
icon="play"
/>
)}
</StackHeader>
2023-06-27 03:16:59 +00:00
<View style={{ padding: PADDING, flex: 1 }}>
<ScrollView style={{ flex: 1 }}>
2023-08-21 12:25:29 +00:00
<AppInput
label="Title"
value={title}
onChangeText={(value) => setTitle(value)}
/>
<Text style={styles.title}>Days</Text>
2023-06-27 03:16:59 +00:00
{DAYS.map((day) => (
2022-09-26 03:10:13 +00:00
<Switch
key={day}
2023-06-27 03:16:59 +00:00
onChange={(value) => toggleDay(value, day)}
value={days.includes(day)}
title={day}
/>
))}
2023-06-27 03:16:59 +00:00
<Text style={[styles.title, { marginTop: MARGIN }]}>Workouts</Text>
{names.length === 0 ? (
<View>
<Text>No workouts found.</Text>
</View>
) : (
names.map((name) => (
<Switch
key={name}
onChange={(value) => toggleWorkout(value, name)}
value={workouts.includes(name)}
title={name}
/>
))
)}
</ScrollView>
<Button
disabled={workouts.length === 0 && days.length === 0}
style={styles.button}
mode="outlined"
2023-10-19 05:28:56 +00:00
icon="content-save"
onPress={async () => {
await save();
drawerNavigate("Plans");
}}
2023-06-27 03:16:59 +00:00
>
Save
</Button>
</View>
</>
);
2022-07-06 05:40:53 +00:00
}
2022-07-07 04:17:55 +00:00
const styles = StyleSheet.create({
title: {
fontSize: 20,
marginBottom: MARGIN,
2022-07-07 04:17:55 +00:00
},
button: {},
});