From ba05134015dc798f7dde864a7a972587d3ee4f0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=98=99=E2=97=A6=20The=20Tablet=20=E2=9D=80=20GamerGirla?= =?UTF-8?q?ndCo=20=E2=97=A6=E2=9D=A7?= Date: Tue, 10 Oct 2023 22:19:18 -0400 Subject: [PATCH] refactor(client-side): create utilities mainly intended to avoid repitition in long lists of data like stories, authors, and/or bands --- lib/client/listActions.ts | 78 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 lib/client/listActions.ts diff --git a/lib/client/listActions.ts b/lib/client/listActions.ts new file mode 100644 index 0000000..80add2b --- /dev/null +++ b/lib/client/listActions.ts @@ -0,0 +1,78 @@ +import { ListGridType } from "ant-design-vue/es/list"; +import { FavPayload, HidePayload, SubPayload } from "./types/form/favSub"; + +const base = `/user/me`; + +export const favourites = ( + values: (any & { _id: number })[], + id: number, + remove: boolean, + type: "story" | "author", +) => { + values?.splice( + values!.findIndex((a) => a._id == id), + 1, + ); + const key = type === "story" ? "stories" : "authors"; + const todo = [id]; + useApiFetch(`${base}/favs`, { + method: "put", + body: { + [key]: { + pull: remove ? todo : [], + push: !remove ? todo : [], + }, + } as FavPayload, + }); +}; + +export const subscriptions = ( + values: (any & { _id: number })[], + id: number, + action: "hide" | "subscribe" | "unsubscribe", + type: "bands" | "authors", +) => { + values?.splice( + values!.findIndex((a) => a._id == id), + 1, + ); + if (action == "hide") { + useApiFetch(`${base}/${action}`, { + body: { + push: { + [type]: [id], + }, + pull: {}, + } as HidePayload, + method: "put", + }); + } else if (action == "subscribe") { + useApiFetch(`${base}/subscriptions`, { + body: { + push: { + [type]: [id], + }, + pull: {}, + } as SubPayload, + method: "put", + }); + } else if (action == "unsubscribe") { + useApiFetch(`${base}/subscriptions`, { + body: { + pull: { + [type]: [id], + }, + push: {}, + } as SubPayload, + method: "put", + }); + } +}; +export const bp: ListGridType = { + gutter: 1, + xs: 1, + sm: 2, + md: 3, + lg: 4, + xl: 5, +};