Skip to content

Commit 675918c

Browse files
committed
refactor: Move outfit get/set/rm to centralized helper
1 parent 0a0e365 commit 675918c

3 files changed

Lines changed: 61 additions & 52 deletions

File tree

src/composables/storage.ts

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* Created Date: 2026-03-23 21:34:56
55
* Author: 3urobeat
66
*
7-
* Last Modified: 2026-03-29 18:41:59
7+
* Last Modified: 2026-03-29 18:46:54
88
* Modified By: 3urobeat
99
*
1010
* Copyright (c) 2026 3urobeat <https://github.com/3urobeat>
@@ -76,6 +76,53 @@ export async function rmClothingToServer(id: string) {
7676

7777
return await res.json();
7878
}
79+
80+
export async function getAllOutfitsFromServer(): Promise<Outfit[]> {
81+
return (await useFetch("/api/get-all-outfits")).data.value!;
82+
}
83+
84+
export async function getOutfitFromServer(id: string): Promise<Outfit> {
85+
const res = await fetch("/api/get-outfit", {
86+
method: "POST",
87+
headers: {
88+
"Content-Type": "application/json"
89+
},
90+
body: JSON.stringify({
91+
id: id
92+
})
93+
});
94+
95+
return await res.json();
96+
}
97+
98+
export async function setOutfitToServer(data: Outfit) {
99+
const res = await fetch("/api/set-outfit", {
100+
method: "POST",
101+
headers: {
102+
"Content-Type": "application/json"
103+
},
104+
body: JSON.stringify({
105+
outfit: data
106+
})
107+
});
108+
109+
return await res.json();
110+
}
111+
112+
export async function rmOutfitToServer(id: string) {
113+
const res = await fetch("/api/rm-outfit", {
114+
method: "POST",
115+
headers: {
116+
"Content-Type": "application/json"
117+
},
118+
body: JSON.stringify({
119+
id: id
120+
})
121+
});
122+
123+
return await res.json();
124+
}
125+
79126
export async function initLabels() {
80127
storedLabels.value = (await useFetch("/api/get-all-labels")).data.value!;
81128
}

src/pages/outfits/index.vue

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* Created Date: 2025-09-08 15:40:46
66
* Author: 3urobeat
77
*
8-
* Last Modified: 2026-03-28 21:14:00
8+
* Last Modified: 2026-03-29 18:45:58
99
* Modified By: 3urobeat
1010
*
1111
* Copyright (c) 2025 - 2026 3urobeat <https://github.com/3urobeat>
@@ -95,6 +95,7 @@
9595
import type { Label } from "~/model/label";
9696
import type { Outfit } from "~/model/item";
9797
import { defaultSortMode, type sortModes } from "~/model/sort-modes";
98+
import { getAllOutfitsFromServer } from "~/composables/storage";
9899
99100
100101
// Set page properties
@@ -103,20 +104,15 @@
103104
});
104105
105106
106-
// Get global cache from app.vue
107-
108-
// Cache
109-
const storedOutfits: Ref<Outfit[]> = ref([]);
107+
// Get labels and outfits from cache
110108
const storedLabels: Ref<Label[]> = getAllLabelsFromServer();
109+
const storedOutfits: Ref<Outfit[]> = ref([]);
110+
storedOutfits.value = await getAllOutfitsFromServer();
111111
112112
// Get refs to props exported by defineExpose() in TitleBarFull
113113
const titleBarFull: Ref<{ selectedSort: sortModes, selectedFilters: string[], selectedScaling: number, toggleFilter: (thisFilter: string) => void }> = ref({ selectedSort: defaultSortMode, selectedFilters: [], selectedScaling: 0, toggleFilter: () => {} }); // TODO: Can this be an exported type somewhere?
114114
115115
116-
// Get all outfits and their details on load
117-
let res = await useFetch("/api/get-all-outfits");
118-
storedOutfits.value = res.data.value!; // TODO: Error handling
119-
120116
// Pre-calculate items that should be shown. Can be accessed multiple times in template without re-calculation. Updates when sort/filter/search changes due to reactivity
121117
let outfitsToShow = computed(() => getItemsToShow(storedOutfits.value, titleBarFull.value.selectedSort, titleBarFull.value.selectedFilters) as Outfit[]);
122118

