2022-07-11 00:28:30 +00:00
|
|
|
import {
|
2022-08-28 08:54:07 +00:00
|
|
|
NavigationProp,
|
2022-07-11 00:28:30 +00:00
|
|
|
RouteProp,
|
|
|
|
useNavigation,
|
|
|
|
useRoute,
|
2023-08-12 03:22:50 +00:00
|
|
|
} 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 { PlanPageParams } from "./plan-page-params";
|
|
|
|
import StackHeader from "./StackHeader";
|
|
|
|
import Switch from "./Switch";
|
|
|
|
import { DAYS } from "./time";
|
2022-07-06 05:40:53 +00:00
|
|
|
|
2022-07-11 00:28:30 +00:00
|
|
|
export default function EditPlan() {
|
2023-08-12 03:22:50 +00:00
|
|
|
const { params } = useRoute<RouteProp<PlanPageParams, "EditPlan">>();
|
|
|
|
const { plan } = params;
|
2022-09-18 06:45:05 +00:00
|
|
|
const [days, setDays] = useState<string[]>(
|
2023-08-12 03:22:50 +00:00
|
|
|
plan.days ? plan.days.split(",") : []
|
|
|
|
);
|
2022-07-11 00:28:30 +00:00
|
|
|
const [workouts, setWorkouts] = useState<string[]>(
|
2023-08-12 03:22:50 +00:00
|
|
|
plan.workouts ? plan.workouts.split(",") : []
|
|
|
|
);
|
|
|
|
const [names, setNames] = useState<string[]>([]);
|
|
|
|
const navigation = useNavigation<NavigationProp<PlanPageParams>>();
|
2022-07-11 00:28:30 +00:00
|
|
|
|
2022-07-06 05:40:53 +00:00
|
|
|
useEffect(() => {
|
2022-10-31 00:20:36 +00:00
|
|
|
setRepo
|
|
|
|
.createQueryBuilder()
|
2023-08-12 03:22:50 +00:00
|
|
|
.select("name")
|
2022-10-31 00:20:36 +00:00
|
|
|
.distinct(true)
|
2023-08-12 03:22:50 +00:00
|
|
|
.orderBy("name")
|
2022-10-31 00:20:36 +00:00
|
|
|
.getRawMany()
|
2023-06-27 03:16:59 +00:00
|
|
|
.then((values) => {
|
2023-08-12 03:22:50 +00:00
|
|
|
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 () => {
|
2023-08-12 03:22:50 +00:00
|
|
|
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(",");
|
|
|
|
await planRepo.save({ days: newDays, workouts: newWorkouts, id: plan.id });
|
|
|
|
}, [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) {
|
2023-08-12 03:22:50 +00:00
|
|
|
setWorkouts([...workouts, name]);
|
2022-07-09 23:51:52 +00:00
|
|
|
} else {
|
2023-08-12 03:22:50 +00:00
|
|
|
setWorkouts(workouts.filter((workout) => workout !== name));
|
2022-07-09 23:51:52 +00:00
|
|
|
}
|
|
|
|
},
|
2023-08-12 03:22:50 +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) {
|
2023-08-12 03:22:50 +00:00
|
|
|
setDays([...days, day]);
|
2022-07-09 23:51:52 +00:00
|
|
|
} else {
|
2023-08-12 03:22:50 +00:00
|
|
|
setDays(days.filter((d) => d !== day));
|
2022-07-09 23:51:52 +00:00
|
|
|
}
|
|
|
|
},
|
2023-08-12 03:22:50 +00:00
|
|
|
[setDays, days]
|
|
|
|
);
|
2022-07-06 05:40:53 +00:00
|
|
|
|
|
|
|
return (
|
2022-10-23 06:13:58 +00:00
|
|
|
<>
|
2023-01-08 05:05:59 +00:00
|
|
|
<StackHeader
|
2023-08-12 03:22:50 +00:00
|
|
|
title={typeof plan.id === "number" ? "Edit plan" : "Add plan"}
|
2023-06-27 03:16:59 +00:00
|
|
|
>
|
2023-08-12 03:22:50 +00:00
|
|
|
{typeof plan.id === "number" && (
|
2023-07-03 23:17:58 +00:00
|
|
|
<IconButton
|
|
|
|
onPress={async () => {
|
2023-08-12 03:22:50 +00:00
|
|
|
await save();
|
|
|
|
const newPlan = await planRepo.findOne({
|
|
|
|
where: { id: plan.id },
|
|
|
|
});
|
2023-07-23 01:56:12 +00:00
|
|
|
let first = await setRepo.findOne({
|
|
|
|
where: { name: workouts[0] },
|
2023-08-12 03:22:50 +00:00
|
|
|
order: { created: "desc" },
|
|
|
|
});
|
|
|
|
if (!first) first = { ...defaultSet, name: workouts[0] };
|
|
|
|
delete first.id;
|
|
|
|
navigation.navigate("StartPlan", { plan: newPlan, first });
|
2023-07-03 23:17:58 +00:00
|
|
|
}}
|
2023-08-12 03:22:50 +00:00
|
|
|
icon="play-arrow"
|
2023-07-03 23:17:58 +00:00
|
|
|
/>
|
|
|
|
)}
|
2023-03-27 23:15:56 +00:00
|
|
|
</StackHeader>
|
2023-06-27 03:16:59 +00:00
|
|
|
<View style={{ padding: PADDING, flex: 1 }}>
|
|
|
|
<ScrollView style={{ flex: 1 }}>
|
2022-10-23 06:13:58 +00:00
|
|
|
<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
|
2022-10-23 06:13:58 +00:00
|
|
|
key={day}
|
2023-06-27 03:16:59 +00:00
|
|
|
onChange={(value) => toggleDay(value, day)}
|
2022-12-30 06:49:54 +00:00
|
|
|
value={days.includes(day)}
|
|
|
|
title={day}
|
|
|
|
/>
|
2022-10-23 06:13:58 +00:00
|
|
|
))}
|
2023-06-27 03:16:59 +00:00
|
|
|
<Text style={[styles.title, { marginTop: MARGIN }]}>Workouts</Text>
|
2023-08-12 03:22:50 +00:00
|
|
|
{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}
|
|
|
|
/>
|
|
|
|
))
|
|
|
|
)}
|
2022-10-23 06:13:58 +00:00
|
|
|
</ScrollView>
|
2022-11-16 05:48:16 +00:00
|
|
|
|
|
|
|
<Button
|
|
|
|
disabled={workouts.length === 0 && days.length === 0}
|
|
|
|
style={styles.button}
|
2023-08-12 03:22:50 +00:00
|
|
|
mode="outlined"
|
|
|
|
icon="save"
|
2023-07-31 03:54:32 +00:00
|
|
|
onPress={async () => {
|
2023-08-12 03:22:50 +00:00
|
|
|
await save();
|
|
|
|
navigation.navigate("PlanList");
|
2023-07-31 03:54:32 +00:00
|
|
|
}}
|
2023-06-27 03:16:59 +00:00
|
|
|
>
|
2022-11-16 05:48:16 +00:00
|
|
|
Save
|
|
|
|
</Button>
|
2022-10-23 06:13:58 +00:00
|
|
|
</View>
|
|
|
|
</>
|
2023-08-12 03:22:50 +00:00
|
|
|
);
|
2022-07-06 05:40:53 +00:00
|
|
|
}
|
2022-07-07 04:17:55 +00:00
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
|
|
|
title: {
|
|
|
|
fontSize: 20,
|
2022-09-16 09:07:02 +00:00
|
|
|
marginBottom: MARGIN,
|
2022-07-07 04:17:55 +00:00
|
|
|
},
|
2022-11-30 05:14:34 +00:00
|
|
|
button: {},
|
2023-08-12 03:22:50 +00:00
|
|
|
});
|