Skip to content

Commit

Permalink
get the mesocycle in runes too
Browse files Browse the repository at this point in the history
  • Loading branch information
WhyAsh5114 committed Jun 23, 2024
1 parent 6c2ddd6 commit 5884244
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import DndComponent from '$lib/components/mesocycleAndExerciseSplit/DndComponent.svelte';
import SwapExercisesDialog from '$lib/components/mesocycleAndExerciseSplit/SwapExercisesDialog.svelte';
import ExerciseSplitDrawer from '$lib/components/mesocycleAndExerciseSplit/ExerciseSplitDrawer.svelte';
import type { Mesocycle } from '@prisma/client';
let swapDialogOpen = $state(false);
let reordering = $state(false);
Expand Down Expand Up @@ -68,6 +69,7 @@
</div>
<ExerciseSplitDrawer
context="mesocycle"
mesocycle={mesocycleExerciseSplitRunes.mesocycle as Mesocycle}
addExercise={mesocycleExerciseSplitRunes.addExercise}
editExercise={mesocycleExerciseSplitRunes.editExercise}
setEditingExercise={mesocycleExerciseSplitRunes.setEditingExercise}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Prisma } from '@prisma/client';
import type { Mesocycle, Prisma } from '@prisma/client';

export type FullMesocycleWithExerciseSplit = Prisma.MesocycleGetPayload<{
include: { mesocycleExerciseSplitDays: { include: { mesocycleSplitDayExercises: true } } };
Expand All @@ -12,7 +12,7 @@ type MesocycleExerciseTemplateWithoutIds =
Prisma.MesocycleExerciseTemplateCreateWithoutMesocycleExerciseSplitDayInput;

export function createMesocycleExerciseSplitRunes() {
let mesocycleId: string | null = $state(null);
let mesocycle: Mesocycle | null = $state(null);
let splitDays: MesocycleExerciseSplitDayWithoutIds[] = $state(
Array.from({ length: 7 }).map(() => ({ name: '', isRestDay: false }))
);
Expand All @@ -24,7 +24,7 @@ export function createMesocycleExerciseSplitRunes() {

if (globalThis.localStorage) {
const savedState = localStorage.getItem('mesocycleExerciseSplitRunes');
if (savedState) ({ splitDays, splitExercises, mesocycleId } = JSON.parse(savedState));
if (savedState) ({ splitDays, splitExercises, mesocycle } = JSON.parse(savedState));
}

function addSplitDay() {
Expand Down Expand Up @@ -129,12 +129,12 @@ export function createMesocycleExerciseSplitRunes() {
function saveStoresToLocalStorage() {
localStorage.setItem(
'mesocycleExerciseSplitRunes',
JSON.stringify({ splitDays, splitExercises, mesocycleId })
JSON.stringify({ splitDays, splitExercises, mesocycle })
);
}

function resetStores() {
mesocycleId = null;
mesocycle = null;
splitDays = Array.from({ length: 7 }).map(() => ({ name: '', isRestDay: false }));
splitExercises = [];
selectedSplitDayIndex = 0;
Expand All @@ -145,10 +145,11 @@ export function createMesocycleExerciseSplitRunes() {

function loadExerciseSplit(mesocycleWithExerciseSplit: FullMesocycleWithExerciseSplit) {
// Same mesocycle, don't reset and load new data, reuse and continue editing
if (mesocycleWithExerciseSplit.id === mesocycleId) return;
if (mesocycleWithExerciseSplit.id === mesocycle?.id) return;

resetStores();
mesocycleId = mesocycleWithExerciseSplit.id;
const { mesocycleExerciseSplitDays, ...mesocycleData } = mesocycleWithExerciseSplit;
mesocycle = mesocycleData;
splitDays = mesocycleWithExerciseSplit.mesocycleExerciseSplitDays.map((splitDay) => {
const { id, mesocycleId, ...rest } = splitDay;
return rest;
Expand All @@ -163,12 +164,6 @@ export function createMesocycleExerciseSplitRunes() {
}

return {
get mesocycleId() {
return mesocycleId;
},
set mesocycleId(value) {
mesocycleId = value;
},
get splitDays() {
return splitDays;
},
Expand All @@ -190,6 +185,7 @@ export function createMesocycleExerciseSplitRunes() {
get copiedExercises() {
return copiedExercises;
},
mesocycle,
addSplitDay,
removeSplitDay,
toggleSplitDay,
Expand Down

0 comments on commit 5884244

Please sign in to comment.