From 84af0bf7f1f0b8979e4663b6201ebfe02cf5c0ce Mon Sep 17 00:00:00 2001 From: nico Date: Wed, 9 Oct 2024 13:51:18 +0800 Subject: [PATCH] Fix API List Banner, Edit Banner, Create Banner --- src/module/banner/ui/create_banner.tsx | 111 ++++++++++++++++--------- src/module/banner/ui/edit_banner.tsx | 31 ++++--- src/module/banner/ui/list_banner.tsx | 57 ++++--------- 3 files changed, 106 insertions(+), 93 deletions(-) diff --git a/src/module/banner/ui/create_banner.tsx b/src/module/banner/ui/create_banner.tsx index 25e938d..51a50ee 100644 --- a/src/module/banner/ui/create_banner.tsx +++ b/src/module/banner/ui/create_banner.tsx @@ -30,25 +30,6 @@ function CreateBanner() { image: false }) - - function onValidation(kategori: string, val: any) { - if (kategori == 'title') { - setListData({ ...listData, title: val }) - if (val === "") { - setTouched({ ...touched, title: true }) - } else { - setTouched({ ...touched, title: false }) - } - } else if (kategori == 'image') { - if (imgForm) { - setTouched({ ...touched, image: false }) - } else { - setTouched({ ...touched, image: true }) - } - } - } - - async function onSubmit(val: boolean) { if (!imgForm || !listData.title) { toast.error("Mohon lengkapi semua data"); @@ -69,8 +50,8 @@ function CreateBanner() { router.push('/banner') } else { toast.error(res.message); + setModal(false); } - setModal(false); } catch (error) { toast.error("Error"); } finally { @@ -79,9 +60,46 @@ function CreateBanner() { } } - // async function loadData() { - // const - // } + + + function onCheck() { + const cek = checkAll() + if (!cek) + return false + setModal(true) + } + function checkAll() { + let nilai = true + + if (listData.title == "") { + setTouched(touched => ({ ...touched, title: true })) + nilai = false + } + + if (!imgForm) { + setTouched(touched => ({ ...touched, image: true })) + nilai = false + } + + + return nilai + } + function onValidation(kategori: string, val: any) { + if (kategori == 'title') { + setListData({ ...listData, title: val }) + if (val === "") { + setTouched({ ...touched, title: true }) + } else { + setTouched({ ...touched, title: false }) + } + } else if (kategori == 'image') { + if (imgForm) { + setTouched({ ...touched, image: true }) + } else { + setTouched({ ...touched, image: false }) + } + } + } return ( @@ -102,7 +120,7 @@ function CreateBanner() { }} activateOnClick={false} maxSize={1 * 1024 ** 2} - accept={['image/png', 'imagfe/jpeg', 'image/heic']} + accept={['image/png', 'image/jpeg', 'image/heic']} onReject={(files) => { return toast.error('File yang diizinkan: .png, .jpg, dan .heic dengan ukuran maksimal 1 MB') }} @@ -111,13 +129,20 @@ function CreateBanner() { { img ? - + + : @@ -138,7 +163,7 @@ function CreateBanner() { Klik Untuk Upload Image - Ukuran Foto Tidak Boleh Lebih Dari 1MB + Ukuran Foto Tidak Boleh Lebih Dari 1 MB @@ -146,12 +171,19 @@ function CreateBanner() { + + {touched.image && !imgForm && ( + + Silahkan Pilih Gambar + + )} + - Judul Banner} - value={listData.title} + label="Judul Banner" placeholder='Judul Banner' + value={listData.title} onChange={(e) => { setListData({ ...listData, title: e.target.value }) onValidation('title', e.target.value) @@ -162,8 +194,13 @@ function CreateBanner() { borderRadius: 10, }, }} - - + required + size='md' + error={ + touched.title && ( + listData.title == "" ? "Judul Banner Tidak Boleh Kosong" : null + ) + } /> { - if (touched.title || touched.image) { - toast.error("Mohon Isi Semua Data") - } else { - setModal(true) - } - }} + bg={tema.get().utama} + radius={30} + fullWidth + onClick={() => { onCheck() }} >Simpan diff --git a/src/module/banner/ui/edit_banner.tsx b/src/module/banner/ui/edit_banner.tsx index 411c731..30c5d91 100644 --- a/src/module/banner/ui/edit_banner.tsx +++ b/src/module/banner/ui/edit_banner.tsx @@ -2,7 +2,7 @@ import { LayoutNavbarNew, TEMA, WARNA } from '@/module/_global'; import LayoutModal from '@/module/_global/layout/layout_modal'; import { useHookstate } from '@hookstate/core'; -import { Box, Button, Image, Paper, rem, TextInput } from '@mantine/core'; +import { Box, Button, Image, Paper, rem, Text, TextInput } from '@mantine/core'; import { Dropzone } from '@mantine/dropzone'; import { useShallowEffect } from '@mantine/hooks'; import _ from 'lodash'; @@ -44,14 +44,20 @@ export default function EditBanner() { } } else if (kategori == 'image') { if (imgForm) { - setTouched({ ...touched, image: false }) - } else { setTouched({ ...touched, image: true }) + } else { + setTouched({ ...touched, image: false }) } } } - + function onCheck() { + if (Object.values(touched).some((v) => v == true)) + return false + setModal(true) + } + + async function getOneData() { try { const res = await funGetOneBanner(param.id) @@ -128,7 +134,7 @@ export default function EditBanner() { { setData({ ...data, title: e.target.value }) @@ -142,6 +148,11 @@ export default function EditBanner() { }} required size='md' + error={ + touched.title && ( + data.title == "" ? "Judul Banner Tidak Boleh Kosong" : null + ) + } /> diff --git a/src/module/banner/ui/list_banner.tsx b/src/module/banner/ui/list_banner.tsx index 4d8de51..7859b60 100644 --- a/src/module/banner/ui/list_banner.tsx +++ b/src/module/banner/ui/list_banner.tsx @@ -27,17 +27,12 @@ function ListBanner() { const [idData, setIdData] = useState('') const [isPage, setPage] = useState(1) const [searchQuerry, setSearchQuerry] = useState('') - // const { value: containerRef } = useHookstate(currentScroll); - - const handleList = () => { - setIsList(!isList) - } const fetchData = async (loading: boolean) => { try { if (loading) setLoading(true) - const response = await funGetAllBanner('?search=' + searchQuerry) + const response = await funGetAllBanner('?page=' + isPage) if (response.success) { setData(response.data.map((banner: { image: any; }) => ({ ...banner, image: banner.image }))); } else { @@ -60,7 +55,7 @@ function ListBanner() { useShallowEffect(() => { fetchData(true) - }, [searchQuerry]) + }, [isPage]) useShallowEffect(() => { fetchData(false) @@ -92,29 +87,14 @@ function ListBanner() { return ( - } - placeholder='pencarian' - value={searchQuerry} - onChange={(val) => { searchBanner(val.target.value) }} - /> - - - - - - - {isData.map((v, index) => ( - { + {isData.length == 0 ? + + Tidak ada Banner + + : + isData.map((v, i) => { + return ( + { setIdData(v.id); setIdDataStorage(v.image); setExtension(v.extension); @@ -129,7 +109,7 @@ function ListBanner() { border: `1px solid ${tema.get().bgTotalKegiatan}` }}> - + - - + {v.title} - - ))} - - - - - - + )}) + + } + setOpenDrawer(false)}>