This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
import { ref } from 'vue';
|
||||
|
||||
import {
|
||||
deleteImage,
|
||||
listAdminImages,
|
||||
reorderImage,
|
||||
retryImageProcessing,
|
||||
setPrimaryImage,
|
||||
updateImageAltText,
|
||||
} from '@/services/admin-image-service.js';
|
||||
|
||||
function csrfToken() {
|
||||
return document.cookie.split('; ').find((value) => value.startsWith('pokemon_inventory_csrf='))?.split('=')[1] ?? '';
|
||||
}
|
||||
|
||||
export function useAdminImages(inventoryItemId) {
|
||||
const images = ref([]);
|
||||
const isLoading = ref(false);
|
||||
const isSaving = ref(false);
|
||||
const error = ref(null);
|
||||
|
||||
async function load() {
|
||||
isLoading.value = true;
|
||||
error.value = null;
|
||||
try { images.value = await listAdminImages(inventoryItemId); }
|
||||
catch (requestError) { error.value = requestError; }
|
||||
finally { isLoading.value = false; }
|
||||
}
|
||||
|
||||
async function apply(action) {
|
||||
isSaving.value = true;
|
||||
error.value = null;
|
||||
try { await action(csrfToken()); await load(); }
|
||||
catch (requestError) { error.value = requestError; }
|
||||
finally { isSaving.value = false; }
|
||||
}
|
||||
|
||||
return {
|
||||
images,
|
||||
isLoading,
|
||||
isSaving,
|
||||
error,
|
||||
load,
|
||||
reorder: (imageId, sortOrder) => apply((token) => reorderImage(imageId, sortOrder, token)),
|
||||
setPrimary: (imageId) => apply((token) => setPrimaryImage(imageId, token)),
|
||||
updateAltText: (imageId, altText) => apply((token) => updateImageAltText(imageId, altText, token)),
|
||||
retry: (imageId) => apply((token) => retryImageProcessing(imageId, token)),
|
||||
remove: (imageId) => apply((token) => deleteImage(imageId, token)),
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user