src/pages/outfits/view.vue

Lines changed: 8 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* Created Date: 2025-09-10 17:37:07
66
* Author: 3urobeat
77
*
8-
* Last Modified: 2026-03-27 19:03:05
8+
* Last Modified: 2026-03-29 18:50:04
99
* Modified By: 3urobeat
1010
*
1111
* Copyright (c) 2025 - 2026 3urobeat <https://github.com/3urobeat>
@@ -243,6 +243,7 @@
243243
import { defaultSortMode } from "~/model/sort-modes";
244244
import { responseIndicatorFailure, responseIndicatorSuccess } from "~/composables/responseIndicator";
245245
import threedModelViewer from "~/components/threedModelViewer.vue";
246+
import { getAllClothesFromServer, getOutfitFromServer, rmOutfitToServer, setOutfitToServer } from "~/composables/storage";
246247
247248
248249
// Get from cache
@@ -252,14 +253,15 @@
252253
// Refs
253254
const thisOutfit: Ref<Outfit> = ref({ id: "", title: "", clothes: [], labelIDs: [], previewImgPath: "", addedTimestamp: 0, modifiedTimestamp: 0 });
254255
const bodyPartLabels: Ref<Label[]> = ref([]);
255-
const storedClothes: Ref<Clothing[]> = ref([]); // Edit Mode only
256+
const storedClothes: Ref<Clothing[]> = ref([]);
257+
storedClothes.value = await getAllClothesFromServer();
256258
257259
258260
// Check if edit mode is enabled based on if name of this route is outfits-view or outfits-edit
259261
const editModeEnabled = (useRoute().name == "outfits-edit");
260262
261263
// Get ID of the outfit to view from query parameters
262-
const outfitId = useRoute().query.id || "new";
264+
const outfitId = (useRoute().query.id || "new").toString();
263265
264266
// Redirect to edit page if view was opened with id new
265267
if (!editModeEnabled && outfitId == "new") useRouter().push("/outfits/edit?id=new");
@@ -272,27 +274,11 @@
272274
}
273275
274276
275-
// Get outfits and their data
276-
let clothingRes = await useFetch("/api/get-all-clothes");
277-
storedClothes.value = clothingRes.data.value!; // TODO: Error handling
278-
279-
280-
281277
// Load images for clothes // TODO: Lazy load
282278
onMounted(async () => {
283279
// Get outfit data if not new
284280
if (outfitId != "new") {
285-
const outfitRes = await fetch("/api/get-outfit", {
286-
method: "POST",
287-
headers: {
288-
"Content-Type": "application/json"
289-
},
290-
body: JSON.stringify({
291-
id: outfitId
292-
})
293-
});
294-
295-
thisOutfit.value = await outfitRes.json(); // TODO: Error handling
281+
thisOutfit.value = await getOutfitFromServer(outfitId);
296282
}
297283
});
298284
@@ -356,17 +342,7 @@
356342
357343
// Send request to API if user confirmed
358344
if (confirmed) {
359-
const res = await fetch("/api/rm-outfit", {
360-
method: "POST",
361-
headers: {
362-
"Content-Type": "application/json"
363-
},
364-
body: JSON.stringify({
365-
id: thisOutfit.value.id
366-
})
367-
});
368-
369-
const resBody = await res.json();
345+
const resBody = await rmOutfitToServer(thisOutfit.value.id);
370346
371347
// Indicate success/failure
372348
if (resBody.success) {
@@ -389,17 +365,7 @@
389365
async function saveChanges() {
390366
391367
// Send data to API
392-
const res = await fetch("/api/set-outfit", {
393-
method: "POST",
394-
headers: {
395-
"Content-Type": "application/json"
396-
},
397-
body: JSON.stringify({
398-
outfit: thisOutfit.value
399-
})
400-
});
401-
402-
const resBody = await res.json();
368+
const resBody = await setOutfitToServer(thisOutfit.value);
403369
404370
// Update local refs depending on success/failure and indicate result
405371
if (resBody.success) {

0 commit comments

Comments
 (0)