feat: admin stiker
deskrispi: - feature update status sticker
This commit is contained in:
39
src/app/api/sticker/[id]/activation/route.ts
Normal file
39
src/app/api/sticker/[id]/activation/route.ts
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
import { NextResponse } from "next/server";
|
||||||
|
import { prisma } from "@/lib";
|
||||||
|
|
||||||
|
export { PUT };
|
||||||
|
|
||||||
|
async function PUT(request: Request, { params }: { params: { id: string } }) {
|
||||||
|
const method = request.method;
|
||||||
|
if (method !== "PUT") {
|
||||||
|
return NextResponse.json(
|
||||||
|
{ success: false, message: "Method not allowed" },
|
||||||
|
{ status: 405 }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const { id } = params;
|
||||||
|
const data = await request.json();
|
||||||
|
|
||||||
|
const sticker = await prisma.sticker.update({
|
||||||
|
where: {
|
||||||
|
id: id,
|
||||||
|
},
|
||||||
|
data: {
|
||||||
|
isActive: data.isActive,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
return NextResponse.json(
|
||||||
|
{ success: true, message: "Success update data sticker", data: sticker },
|
||||||
|
{ status: 200 }
|
||||||
|
);
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error update data sticker", error);
|
||||||
|
return NextResponse.json(
|
||||||
|
{ success: false, message: "Error update data sticker" },
|
||||||
|
{ status: 500 }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -62,7 +62,7 @@ async function GET(request: Request) {
|
|||||||
try {
|
try {
|
||||||
const sticker = await prisma.sticker.findMany({
|
const sticker = await prisma.sticker.findMany({
|
||||||
orderBy: {
|
orderBy: {
|
||||||
updatedAt: "desc",
|
createdAt: "desc",
|
||||||
},
|
},
|
||||||
include: {
|
include: {
|
||||||
MasterEmotions: true,
|
MasterEmotions: true,
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ export function Admin_ComponentModal({
|
|||||||
centered
|
centered
|
||||||
title={title}
|
title={title}
|
||||||
withCloseButton={withCloseButton ? withCloseButton : false}
|
withCloseButton={withCloseButton ? withCloseButton : false}
|
||||||
closeOnClickOutside={closeOnClickOutside}
|
closeOnClickOutside={closeOnClickOutside ? closeOnClickOutside : false}
|
||||||
>
|
>
|
||||||
{children}
|
{children}
|
||||||
</Modal>
|
</Modal>
|
||||||
|
|||||||
@@ -165,3 +165,37 @@ export const apiAdminDeleteSticker = async ({ id }: { id: string }) => {
|
|||||||
throw error; // Re-throw the error to handle it in the calling function
|
throw error; // Re-throw the error to handle it in the calling function
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const apiAdminUpdateStatusStickerById = async ({ data }: { data: any }) => {
|
||||||
|
try {
|
||||||
|
// Fetch token from cookie
|
||||||
|
const { token } = await fetch("/api/get-cookie").then((res) => res.json());
|
||||||
|
if (!token) {
|
||||||
|
console.error("No token found");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
const response = await fetch(`/api/sticker/${data.id}/activation`, {
|
||||||
|
method: "PUT",
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
Accept: "application/json",
|
||||||
|
Authorization: `Bearer ${token}`,
|
||||||
|
},
|
||||||
|
body: JSON.stringify(data),
|
||||||
|
});
|
||||||
|
|
||||||
|
// Check if the response is OK
|
||||||
|
if (!response.ok) {
|
||||||
|
const errorData = await response.json().catch(() => null);
|
||||||
|
console.error("Failed to update status sticker", response.statusText, errorData);
|
||||||
|
throw new Error(errorData?.message || "Failed to update status sticker");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return the JSON response
|
||||||
|
return await response.json();
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error update status sticker", error);
|
||||||
|
throw error; // Re-throw the error to handle it in the calling function
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -168,11 +168,6 @@ export default function AdminAppInformation_ViewStickerDetail() {
|
|||||||
fileId: data?.fileId as string,
|
fileId: data?.fileId as string,
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!deleteFile.success) {
|
|
||||||
ComponentGlobal_NotifikasiPeringatan("Gagal delete gambar");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
setLoadingDelete(false);
|
setLoadingDelete(false);
|
||||||
ComponentAdminGlobal_NotifikasiBerhasil("Berhasil dihapus");
|
ComponentAdminGlobal_NotifikasiBerhasil("Berhasil dihapus");
|
||||||
router.back();
|
router.back();
|
||||||
|
|||||||
@@ -19,8 +19,9 @@ import {
|
|||||||
ScrollArea,
|
ScrollArea,
|
||||||
Spoiler,
|
Spoiler,
|
||||||
Stack,
|
Stack,
|
||||||
|
Switch,
|
||||||
Table,
|
Table,
|
||||||
Text
|
Text,
|
||||||
} from "@mantine/core";
|
} from "@mantine/core";
|
||||||
import { useShallowEffect } from "@mantine/hooks";
|
import { useShallowEffect } from "@mantine/hooks";
|
||||||
import { IconPencil, IconPlus } from "@tabler/icons-react";
|
import { IconPencil, IconPlus } from "@tabler/icons-react";
|
||||||
@@ -29,18 +30,25 @@ import { useRouter } from "next/navigation";
|
|||||||
import { useState } from "react";
|
import { useState } from "react";
|
||||||
import { ComponentAdminGlobal_TitlePage } from "../../_admin_global/_component";
|
import { ComponentAdminGlobal_TitlePage } from "../../_admin_global/_component";
|
||||||
import { Admin_ComponentBoxStyle } from "../../_admin_global/_component/comp_admin_boxstyle";
|
import { Admin_ComponentBoxStyle } from "../../_admin_global/_component/comp_admin_boxstyle";
|
||||||
import { apiAdminGetSticker } from "../lib/api_fetch_stiker";
|
import { Admin_ComponentModal } from "../../_admin_global/_component/comp_admin_modal";
|
||||||
|
import { ComponentAdminGlobal_NotifikasiBerhasil } from "../../_admin_global/admin_notifikasi/notifikasi_berhasil";
|
||||||
|
import { ComponentAdminGlobal_NotifikasiGagal } from "../../_admin_global/admin_notifikasi/notifikasi_gagal";
|
||||||
|
import {
|
||||||
|
apiAdminGetSticker,
|
||||||
|
apiAdminUpdateStatusStickerById,
|
||||||
|
} from "../lib/api_fetch_stiker";
|
||||||
|
|
||||||
export default function AdminAppInformation_ViewSticker() {
|
export default function AdminAppInformation_ViewSticker() {
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
const [loadingCreate, setLoadingCreate] = useState(false);
|
const [loadingCreate, setLoadingCreate] = useState(false);
|
||||||
const [loadingDetail, setLoadingDetail] = useState<string | null>(null);
|
const [loadingDetail, setLoadingDetail] = useState<string | null>(null);
|
||||||
const [dataSticker, setDataSticker] = useState<ISticker[] | null>(null);
|
const [dataSticker, setDataSticker] = useState<ISticker[] | null>(null);
|
||||||
const [isActivation, setIsActivation] = useState(false);
|
const [dataUpdate, setDataUpdate] = useState({
|
||||||
const [updateStatus, setUpdateStatus] = useState({
|
|
||||||
id: "",
|
id: "",
|
||||||
active: false,
|
isActive: false,
|
||||||
});
|
});
|
||||||
|
const [loadingUpdate, setLoadingUpdate] = useState(false);
|
||||||
|
const [opened, setOpened] = useState(false);
|
||||||
|
|
||||||
useShallowEffect(() => {
|
useShallowEffect(() => {
|
||||||
const fetchData = async () => {
|
const fetchData = async () => {
|
||||||
@@ -57,6 +65,44 @@ export default function AdminAppInformation_ViewSticker() {
|
|||||||
fetchData();
|
fetchData();
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
const handleUpdateActivation = async ({
|
||||||
|
id,
|
||||||
|
value,
|
||||||
|
}: {
|
||||||
|
id: string;
|
||||||
|
value: boolean;
|
||||||
|
}) => {
|
||||||
|
const data = {
|
||||||
|
id: id,
|
||||||
|
isActive: value,
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
try {
|
||||||
|
setLoadingUpdate(true);
|
||||||
|
const updt = await apiAdminUpdateStatusStickerById({
|
||||||
|
data: data as any,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (updt.success) {
|
||||||
|
const cloneData = [...(dataSticker || [])];
|
||||||
|
const index = cloneData.findIndex((e) => e.id === id);
|
||||||
|
if (index !== -1) {
|
||||||
|
cloneData[index].isActive = value;
|
||||||
|
setDataSticker([...cloneData]);
|
||||||
|
}
|
||||||
|
|
||||||
|
ComponentAdminGlobal_NotifikasiBerhasil(updt.message);
|
||||||
|
} else {
|
||||||
|
ComponentAdminGlobal_NotifikasiGagal(updt.message);
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.log("Error update status sticker", error);
|
||||||
|
} finally {
|
||||||
|
setLoadingUpdate(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<Stack>
|
<Stack>
|
||||||
@@ -94,9 +140,9 @@ export default function AdminAppInformation_ViewSticker() {
|
|||||||
<th>
|
<th>
|
||||||
<Center c={AdminColor.white}>Aksi</Center>
|
<Center c={AdminColor.white}>Aksi</Center>
|
||||||
</th>
|
</th>
|
||||||
{/* <th>
|
<th>
|
||||||
<Center c={AdminColor.white}>Status</Center>
|
<Center c={AdminColor.white}>Status</Center>
|
||||||
</th> */}
|
</th>
|
||||||
|
|
||||||
<th>
|
<th>
|
||||||
<Center c={AdminColor.white}>Stiker</Center>
|
<Center c={AdminColor.white}>Stiker</Center>
|
||||||
@@ -112,10 +158,9 @@ export default function AdminAppInformation_ViewSticker() {
|
|||||||
router,
|
router,
|
||||||
loadingDetail,
|
loadingDetail,
|
||||||
setLoadingDetail,
|
setLoadingDetail,
|
||||||
isActivation,
|
setOpened,
|
||||||
setIsActivation,
|
dataUpdate,
|
||||||
updateStatus,
|
setDataUpdate,
|
||||||
setUpdateStatus,
|
|
||||||
})}
|
})}
|
||||||
</tbody>
|
</tbody>
|
||||||
</Table>
|
</Table>
|
||||||
@@ -123,6 +168,41 @@ export default function AdminAppInformation_ViewSticker() {
|
|||||||
</Admin_ComponentBoxStyle>
|
</Admin_ComponentBoxStyle>
|
||||||
)}
|
)}
|
||||||
</Stack>
|
</Stack>
|
||||||
|
|
||||||
|
<Admin_ComponentModal opened={opened} onClose={() => setOpened(false)}>
|
||||||
|
<Stack>
|
||||||
|
<Text fw={500} c={AdminColor.white}>
|
||||||
|
Apakah anda yakin ingin mengubah status stiker ini ?
|
||||||
|
</Text>
|
||||||
|
<Group position="center">
|
||||||
|
<Button
|
||||||
|
radius={"xl"}
|
||||||
|
onClick={() => {
|
||||||
|
setOpened(false);
|
||||||
|
setLoadingUpdate(false);
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
Tidak
|
||||||
|
</Button>
|
||||||
|
<Button
|
||||||
|
loading={loadingUpdate}
|
||||||
|
loaderPosition="center"
|
||||||
|
radius={"xl"}
|
||||||
|
bg={MainColor.green}
|
||||||
|
color="green"
|
||||||
|
onClick={() => {
|
||||||
|
handleUpdateActivation({
|
||||||
|
id: dataUpdate.id,
|
||||||
|
value: dataUpdate.isActive,
|
||||||
|
});
|
||||||
|
setOpened(false);
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
Ya
|
||||||
|
</Button>
|
||||||
|
</Group>
|
||||||
|
</Stack>
|
||||||
|
</Admin_ComponentModal>
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -132,10 +212,12 @@ type RowTableProps = {
|
|||||||
router: AppRouterInstance;
|
router: AppRouterInstance;
|
||||||
loadingDetail: string | null;
|
loadingDetail: string | null;
|
||||||
setLoadingDetail: (val: string | null) => void;
|
setLoadingDetail: (val: string | null) => void;
|
||||||
isActivation: boolean;
|
setOpened: (val: boolean) => void;
|
||||||
setIsActivation: (val: boolean) => void;
|
dataUpdate: {
|
||||||
updateStatus: { id: string; active: boolean };
|
id: string;
|
||||||
setUpdateStatus: (val: { id: string; active: boolean }) => void;
|
isActive: boolean;
|
||||||
|
};
|
||||||
|
setDataUpdate: (val: { id: string; isActive: boolean }) => void;
|
||||||
};
|
};
|
||||||
|
|
||||||
const rowTable = ({
|
const rowTable = ({
|
||||||
@@ -143,10 +225,9 @@ const rowTable = ({
|
|||||||
router,
|
router,
|
||||||
loadingDetail,
|
loadingDetail,
|
||||||
setLoadingDetail,
|
setLoadingDetail,
|
||||||
isActivation,
|
setOpened,
|
||||||
setIsActivation,
|
dataUpdate,
|
||||||
updateStatus,
|
setDataUpdate,
|
||||||
setUpdateStatus,
|
|
||||||
}: RowTableProps) => {
|
}: RowTableProps) => {
|
||||||
if (!Array.isArray(dataSticker) || dataSticker.length === 0) {
|
if (!Array.isArray(dataSticker) || dataSticker.length === 0) {
|
||||||
return (
|
return (
|
||||||
@@ -184,23 +265,20 @@ const rowTable = ({
|
|||||||
</Button>
|
</Button>
|
||||||
</Center>
|
</Center>
|
||||||
</td>
|
</td>
|
||||||
{/* <td>
|
<td>
|
||||||
<Center>
|
<Center>
|
||||||
<Switch
|
<Switch
|
||||||
checked={e.isActive}
|
checked={e.isActive}
|
||||||
color="green"
|
color="yellow"
|
||||||
onLabel="ON"
|
onLabel="ON"
|
||||||
offLabel="OFF"
|
offLabel="OFF"
|
||||||
onChange={(val) => {
|
onChange={(val) => {
|
||||||
setIsActivation(true);
|
setDataUpdate({ id: e.id, isActive: val.currentTarget.checked });
|
||||||
setUpdateStatus({
|
setOpened(true);
|
||||||
id: e?.id,
|
|
||||||
active: val.currentTarget.checked as any,
|
|
||||||
});
|
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
</Center>
|
</Center>
|
||||||
</td> */}
|
</td>
|
||||||
<td>
|
<td>
|
||||||
<Center>
|
<Center>
|
||||||
<Paper bg="gray" p={"xs"}>
|
<Paper bg="gray" p={"xs"}>
|
||||||
|
|||||||
Reference in New Issue
Block a user