Massive/EditPlan.tsx

143 lines
4.1 KiB
TypeScript
Raw Normal View History

2022-07-11 00:28:30 +00:00
import {
RouteProp,
useFocusEffect,
useNavigation,
useRoute,
} from '@react-navigation/native';
2022-07-09 23:51:52 +00:00
import React, {useCallback, useContext, 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';
import {DatabaseContext} from './App';
2022-07-17 01:45:31 +00:00
import MassiveSwitch from './MassiveSwitch';
2022-07-11 00:28:30 +00:00
import {PlanPageParams} from './PlanPage';
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 db = useContext(DatabaseContext);
2022-07-11 00:28:30 +00:00
const navigation = useNavigation();
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-07-09 01:27:19 +00:00
const refresh = async () => {
const [namesResult] = await db.executeSql(
'SELECT DISTINCT name FROM sets',
);
if (!namesResult.rows.length) return setNames([]);
setNames(namesResult.rows.raw().map(({name}) => name));
};
refresh();
2022-07-11 00:28:30 +00:00
}, [db]);
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(',');
2022-07-11 00:28:30 +00:00
if (!params.plan.id)
2022-07-06 05:40:53 +00:00
await db.executeSql(`INSERT INTO plans(days, workouts) VALUES (?, ?)`, [
2022-07-07 04:17:55 +00:00
newDays,
newWorkouts,
2022-07-06 05:40:53 +00:00
]);
else
await db.executeSql(
`UPDATE plans SET days = ?, workouts = ? WHERE id = ?`,
2022-07-11 00:28:30 +00:00
[newDays, newWorkouts, params.plan.id],
2022-07-06 05:40:53 +00:00
);
2022-07-11 00:28:30 +00:00
navigation.goBack();
2022-07-11 00:32:13 +00:00
}, [days, workouts, db, 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 (
2022-07-11 00:28:30 +00:00
<View style={{padding: 10}}>
<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: 10}]}>Workouts</Text>
{names.length === 0 && (
<Text style={{maxWidth: '80%'}}>
No sets found. Try going to the home page and adding some workouts
first.
</Text>
)}
{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>
<Button
style={{marginTop: 10}}
mode="contained"
icon="save"
onPress={save}>
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: 10,
},
row: {
flexDirection: 'row',
2022-07-09 05:10:28 +00:00
flexWrap: 'wrap',
2022-07-07 04:17:55 +00:00
},
});