Skip to content

Commit

Permalink
add button to delete claimed items (#90)
Browse files Browse the repository at this point in the history
* add button to delete claimed items

* lint
  • Loading branch information
cmintey authored Dec 30, 2023
1 parent caf170c commit 8dce023
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 7 deletions.
7 changes: 5 additions & 2 deletions src/lib/api/items.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ export class ItemsAPI {
return await fetch(`/api/items${path}`, options);
};

delete = async (groupId?: string) => {
return await this._makeRequest("DELETE", groupId ? `?groupId=${groupId}` : "");
delete = async (groupId?: string, claimed?: boolean) => {
const searchParams = new URLSearchParams();
if (groupId) searchParams.append("groupId", groupId);
if (claimed) searchParams.append("claimed", `${claimed}`);
return await this._makeRequest("DELETE", "?" + searchParams.toString());
};
}
10 changes: 6 additions & 4 deletions src/lib/components/admin/Actions/ClearListsButton.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import { getModalStore, getToastStore, type ModalSettings } from "@skeletonlabs/skeleton";
export let groupId: string | undefined = undefined;
export let claimed: boolean = false;
const modalStore = getModalStore();
const toastStore = getToastStore();
Expand All @@ -13,13 +14,13 @@
const settings: ModalSettings = {
type: "confirm",
title: "Please Confirm",
body: `Are you sure you wish to clear all wishlists ${
groupId ? "" : "across <b>all groups</b>"
body: `Are you sure you wish to clear ${claimed ? "claimed items" : "all wishlists"} ${
groupId ? "in this group" : "across <b>all groups</b>"
}? <b>This action is irreversible!</b>`,
// confirm = TRUE | cancel = FALSE
response: async (r: boolean) => {
if (r) {
const resp = await itemsAPI.delete(groupId);
const resp = await itemsAPI.delete(groupId, claimed);
if (resp.ok) {
invalidateAll();
Expand All @@ -45,5 +46,6 @@
</script>

<button class="variant-filled-error btn w-fit" type="button" on:click={handleDelete}>
Clear {groupId ? "" : "All"} Lists
Clear {groupId ? "" : "All"}
{claimed ? "Claimed Items" : "Lists"}
</button>
1 change: 1 addition & 0 deletions src/lib/components/admin/ActionsForm.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@

<div class="flex space-x-2">
<ClearListsButton />
<ClearListsButton claimed />
</div>
1 change: 1 addition & 0 deletions src/routes/admin/groups/[groupId]/members/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -122,5 +122,6 @@
<div>
<button class="variant-filled-error btn w-fit" on:click={deleteGroup}>Delete Group</button>
<ClearListsButton groupId={$page.params.groupId} />
<ClearListsButton claimed groupId={$page.params.groupId} />
</div>
</div>
4 changes: 3 additions & 1 deletion src/routes/api/items/+server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { _authCheck } from "../groups/[groupId]/auth";

export const DELETE: RequestHandler = async ({ locals, request }) => {
const groupId = new URL(request.url).searchParams.get("groupId");
const claimed = new URL(request.url).searchParams.get("claimed");
if (groupId) {
const { authenticated } = await _authCheck(locals.validate, groupId);
if (!authenticated) {
Expand All @@ -24,7 +25,8 @@ export const DELETE: RequestHandler = async ({ locals, request }) => {
try {
const items = await client.item.deleteMany({
where: {
groupId: groupId ? groupId : undefined
groupId: groupId ? groupId : undefined,
pledgedById: claimed && Boolean(claimed) ? { not: null } : undefined
}
});
return new Response(JSON.stringify(items), { status: 200 });
Expand Down

0 comments on commit 8dce023

Please sign in to comment.