Senin, 26 May 2025 :

Yang Sudah Di Kerjakan
* Tampilan UI Admin di menu ekonomi
* API Create, dan delete berita

Yang Akan Dikerjakan:
* API Di Menu Desa : Edit Berita
* Tampilan UI Admin Di Menu Inovasi
This commit is contained in:
2025-05-26 14:26:10 +08:00
parent 92de697ae0
commit 02738104b5
16 changed files with 631 additions and 140 deletions

View File

@@ -10,6 +10,7 @@ import { useRouter } from 'next/navigation';
import { useState } from 'react';
import { toast } from 'react-toastify';
import { useProxy } from 'valtio/utils';
import { ModalKonfirmasiHapus } from '../../_com/modalKonfirmasiHapus';
import stateDashboardBerita from '../../_state/desa/berita';
import { BeritaEditor } from './_com/BeritaEditor';
@@ -23,99 +24,6 @@ function Page() {
);
}
// function BeritaCreate() {
// const beritaState = useProxy(stateDashboardBerita);
// const [previewImage, setPreviewImage] = useState<string | null>(null);
// const [file, setFile] = useState<File | null>(null);
// const [editorInstance, setEditorInstance] = useState<any>(null);
// const handleSubmit = async () => {
// if (!file) {
// return toast.warn("Pilih file gambar terlebih dahulu");
// }
// if (!editorInstance) return toast.error("Editor belum siap");
// const htmlContent = editorInstance.getHTML();
// if (!htmlContent || htmlContent === '<p></p>') return toast.warn("Konten tidak boleh kosong");
// beritaState.berita.create.form.content = htmlContent;
// console.log(beritaState.berita.create.form)
// // Upload gambar dulu
// const res = await ApiFetch.api.fileStorage.create.post({
// file,
// name: file.name,
// });
// const uploaded = res.data?.data;
// if (!uploaded?.id) {
// return toast.error("Gagal upload gambar");
// }
// // Simpan ID gambar ke form
// beritaState.berita.create.form.imageId = uploaded.id;
// // Submit data berita
// await beritaState.berita.create.create();
// };
// return (
// <Box py={10}>
// <Paper bg={colors["white-1"]} p={"md"}>
// <Stack gap={"xs"}>
// <SelectCategory
// onChange={(val) => {
// beritaState.berita.create.form.kategoriBeritaId = val.id;
// }}
// />
// <TextInput
// onChange={(val) => {
// beritaState.berita.create.form.judul = val.target.value;
// }}
// label={"Judul"}
// placeholder="masukkan judul"
// />
// <TextInput
// onChange={(val) => {
// beritaState.berita.create.form.deskripsi = val.target.value;
// }}
// label={"Deskripsi"}
// placeholder="masukkan deskripsi"
// />
// <FileInput
// label="Upload Gambar"
// onChange={async (e) => {
// if (!e) return;
// setFile(e);
// const base64 = await e.arrayBuffer().then((buf) =>
// "data:image/png;base64," + Buffer.from(buf).toString("base64")
// );
// setPreviewImage(base64);
// }}
// />
// {previewImage ? (
// <Image alt="" src={previewImage} w={200} h={200} />
// ) : (
// <Center w={200} h={200} bg={"gray"}>
// <IconImageInPicture />
// </Center>
// )}
// <BeritaEditor
// showSubmit={false}
// onEditorReady={(ed) => setEditorInstance(ed)}
// />
// <Button onClick={handleSubmit}>Simpan Berita</Button>
// </Stack>
// </Paper>
// </Box>
// );
// }
function BeritaCreate() {
const beritaState = useProxy(stateDashboardBerita);
const [previewImage, setPreviewImage] = useState<string | null>(null);
@@ -236,19 +144,96 @@ function BeritaCreate() {
// function BeritaList() {
// const beritaState = useProxy(stateDashboardBerita)
// useShallowEffect(() => {
// beritaState.berita.findMany.load()
// }, [])
// const router = useRouter()
// if (!beritaState.berita.findMany.data) return <Stack py={10}>
// {Array.from({ length: 10 }).map((v, k) => <Skeleton key={k} h={40} />)}
// </Stack>
// return (
// <Box py={10}>
// <Paper bg={colors['white-1']} p={'md'}>
// <Stack>
// <Text fz={"xl"} fw={"bold"}>List Berita</Text>
// <SimpleGrid cols={{ base: 1, md: 4 }}>
// {beritaState.berita.findMany.data?.map((item) => (
// <Paper key={item.id} bg={colors['BG-trans']} p={'md'}>
// <Box >
// <Flex justify="flex-end" mt={10}>
// <ActionIcon
// onClick={() => beritaState.berita.delete.byId(item.id)}
// disabled={beritaState.berita.delete.loading}
// color={colors['blue-button']} variant='transparent'>
// <IconX size={20} />
// </ActionIcon>
// <ActionIcon onClick={() => {
// router.push("/desa/berita/edit");
// }} color={colors['blue-button']} variant='transparent'>
// <IconEdit size={20} />
// </ActionIcon>
// </Flex>
// <Text fw={"bold"} fz={"sm"}>
// Kategori
// </Text>
// <Text>{item.kategoriBerita?.name}</Text>
// <Text fw={"bold"} fz={"sm"}>
// Judul
// </Text>
// <Text>{item.judul}</Text>
// <Text lineClamp={1} fw={"bold"} fz={"sm"}>
// Deskripsi
// </Text>
// <Text size='sm' lineClamp={2}>{item.deskripsi}</Text>
// <Text fw={"bold"} fz={"sm"}>
// Gambar
// </Text>
// <Image w={{ base: 100, md: 150 }} src={item.image?.link} alt="gambar" />
// </Box>
// </Paper>
// ))}
// </SimpleGrid>
// </Stack>
// </Paper>
// </Box>
// )
// }
function BeritaList() {
const beritaState = useProxy(stateDashboardBerita)
const [modalHapus, setModalHapus] = useState(false)
const [selectedId, setSelectedId] = useState<string | null>(null)
useShallowEffect(() => {
beritaState.berita.findMany.load()
}, [])
const router = useRouter()
if (!beritaState.berita.findMany.data) return <Stack py={10}>
{Array.from({ length: 10 }).map((v, k) => <Skeleton key={k} h={40} />)}
</Stack>
const handleHapus = () => {
if (selectedId) {
beritaState.berita.delete.byId(selectedId)
setModalHapus(false)
setSelectedId(null)
}
}
if (!beritaState.berita.findMany.data) {
return (
<Stack py={10}>
{Array.from({ length: 10 }).map((_, k) => (
<Skeleton key={k} h={40} />
))}
</Stack>
)
}
return (
<Box py={10}>
<Paper bg={colors['white-1']} p={'md'}>
@@ -257,46 +242,52 @@ function BeritaList() {
<SimpleGrid cols={{ base: 1, md: 4 }}>
{beritaState.berita.findMany.data?.map((item) => (
<Paper key={item.id} bg={colors['BG-trans']} p={'md'}>
<Box >
<Box>
<Flex justify="flex-end" mt={10}>
<ActionIcon
onClick={() => beritaState.berita.delete.byId(item.id)}
onClick={() => {
setSelectedId(item.id)
setModalHapus(true)
}}
disabled={beritaState.berita.delete.loading}
color={colors['blue-button']} variant='transparent'>
color={colors['blue-button']} variant='transparent'
>
<IconX size={20} />
</ActionIcon>
<ActionIcon onClick={() => {
router.push("/desa/berita/edit");
}} color={colors['blue-button']} variant='transparent'>
<ActionIcon
onClick={() => router.push("/desa/berita/edit")}
color={colors['blue-button']} variant='transparent'
>
<IconEdit size={20} />
</ActionIcon>
</Flex>
<Text fw={"bold"} fz={"sm"}>
Kategori
</Text>
<Text fw={"bold"} fz={"sm"}>Kategori</Text>
<Text>{item.kategoriBerita?.name}</Text>
<Text fw={"bold"} fz={"sm"}>
Judul
</Text>
<Text fw={"bold"} fz={"sm"}>Judul</Text>
<Text>{item.judul}</Text>
<Text lineClamp={1} fw={"bold"} fz={"sm"}>
Deskripsi
</Text>
<Text lineClamp={1} fw={"bold"} fz={"sm"}>Deskripsi</Text>
<Text size='sm' lineClamp={2}>{item.deskripsi}</Text>
<Text fw={"bold"} fz={"sm"}>
Gambar
</Text>
<Image w={{ base: 100, md: 150 }} src={item.image?.link} alt="gambar" />
<Text fw={"bold"} fz={"sm"}>Gambar</Text>
<Image w={{ base: 150, md: 150, lg: 150 }} src={item.image?.link} alt="gambar" />
</Box>
</Paper>
))}
</SimpleGrid>
</Stack>
</Paper>
{/* Modal Konfirmasi Hapus */}
<ModalKonfirmasiHapus
opened={modalHapus}
onClose={() => setModalHapus(false)}
onConfirm={handleHapus}
text='Apakah anda yakin ingin menghapus berita ini?'
/>
</Box>
)
}
function SelectCategory({
onChange,
}: {