Skip to content

Commit

Permalink
using persisted store package
Browse files Browse the repository at this point in the history
  • Loading branch information
WhyAsh5114 committed Dec 23, 2023
1 parent 753f2b2 commit 0a29859
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 99 deletions.
14 changes: 13 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"@vercel/speed-insights": "^1.0.0",
"chart.js": "^4.4.1",
"dotenv": "^16.3.1",
"mongodb": "^6.2.0"
"mongodb": "^6.2.0",
"svelte-persisted-store": "^0.8.0"
}
}
91 changes: 28 additions & 63 deletions src/routes/mesocycles/[mode]/mesocycleStore.ts
Original file line number Diff line number Diff line change
@@ -1,67 +1,32 @@
/* eslint-disable svelte/no-ignored-unsubscribe */
import { getTotalDuration } from "$lib/util/MesocycleTemplate";
import { type Writable, writable } from "svelte/store";

let ls: Storage | undefined;
if (typeof window !== "undefined") {
ls = localStorage;
}

export const customizeRIRProgression: Writable<boolean> = writable(
JSON.parse(ls?.getItem("customizeRIRProgression") || "false")
);

export const mesocycleName: Writable<string> = writable(
JSON.parse(ls?.getItem("mesocycleName") || '""')
);
export const mesocycleDuration: Writable<number> = writable(
JSON.parse(ls?.getItem("mesocycleDuration") || "6")
);
export const mesocycleStartRIR: Writable<number> = writable(
JSON.parse(ls?.getItem("mesocycleStartRIR") || "3")
);
export const mesocycleRIRProgression: Writable<RIRProgressionData[]> = writable(
JSON.parse(ls?.getItem("RIRProgressionData") || "[]")
);

export const exerciseSplit: Writable<MesocycleTemplate["exerciseSplit"]> = writable(
JSON.parse(
ls?.getItem("exerciseSplit") ||
JSON.stringify(Array.from({ length: 7 }, () => ({ name: "", exercises: [] })))
)
);

export const mesocycleCaloricState: Writable<MesocycleTemplate["caloricBalance"]> = writable(
JSON.parse(ls?.getItem("caloricBalance") || "0")
);
export const mesocycleSpecialization: Writable<boolean> = writable(
JSON.parse(ls?.getItem("mesocycleSpecialization") || "false")
);
export const specializedMuscleGroups: Writable<MuscleGroup[]> = writable(
JSON.parse(ls?.getItem("specializedMuscleGroups") || "[]")
);
export const editingMesocycleId: Writable<string> = writable(
JSON.parse(ls?.getItem("editingMesocycleId") || '""')
);

customizeRIRProgression.subscribe(
(val) => ls?.setItem("customizeRIRProgression", JSON.stringify(val))
);
mesocycleName.subscribe((val) => ls?.setItem("mesocycleName", JSON.stringify(val)));
mesocycleDuration.subscribe((val) => ls?.setItem("mesocycleDuration", JSON.stringify(val)));
mesocycleStartRIR.subscribe((val) => ls?.setItem("mesocycleStartRIR", JSON.stringify(val)));
mesocycleRIRProgression.subscribe(
(val) => ls?.setItem("mesocycleRIRProgression", JSON.stringify(val))
);
exerciseSplit.subscribe((val) => ls?.setItem("exerciseSplit", JSON.stringify(val)));
mesocycleCaloricState.subscribe((val) => ls?.setItem("mesocycleCaloricState", JSON.stringify(val)));
mesocycleSpecialization.subscribe(
(val) => ls?.setItem("mesocycleSpecialization", JSON.stringify(val))
);
specializedMuscleGroups.subscribe(
(val) => ls?.setItem("specializedMuscleGroups", JSON.stringify(val))
);
editingMesocycleId.subscribe((val) => ls?.setItem("editingMesocycleId", JSON.stringify(val)));
import { type Writable } from "svelte/store";
import { persisted } from "svelte-persisted-store";

