Skip to content

Commit

Permalink
Toggle is_shelved for Practices
Browse files Browse the repository at this point in the history
  • Loading branch information
michalparkola committed Sep 25, 2024
1 parent 255e264 commit d75a87e
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 6 deletions.
45 changes: 42 additions & 3 deletions screens/practice/Practice.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import React from "react";
import { Text, View, FlatList } from "react-native";
import React, { useState, useEffect } from "react";
import { Text, View, FlatList, Switch } from "react-native";

import { usePractice } from "@/hooks/usePractice";
import { usePractice } from "./usePractice";
import { useUpdatePractice } from "./useUpdatePractice";
import { useReps } from "@/hooks/useReps";

import EditablePracticeTitle from "./EditablePracticeTitle";
Expand All @@ -12,6 +13,8 @@ import RecipeListForPractice from "@/screens/recipes/RecipeListForPractice";
import { AddRecipeToPractice } from "../recipes/AddRecipe";
import ProgramsListForPractice from "../programs/ProgramsListForPractice";

import { gs } from "@/global-styles";

interface Props {
practiceId: string;
}
Expand All @@ -23,15 +26,43 @@ export default function PracticeView({ practiceId }: Props) {
data: practice,
} = usePractice(practiceId);

const updatePracticeMutation = useUpdatePractice(Number(practiceId));

const {
isPending: isPendingReps,
error: errorReps,
data: reps,
} = useReps(practiceId);

const [isShelved, setIsShelved] = useState(practice?.is_shelved ?? false);

// TODO: do I really need this useEffect?
useEffect(() => {
if (practice) {
setIsShelved(practice.is_shelved ?? false);
}
}, [practice]);

if (isPendingPractice || errorPractice) return <Text>Loading...</Text>;
if (isPendingReps || errorReps) return <Text>Loading...</Text>;

function handleIsShelvedSwitch(switch_value: boolean) {
updatePracticeMutation.mutate(
{
new_title: practice?.do100reps_title || "",
new_is_shelved: switch_value,
},
{
onSuccess: () => {
setIsShelved(switch_value);
},
onError: (error) => {
console.error(error);
},
}
);
}

return (
<FlatList
style={{ padding: 12 }}
Expand All @@ -42,6 +73,14 @@ export default function PracticeView({ practiceId }: Props) {
practice_id={practice.id.toString()}
practice_title={practice.do100reps_title ?? ""}
/>
<Text style={gs.h2}>Shelved</Text>
<Switch
style={{ margin: 12 }}
value={isShelved}
onValueChange={(value) => {
handleIsShelvedSwitch(value);
}}
/>
<PracticeProgress completed_reps_count={reps.length} />
<ProgramsListForPractice practice_id={practice.id} />
<RecipeListForPractice practice_title={practice.name} />
Expand Down
19 changes: 19 additions & 0 deletions screens/practice/useUpdatePractice.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { useQueryClient, useMutation } from "@tanstack/react-query";
import { updatePractice } from "@/supabase/supabase-queries";

export function useUpdatePractice(practice_id: number) {
const queryClient = useQueryClient();
return useMutation({
mutationFn: ({
new_title,
new_is_shelved,
}: {
new_title: string;
new_is_shelved: boolean;
}) => updatePractice(practice_id, new_title, new_is_shelved),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ["practice", practice_id] });
queryClient.invalidateQueries({ queryKey: ["practices"] });
},
});
}
34 changes: 31 additions & 3 deletions supabase/supabase-queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,15 @@ export async function getSupabasePracticeById(practiceid: string) {
}
}

export async function savePracticeTitle(id: string, title: string) {
// TODO use general updatePractice??
export async function savePracticeTitle(
practice_id: string,
new_title: string,
) {
await supabase
.from("Practices")
.update({ do100reps_title: title })
.eq("id", id);
.update({ do100reps_title: new_title })
.eq("id", practice_id);
}

export async function getSupabaseRepsByPracticeId(practiceId: string) {
Expand Down Expand Up @@ -340,3 +344,27 @@ export async function updateActivity(
throw new Error(error.message);
}
}

export async function updatePractice(
practice_id: number,
new_title: string,
new_is_shelved: boolean,
) {
if (!new_title) {
return;
}

const { data, error } = await supabase
.from("Practices")
.update({
do100reps_title: new_title,
is_shelved: new_is_shelved,
})
.eq("id", practice_id);

if (error) {
throw new Error(error.message);
} else {
return data;
}
}

0 comments on commit d75a87e

Please sign in to comment.