import { RouteProp, useFocusEffect, useNavigation, useRoute, } from '@react-navigation/native' import { useCallback, useState } from 'react' import { View } from 'react-native' import DocumentPicker from 'react-native-document-picker' import { Button, Card, TouchableRipple } from 'react-native-paper' import { In } from 'typeorm' import AppInput from './AppInput' import ConfirmDialog from './ConfirmDialog' import { MARGIN, PADDING } from './constants' import { setRepo, settingsRepo } from './db' import GymSet from './gym-set' import { HomePageParams } from './home-page-params' import Settings from './settings' import StackHeader from './StackHeader' export default function EditSets() { const { params } = useRoute>() const { ids } = params const navigation = useNavigation() const [settings, setSettings] = useState({} as Settings) const [name, setName] = useState('') const [reps, setReps] = useState('') const [weight, setWeight] = useState('') const [newImage, setNewImage] = useState('') const [unit, setUnit] = useState('') const [showRemove, setShowRemove] = useState(false) const [names, setNames] = useState('') const [oldReps, setOldReps] = useState('') const [weights, setWeights] = useState('') const [units, setUnits] = useState('') const [selection, setSelection] = useState({ start: 0, end: 1, }) useFocusEffect( useCallback(() => { settingsRepo.findOne({ where: {} }).then(setSettings) setRepo.find({ where: { id: In(ids) } }).then((sets) => { setNames(sets.map((set) => set.name).join(', ')) setOldReps(sets.map((set) => set.reps).join(', ')) setWeights(sets.map((set) => set.weight).join(', ')) setUnits(sets.map((set) => set.unit).join(', ')) }) }, [ids]), ) const handleSubmit = async () => { console.log(`${EditSets.name}.handleSubmit:`, { uri: newImage, name }) const update: Partial = {} if (name) update.name = name if (reps) update.reps = Number(reps) if (weight) update.weight = Number(weight) if (unit) update.unit = unit if (newImage) update.image = newImage if (Object.keys(update).length > 0) await setRepo.update(ids, update) navigation.goBack() } const changeImage = useCallback(async () => { const { fileCopyUri } = await DocumentPicker.pickSingle({ type: DocumentPicker.types.images, copyTo: 'documentDirectory', }) if (fileCopyUri) setNewImage(fileCopyUri) }, []) const handleRemove = useCallback(async () => { setNewImage('') setShowRemove(false) }, []) return ( <> setSelection(e.nativeEvent.selection)} autoFocus={!!name} /> {settings.showUnit && ( )} {settings.images && newImage && ( setShowRemove(true)} > )} Are you sure you want to remove the image? {settings.images && !newImage && ( )} ) }