export const customizeRIRProgression = persisted("customizeRIRProgression", false);
export const mesocycleName = persisted("mesocycleName", "");
export const mesocycleDuration = persisted("mesocycleDuration", 6);
export const mesocycleStartRIR = persisted("mesocycleStartRIR", 3);
export const mesocycleRIRProgression: Writable<RIRProgressionData[]> = persisted(
"mesocycleRIRProgression",
[]
);
export const exerciseSplit: Writable<MesocycleTemplate["exerciseSplit"]> = persisted(
"exerciseSplit",
Array.from({ length: 7 }, () => ({ name: "", exercises: [] }))
);
export const mesocycleCaloricState: Writable<MesocycleTemplate["caloricBalance"]> = persisted(
"caloricBalance",
0
);
export const mesocycleSpecialization: Writable<boolean> = persisted(
"mesocycleSpecialization",
false
);
export const specializedMuscleGroups: Writable<MuscleGroup[]> = persisted(
"specializedMuscleGroups",
[]
);
export const editingMesocycleId: Writable<string> = persisted("editingMesocycleId", '""');

export function resetStores() {
customizeRIRProgression.set(false);
Expand Down
51 changes: 17 additions & 34 deletions src/routes/workouts/new/newWorkoutStore.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,23 @@
/* eslint-disable svelte/no-ignored-unsubscribe */
import { type Writable, writable } from "svelte/store";
import { type Writable } from "svelte/store";
import { persisted } from "svelte-persisted-store";

let ls: Storage | undefined;
if (typeof window !== "undefined") {
ls = localStorage;
}

export const workoutBeingPerformed: Writable<WorkoutBeingPerformed | null> = writable(
JSON.parse(ls?.getItem("workoutBeingPerformed") || "null")
);
export const exercisesPerformed: Writable<WorkoutExerciseWithoutSetNumbers[] | null> = writable(
JSON.parse(ls?.getItem("exercisesPerformed") || "null")
export const workoutBeingPerformed: Writable<WorkoutBeingPerformed | null> = persisted(
"workoutBeingPerformed",
null
);
export const allExercisesSetsCompleted: Writable<boolean[][]> = writable(
JSON.parse(ls?.getItem("allExercisesSetsCompleted") || "[]")
export const exercisesPerformed: Writable<WorkoutExerciseWithoutSetNumbers[] | null> = persisted(
"exercisesPerformed",
null
);
export const workloadData: Writable<Workout["muscleGroupWorkloads"]> = writable(
JSON.parse(ls?.getItem("muscleGroupWorkloads") || "{}")
export const allExercisesSetsCompleted: Writable<boolean[][]> = persisted(
"allExercisesSetsCompleted",
[]
);
export const sorenessData: Writable<Workout["muscleSorenessToNextWorkout"]> = writable(
JSON.parse(ls?.getItem("muscleSorenessToNextWorkout") || "{}")
export const workloadData: Writable<Workout["muscleGroupWorkloads"]> = persisted(
"muscleGroupWorkloads",
{}
);

workoutBeingPerformed.subscribe((val) => ls?.setItem("workoutBeingPerformed", JSON.stringify(val)));
exercisesPerformed.subscribe((exercises) => {
ls?.setItem("exercisesPerformed", JSON.stringify(exercises));
// Sync with workoutBeingPerformed
workoutBeingPerformed.update((workout) => {
if (exercises && workout) {
workout.exercisesPerformed = exercises;
}
return workout;
});
});
allExercisesSetsCompleted.subscribe(
(val) => ls?.setItem("allExercisesSetsCompleted", JSON.stringify(val))
export const sorenessData: Writable<Workout["muscleSorenessToNextWorkout"]> = persisted(
"muscleSorenessToNextWorkout",
{}
);
workloadData.subscribe((val) => ls?.setItem("workloadData", JSON.stringify(val)));
sorenessData.subscribe((val) => ls?.setItem("sorenessData", JSON.stringify(val)));

0 comments on commit 0a29859

Please sign in to comment.