Sinkronisasi UI & API Admin - User Menu Desa, Submenu Gallery

This commit is contained in:
2025-08-12 11:45:39 +08:00
parent 2fe8b8ce1a
commit c1583c21b1
24 changed files with 1173 additions and 398 deletions

View File

@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import ApiFetch from "@/lib/api-fetch";
import { Prisma } from "@prisma/client";
import { toast } from "react-toastify";
@@ -68,10 +69,34 @@ const foto = proxy({
};
}>[]
| null,
async load() {
const res = await ApiFetch.api.desa.gallery.foto["find-many"].get();
if (res.status === 200) {
foto.findMany.data = res.data?.data ?? [];
page: 1,
totalPages: 1,
loading: false,
search: "",
load: async (page = 1, limit = 10, search = "") => {
foto.findMany.loading = true; // ✅ Akses langsung via nama path
foto.findMany.page = page;
foto.findMany.search = search;
try {
const query: any = { page, limit };
if (search) query.search = search;
const res = await ApiFetch.api.desa.gallery.foto["find-many"].get({ query });
if (res.status === 200 && res.data?.success) {
foto.findMany.data = res.data.data ?? [];
foto.findMany.totalPages = res.data.totalPages ?? 1;
} else {
foto.findMany.data = [];
foto.findMany.totalPages = 1;
}
} catch (err) {
console.error("Gagal fetch foto paginated:", err);
foto.findMany.data = [];
foto.findMany.totalPages = 1;
} finally {
foto.findMany.loading = false;
}
},
},
@@ -215,6 +240,28 @@ const foto = proxy({
foto.update.form = { ...defaultFormFoto };
},
},
findRecent: {
data: [] as Prisma.GalleryFotoGetPayload<{
include: {
imageGalleryFoto: true;
};
}>[],
loading: false,
async load() {
try {
this.loading = true;
const res = await ApiFetch.api.desa.gallery.foto["find-recent"].get();
if (res.status === 200 && res.data?.success) {
this.data = res.data.data ?? [];
}
} catch (error) {
console.error("Gagal fetch foto recent:", error);
} finally {
this.loading = false;
}
},
},
});
const video = proxy({
@@ -257,10 +304,34 @@ const video = proxy({
};
}>[]
| null,
async load() {
const res = await ApiFetch.api.desa.gallery.video["find-many"].get();
if (res.status === 200) {
video.findMany.data = res.data?.data ?? [];
page: 1,
totalPages: 1,
loading: false,
search: "",
load: async (page = 1, limit = 10, search = "") => {
video.findMany.loading = true; // ✅ Akses langsung via nama path
video.findMany.page = page;
video.findMany.search = search;
try {
const query: any = { page, limit };
if (search) query.search = search;
const res = await ApiFetch.api.desa.gallery.video["find-many"].get({ query });
if (res.status === 200 && res.data?.success) {
video.findMany.data = res.data.data ?? [];
video.findMany.totalPages = res.data.totalPages ?? 1;
} else {
video.findMany.data = [];
video.findMany.totalPages = 1;
}
} catch (err) {
console.error("Gagal fetch video paginated:", err);
video.findMany.data = [];
video.findMany.totalPages = 1;
} finally {
video.findMany.loading = false;
}
},
},

View File

@@ -0,0 +1,63 @@
import ApiFetch from "@/lib/api-fetch";
import { proxy } from "valtio";
interface FileItem {
id: string;
name: string;
path: string;
link: string;
mimeType: string;
category: string;
realName: string;
isActive: boolean;
createdAt: string | Date;
updatedAt: string | Date;
deletedAt: string | Date | null;
}
const stateFileStorage = proxy<{
list: FileItem[] | null;
page: number;
limit: number;
total: number | undefined;
load: (params?: { search?: string }) => Promise<void>;
del: (params: { id: string }) => Promise<void>;
}>({
list: null,
page: 1,
limit: 10,
total: undefined,
async load(params?: { search?: string }) {
const { search = "" } = params ?? {};
try {
const { data } = await ApiFetch.api.fileStorage.findMany.get({
query: {
page: this.page,
limit: this.limit,
search,
category: 'image'
},
});
if (data?.data) {
this.list = data.data as FileItem[];
this.total = data.meta?.totalPages;
}
} catch (error) {
console.error('Error loading files:', error);
this.list = [];
this.total = 0;
}
},
async del({ id }: { id: string }) {
try {
await ApiFetch.api.fileStorage.delete({ id });
await this.load();
} catch (error) {
console.error('Error deleting file:', error);
throw error;
}
},
});
export default stateFileStorage;