Massive/EditSet.tsx

95 lines
2.8 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-20 03:48:48 +00:00
import React, {useCallback, useContext} from 'react';
2022-08-28 05:25:31 +00:00
import {NativeModules, View} from 'react-native';
2022-07-20 03:48:48 +00:00
import {IconButton} from 'react-native-paper';
import {PADDING} from './constants';
import {HomePageParams} from './home-page-params';
import {SnackbarContext} from './MassiveSnack';
import Set from './set';
import {addSet, updateSet} from './set.service';
2022-07-20 03:48:48 +00:00
import SetForm from './SetForm';
2022-09-26 01:38:25 +00:00
import {getSettings, settings, updateSettings} from './settings.service';
2022-07-03 01:50:01 +00:00
export default function EditSet() {
2022-07-11 00:28:30 +00:00
const {params} = useRoute<RouteProp<HomePageParams, 'EditSet'>>();
const {set, count, workouts} = params;
const navigation = useNavigation();
const {toast} = useContext(SnackbarContext);
2022-07-11 00:28:30 +00:00
useFocusEffect(
useCallback(() => {
console.log(`${EditSet.name}.focus:`, set);
let title = 'Create set';
if (typeof set.id === 'number') title = 'Edit set';
else if (Number(set.sets) > 0 && settings.newSet === 'predict')
title = `${set.name} (${count + 1} / ${set.sets})`;
navigation.getParent()?.setOptions({
headerLeft: () => (
<IconButton icon="arrow-back" onPress={() => navigation.goBack()} />
),
headerRight: null,
title,
2022-07-11 00:28:30 +00:00
});
}, [navigation, set, count]),
2022-07-11 00:28:30 +00:00
);
const startTimer = useCallback(async (value: Set) => {
if (!settings.alarm) return;
const milliseconds =
Number(value.minutes) * 60 * 1000 + Number(value.seconds) * 1000;
NativeModules.AlarmModule.timer(
milliseconds,
!!settings.vibrate,
settings.sound,
);
2022-09-26 01:38:25 +00:00
const next = new Date();
next.setTime(next.getTime() + milliseconds);
await updateSettings({...settings, nextAlarm: next.toISOString()});
await getSettings();
}, []);
2022-07-20 03:48:48 +00:00
const update = useCallback(
async (value: Set) => {
console.log(`${EditSet.name}.update`, value);
await updateSet(value);
2022-07-20 03:48:48 +00:00
navigation.goBack();
},
[navigation],
2022-07-20 03:48:48 +00:00
);
2022-07-20 03:48:48 +00:00
const add = useCallback(
async (value: Set) => {
console.log(`${EditSet.name}.add`, {set: value});
startTimer(value);
await addSet(value);
if (!settings.notify) return navigation.goBack();
2022-08-24 01:04:45 +00:00
if (
value.weight > set.weight ||
(value.reps > set.reps && value.weight === set.weight)
2022-08-24 01:04:45 +00:00
)
2022-08-27 06:09:45 +00:00
toast("Great work King, that's a new record!", 3000);
2022-07-20 03:48:48 +00:00
navigation.goBack();
},
[navigation, startTimer, set, toast],
2022-07-20 03:48:48 +00:00
);
const save = useCallback(
async (value: Set) => {
if (typeof set.id === 'number') return update(value);
return add(value);
2022-07-20 03:48:48 +00:00
},
[update, add, set.id],
2022-07-20 03:48:48 +00:00
);
2022-07-03 01:50:01 +00:00
return (
<View style={{padding: PADDING}}>
<SetForm save={save} set={set} workouts={workouts} />
2022-07-11 00:28:30 +00:00
</View>
2022-07-03 01:50:01 +00:00
);
}