Massive/EditPlan.tsx

152 lines
4.3 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,
useFocusEffect,
useNavigation,
useRoute,
} from '@react-navigation/native';
import React, {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 {DrawerParamList} from './drawer-param-list';
import {PlanPageParams} from './plan-page-params';
import {addPlan, updatePlan} from './plan.service';
import {getNames} from './set.service';
2022-09-26 03:10:13 +00:00
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() {
const {params} = useRoute<RouteProp<PlanPageParams, 'EditPlan'>>();
2022-09-18 06:45:05 +00:00
const {plan} = params;
const [days, setDays] = useState<string[]>(
plan.days ? plan.days.split(',') : [],
);
2022-07-11 00:28:30 +00:00
const [workouts, setWorkouts] = useState<string[]>(
2022-09-18 06:45:05 +00:00
plan.workouts ? plan.workouts.split(',') : [],
2022-07-11 00:28:30 +00:00
);
2022-07-06 06:26:43 +00:00
const [names, setNames] = useState<string[]>([]);
const navigation = useNavigation<NavigationProp<DrawerParamList>>();
2022-07-11 00:28:30 +00:00
useFocusEffect(
useCallback(() => {
2022-09-18 06:45:05 +00:00
console.log(`${EditPlan.name}.focus:`, {plan});
2022-07-11 00:28:30 +00:00
navigation.getParent()?.setOptions({
headerLeft: () => (
<IconButton icon="arrow-back" onPress={() => navigation.goBack()} />
),
2022-08-25 01:06:50 +00:00
headerRight: () => null,
2022-09-18 06:45:05 +00:00
title: plan.id ? 'Edit plan' : 'Create plan',
2022-07-11 00:28:30 +00:00
});
2022-09-18 06:45:05 +00:00
}, [navigation, plan]),
2022-07-11 00:28:30 +00:00
);
2022-07-06 05:40:53 +00:00
useEffect(() => {
2022-09-04 05:33:05 +00:00
getNames().then(n => {
console.log(EditPlan.name, {n});
setNames(n);
});
}, []);
2022-07-06 05:40:53 +00:00
2022-07-09 23:51:52 +00:00
const save = useCallback(async () => {
2022-09-18 06:45:05 +00:00
console.log(`${EditPlan.name}.save`, {days, workouts, plan});
2022-07-06 05:40:53 +00:00
if (!days || !workouts) return;
2022-07-07 04:17:55 +00:00
const newWorkouts = workouts.filter(workout => workout).join(',');
const newDays = days.filter(day => day).join(',');
if (typeof plan.id === 'undefined')
await addPlan({days: newDays, workouts: newWorkouts});
2022-07-06 05:40:53 +00:00
else
await updatePlan({
days: newDays,
workouts: newWorkouts,
2022-09-18 06:45:05 +00:00
id: plan.id,
});
2022-07-11 00:28:30 +00:00
navigation.goBack();
2022-09-18 06:45:05 +00:00
}, [days, workouts, plan, navigation]);
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]);
} else {
setWorkouts(workouts.filter(workout => workout !== name));
}
},
2022-07-09 23:54:23 +00:00
[setWorkouts, workouts],
2022-07-09 23:51:52 +00:00
);
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]);
} else {
setDays(days.filter(d => d !== day));
}
},
2022-07-09 23:54:23 +00:00
[setDays, days],
2022-07-09 23:51:52 +00:00
);
2022-07-06 05:40:53 +00:00
return (
<View style={{padding: PADDING, flex: 1}}>
<ScrollView style={{flex: 1}}>
2022-07-11 00:28:30 +00:00
<Text style={styles.title}>Days</Text>
{DAYS.map(day => (
2022-09-26 03:10:13 +00:00
<Switch
2022-09-26 23:26:03 +00:00
key={day}
2022-09-26 03:10:13 +00:00
onValueChange={value => toggleDay(value, day)}
onPress={() => toggleDay(!days.includes(day), day)}
value={days.includes(day)}>
{day}
</Switch>
2022-07-11 00:28:30 +00:00
))}
<Text style={[styles.title, {marginTop: MARGIN}]}>Workouts</Text>
2022-09-26 03:10:13 +00:00
{names.length === 0 ? (
<View>
<Text>No workouts found.</Text>
</View>
2022-09-26 03:10:13 +00:00
) : (
names.map(name => (
<Switch
2022-09-26 23:26:03 +00:00
key={name}
2022-07-11 00:28:30 +00:00
onValueChange={value => toggleWorkout(value, name)}
2022-09-26 03:10:13 +00:00
value={workouts.includes(name)}
onPress={() => toggleWorkout(!workouts.includes(name), name)}>
2022-07-11 00:28:30 +00:00
{name}
2022-09-26 03:10:13 +00:00
</Switch>
))
)}
2022-07-11 00:28:30 +00:00
</ScrollView>
{names.length === 0 ? (
<Button
2022-09-18 06:45:05 +00:00
disabled={workouts.length === 0 && days.length === 0}
mode="contained"
onPress={() => {
navigation.goBack();
navigation.navigate('Workouts', {
screen: 'EditWorkout',
params: {value: {name: ''}},
});
}}>
Add workout
</Button>
) : (
<Button
2022-09-18 06:45:05 +00:00
disabled={workouts.length === 0 && days.length === 0}
style={{marginTop: MARGIN}}
mode="contained"
icon="save"
onPress={save}>
Save
</Button>
)}
2022-07-11 00:28:30 +00:00
</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
},
});