UI admin bagian pengumuman dan backendnya
This commit is contained in:
Binary file not shown.
|
After Width: | Height: | Size: 275 KiB |
@@ -1,30 +1,32 @@
|
|||||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||||
import ApiFetch from "@/lib/api-fetch"
|
import ApiFetch from "@/lib/api-fetch";
|
||||||
import { Prisma } from "@prisma/client"
|
import { Prisma } from "@prisma/client";
|
||||||
import { toast } from "react-toastify"
|
import { toast } from "react-toastify";
|
||||||
import { proxy } from "valtio"
|
import { proxy } from "valtio";
|
||||||
import { z } from "zod"
|
import { z } from "zod";
|
||||||
|
|
||||||
const templateFormPengumuman = z.object({
|
const templateFormPengumuman = z.object({
|
||||||
judul: z.string().min(3, "Judul minimal 3 karakter"),
|
judul: z.string().min(3, "Judul minimal 3 karakter"),
|
||||||
deskripsi: z.string().min(3, "Deskripsi minimal 3 karakter"),
|
deskripsi: z.string().min(3, "Deskripsi minimal 3 karakter"),
|
||||||
content: z.string().min(3, "Content minimal 3 karakter"),
|
content: z.string().min(3, "Content minimal 3 karakter"),
|
||||||
categoryPengumumanId: z.string().nonempty(),
|
categoryPengumumanId: z.string().nonempty(),
|
||||||
})
|
});
|
||||||
|
|
||||||
const category = proxy ({
|
const category = proxy({
|
||||||
findMany: {
|
findMany: {
|
||||||
data: null as
|
data: null as
|
||||||
| null
|
| null
|
||||||
| Prisma.CategoryPengumumanGetPayload<{ omit: { isActive: true } }>[],
|
| Prisma.CategoryPengumumanGetPayload<{ omit: { isActive: true } }>[],
|
||||||
async load() {
|
async load() {
|
||||||
const res = await ApiFetch.api.desa.pengumuman.category["find-many"].get();
|
const res = await ApiFetch.api.desa.pengumuman.category[
|
||||||
|
"find-many"
|
||||||
|
].get();
|
||||||
if (res.status === 200) {
|
if (res.status === 200) {
|
||||||
category.findMany.data = res.data?.data as any ?? [];
|
category.findMany.data = (res.data?.data as any) ?? [];
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
}
|
},
|
||||||
})
|
});
|
||||||
|
|
||||||
type PengumumanForm = Prisma.PengumumanGetPayload<{
|
type PengumumanForm = Prisma.PengumumanGetPayload<{
|
||||||
select: {
|
select: {
|
||||||
@@ -32,10 +34,10 @@ type PengumumanForm = Prisma.PengumumanGetPayload<{
|
|||||||
deskripsi: true;
|
deskripsi: true;
|
||||||
content: true;
|
content: true;
|
||||||
categoryPengumumanId: true;
|
categoryPengumumanId: true;
|
||||||
}
|
};
|
||||||
}>
|
}>;
|
||||||
const pengumuman = proxy({
|
const pengumuman = proxy({
|
||||||
create: {
|
create: {
|
||||||
form: {} as PengumumanForm,
|
form: {} as PengumumanForm,
|
||||||
loading: false,
|
loading: false,
|
||||||
async create() {
|
async create() {
|
||||||
@@ -48,36 +50,197 @@ create: {
|
|||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
pengumuman.create.loading = true;
|
pengumuman.create.loading = true;
|
||||||
const res = await ApiFetch.api.desa.pengumuman["create"].post(pengumuman.create.form)
|
const res = await ApiFetch.api.desa.pengumuman["create"].post(
|
||||||
|
pengumuman.create.form
|
||||||
|
);
|
||||||
if (res.status === 200) {
|
if (res.status === 200) {
|
||||||
pengumuman.findMany.load();
|
pengumuman.findMany.load();
|
||||||
return toast.success("success create");
|
return toast.success("success create");
|
||||||
}
|
}
|
||||||
console.log(res)
|
console.log(res);
|
||||||
return toast.error("failed create");
|
return toast.error("failed create");
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log((error as Error).message);
|
console.log((error as Error).message);
|
||||||
} finally{
|
} finally {
|
||||||
pengumuman.create.loading = false;
|
pengumuman.create.loading = false;
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
},
|
},
|
||||||
findMany: {
|
findMany: {
|
||||||
data: null as
|
data: null as
|
||||||
| Prisma.PengumumanGetPayload<{omit: {isActive: true}}>[]
|
| Prisma.PengumumanGetPayload<{
|
||||||
|
include: {
|
||||||
|
CategoryPengumuman: true;
|
||||||
|
}
|
||||||
|
}>[]
|
||||||
| null,
|
| null,
|
||||||
async load () {
|
async load() {
|
||||||
const res = await ApiFetch.api.desa.pengumuman["find-many"].get();
|
const res = await ApiFetch.api.desa.pengumuman["find-many"].get();
|
||||||
console.log(res)
|
console.log(res);
|
||||||
if (res.status === 200) {
|
if (res.status === 200) {
|
||||||
pengumuman.findMany.data = res.data?.data ?? [];
|
pengumuman.findMany.data = res.data?.data ?? [];
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
// findUnique: {
|
||||||
|
// data: null as
|
||||||
|
// | Prisma.PengumumanGetPayload<{
|
||||||
|
// include: {
|
||||||
|
// CategoryPengumuman: true;
|
||||||
|
// }
|
||||||
|
// }>
|
||||||
|
// | null,
|
||||||
|
// async load(id: string) {
|
||||||
|
// try {
|
||||||
|
// const res = await fetch(`/api/desa/pengumuman/${id}`);
|
||||||
|
// if (res.ok) {
|
||||||
|
// const data = await res.json();
|
||||||
|
// pengumuman.findUnique.data = data.data ?? null;
|
||||||
|
// } else {
|
||||||
|
// console.error('Failed to fetch pengumuman:', res.statusText);
|
||||||
|
// pengumuman.findUnique.data = null;
|
||||||
|
// }
|
||||||
|
// } catch (error) {
|
||||||
|
// console.error('Error fetching pengumuman:', error);
|
||||||
|
// pengumuman.findUnique.data = null;
|
||||||
|
// }
|
||||||
|
// },
|
||||||
|
// },
|
||||||
|
delete: {
|
||||||
|
loading: false,
|
||||||
|
async byId(id: string) {
|
||||||
|
if (!id) return toast.warn("ID tidak valid");
|
||||||
|
|
||||||
|
try {
|
||||||
|
pengumuman.delete.loading = true;
|
||||||
|
|
||||||
|
const response = await fetch(`/api/desa/pengumuman/delete/${id}`, {
|
||||||
|
method: "DELETE",
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const result = await response.json();
|
||||||
|
|
||||||
|
if (response.ok && result?.success) {
|
||||||
|
toast.success(result.message || "Pengumuman berhasil dihapus");
|
||||||
|
await pengumuman.findMany.load(); // refresh list
|
||||||
|
} else {
|
||||||
|
toast.error(result?.message || "Gagal menghapus pengumuman");
|
||||||
}
|
}
|
||||||
}
|
} catch (error) {
|
||||||
})
|
console.error("Gagal delete:", error);
|
||||||
|
toast.error("Terjadi kesalahan saat menghapus pengumuman");
|
||||||
|
} finally {
|
||||||
|
pengumuman.delete.loading = false;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
update: {
|
||||||
|
id: "",
|
||||||
|
form: {} as PengumumanForm,
|
||||||
|
loading: false,
|
||||||
|
|
||||||
|
async load(id: string) {
|
||||||
|
if (!id) {
|
||||||
|
toast.warn("ID tidak valid");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await fetch(`/api/desa/pengumuman/${id}`, {
|
||||||
|
method: "GET",
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error(`HTTP error! status: ${response.status}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const result = await response.json();
|
||||||
|
|
||||||
|
if (result?.success) {
|
||||||
|
const data = result.data;
|
||||||
|
this.id = data.id;
|
||||||
|
this.form = {
|
||||||
|
judul: data.judul,
|
||||||
|
deskripsi: data.deskripsi,
|
||||||
|
content: data.content,
|
||||||
|
categoryPengumumanId: data.categoryPengumumanId || "",
|
||||||
|
};
|
||||||
|
return data;
|
||||||
|
} else {
|
||||||
|
throw new Error(result?.message || "Gagal mengambil data pengumuman");
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error((error as Error).message);
|
||||||
|
toast.error("Terjadi kesalahan saat mengambil data pengumuman");
|
||||||
|
} finally {
|
||||||
|
pengumuman.update.loading = false;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
async update() {
|
||||||
|
const cek = templateFormPengumuman.safeParse(pengumuman.update.form);
|
||||||
|
if (!cek.success) {
|
||||||
|
const err = `[${cek.error.issues
|
||||||
|
.map((v) => `${v.path.join(".")}`)
|
||||||
|
.join("\n")}] required`;
|
||||||
|
toast.error(err);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
pengumuman.update.loading = true;
|
||||||
|
|
||||||
|
const response = await fetch(`/api/desa/pengumuman/${this.id}`, {
|
||||||
|
method: "PUT",
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
},
|
||||||
|
body: JSON.stringify({
|
||||||
|
judul: this.form.judul,
|
||||||
|
deskripsi: this.form.deskripsi,
|
||||||
|
content: this.form.content,
|
||||||
|
categoryPengumumanId: this.form.categoryPengumumanId,
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
const errorData = await response.json().catch(() => ({}));
|
||||||
|
throw new Error(
|
||||||
|
errorData.message || `HTTP error! status: ${response.status}`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const result = await response.json();
|
||||||
|
|
||||||
|
if (result.success) {
|
||||||
|
toast.success("Berhasil update pengumuman");
|
||||||
|
await pengumuman.findMany.load(); // refresh list
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
throw new Error(result.message || "Gagal update pengumuman");
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error updating pengumuman:", error);
|
||||||
|
toast.error(
|
||||||
|
error instanceof Error
|
||||||
|
? error.message
|
||||||
|
: "Terjadi kesalahan saat update pengumuman"
|
||||||
|
);
|
||||||
|
return false;
|
||||||
|
} finally {
|
||||||
|
pengumuman.update.loading = false;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
const stateDesaPengumuman = proxy({
|
const stateDesaPengumuman = proxy({
|
||||||
category,
|
category,
|
||||||
pengumuman
|
pengumuman,
|
||||||
})
|
});
|
||||||
export default stateDesaPengumuman
|
export default stateDesaPengumuman;
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import colors from '@/con/colors';
|
|||||||
import { Box, Button, Group, Paper, Stack, Text, TextInput, Title } from '@mantine/core';
|
import { Box, Button, Group, Paper, Stack, Text, TextInput, Title } from '@mantine/core';
|
||||||
import { IconArrowBack } from '@tabler/icons-react';
|
import { IconArrowBack } from '@tabler/icons-react';
|
||||||
import { useRouter } from 'next/navigation';
|
import { useRouter } from 'next/navigation';
|
||||||
import { KeamananEditor } from '../../../keamanan/_com/keamananEditor';
|
import { KeamananEditor } from '@/app/admin/(dashboard)/keamanan/_com/keamananEditor';
|
||||||
|
|
||||||
function EditPengumuman() {
|
function EditPengumuman() {
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
@@ -1,15 +1,14 @@
|
|||||||
'use client'
|
'use client'
|
||||||
|
|
||||||
import { Box, Button, Flex, Image, Paper, Stack, Text } from '@mantine/core';
|
import { Box, Button, Paper } from '@mantine/core';
|
||||||
import { IconArrowBack, IconEdit, IconX } from '@tabler/icons-react';
|
import { IconArrowBack } from '@tabler/icons-react';
|
||||||
import { useRouter } from 'next/navigation';
|
import { useRouter } from 'next/navigation';
|
||||||
|
|
||||||
import colors from '@/con/colors';
|
import colors from '@/con/colors';
|
||||||
// import { ModalKonfirmasiHapus } from '../../../_com/modalKonfirmasiHapus';
|
|
||||||
// import stateDashboardBerita from '../../../_state/desa/berita';
|
|
||||||
|
|
||||||
function DetailPengumuman() {
|
function DetailPengumuman() {
|
||||||
// const pengumumanState = useProxy(stateDashboardpengumuman)
|
// const pengumumanState = useProxy(stateDesaPengumuman)
|
||||||
// const [modalHapus, setModalHapus] = useState(false)
|
// const [modalHapus, setModalHapus] = useState(false)
|
||||||
// const [selectedId, setSelectedId] = useState<string | null>(null)
|
// const [selectedId, setSelectedId] = useState<string | null>(null)
|
||||||
// const params = useParams()
|
// const params = useParams()
|
||||||
@@ -32,9 +31,7 @@ function DetailPengumuman() {
|
|||||||
// if (!pengumumanState.pengumuman.findUnique.data) {
|
// if (!pengumumanState.pengumuman.findUnique.data) {
|
||||||
// return (
|
// return (
|
||||||
// <Stack py={10}>
|
// <Stack py={10}>
|
||||||
// {Array.from({ length: 10 }).map((_, k) => (
|
// <Skeleton h={400} />
|
||||||
// <Skeleton key={k} h={40} />
|
|
||||||
// ))}
|
|
||||||
// </Stack>
|
// </Stack>
|
||||||
// )
|
// )
|
||||||
// }
|
// }
|
||||||
@@ -47,51 +44,47 @@ function DetailPengumuman() {
|
|||||||
</Button>
|
</Button>
|
||||||
</Box>
|
</Box>
|
||||||
<Paper bg={colors['white-1']} w={{ base: "100%", md: "100%", lg: "50%" }} p={'md'}>
|
<Paper bg={colors['white-1']} w={{ base: "100%", md: "100%", lg: "50%" }} p={'md'}>
|
||||||
<Stack>
|
{/* <Stack>
|
||||||
<Text fz={"xl"} fw={"bold"}>Detail Pengumuman</Text>
|
<Text fz={"xl"} fw={"bold"}>Detail Pengumuman</Text>
|
||||||
<Paper bg={colors['BG-trans']} p={'md'}>
|
{pengumumanState.pengumuman.findUnique.data ? (
|
||||||
|
<Paper key={pengumumanState.pengumuman.findUnique.data.id} bg={colors['BG-trans']} p={'md'}>
|
||||||
<Stack gap={"xs"}>
|
<Stack gap={"xs"}>
|
||||||
<Box>
|
<Box>
|
||||||
<Text fw={"bold"} fz={"lg"}>Kategori</Text>
|
<Text fw={"bold"} fz={"lg"}>Kategori</Text>
|
||||||
<Text fz={"lg"}>Pendidikan</Text>
|
<Text fz={"lg"}>{pengumumanState.pengumuman.findUnique.data?.CategoryPengumuman?.name}</Text>
|
||||||
</Box>
|
</Box>
|
||||||
<Box>
|
<Box>
|
||||||
<Text fw={"bold"} fz={"lg"}>Judul</Text>
|
<Text fw={"bold"} fz={"lg"}>Judul</Text>
|
||||||
<Text fz={"lg"}>Pengumuman Pendidikan</Text>
|
<Text fz={"lg"}>{pengumumanState.pengumuman.findUnique.data?.judul}</Text>
|
||||||
</Box>
|
|
||||||
<Box>
|
|
||||||
<Text fw={"bold"} fz={"lg"}>Deskripsi Singkat</Text>
|
|
||||||
<Text fz={"lg"}>Pengumuman Pendidikan</Text>
|
|
||||||
</Box>
|
</Box>
|
||||||
<Box>
|
<Box>
|
||||||
<Text fw={"bold"} fz={"lg"}>Deskripsi</Text>
|
<Text fw={"bold"} fz={"lg"}>Deskripsi</Text>
|
||||||
<Text fz={"lg"}>Pengumuman Pendidikan</Text>
|
<Text fz={"lg"}>{pengumumanState.pengumuman.findUnique.data?.deskripsi}</Text>
|
||||||
</Box>
|
|
||||||
<Box>
|
|
||||||
<Text fw={"bold"} fz={"lg"}>Tanggal</Text>
|
|
||||||
<Text fz={"lg"}>2025-06-04</Text>
|
|
||||||
</Box>
|
|
||||||
<Box>
|
|
||||||
<Text fw={"bold"} fz={"lg"}>Waktu</Text>
|
|
||||||
<Text fz={"lg"}>08:00 - 17:00</Text>
|
|
||||||
</Box>
|
|
||||||
<Box>
|
|
||||||
<Text fw={"bold"} fz={"lg"}>Gambar</Text>
|
|
||||||
<Image w={{ base: 150, md: 150, lg: 150 }} src={"/"} alt="gambar" />
|
|
||||||
</Box>
|
</Box>
|
||||||
<Box>
|
<Box>
|
||||||
<Text fw={"bold"} fz={"lg"}>Konten</Text>
|
<Text fw={"bold"} fz={"lg"}>Konten</Text>
|
||||||
<Text fz={"lg"}>Pengumuman Pendidikan</Text>
|
<Text fz={"lg"} dangerouslySetInnerHTML={{ __html: pengumumanState.pengumuman.findUnique.data?.content }} />
|
||||||
</Box>
|
</Box>
|
||||||
<Flex gap={"xs"} mt={10}>
|
<Flex gap={"xs"} mt={10}>
|
||||||
<Button
|
<Button
|
||||||
onClick={() => router.push("/admin/desa/pengumuman")}
|
onClick={() => {
|
||||||
|
if (pengumumanState.pengumuman.findUnique.data) {
|
||||||
|
setSelectedId(pengumumanState.pengumuman.findUnique.data.id);
|
||||||
|
setModalHapus(true);
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
disabled={pengumumanState.pengumuman.delete.loading || !pengumumanState.pengumuman.findUnique.data}
|
||||||
color={"red"}
|
color={"red"}
|
||||||
>
|
>
|
||||||
<IconX size={20} />
|
<IconX size={20} />
|
||||||
</Button>
|
</Button>
|
||||||
<Button
|
<Button
|
||||||
onClick={() => router.push("/admin/desa/pengumuman/edit")}
|
onClick={() => {
|
||||||
|
if (pengumumanState.pengumuman.findUnique.data) {
|
||||||
|
router.push(`/admin/desa/pengumuman/${pengumumanState.pengumuman.findUnique.data.id}/edit`);
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
disabled={!pengumumanState.pengumuman.findUnique.data}
|
||||||
color={"green"}
|
color={"green"}
|
||||||
>
|
>
|
||||||
<IconEdit size={20} />
|
<IconEdit size={20} />
|
||||||
@@ -99,7 +92,8 @@ function DetailPengumuman() {
|
|||||||
</Flex>
|
</Flex>
|
||||||
</Stack>
|
</Stack>
|
||||||
</Paper>
|
</Paper>
|
||||||
</Stack>
|
) : null}
|
||||||
|
</Stack> */}
|
||||||
</Paper>
|
</Paper>
|
||||||
|
|
||||||
{/* Modal Konfirmasi Hapus
|
{/* Modal Konfirmasi Hapus
|
||||||
@@ -1,48 +1,74 @@
|
|||||||
'use client'
|
'use client'
|
||||||
import colors from '@/con/colors';
|
import colors from '@/con/colors';
|
||||||
import { Box, Button, Group, Paper, Stack, Text, TextInput, Title } from '@mantine/core';
|
import { Box, Button, Group, Paper, Select, Stack, Text, TextInput, Title } from '@mantine/core';
|
||||||
|
import { useShallowEffect } from '@mantine/hooks';
|
||||||
|
import { Prisma } from '@prisma/client';
|
||||||
import { IconArrowBack } from '@tabler/icons-react';
|
import { IconArrowBack } from '@tabler/icons-react';
|
||||||
import { useRouter } from 'next/navigation';
|
import { useRouter } from 'next/navigation';
|
||||||
import { KeamananEditor } from '../../../keamanan/_com/keamananEditor';
|
import { useProxy } from 'valtio/utils';
|
||||||
|
import CreateEditor from '../../../_com/createEditor';
|
||||||
|
import stateDesaPengumuman from '../../../_state/desa/pengumuman';
|
||||||
|
|
||||||
function CreatePengumuman() {
|
function CreatePengumuman() {
|
||||||
|
const pengumumanState = useProxy(stateDesaPengumuman)
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
|
|
||||||
|
const handleSubmit = async () => {
|
||||||
|
await pengumumanState.pengumuman.create.create()
|
||||||
|
resetForm()
|
||||||
|
router.push("/admin/desa/pengumuman")
|
||||||
|
}
|
||||||
|
|
||||||
|
const resetForm = () => {
|
||||||
|
pengumumanState.pengumuman.create.form = {
|
||||||
|
judul: "",
|
||||||
|
deskripsi: "",
|
||||||
|
content: "",
|
||||||
|
categoryPengumumanId: "",
|
||||||
|
};
|
||||||
|
};
|
||||||
return (
|
return (
|
||||||
<Box>
|
<Box>
|
||||||
<Box mb={10}>
|
<Box mb={10}>
|
||||||
<Button onClick={() => router.back()} variant='subtle' color={'blue'}>
|
<Button onClick={() => router.back()} variant='subtle' color={'blue'}>
|
||||||
<IconArrowBack color={colors['blue-button']} size={25}/>
|
<IconArrowBack color={colors['blue-button']} size={25} />
|
||||||
</Button>
|
</Button>
|
||||||
</Box>
|
</Box>
|
||||||
|
|
||||||
<Paper w={{base: '100%', md: '50%'}} bg={colors['white-1']} p={'md'}>
|
<Paper w={{ base: '100%', md: '50%' }} bg={colors['white-1']} p={'md'}>
|
||||||
<Stack gap={"xs"}>
|
<Stack gap={"xs"}>
|
||||||
<Title order={4}>Create Pengumuman</Title>
|
<Title order={4}>Create Pengumuman</Title>
|
||||||
<TextInput
|
<TextInput
|
||||||
label={<Text fw={"bold"} fz={"sm"}>Judul</Text>}
|
label={<Text fw={"bold"} fz={"sm"}>Judul</Text>}
|
||||||
placeholder='Masukkan judul'
|
placeholder='Masukkan judul'
|
||||||
|
onChange={(val) => {
|
||||||
|
pengumumanState.pengumuman.create.form.judul = val.target.value
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
<SelectCategory
|
||||||
|
onChange={(val) => {
|
||||||
|
pengumumanState.pengumuman.create.form.categoryPengumumanId = val.id;
|
||||||
|
}}
|
||||||
/>
|
/>
|
||||||
<TextInput
|
<TextInput
|
||||||
label={<Text fw={"bold"} fz={"sm"}>Deskripsi Singkat</Text>}
|
label={<Text fw={"bold"} fz={"sm"}>Deskripsi Singkat</Text>}
|
||||||
placeholder='Masukkan deskripsi singkat'
|
placeholder='Masukkan deskripsi singkat'
|
||||||
/>
|
onChange={(val) => {
|
||||||
<TextInput
|
pengumumanState.pengumuman.create.form.deskripsi = val.target.value
|
||||||
label={<Text fw={"bold"} fz={"sm"}>Tanggal</Text>}
|
}}
|
||||||
placeholder='Masukkan tanggal'
|
|
||||||
/>
|
|
||||||
<TextInput
|
|
||||||
label={<Text fw={"bold"} fz={"sm"}>Waktu</Text>}
|
|
||||||
placeholder='Masukkan waktu'
|
|
||||||
/>
|
/>
|
||||||
<Box>
|
<Box>
|
||||||
<Text fw={"bold"} fz={"sm"}>Deskripsi</Text>
|
<Text fz={"sm"} fw={"bold"}>Deskripsi</Text>
|
||||||
<KeamananEditor
|
<CreateEditor
|
||||||
showSubmit={false}
|
value={pengumumanState.pengumuman.create.form.content}
|
||||||
|
onChange={(htmlContent) => {
|
||||||
|
pengumumanState.pengumuman.create.form.content = htmlContent;
|
||||||
|
}}
|
||||||
/>
|
/>
|
||||||
</Box>
|
</Box>
|
||||||
|
|
||||||
<Group>
|
<Group>
|
||||||
<Button bg={colors['blue-button']}>Submit</Button>
|
<Button onClick={handleSubmit} bg={colors['blue-button']}>Submit</Button>
|
||||||
</Group>
|
</Group>
|
||||||
</Stack>
|
</Stack>
|
||||||
</Paper>
|
</Paper>
|
||||||
@@ -50,4 +76,35 @@ function CreatePengumuman() {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function SelectCategory({
|
||||||
|
onChange,
|
||||||
|
}: {
|
||||||
|
onChange: (value: Prisma.CategoryPengumumanGetPayload<{ select: { name: true; id: true; } }>) => void;
|
||||||
|
}) {
|
||||||
|
const categoryState = useProxy(stateDesaPengumuman.category);
|
||||||
|
|
||||||
|
useShallowEffect(() => {
|
||||||
|
categoryState.findMany.load();
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Select
|
||||||
|
label={<Text fw={"bold"} fz={"sm"}>Kategori</Text>}
|
||||||
|
placeholder='Pilih kategori'
|
||||||
|
data={categoryState.findMany.data?.map((item) => ({
|
||||||
|
label: item.name,
|
||||||
|
value: item.id,
|
||||||
|
}))}
|
||||||
|
onChange={(val) => {
|
||||||
|
const selected = categoryState.findMany.data?.find((item) => item.id === val);
|
||||||
|
if (selected) {
|
||||||
|
onChange(selected);
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
searchable
|
||||||
|
nothingFoundMessage="Tidak ditemukan"
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
export default CreatePengumuman;
|
export default CreatePengumuman;
|
||||||
|
|||||||
@@ -1,9 +1,14 @@
|
|||||||
'use client'
|
'use client'
|
||||||
import colors from '@/con/colors';
|
import colors from '@/con/colors';
|
||||||
import { Box, Button, Grid, GridCol, Image, Paper, Stack, Table, TableTbody, TableTd, TableTh, TableThead, TableTr, Text } from '@mantine/core';
|
import { Box, Button, Grid, GridCol, Paper, Skeleton, Stack, Table, TableTbody, TableTd, TableTh, TableThead, TableTr, Text } from '@mantine/core';
|
||||||
|
import { useShallowEffect } from '@mantine/hooks';
|
||||||
import { IconCircleDashedPlus, IconDeviceImacCog, IconSearch } from '@tabler/icons-react';
|
import { IconCircleDashedPlus, IconDeviceImacCog, IconSearch } from '@tabler/icons-react';
|
||||||
import { useRouter } from 'next/navigation';
|
import { useRouter } from 'next/navigation';
|
||||||
|
import { useProxy } from 'valtio/utils';
|
||||||
import HeaderSearch from '../../_com/header';
|
import HeaderSearch from '../../_com/header';
|
||||||
|
import stateDesaPengumuman from '../../_state/desa/pengumuman';
|
||||||
|
import { ModalKonfirmasiHapus } from '../../_com/modalKonfirmasiHapus';
|
||||||
|
import { useState } from 'react';
|
||||||
|
|
||||||
|
|
||||||
function Page() {
|
function Page() {
|
||||||
@@ -20,31 +25,31 @@ function Page() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function PengumumanList() {
|
function PengumumanList() {
|
||||||
// const pengumumanState = useProxy(stateDashboardPengumuman)
|
const pengumumanState = useProxy(stateDesaPengumuman)
|
||||||
// const [modalHapus, setModalHapus] = useState(false)
|
const [modalHapus, setModalHapus] = useState(false)
|
||||||
// const [selectedId, setSelectedId] = useState<string | null>(null)
|
const [selectedId, setSelectedId] = useState<string | null>(null)
|
||||||
|
|
||||||
// useShallowEffect(() => {
|
useShallowEffect(() => {
|
||||||
// pengumumanState.pengumuman.findMany.load()
|
pengumumanState.pengumuman.findMany.load()
|
||||||
// }, [])
|
}, [])
|
||||||
|
|
||||||
const router = useRouter()
|
const router = useRouter()
|
||||||
|
|
||||||
// const handleHapus = () => {
|
const handleHapus = () => {
|
||||||
// if (selectedId) {
|
if (selectedId) {
|
||||||
// pengumumanState.pengumuman.delete.byId(selectedId)
|
pengumumanState.pengumuman.delete.byId(selectedId)
|
||||||
// setModalHapus(false)
|
setModalHapus(false)
|
||||||
// setSelectedId(null)
|
setSelectedId(null)
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
|
|
||||||
// if (!pengumumanState.pengumuman.findMany.data) {
|
if (!pengumumanState.pengumuman.findMany.data) {
|
||||||
// return (
|
return (
|
||||||
// <Stack py={10}>
|
<Stack py={10}>
|
||||||
// <Skeleton h={500} />
|
<Skeleton h={500} />
|
||||||
// </Stack>
|
</Stack>
|
||||||
// )
|
)
|
||||||
// }
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Box py={10}>
|
<Box py={10}>
|
||||||
@@ -66,42 +71,39 @@ function PengumumanList() {
|
|||||||
<TableTr>
|
<TableTr>
|
||||||
<TableTh w={250}>Judul</TableTh>
|
<TableTh w={250}>Judul</TableTh>
|
||||||
<TableTh w={250}>Kategori</TableTh>
|
<TableTh w={250}>Kategori</TableTh>
|
||||||
<TableTh w={250}>Image</TableTh>
|
|
||||||
<TableTh w={200}>Detail</TableTh>
|
<TableTh w={200}>Detail</TableTh>
|
||||||
|
|
||||||
</TableTr>
|
</TableTr>
|
||||||
</TableThead>
|
</TableThead>
|
||||||
<TableTbody >
|
<TableTbody >
|
||||||
|
{pengumumanState.pengumuman.findMany.data?.map((item) => (
|
||||||
<TableTr>
|
<TableTr key={item.id}>
|
||||||
<TableTd >
|
<TableTd >
|
||||||
<Box w={100}>
|
<Box w={100}>
|
||||||
<Text truncate="end" fz={"sm"}>Judul</Text>
|
<Text truncate="end" fz={"sm"}>{item.judul}</Text>
|
||||||
</Box>
|
</Box>
|
||||||
</TableTd>
|
</TableTd>
|
||||||
<TableTd >Kesehatan</TableTd>
|
<TableTd >{item.CategoryPengumuman?.name}</TableTd>
|
||||||
<TableTd>
|
|
||||||
<Image w={100} src={"/"} alt="gambar" />
|
|
||||||
</TableTd>
|
|
||||||
<TableTd>
|
<TableTd>
|
||||||
<Button bg={"green"} onClick={() => router.push(`/admin/desa/pengumuman/detail`)}>
|
<Button bg={"green"} onClick={() => router.push(`/admin/desa/pengumuman/detail`)}>
|
||||||
<IconDeviceImacCog size={25} />
|
<IconDeviceImacCog size={25} />
|
||||||
</Button>
|
</Button>
|
||||||
</TableTd>
|
</TableTd>
|
||||||
</TableTr>
|
</TableTr>
|
||||||
|
))}
|
||||||
</TableTbody>
|
</TableTbody>
|
||||||
</Table>
|
</Table>
|
||||||
</Box>
|
</Box>
|
||||||
</Stack>
|
</Stack>
|
||||||
</Paper>
|
</Paper>
|
||||||
|
|
||||||
{/* Modal Konfirmasi Hapus
|
{/* Modal Konfirmasi Hapus */}
|
||||||
<ModalKonfirmasiHapus
|
<ModalKonfirmasiHapus
|
||||||
opened={modalHapus}
|
opened={modalHapus}
|
||||||
onClose={() => setModalHapus(false)}
|
onClose={() => setModalHapus(false)}
|
||||||
onConfirm={handleHapus}
|
onConfirm={handleHapus}
|
||||||
text='Apakah anda yakin ingin menghapus berita ini?'
|
text='Apakah anda yakin ingin menghapus berita ini?'
|
||||||
/> */}
|
/>
|
||||||
</Box>
|
</Box>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
39
src/app/api/[[...slugs]]/_lib/desa/pengumuman/del.ts
Normal file
39
src/app/api/[[...slugs]]/_lib/desa/pengumuman/del.ts
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
import prisma from "@/lib/prisma";
|
||||||
|
import { Context } from "elysia";
|
||||||
|
|
||||||
|
const pengumumanDelete = async (context: Context) => {
|
||||||
|
const id = context.params?.id as string;
|
||||||
|
|
||||||
|
if (!id) {
|
||||||
|
return {
|
||||||
|
status: 400,
|
||||||
|
body: "ID tidak ditemukan",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const pengumuman = await prisma.pengumuman.findUnique({
|
||||||
|
where: {id},
|
||||||
|
include: {
|
||||||
|
CategoryPengumuman: true,
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
if (!pengumuman) {
|
||||||
|
return {
|
||||||
|
status: 404,
|
||||||
|
body: "Pengumuman tidak ditemukan",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
await prisma.pengumuman.delete({
|
||||||
|
where: {id},
|
||||||
|
})
|
||||||
|
|
||||||
|
return {
|
||||||
|
status: 200,
|
||||||
|
success: true,
|
||||||
|
message: "Pengumuman berhasil dihapus",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default pengumumanDelete
|
||||||
37
src/app/api/[[...slugs]]/_lib/desa/pengumuman/find-by-id.ts
Normal file
37
src/app/api/[[...slugs]]/_lib/desa/pengumuman/find-by-id.ts
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
import prisma from "@/lib/prisma";
|
||||||
|
import { Context } from "elysia";
|
||||||
|
|
||||||
|
const pengumumanFindById = async (context: Context) => {
|
||||||
|
const id = context.params?.id as string;
|
||||||
|
|
||||||
|
if (!id) {
|
||||||
|
return {
|
||||||
|
status: 400,
|
||||||
|
body: "ID tidak diberikan",
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
const pengumuman = await prisma.pengumuman.findUnique({
|
||||||
|
where: { id },
|
||||||
|
include: {
|
||||||
|
CategoryPengumuman: true,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!pengumuman) {
|
||||||
|
return {
|
||||||
|
status: 404,
|
||||||
|
body: "Pengumuman tidak ditemukan",
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
status: 200,
|
||||||
|
success: true,
|
||||||
|
message: "Success fetch pengumuman by ID",
|
||||||
|
data: pengumuman,
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
export default pengumumanFindById;
|
||||||
|
|
||||||
@@ -1,8 +1,23 @@
|
|||||||
import prisma from "@/lib/prisma";
|
import prisma from "@/lib/prisma";
|
||||||
|
|
||||||
export default async function pengumumanFindMany() {
|
export default async function pengumumanFindMany() {
|
||||||
const res = await prisma.pengumuman.findMany();
|
try {
|
||||||
|
const data = await prisma.pengumuman.findMany({
|
||||||
|
where: { isActive: true },
|
||||||
|
include: {
|
||||||
|
CategoryPengumuman: true,
|
||||||
|
},
|
||||||
|
});
|
||||||
return {
|
return {
|
||||||
data: res,
|
success: true,
|
||||||
|
message: "Success fetch pengumuman",
|
||||||
|
data,
|
||||||
};
|
};
|
||||||
|
} catch (e) {
|
||||||
|
console.error("Find many error:", e);
|
||||||
|
return {
|
||||||
|
success: false,
|
||||||
|
message: "Failed fetch pengumuman",
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -3,10 +3,15 @@ import { pengumumanCreate } from "./create";
|
|||||||
import pengumumanFindMany from "./find-many";
|
import pengumumanFindMany from "./find-many";
|
||||||
import { t } from "elysia";
|
import { t } from "elysia";
|
||||||
import pengumumanCategoryFindMany from "./category";
|
import pengumumanCategoryFindMany from "./category";
|
||||||
|
import pengumumanDelete from "./del";
|
||||||
|
import pengumumanFindById from "./find-by-id";
|
||||||
|
import pengumumanUpdate from "./updt";
|
||||||
|
|
||||||
const Pengumuman = new Elysia({ prefix: "/pengumuman", tags: ["Desa/Pengumuman"] })
|
const Pengumuman = new Elysia({ prefix: "/pengumuman", tags: ["Desa/Pengumuman"] })
|
||||||
.get("/category/find-many", pengumumanCategoryFindMany)
|
.get("/category/find-many", pengumumanCategoryFindMany)
|
||||||
.get("/find-many", pengumumanFindMany)
|
.get("/find-many", pengumumanFindMany)
|
||||||
|
.get("/:id", pengumumanFindById)
|
||||||
|
.delete("/delete/:id", pengumumanDelete)
|
||||||
.post("/create", pengumumanCreate, {
|
.post("/create", pengumumanCreate, {
|
||||||
body: t.Object({
|
body: t.Object({
|
||||||
judul: t.String(),
|
judul: t.String(),
|
||||||
@@ -14,6 +19,15 @@ const Pengumuman = new Elysia({ prefix: "/pengumuman", tags: ["Desa/Pengumuman"]
|
|||||||
content: t.String(),
|
content: t.String(),
|
||||||
categoryPengumumanId: t.Union([t.String(), t.Null()]),
|
categoryPengumumanId: t.Union([t.String(), t.Null()]),
|
||||||
}),
|
}),
|
||||||
|
})
|
||||||
|
.put("/:id", pengumumanUpdate, {
|
||||||
|
body: t.Object({
|
||||||
|
id: t.String(),
|
||||||
|
judul: t.String(),
|
||||||
|
deskripsi: t.String(),
|
||||||
|
content: t.String(),
|
||||||
|
categoryPengumumanId: t.Union([t.String(), t.Null()]),
|
||||||
|
}),
|
||||||
});
|
});
|
||||||
|
|
||||||
export default Pengumuman;
|
export default Pengumuman;
|
||||||
39
src/app/api/[[...slugs]]/_lib/desa/pengumuman/updt.ts
Normal file
39
src/app/api/[[...slugs]]/_lib/desa/pengumuman/updt.ts
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
import prisma from "@/lib/prisma";
|
||||||
|
import { Prisma } from "@prisma/client";
|
||||||
|
import { Context } from "elysia";
|
||||||
|
|
||||||
|
type FormUpdate = Prisma.PengumumanGetPayload<{
|
||||||
|
select: {
|
||||||
|
id: true;
|
||||||
|
judul: true;
|
||||||
|
deskripsi: true;
|
||||||
|
content: true;
|
||||||
|
categoryPengumumanId: true;
|
||||||
|
imageId: true;
|
||||||
|
};
|
||||||
|
}>;
|
||||||
|
|
||||||
|
async function pengumumanUpdate(context: Context) {
|
||||||
|
const body = context.body as FormUpdate;
|
||||||
|
|
||||||
|
await prisma.pengumuman.update({
|
||||||
|
where: { id: body.id },
|
||||||
|
data: {
|
||||||
|
judul: body.judul,
|
||||||
|
deskripsi: body.deskripsi,
|
||||||
|
content: body.content,
|
||||||
|
categoryPengumumanId: body.categoryPengumumanId,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
return {
|
||||||
|
success: true,
|
||||||
|
message: "Success update pengumuman",
|
||||||
|
data: {
|
||||||
|
...body,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export default pengumumanUpdate;
|
||||||
|
|
||||||
Reference in New Issue
Block a user