Use events for gym set CRUD

This commit is contained in:
Brandon Presley 2023-10-18 19:06:13 +13:00
parent cfcc15600c
commit a3b376badb
9 changed files with 68 additions and 63 deletions

View File

@ -15,12 +15,13 @@ import AppInput from "./AppInput";
import ConfirmDialog from "./ConfirmDialog"; import ConfirmDialog from "./ConfirmDialog";
import { MARGIN, PADDING } from "./constants"; import { MARGIN, PADDING } from "./constants";
import { getNow, setRepo, settingsRepo } from "./db"; import { getNow, setRepo, settingsRepo } from "./db";
import GymSet from "./gym-set"; import GymSet, { GYM_SET_CREATED, GYM_SET_UPDATED } from "./gym-set";
import { HomePageParams } from "./home-page-params"; import { HomePageParams } from "./home-page-params";
import Settings from "./settings"; import Settings from "./settings";
import StackHeader from "./StackHeader"; import StackHeader from "./StackHeader";
import { toast } from "./toast"; import { toast } from "./toast";
import { fixNumeric } from "./fix-numeric"; import { fixNumeric } from "./fix-numeric";
import { emitter } from "./emitter";
export default function EditSet() { export default function EditSet() {
const { params } = useRoute<RouteProp<HomePageParams, "EditSet">>(); const { params } = useRoute<RouteProp<HomePageParams, "EditSet">>();
@ -64,18 +65,20 @@ export default function EditSet() {
[settings] [settings]
); );
const added = async (value: GymSet) => { const notify = (value: Partial<GymSet>) => {
startTimer(value.name); if (!settings.notify) return navigate("Sets");
console.log(`${EditSet.name}.add`, { set: value });
if (!settings.notify) return navigate("Sets", { reset: value.id });
if ( if (
value.weight > set.weight || value.weight > set.weight ||
(value.reps > set.reps && value.weight === set.weight) (value.reps > set.reps && value.weight === set.weight)
) { ) {
toast("Great work King! That's a new record."); toast("Great work King! That's a new record.");
} }
console.log("Navigating..."); };
navigate("Sets", { reset: value.id });
const added = async (value: GymSet) => {
console.log(`${EditSet.name}.added:`, value);
emitter.emit(GYM_SET_CREATED);
startTimer(value.name);
}; };
const handleSubmit = async () => { const handleSubmit = async () => {
@ -104,9 +107,10 @@ export default function EditSet() {
if (typeof set.id !== "number") newSet.created = await getNow(); if (typeof set.id !== "number") newSet.created = await getNow();
const saved = await setRepo.save(newSet); const saved = await setRepo.save(newSet);
notify(newSet);
if (typeof set.id !== "number") return added(saved); if (typeof set.id !== "number") return added(saved);
if (createdDirty) navigate("Sets", { reset: saved.id }); else emitter.emit(GYM_SET_UPDATED, saved);
else navigate("Sets", { refresh: saved }); navigate("Sets");
}; };
const changeImage = useCallback(async () => { const changeImage = useCallback(async () => {

View File

@ -13,8 +13,9 @@ import AppInput from "./AppInput";
import ConfirmDialog from "./ConfirmDialog"; import ConfirmDialog from "./ConfirmDialog";
import { MARGIN, PADDING } from "./constants"; import { MARGIN, PADDING } from "./constants";
import { getNow, planRepo, setRepo, settingsRepo } from "./db"; import { getNow, planRepo, setRepo, settingsRepo } from "./db";
import { emitter } from "./emitter";
import { fixNumeric } from "./fix-numeric"; import { fixNumeric } from "./fix-numeric";
import GymSet, { defaultSet } from "./gym-set"; import GymSet, { defaultSet, GYM_SET_CREATED } from "./gym-set";
import Settings from "./settings"; import Settings from "./settings";
import StackHeader from "./StackHeader"; import StackHeader from "./StackHeader";
import { toast } from "./toast"; import { toast } from "./toast";
@ -79,6 +80,7 @@ export default function EditWorkout() {
steps, steps,
created: now, created: now,
}); });
emitter.emit(GYM_SET_CREATED);
navigate("WorkoutList", { reset: new Date().getTime() }); navigate("WorkoutList", { reset: new Date().getTime() });
}; };

View File

@ -14,7 +14,9 @@ import AppInput from "./AppInput";
import ConfirmDialog from "./ConfirmDialog"; import ConfirmDialog from "./ConfirmDialog";
import { MARGIN, PADDING } from "./constants"; import { MARGIN, PADDING } from "./constants";
import { planRepo, setRepo, settingsRepo } from "./db"; import { planRepo, setRepo, settingsRepo } from "./db";
import { emitter } from "./emitter";
import { fixNumeric } from "./fix-numeric"; import { fixNumeric } from "./fix-numeric";
import { GYM_SET_CREATED } from "./gym-set";
import Settings from "./settings"; import Settings from "./settings";
import StackHeader from "./StackHeader"; import StackHeader from "./StackHeader";
import { toast } from "./toast"; import { toast } from "./toast";
@ -74,6 +76,7 @@ export default function EditWorkouts() {
image: removeImage ? "" : uri, image: removeImage ? "" : uri,
} }
); );
emitter.emit(GYM_SET_CREATED);
for (const oldName of params.names) { for (const oldName of params.names) {
await planRepo await planRepo
.createQueryBuilder() .createQueryBuilder()

View File

@ -12,7 +12,12 @@ import { LIMIT } from "./constants";
import { getNow, setRepo, settingsRepo } from "./db"; import { getNow, setRepo, settingsRepo } from "./db";
import DrawerHeader from "./DrawerHeader"; import DrawerHeader from "./DrawerHeader";
import { emitter } from "./emitter"; import { emitter } from "./emitter";
import GymSet, { defaultSet } from "./gym-set"; import GymSet, {
defaultSet,
GYM_SET_CREATED,
GYM_SET_DELETED,
GYM_SET_UPDATED,
} from "./gym-set";
import { HomePageParams } from "./home-page-params"; import { HomePageParams } from "./home-page-params";
import ListMenu from "./ListMenu"; import ListMenu from "./ListMenu";
import Page from "./Page"; import Page from "./Page";
@ -30,21 +35,12 @@ export default function SetList() {
const { params } = useRoute<RouteProp<HomePageParams, "Sets">>(); const { params } = useRoute<RouteProp<HomePageParams, "Sets">>();
const [term, setTerm] = useState(params?.search || ""); const [term, setTerm] = useState(params?.search || "");
const refresh = async (gymSet: GymSet) => {
console.log(`${SetList.name}.refresh:`, gymSet);
if (!sets) return;
const newSets = sets.map((oldSet) =>
oldSet.id === gymSet.id ? gymSet : oldSet
);
setSets(newSets);
};
const reset = useCallback( const reset = useCallback(
async ({ value, skip }: { value: string; skip: number }) => { async (value: string) => {
const newSets = await setRepo.find({ const newSets = await setRepo.find({
where: { name: Like(`%${value.trim()}%`), hidden: 0 as any }, where: { name: Like(`%${value.trim()}%`), hidden: 0 as any },
take: LIMIT, take: LIMIT,
skip, skip: 0,
order: { created: "DESC" }, order: { created: "DESC" },
}); });
console.log(`${SetList.name}.reset:`, { value, offset }); console.log(`${SetList.name}.reset:`, { value, offset });
@ -56,37 +52,43 @@ export default function SetList() {
useEffect(() => { useEffect(() => {
settingsRepo.findOne({ where: {} }).then(setSettings); settingsRepo.findOne({ where: {} }).then(setSettings);
const description = emitter.addListener(SETTINGS, () => { reset("");
settingsRepo.findOne({ where: {} }).then(setSettings); /* eslint-disable react-hooks/exhaustive-deps */
});
return description.remove;
}, []); }, []);
useEffect(() => {
const updated = (gymSet: GymSet) => {
if (!sets) console.log({ sets });
console.log(`${SetList.name}.updated:`, { gymSet, length: sets.length });
const newSets = sets.map((set) => {
if (set.id !== gymSet.id) return set;
if (gymSet.created === undefined) gymSet.created = set.created;
return gymSet;
});
setSets(newSets);
};
const descriptions = [
emitter.addListener(SETTINGS, () => {
settingsRepo.findOne({ where: {} }).then(setSettings);
}),
emitter.addListener(GYM_SET_UPDATED, updated),
emitter.addListener(GYM_SET_CREATED, () => reset("")),
emitter.addListener(GYM_SET_DELETED, () => reset("")),
];
return () => descriptions.forEach((description) => description.remove());
}, [sets]);
const search = (value: string) => { const search = (value: string) => {
console.log(`${SetList.name}.search:`, value); console.log(`${SetList.name}.search:`, value);
setTerm(value); setTerm(value);
setOffset(0); setOffset(0);
reset({ reset(value);
skip: 0,
value,
});
}; };
useEffect(() => { useEffect(() => {
console.log(`${SetList.name}.useEffect:`, params); console.log(`${SetList.name}.useEffect:`, params);
if (!params)
reset({
skip: 0,
value: "",
});
if (params?.search) search(params.search); if (params?.search) search(params.search);
else if (params?.refresh) refresh(params.refresh);
else if (params?.reset)
reset({
skip: 0,
value: term,
});
/* eslint-disable react-hooks/exhaustive-deps */
}, [params]); }, [params]);
const renderItem = useCallback( const renderItem = useCallback(
@ -155,10 +157,7 @@ export default function SetList() {
const remove = async () => { const remove = async () => {
setIds([]); setIds([]);
await setRepo.delete(ids.length > 0 ? ids : {}); await setRepo.delete(ids.length > 0 ? ids : {});
return reset({ return reset(term);
skip: 0,
value: term,
});
}; };
const select = useCallback(() => { const select = useCallback(() => {
@ -187,10 +186,7 @@ export default function SetList() {
onRefresh={() => { onRefresh={() => {
setOffset(0); setOffset(0);
setRefreshing(true); setRefreshing(true);
reset({ reset(term).finally(() => setRefreshing(false));
skip: 0,
value: term,
}).finally(() => setRefreshing(false));
}} }}
/> />
); );

View File

@ -14,8 +14,9 @@ import { MARGIN, PADDING } from "./constants";
import CountMany from "./count-many"; import CountMany from "./count-many";
import { AppDataSource } from "./data-source"; import { AppDataSource } from "./data-source";
import { getNow, setRepo, settingsRepo } from "./db"; import { getNow, setRepo, settingsRepo } from "./db";
import { emitter } from "./emitter";
import { fixNumeric } from "./fix-numeric"; import { fixNumeric } from "./fix-numeric";
import GymSet from "./gym-set"; import GymSet, { GYM_SET_CREATED, GYM_SET_UPDATED } from "./gym-set";
import { PlanPageParams } from "./plan-page-params"; import { PlanPageParams } from "./plan-page-params";
import Settings from "./settings"; import Settings from "./settings";
import StackHeader from "./StackHeader"; import StackHeader from "./StackHeader";
@ -102,7 +103,8 @@ export default function StartPlan() {
created: now, created: now,
hidden: false, hidden: false,
}; };
await setRepo.save(newSet); const saved = await setRepo.save(newSet);
emitter.emit(GYM_SET_CREATED, saved);
await refresh(); await refresh();
if ( if (
settings.notify && settings.notify &&

View File

@ -5,6 +5,8 @@ import { List, Menu, RadioButton, useTheme } from "react-native-paper";
import { Like } from "typeorm"; import { Like } from "typeorm";
import CountMany from "./count-many"; import CountMany from "./count-many";
import { getNow, setRepo } from "./db"; import { getNow, setRepo } from "./db";
import { emitter } from "./emitter";
import { GYM_SET_DELETED } from "./gym-set";
import { HomePageParams } from "./home-page-params"; import { HomePageParams } from "./home-page-params";
import { PlanPageParams } from "./plan-page-params"; import { PlanPageParams } from "./plan-page-params";
import { toast } from "./toast"; import { toast } from "./toast";
@ -38,6 +40,7 @@ export default function StartPlanItem(props: Props) {
setShowMenu(false); setShowMenu(false);
if (!first) return toast("Nothing to undo."); if (!first) return toast("Nothing to undo.");
await setRepo.delete(first.id); await setRepo.delete(first.id);
emitter.emit(GYM_SET_DELETED, first);
onUndo(); onUndo();
}, [setShowMenu, onUndo, item.name]); }, [setShowMenu, onUndo, item.name]);

View File

@ -12,7 +12,7 @@ import { LIMIT } from "./constants";
import { setRepo, settingsRepo } from "./db"; import { setRepo, settingsRepo } from "./db";
import DrawerHeader from "./DrawerHeader"; import DrawerHeader from "./DrawerHeader";
import { emitter } from "./emitter"; import { emitter } from "./emitter";
import GymSet from "./gym-set"; import GymSet, { GYM_SET_DELETED } from "./gym-set";
import ListMenu from "./ListMenu"; import ListMenu from "./ListMenu";
import Page from "./Page"; import Page from "./Page";
import SetList from "./SetList"; import SetList from "./SetList";
@ -131,6 +131,7 @@ export default function WorkoutList() {
const remove = async () => { const remove = async () => {
setNames([]); setNames([]);
if (names.length > 0) await setRepo.delete({ name: In(names) }); if (names.length > 0) await setRepo.delete({ name: In(names) });
emitter.emit(GYM_SET_DELETED);
await reset(term); await reset(term);
}; };

View File

@ -1,5 +1,9 @@
import { Column, Entity, PrimaryGeneratedColumn } from "typeorm"; import { Column, Entity, PrimaryGeneratedColumn } from "typeorm";
export const GYM_SET_UPDATED = "gym-set-updated";
export const GYM_SET_DELETED = "gym-set-deleted";
export const GYM_SET_CREATED = "gym-set-created";
@Entity("sets") @Entity("sets")
export default class GymSet { export default class GymSet {
@PrimaryGeneratedColumn() @PrimaryGeneratedColumn()

View File

@ -3,16 +3,6 @@ import GymSet from "./gym-set";
export type HomePageParams = { export type HomePageParams = {
Sets: { Sets: {
search?: string; search?: string;
/**
* Update the specified set
*/
refresh?: GymSet;
/**
* Reload the list with limit = 0
*/
reset?: number;
}; };
EditSet: { EditSet: {
set: GymSet; set: GymSet;