Skip to content

Commit

Permalink
change recipe shaleved toggle to match program and practice
Browse files Browse the repository at this point in the history
  • Loading branch information
michalparkola committed Sep 25, 2024
1 parent 0079369 commit 8152595
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 36 deletions.
22 changes: 0 additions & 22 deletions components/EditableTODOSwitch.tsx

This file was deleted.

44 changes: 30 additions & 14 deletions screens/recipes/Recipe.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import React from "react";
import { View, ScrollView, Text } from "react-native";
import React, { useState } from "react";
import { View, ScrollView, Text, Switch } from "react-native";
import { useQuery } from "@tanstack/react-query";
import { useUpdateRecipe } from "./useUpdateRecipe";
import { getNugget } from "@/supabase/supabase-queries";
import { gs } from "@/global-styles";
import EditableTextInputWithCancelSave from "@/components/EditableTextInputWithCancelSave";
import EditableTODOSwitch from "@/components/EditableTODOSwitch";

interface Props {
nugget_id: string;
Expand All @@ -22,7 +21,9 @@ export default function Recipe({ nugget_id }: Props) {
queryKey: ["nugget", nugget_id],
});

const updateNuggetMutation = useUpdateRecipe(Number(nugget_id));
const updateRecipeMutation = useUpdateRecipe(Number(nugget_id));

const [isShelved, setIsShelved] = useState(nugget?.is_todo ?? true);

if (isPending) return <Text>Loading recipe...</Text>;
if (error)
Expand All @@ -31,25 +32,40 @@ export default function Recipe({ nugget_id }: Props) {
Error getting recipe: {error.name}, {error.message}
</Text>
);

function handleIsShelvedSwitch(switch_value: boolean) {
if (!nugget) return;

updateRecipeMutation.mutate(
{ ...nugget, is_todo: !nugget.is_todo },
{
onSuccess: () => {
setIsShelved(switch_value);
},
onError: (error) => {
console.error(error);
},
}
);
}

return (
<ScrollView contentContainerStyle={{ margin: 12 }}>
<Text style={gs.label}>Shelved</Text>
<EditableTODOSwitch
is_todo={nugget.is_todo}
handleToggle={() => {
updateNuggetMutation.mutate({ ...nugget, is_todo: !nugget.is_todo });
<Switch
style={{ margin: 12 }}
value={isShelved}
onValueChange={(value) => {
console.log("Swith: ", value);
handleIsShelvedSwitch(value);
}}
/>
<Text style={gs.label}>Created at</Text>
<Text style={{ margin: 12 }}>{nugget.created_at}</Text>
<Text style={gs.label}>Practice</Text>
<Text style={{ margin: 12 }}>{nugget.practice}</Text>
<Text style={gs.label}>Title</Text>
<View style={{ padding: 6 }}>
<EditableTextInputWithCancelSave
text={nugget.title || ""}
handleSave={(text: string) =>
updateNuggetMutation.mutate({ ...nugget, title: text })
updateRecipeMutation.mutate({ ...nugget, title: text })
}
/>
</View>
Expand All @@ -58,7 +74,7 @@ export default function Recipe({ nugget_id }: Props) {
<EditableTextInputWithCancelSave
text={nugget.text || ""}
handleSave={(text: string) =>
updateNuggetMutation.mutate({ ...nugget, text: text })
updateRecipeMutation.mutate({ ...nugget, text: text })
}
/>
</View>
Expand Down
1 change: 1 addition & 0 deletions screens/recipes/useUpdateRecipe.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export function useUpdateRecipe(nugget_id: number) {
queryKey: ["nugget", nugget_id],
});
queryClient.invalidateQueries({ queryKey: ["nuggets"] });
queryClient.invalidateQueries({ queryKey: ["nugget", nugget_id] });
},
});
}

0 comments on commit 8152595

Please sign in to comment.