Skip to content

Commit 4586551

Browse files
committed
Add needed queries to delete config objects
1 parent d2c1b9c commit 4586551

File tree

5 files changed

+232
-52
lines changed

5 files changed

+232
-52
lines changed

src/components/WorkoutRoutines/Detail/SlotDetails.tsx

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,34 @@
11
import { Box, Grid, Typography } from "@mui/material";
2+
import { BaseConfig } from "components/WorkoutRoutines/models/BaseConfig";
23
import { Slot } from "components/WorkoutRoutines/models/Slot";
4+
import { SlotConfig } from "components/WorkoutRoutines/models/SlotConfig";
35
import { ConfigDetailsField } from "components/WorkoutRoutines/widgets/forms/BaseConfigForm";
46
import { SlotConfigForm } from "components/WorkoutRoutines/widgets/forms/SlotConfigForm";
57
import React from "react";
68

9+
const configTypes = ["weight", "max-weight", "reps", "max-reps", "sets", "rest", "max-rest", "rir"] as const;
10+
type ConfigType = typeof configTypes[number];
11+
12+
const getConfigComponent = (type: ConfigType, configs: BaseConfig[], routineId: number, slotId: number) => {
13+
return configs.length > 0
14+
?
15+
<ConfigDetailsField
16+
config={configs[0]}
17+
type={type}
18+
routineId={routineId} />
19+
20+
: <ConfigDetailsField
21+
type={type}
22+
routineId={routineId}
23+
slotId={slotId} />
24+
;
25+
};
26+
727
export const SlotDetails = (props: { slot: Slot, routineId: number, simpleMode: boolean }) => {
828

929
return (
1030
<>
11-
{props.slot.configs.map((slotConfig) => (
31+
{props.slot.configs.map((slotConfig: SlotConfig) => (
1232
<React.Fragment key={slotConfig.id}>
1333
<p>
1434
<small>SlotConfigId {slotConfig.id}</small>
@@ -25,25 +45,21 @@ export const SlotDetails = (props: { slot: Slot, routineId: number, simpleMode:
2545
{props.simpleMode ? (
2646
// Show only weight, reps, and sets in simple mode
2747
<>
28-
{slotConfig.weightConfigs.map((config) => (
29-
<Grid item xs={12} sm={4} key={`weight-${config.id}`}>
30-
<ConfigDetailsField config={config} type="weight" routineId={props.routineId} />
31-
</Grid>
32-
))}
33-
{slotConfig.repsConfigs.map((config) => (
34-
<Grid item xs={12} sm={4} key={`reps-${config.id}`}>
35-
<ConfigDetailsField config={config} type="reps" routineId={props.routineId} />
36-
</Grid>
37-
))}
38-
{slotConfig.nrOfSetsConfigs.map((config) => (
39-
<Grid item xs={12} sm={4} key={`sets-${config.id}`}>
40-
<ConfigDetailsField config={config} type="sets" routineId={props.routineId} />
41-
</Grid>
42-
))}
48+
<Grid item xs={12} sm={4} key={`sets-config-${slotConfig.id}`}>
49+
{getConfigComponent('sets', slotConfig.nrOfSetsConfigs, props.routineId, slotConfig.id)}
50+
</Grid>
51+
<Grid item xs={12} sm={4} key={`weight-config-${slotConfig.id}`}>
52+
{getConfigComponent('weight', slotConfig.weightConfigs, props.routineId, slotConfig.id)}
53+
</Grid>
54+
<Grid item xs={12} sm={4} key={`reps-config-${slotConfig.id}`}>
55+
{getConfigComponent('reps', slotConfig.repsConfigs, props.routineId, slotConfig.id)}
56+
</Grid>
57+
4358
</>
4459
) : (
4560
// Show all config details in advanced mode, also in a grid
4661
<>
62+
<h1>TODO!</h1>
4763
{slotConfig.weightConfigs.map((config) => (
4864
<Grid item xs={12} sm={4} key={`weight-${config.id}`}>
4965
<ConfigDetailsField config={config} type="weight" routineId={props.routineId} />

src/components/WorkoutRoutines/queries/configs.ts

Lines changed: 97 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@ import {
99
addRestConfig,
1010
addRirConfig,
1111
addWeightConfig,
12+
deleteMaxRepsConfig,
13+
deleteMaxRestConfig,
14+
deleteMaxWeightConfig,
15+
deleteNrOfSetsConfig,
16+
deleteRepsConfig,
17+
deleteRestConfig,
18+
deleteRirConfig,
19+
deleteWeightConfig,
1220
EditBaseConfigParams,
1321
editMaxRepsConfig,
1422
editMaxRestConfig,
@@ -22,6 +30,9 @@ import {
2230
import { QueryKey, } from "utils/consts";
2331

2432

33+
/*
34+
* Weight config
35+
*/
2536
export const useEditWeightConfigQuery = (routineId: number) => {
2637
const queryClient = useQueryClient();
2738

@@ -30,7 +41,6 @@ export const useEditWeightConfigQuery = (routineId: number) => {
3041
onSuccess: () => queryClient.invalidateQueries([QueryKey.ROUTINE_DETAIL, routineId])
3142
});
3243
};
33-
3444
export const useAddWeightConfigQuery = (routineId: number) => {
3545
const queryClient = useQueryClient();
3646

@@ -39,7 +49,19 @@ export const useAddWeightConfigQuery = (routineId: number) => {
3949
onSuccess: () => queryClient.invalidateQueries([QueryKey.ROUTINE_DETAIL, routineId])
4050
});
4151
};
52+
export const useDeleteWeightConfigQuery = (routineId: number) => {
53+
const queryClient = useQueryClient();
54+
55+
return useMutation({
56+
mutationFn: (id: number) => deleteWeightConfig(id),
57+
onSuccess: () => queryClient.invalidateQueries([QueryKey.ROUTINE_DETAIL, routineId])
58+
});
59+
};
4260

61+
62+
/*
63+
* Max Weight config
64+
*/
4365
export const useEditMaxWeightConfigQuery = (routineId: number) => {
4466
const queryClient = useQueryClient();
4567

@@ -48,7 +70,6 @@ export const useEditMaxWeightConfigQuery = (routineId: number) => {
4870
onSuccess: () => queryClient.invalidateQueries([QueryKey.ROUTINE_DETAIL, routineId])
4971
});
5072
};
51-
5273
export const useAddMaxWeightConfigQuery = (routineId: number) => {
5374
const queryClient = useQueryClient();
5475

@@ -57,7 +78,18 @@ export const useAddMaxWeightConfigQuery = (routineId: number) => {
5778
onSuccess: () => queryClient.invalidateQueries([QueryKey.ROUTINE_DETAIL, routineId])
5879
});
5980
};
81+
export const useDeleteMaxWeightConfigQuery = (routineId: number) => {
82+
const queryClient = useQueryClient();
83+
84+
return useMutation({
85+
mutationFn: (id: number) => deleteMaxWeightConfig(id),
86+
onSuccess: () => queryClient.invalidateQueries([QueryKey.ROUTINE_DETAIL, routineId])
87+
});
88+
};
6089

90+
/*
91+
* Reps config
92+
*/
6193
export const useEditRepsConfigQuery = (routineId: number) => {
6294
const queryClient = useQueryClient();
6395

@@ -74,7 +106,18 @@ export const useAddRepsConfigQuery = (routineId: number) => {
74106
onSuccess: () => queryClient.invalidateQueries([QueryKey.ROUTINE_DETAIL, routineId])
75107
});
76108
};
109+
export const useDeleteRepsConfigQuery = (routineId: number) => {
110+
const queryClient = useQueryClient();
77111

112+
return useMutation({
113+
mutationFn: (id: number) => deleteRepsConfig(id),
114+
onSuccess: () => queryClient.invalidateQueries([QueryKey.ROUTINE_DETAIL, routineId])
115+
});
116+
};
117+
118+
/*
119+
* Max Reps config
120+
*/
78121
export const useEditMaxRepsConfigQuery = (routineId: number) => {
79122
const queryClient = useQueryClient();
80123

@@ -91,7 +134,18 @@ export const useAddMaxRepsConfigQuery = (routineId: number) => {
91134
onSuccess: () => queryClient.invalidateQueries([QueryKey.ROUTINE_DETAIL, routineId])
92135
});
93136
};
137+
export const useDeleteMaxRepsConfigQuery = (routineId: number) => {
138+
const queryClient = useQueryClient();
94139

140+
return useMutation({
141+
mutationFn: (id: number) => deleteMaxRepsConfig(id),
142+
onSuccess: () => queryClient.invalidateQueries([QueryKey.ROUTINE_DETAIL, routineId])
143+
});
144+
};
145+
146+
/*
147+
* Nr of Sets config
148+
*/
95149
export const useEditNrOfSetsConfigQuery = (routineId: number) => {
96150
const queryClient = useQueryClient();
97151

@@ -108,7 +162,18 @@ export const useAddNrOfSetsConfigQuery = (routineId: number) => {
108162
onSuccess: () => queryClient.invalidateQueries([QueryKey.ROUTINE_DETAIL, routineId])
109163
});
110164
};
165+
export const useDeleteNrOfSetsConfigQuery = (routineId: number) => {
166+
const queryClient = useQueryClient();
167+
168+
return useMutation({
169+
mutationFn: (id: number) => deleteNrOfSetsConfig(id),
170+
onSuccess: () => queryClient.invalidateQueries([QueryKey.ROUTINE_DETAIL, routineId])
171+
});
172+
};
111173

174+
/*
175+
* RiR
176+
*/
112177
export const useEditRiRConfigQuery = (routineId: number) => {
113178
const queryClient = useQueryClient();
114179

@@ -125,7 +190,18 @@ export const useAddRiRConfigQuery = (routineId: number) => {
125190
onSuccess: () => queryClient.invalidateQueries([QueryKey.ROUTINE_DETAIL, routineId])
126191
});
127192
};
193+
export const useDeleteRiRConfigQuery = (routineId: number) => {
194+
const queryClient = useQueryClient();
195+
196+
return useMutation({
197+
mutationFn: (id: number) => deleteRirConfig(id),
198+
onSuccess: () => queryClient.invalidateQueries([QueryKey.ROUTINE_DETAIL, routineId])
199+
});
200+
};
128201

202+
/*
203+
* Rest time config
204+
*/
129205
export const useEditRestConfigQuery = (routineId: number) => {
130206
const queryClient = useQueryClient();
131207

@@ -142,7 +218,18 @@ export const useAddRestConfigQuery = (routineId: number) => {
142218
onSuccess: () => queryClient.invalidateQueries([QueryKey.ROUTINE_DETAIL, routineId])
143219
});
144220
};
221+
export const useDeleteRestConfigQuery = (routineId: number) => {
222+
const queryClient = useQueryClient();
223+
224+
return useMutation({
225+
mutationFn: (id: number) => deleteRestConfig(id),
226+
onSuccess: () => queryClient.invalidateQueries([QueryKey.ROUTINE_DETAIL, routineId])
227+
});
228+
};
145229

230+
/*
231+
* Max Rest time config
232+
*/
146233
export const useEditMaxRestConfigQuery = (routineId: number) => {
147234
const queryClient = useQueryClient();
148235

@@ -159,5 +246,13 @@ export const useAddMaxRestConfigQuery = (routineId: number) => {
159246
onSuccess: () => queryClient.invalidateQueries([QueryKey.ROUTINE_DETAIL, routineId])
160247
});
161248
};
249+
export const useDeleteMaxRestConfigQuery = (routineId: number) => {
250+
const queryClient = useQueryClient();
251+
252+
return useMutation({
253+
mutationFn: (id: number) => deleteMaxRestConfig(id),
254+
onSuccess: () => queryClient.invalidateQueries([QueryKey.ROUTINE_DETAIL, routineId])
255+
});
256+
};
162257

163258

src/components/WorkoutRoutines/queries/index.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,15 @@ export {
1818
useEditNrOfSetsConfigQuery,
1919
useEditRepsConfigQuery,
2020
useEditRestConfigQuery,
21-
useEditRiRConfigQuery
21+
useEditRiRConfigQuery,
22+
useDeleteWeightConfigQuery,
23+
useDeleteMaxWeightConfigQuery,
24+
useDeleteRepsConfigQuery,
25+
useDeleteMaxRepsConfigQuery,
26+
useDeleteNrOfSetsConfigQuery,
27+
useDeleteRiRConfigQuery,
28+
useDeleteRestConfigQuery,
29+
useDeleteMaxRestConfigQuery
2230
} from './configs';
2331

2432
export { useEditDayQuery } from './days';

0 commit comments

Comments
 (0)