Skip to content

Commit

Permalink
feat(GIST-53): delete gist on ui (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
Courtcircuits authored Oct 8, 2024
1 parent ff200cc commit d73d5b7
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 18 deletions.
42 changes: 24 additions & 18 deletions src/components/feature/mygist-list-feature.tsx
Original file line number Diff line number Diff line change
@@ -1,39 +1,45 @@
'use client'
import { Gist } from '@/types'
import MyGistList from '../ui/mygist-list'
import { useGists } from '@/lib/queries/gists.queries'
"use client";
import { Gist } from "@/types";
import MyGistList from "../ui/mygist-list";
import { useDeleteGist, useGists } from "@/lib/queries/gists.queries";

// TODO: Get the gists list

const gistMock: Gist[] = [
{
id: '1',
name: 'My first Gist',
id: "1",
name: "My first Gist",
code: 'console.log("Hello, World!")',
},
{
id: '2',
name: 'My second Gist',
id: "2",
name: "My second Gist",
code: 'console.log("Hello, World!")',
},
{
id: '3',
name: 'My third Gist',
id: "3",
name: "My third Gist",
code: 'console.log("Hello, World!")',
},
{
id: '4',
name: 'My fourth Gist',
id: "4",
name: "My fourth Gist",
code: 'console.log("Hello, World!")',
},
]
];

const handleDeleteGist = (id: string) => {
console.log(`Deleting gist with ID: ${id}`)
}
const handleDeleteGist = (id: string) => {};

export function MyGistListFeature() {
const { data } = useGists()
const { data } = useGists();
const { mutate: deleteGist } = useDeleteGist({
onSuccess: (id) => {
console.log(`Deleting gist with ID: ${id}`);
},
});
const handleDeleteGist = (id: string) => {
deleteGist(id);
};

return <MyGistList gists={data || []} onDeleteGist={handleDeleteGist} />
return <MyGistList gists={data || []} onDeleteGist={handleDeleteGist} />;
}
31 changes: 31 additions & 0 deletions src/lib/queries/gists.queries.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,15 @@ const fetchPatchGistContent = async (
};
};

const fetchDeleteGist = async (id: string) => {
await ky
.delete(`${getBackendURL()}/gists/${id}`, {
credentials: "include",
})
.then();
return id;
};

//hooks

export const useGists = () => {
Expand Down Expand Up @@ -196,3 +205,25 @@ export const usePatchGistContent = ({
});
return { mutate, error, data, isPending };
};

export const useDeleteGist = ({
onSuccess,
}: {
onSuccess: (id: string) => void;
}) => {
const queryClient = useQueryClient();

const { mutate, error, data, isPending } = useMutation({
mutationFn: (id: string) => {
return fetchDeleteGist(id);
},
onSuccess: (gistID) => {
queryClient.setQueryData(["gists"], (oldData: any) => {
return oldData.filter((gist: Gist) => gist.id !== gistID.toString());
});
onSuccess(gistID.toString());
},
});

return { mutate, error, data, isPending };
};

0 comments on commit d73d5b7

Please sign in to comment.