/* eslint-disable @typescript-eslint/no-unused-vars */ /* eslint-disable @typescript-eslint/no-explicit-any */ 'use client'; import colors from '@/con/colors'; import { Box, Button, Group, Loader, Paper, Stack, Text, TextInput, Title } from '@mantine/core'; import { IconArrowBack } from '@tabler/icons-react'; import { useRouter } from 'next/navigation'; import { useState } from 'react'; import { toast } from 'react-toastify'; import { useProxy } from 'valtio/utils'; import CreateEditor from '../../../_com/createEditor'; import SelectIconProgram from '../../../_com/selectIcon'; import programKemiskinanState from '../../../_state/ekonomi/program-kemiskinan'; function CreateProgramKemiskinan() { const programState = useProxy(programKemiskinanState); const router = useRouter(); const [lineChart, setLineChart] = useState([]); const [isSubmitting, setIsSubmitting] = useState(false); // Helper function to check if HTML content is empty const isHtmlEmpty = (html: string) => { // Remove all HTML tags and check if there's any text content const textContent = html.replace(/<[^>]*>/g, '').trim(); return textContent === ''; }; // Check if form is valid const isFormValid = () => { return ( programState.create.form.nama?.trim() !== '' && programState.create.form.icon?.trim() !== '' && !isHtmlEmpty(programState.create.form.deskripsi) && programState.create.form.statistik.jumlah?.trim() !== '' && programState.create.form.statistik.tahun?.trim() !== '' ); }; const resetForm = () => { programState.create.form = { nama: '', deskripsi: '', icon: '', statistik: { tahun: '', jumlah: '', }, }; }; const handleSubmit = async () => { try { setIsSubmitting(true); if (!programState.create.form.nama || !programState.create.form.deskripsi) { return toast.warn('Judul dan deskripsi wajib diisi'); } const id = await programState.create.create(); if (id) { const idStr = String(id); await programState.findUnique.load(idStr); if (programState.findUnique.data) { setLineChart([programState.findUnique.data]); } toast.success('Program berhasil ditambahkan'); } else { toast.error('Gagal menambahkan program, coba lagi'); } resetForm(); router.push('/admin/ekonomi/program-kemiskinan'); } catch (error) { console.error('Gagal menyimpan data:', error); toast.error('Terjadi kesalahan saat menyimpan data'); } finally { setIsSubmitting(false); } }; return ( {/* Header dengan tombol back */} Tambah Program Kemiskinan {/* Judul Program */} (programState.create.form.nama = val.target.value)} required /> {/* Ikon Program */} Ikon Program Kreatif Desa (programState.create.form.icon = value)} /> {/* Deskripsi */} Deskripsi { programState.create.form.deskripsi = val; }} /> {/* Statistik */} Statistik Jumlah Masyarakat Miskin (programState.create.form.statistik.jumlah = val.target.value) } label="Jumlah" placeholder="Masukkan jumlah masyarakat miskin" required /> (programState.create.form.statistik.tahun = val.target.value) } label="Tahun" placeholder="Masukkan tahun" required /> {/* Tombol Submit */} {/* Tombol Simpan */} ); } export default CreateProgramKemiskinan;