Massive/EditPlan.tsx

152 lines
4.2 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';
2022-08-25 01:06:50 +00:00
import {ScrollView, StyleSheet, Text, View} from 'react-native';
import {Button, IconButton} from 'react-native-paper';
2022-09-02 00:33:53 +00:00
import {DrawerParamList} from './App';
import {MARGIN, PADDING} from './constants';
2022-07-17 01:45:31 +00:00
import MassiveSwitch from './MassiveSwitch';
import {addPlan, updatePlan} from './plan.service';
2022-07-11 00:28:30 +00:00
import {PlanPageParams} from './PlanPage';
import {getNames} from './set.service';
2022-07-09 04:38:57 +00:00
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'>>();
const [days, setDays] = useState<string[]>(params.plan.days.split(','));
const [workouts, setWorkouts] = useState<string[]>(
params.plan.workouts.split(','),
);
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(() => {
navigation.getParent()?.setOptions({
headerLeft: () => (
<IconButton icon="arrow-back" onPress={() => navigation.goBack()} />
),
2022-08-25 01:06:50 +00:00
headerRight: () => null,
title: params.plan.id ? 'Edit plan' : 'Create plan',
2022-07-11 00:28:30 +00:00
});
}, [navigation, params.plan.id]),
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-07-11 00:28:30 +00:00
console.log(`${EditPlan.name}.save`, {days, workouts, params});
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 (!params.plan.id) await addPlan({days: newDays, workouts: newWorkouts});
2022-07-06 05:40:53 +00:00
else
await updatePlan({
days: newDays,
workouts: newWorkouts,
id: params.plan.id,
});
2022-07-11 00:28:30 +00:00
navigation.goBack();
}, [days, workouts, params, 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}}>
2022-07-11 00:28:30 +00:00
<ScrollView style={{height: '90%'}}>
<Text style={styles.title}>Days</Text>
{DAYS.map(day => (
<View key={day} style={[styles.row, {alignItems: 'center'}]}>
2022-07-17 01:45:31 +00:00
<MassiveSwitch
2022-07-11 00:28:30 +00:00
value={days.includes(day)}
onValueChange={value => toggleDay(value, day)}
/>
<Text onPress={() => toggleDay(!days.includes(day), day)}>
{day}
</Text>
</View>
))}
<Text style={[styles.title, {marginTop: MARGIN}]}>Workouts</Text>
2022-07-11 00:28:30 +00:00
{names.length === 0 && (
<View>
<Text>No workouts found.</Text>
</View>
2022-07-11 00:28:30 +00:00
)}
{names.map(name => (
<View key={name} style={[styles.row, {alignItems: 'center'}]}>
2022-07-17 01:45:31 +00:00
<MassiveSwitch
2022-07-11 00:28:30 +00:00
value={workouts.includes(name)}
onValueChange={value => toggleWorkout(value, name)}
/>
<Text onPress={() => toggleWorkout(!workouts.includes(name), name)}>
{name}
</Text>
</View>
))}
</ScrollView>
{names.length === 0 ? (
<Button
mode="contained"
onPress={() => {
navigation.goBack();
navigation.navigate('Workouts', {
screen: 'EditWorkout',
params: {value: {name: ''}},
});
}}>
Add workout
</Button>
) : (
<Button
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
},
row: {
flexDirection: 'row',
2022-07-09 05:10:28 +00:00
flexWrap: 'wrap',
2022-07-07 04:17:55 +00:00
},
});