Fix Edit di Admin APbdes, dan fix data real di apbdes user
This commit is contained in:
@@ -7,14 +7,14 @@ import { z } from "zod";
|
|||||||
|
|
||||||
// --- Zod Schema ---
|
// --- Zod Schema ---
|
||||||
const ApbdesItemSchema = z.object({
|
const ApbdesItemSchema = z.object({
|
||||||
kode: z.string().min(1),
|
kode: z.string().min(1, "Kode wajib diisi"),
|
||||||
uraian: z.string().min(1),
|
uraian: z.string().min(1, "Uraian wajib diisi"),
|
||||||
anggaran: z.number().min(0),
|
anggaran: z.number().min(0),
|
||||||
realisasi: z.number().min(0),
|
realisasi: z.number().min(0),
|
||||||
selisih: z.number(),
|
selisih: z.number(),
|
||||||
persentase: z.number().min(0).max(1000), // allow >100% if overbudget
|
persentase: z.number(),
|
||||||
level: z.number().int().min(1).max(3),
|
level: z.number().int().min(1).max(3),
|
||||||
tipe: z.string().min(1), // "pendapatan" | "belanja"
|
tipe: z.enum(['pendapatan', 'belanja', 'pembiayaan']).nullable().optional(),
|
||||||
});
|
});
|
||||||
|
|
||||||
const ApbdesFormSchema = z.object({
|
const ApbdesFormSchema = z.object({
|
||||||
@@ -38,6 +38,9 @@ function normalizeItem(item: Partial<z.infer<typeof ApbdesItemSchema>>): z.infer
|
|||||||
const anggaran = item.anggaran ?? 0;
|
const anggaran = item.anggaran ?? 0;
|
||||||
const realisasi = item.realisasi ?? 0;
|
const realisasi = item.realisasi ?? 0;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// ✅ Formula yang benar
|
// ✅ Formula yang benar
|
||||||
const selisih = anggaran - realisasi; // positif = sisa anggaran, negatif = over budget
|
const selisih = anggaran - realisasi; // positif = sisa anggaran, negatif = over budget
|
||||||
const persentase = anggaran > 0 ? (realisasi / anggaran) * 100 : 0; // persentase realisasi terhadap anggaran
|
const persentase = anggaran > 0 ? (realisasi / anggaran) * 100 : 0; // persentase realisasi terhadap anggaran
|
||||||
@@ -56,58 +59,6 @@ function normalizeItem(item: Partial<z.infer<typeof ApbdesItemSchema>>): z.infer
|
|||||||
|
|
||||||
// --- State Utama ---
|
// --- State Utama ---
|
||||||
const apbdes = proxy({
|
const apbdes = proxy({
|
||||||
// create: {
|
|
||||||
// form: { ...defaultApbdesForm },
|
|
||||||
// loading: false,
|
|
||||||
|
|
||||||
// addItem(item: Partial<z.infer<typeof ApbdesItemSchema>>) {
|
|
||||||
// const normalized = normalizeItem(item);
|
|
||||||
// this.form.items.push(normalized);
|
|
||||||
// },
|
|
||||||
|
|
||||||
// removeItem(index: number) {
|
|
||||||
// this.form.items.splice(index, 1);
|
|
||||||
// },
|
|
||||||
|
|
||||||
// updateItem(index: number, updates: Partial<z.infer<typeof ApbdesItemSchema>>) {
|
|
||||||
// const current = this.form.items[index];
|
|
||||||
// if (current) {
|
|
||||||
// const updated = normalizeItem({ ...current, ...updates });
|
|
||||||
// this.form.items[index] = updated;
|
|
||||||
// }
|
|
||||||
// },
|
|
||||||
|
|
||||||
// reset() {
|
|
||||||
// this.form = { ...defaultApbdesForm };
|
|
||||||
// },
|
|
||||||
|
|
||||||
// async create() {
|
|
||||||
// const parsed = ApbdesFormSchema.safeParse(this.form);
|
|
||||||
// if (!parsed.success) {
|
|
||||||
// const errors = parsed.error.issues.map((issue) => `${issue.path.join(".")} - ${issue.message}`);
|
|
||||||
// toast.error(`Validasi gagal:\n${errors.join("\n")}`);
|
|
||||||
// return;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// try {
|
|
||||||
// this.loading = true;
|
|
||||||
// const res = await ApiFetch.api.landingpage.apbdes["create"].post(parsed.data);
|
|
||||||
|
|
||||||
// if (res.data?.success) {
|
|
||||||
// toast.success("APBDes berhasil dibuat");
|
|
||||||
// apbdes.findMany.load();
|
|
||||||
// this.reset();
|
|
||||||
// } else {
|
|
||||||
// toast.error(res.data?.message || "Gagal membuat APBDes");
|
|
||||||
// }
|
|
||||||
// } catch (error: any) {
|
|
||||||
// console.error("Create APBDes error:", error);
|
|
||||||
// toast.error(error?.message || "Terjadi kesalahan saat membuat APBDes");
|
|
||||||
// } finally {
|
|
||||||
// this.loading = false;
|
|
||||||
// }
|
|
||||||
// },
|
|
||||||
// },
|
|
||||||
create: {
|
create: {
|
||||||
form: { ...defaultApbdesForm },
|
form: { ...defaultApbdesForm },
|
||||||
loading: false,
|
loading: false,
|
||||||
@@ -305,7 +256,7 @@ const apbdes = proxy({
|
|||||||
selisih: item.selisih,
|
selisih: item.selisih,
|
||||||
persentase: item.persentase,
|
persentase: item.persentase,
|
||||||
level: item.level,
|
level: item.level,
|
||||||
tipe: item.tipe,
|
tipe: item.tipe || 'pendapatan',
|
||||||
})),
|
})),
|
||||||
};
|
};
|
||||||
return data;
|
return data;
|
||||||
|
|||||||
@@ -115,20 +115,22 @@ function EditAPBDes() {
|
|||||||
if (!kode || !uraian) {
|
if (!kode || !uraian) {
|
||||||
return toast.warn('Kode dan uraian wajib diisi');
|
return toast.warn('Kode dan uraian wajib diisi');
|
||||||
}
|
}
|
||||||
|
const finalTipe = level === 1 ? 'pendapatan' : tipe; // Berikan default
|
||||||
|
|
||||||
const selisih = realisasi - anggaran;
|
const selisih = realisasi - anggaran;
|
||||||
const persentase = anggaran > 0 ? (realisasi / anggaran) * 100 : 0;
|
const persentase = anggaran > 0 ? (realisasi / anggaran) * 100 : 0;
|
||||||
|
|
||||||
|
apbdesState.edit.addItem({
|
||||||
|
kode,
|
||||||
|
uraian,
|
||||||
|
anggaran,
|
||||||
|
realisasi,
|
||||||
|
selisih,
|
||||||
|
persentase,
|
||||||
|
level,
|
||||||
|
tipe: finalTipe, // ✅ Tidak akan undefined
|
||||||
|
});
|
||||||
|
|
||||||
apbdesState.edit.addItem({
|
|
||||||
kode,
|
|
||||||
uraian,
|
|
||||||
anggaran,
|
|
||||||
realisasi,
|
|
||||||
selisih,
|
|
||||||
persentase,
|
|
||||||
level,
|
|
||||||
tipe,
|
|
||||||
});
|
|
||||||
|
|
||||||
setNewItem({
|
setNewItem({
|
||||||
kode: '',
|
kode: '',
|
||||||
@@ -374,6 +376,7 @@ function EditAPBDes() {
|
|||||||
data={[
|
data={[
|
||||||
{ value: 'pendapatan', label: 'Pendapatan' },
|
{ value: 'pendapatan', label: 'Pendapatan' },
|
||||||
{ value: 'belanja', label: 'Belanja' },
|
{ value: 'belanja', label: 'Belanja' },
|
||||||
|
{ value: 'pembiayaan', label: 'Pembiayaan' },
|
||||||
]}
|
]}
|
||||||
value={newItem.tipe}
|
value={newItem.tipe}
|
||||||
onChange={(val) => setNewItem({ ...newItem, tipe: (val as any) || 'pendapatan' })}
|
onChange={(val) => setNewItem({ ...newItem, tipe: (val as any) || 'pendapatan' })}
|
||||||
@@ -447,9 +450,13 @@ function EditAPBDes() {
|
|||||||
</Badge>
|
</Badge>
|
||||||
</td>
|
</td>
|
||||||
<td>
|
<td>
|
||||||
<Badge size="sm" color={item.tipe === 'pendapatan' ? 'teal' : 'red'}>
|
{item.tipe ? (
|
||||||
{item.tipe}
|
<Badge color={item.tipe === 'pendapatan' ? 'teal' : 'red'} size="sm">
|
||||||
</Badge>
|
{item.tipe}
|
||||||
|
</Badge>
|
||||||
|
) : (
|
||||||
|
'-'
|
||||||
|
)}
|
||||||
</td>
|
</td>
|
||||||
<td>
|
<td>
|
||||||
<ActionIcon color="red" onClick={() => handleRemoveItem(idx)}>
|
<ActionIcon color="red" onClick={() => handleRemoveItem(idx)}>
|
||||||
|
|||||||
@@ -123,6 +123,8 @@ function CreateAPBDes() {
|
|||||||
return toast.warn("Kode dan uraian wajib diisi");
|
return toast.warn("Kode dan uraian wajib diisi");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const finalTipe = level === 1 ? 'pendapatan' : tipe; // Berikan default
|
||||||
|
|
||||||
const selisih = realisasi - anggaran;
|
const selisih = realisasi - anggaran;
|
||||||
const persentase = anggaran > 0 ? (realisasi / anggaran) * 100 : 0;
|
const persentase = anggaran > 0 ? (realisasi / anggaran) * 100 : 0;
|
||||||
|
|
||||||
@@ -134,7 +136,7 @@ function CreateAPBDes() {
|
|||||||
selisih,
|
selisih,
|
||||||
persentase,
|
persentase,
|
||||||
level,
|
level,
|
||||||
tipe,
|
tipe: finalTipe,
|
||||||
});
|
});
|
||||||
|
|
||||||
// Reset form input
|
// Reset form input
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ type APBDesItemInput = {
|
|||||||
selisih: number;
|
selisih: number;
|
||||||
persentase: number;
|
persentase: number;
|
||||||
level: number;
|
level: number;
|
||||||
tipe: string;
|
tipe?: string | null;
|
||||||
};
|
};
|
||||||
|
|
||||||
type FormCreate = {
|
type FormCreate = {
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ const ApbdesItemSchema = t.Object({
|
|||||||
selisih: t.Number(),
|
selisih: t.Number(),
|
||||||
persentase: t.Number(),
|
persentase: t.Number(),
|
||||||
level: t.Number(),
|
level: t.Number(),
|
||||||
tipe: t.String(), // misal: "pendapatan" atau "belanja"
|
tipe: t.Optional(t.Union([t.String(), t.Null()])) // misal: "pendapatan" atau "belanja"
|
||||||
});
|
});
|
||||||
|
|
||||||
const APBDes = new Elysia({
|
const APBDes = new Elysia({
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ type APBDesItemInput = {
|
|||||||
selisih: number;
|
selisih: number;
|
||||||
persentase: number;
|
persentase: number;
|
||||||
level: number;
|
level: number;
|
||||||
tipe: string;
|
tipe?: string | null;
|
||||||
};
|
};
|
||||||
|
|
||||||
type FormUpdateBody = {
|
type FormUpdateBody = {
|
||||||
@@ -54,7 +54,7 @@ export default async function apbdesUpdate(context: Context) {
|
|||||||
selisih: item.anggaran - item.realisasi,
|
selisih: item.anggaran - item.realisasi,
|
||||||
persentase: item.anggaran > 0 ? (item.realisasi / item.anggaran) * 100 : 0,
|
persentase: item.anggaran > 0 ? (item.realisasi / item.anggaran) * 100 : 0,
|
||||||
level: item.level,
|
level: item.level,
|
||||||
tipe: item.tipe,
|
tipe: item.tipe || null,
|
||||||
isActive: true,
|
isActive: true,
|
||||||
})),
|
})),
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,25 +1,15 @@
|
|||||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
||||||
/* eslint-disable react-hooks/exhaustive-deps */
|
/* eslint-disable react-hooks/exhaustive-deps */
|
||||||
'use client'
|
'use client'
|
||||||
import apbdes from '@/app/admin/(dashboard)/_state/landing-page/apbdes'
|
import apbdes from '@/app/admin/(dashboard)/_state/landing-page/apbdes'
|
||||||
|
import APBDesProgress from '@/app/darmasaba/(tambahan)/apbdes/lib/apbDesaProgress'
|
||||||
import colors from '@/con/colors'
|
import colors from '@/con/colors'
|
||||||
import { BarChart } from '@mantine/charts'
|
import { ActionIcon, BackgroundImage, Box, Button, Center, Flex, Group, Loader, SimpleGrid, Stack, Text } from '@mantine/core'
|
||||||
import { ActionIcon, BackgroundImage, Box, Button, Center, Flex, Group, Loader, Paper, SimpleGrid, Stack, Text, Title } from '@mantine/core'
|
|
||||||
import { IconDownload } from '@tabler/icons-react'
|
import { IconDownload } from '@tabler/icons-react'
|
||||||
import Link from 'next/link'
|
import Link from 'next/link'
|
||||||
import { useEffect, useState } from 'react'
|
import { useEffect, useState } from 'react'
|
||||||
import { useProxy } from 'valtio/utils'
|
import { useProxy } from 'valtio/utils'
|
||||||
import parseJumlah from './lib/convert'
|
|
||||||
|
|
||||||
function Apbdes() {
|
function Apbdes() {
|
||||||
type APBDes = {
|
|
||||||
id: string
|
|
||||||
name: string
|
|
||||||
jumlah: number
|
|
||||||
};
|
|
||||||
|
|
||||||
const [chartData, setChartData] = useState<APBDes[]>([])
|
|
||||||
const [mounted, setMounted] = useState(false);
|
|
||||||
const state = useProxy(apbdes)
|
const state = useProxy(apbdes)
|
||||||
const [loading, setLoading] = useState(false)
|
const [loading, setLoading] = useState(false)
|
||||||
|
|
||||||
@@ -28,22 +18,9 @@ function Apbdes() {
|
|||||||
des: 'Transparansi APBDes Darmasaba adalah langkah nyata menuju tata kelola desa yang bersih, terbuka, dan bertanggung jawab.'
|
des: 'Transparansi APBDes Darmasaba adalah langkah nyata menuju tata kelola desa yang bersih, terbuka, dan bertanggung jawab.'
|
||||||
}
|
}
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
if (state.findMany.data) {
|
|
||||||
setChartData(
|
|
||||||
state.findMany.data.map((item: any) => ({
|
|
||||||
id: item.id,
|
|
||||||
name: item.name,
|
|
||||||
jumlah: parseJumlah(item.jumlah),
|
|
||||||
}))
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}, [state.findMany.data]);
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const loadData = async () => {
|
const loadData = async () => {
|
||||||
try {
|
try {
|
||||||
setMounted(true);
|
|
||||||
setLoading(true)
|
setLoading(true)
|
||||||
await state.findMany.load()
|
await state.findMany.load()
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -70,56 +47,23 @@ function Apbdes() {
|
|||||||
</Stack>
|
</Stack>
|
||||||
</Box>
|
</Box>
|
||||||
|
|
||||||
{/* Chart */}
|
<Group justify="center">
|
||||||
<Box mt={30} style={{ width: '100%', minHeight: 400 }}>
|
<Button
|
||||||
<Paper bg={colors['white-1']} py={50} px={90} mb={"xl"} radius="md" withBorder>
|
component={Link}
|
||||||
<Stack gap={"xs"}>
|
href="/darmasaba/apbdes"
|
||||||
<Title ta={"center"} pb={10} order={2}>
|
radius="xl"
|
||||||
Grafik APBDes
|
size="lg"
|
||||||
</Title>
|
variant="gradient"
|
||||||
{mounted && chartData.length > 0 ? (
|
gradient={{ from: "#26667F", to: "#124170" }}
|
||||||
<BarChart
|
>
|
||||||
orientation="vertical"
|
Lihat Semua Data
|
||||||
h={450}
|
</Button>
|
||||||
barProps={{ radius: 50 }}
|
</Group>
|
||||||
data={chartData}
|
|
||||||
dataKey="name"
|
|
||||||
type="stacked"
|
|
||||||
valueFormatter={(value: number) => {
|
|
||||||
if (value >= 1_000_000_000_000)
|
|
||||||
return `Rp ${(value / 1_000_000_000_000).toFixed(1)} T`;
|
|
||||||
if (value >= 1_000_000_000)
|
|
||||||
return `Rp ${(value / 1_000_000_000).toFixed(1)} M`;
|
|
||||||
if (value >= 1_000_000)
|
|
||||||
return `Rp ${(value / 1_000_000).toFixed(1)} JT`;
|
|
||||||
if (value >= 1_000)
|
|
||||||
return `Rp ${(value / 1_000).toFixed(1)} RB`;
|
|
||||||
return `Rp ${value}`;
|
|
||||||
}}
|
|
||||||
series={[
|
|
||||||
{
|
|
||||||
name: 'jumlah',
|
|
||||||
color: colors['blue-button'],
|
|
||||||
label: 'Jumlah',
|
|
||||||
},
|
|
||||||
]}
|
|
||||||
/>
|
|
||||||
) : (
|
|
||||||
<Text c="dimmed">Belum ada data untuk ditampilkan dalam grafik</Text>
|
|
||||||
)}
|
|
||||||
<Box py={10}>
|
|
||||||
<Group justify='center'>
|
|
||||||
<Flex align="center" gap={10}>
|
|
||||||
<Box bg={colors['blue-button']} w={20} h={20} />
|
|
||||||
<Text>Jumlah</Text>
|
|
||||||
</Flex>
|
|
||||||
</Group>
|
|
||||||
</Box>
|
|
||||||
</Stack>
|
|
||||||
</Paper>
|
|
||||||
</Box>
|
|
||||||
|
|
||||||
<SimpleGrid cols={{ base: 1, sm: 3 }} spacing="lg">
|
{/* Chart */}
|
||||||
|
<APBDesProgress />
|
||||||
|
|
||||||
|
<SimpleGrid mx={{ base: 'md', md: 100 }} cols={{ base: 1, sm: 3 }} spacing="lg" pb={"xl"}>
|
||||||
{loading ? (
|
{loading ? (
|
||||||
<Center mih={200}>
|
<Center mih={200}>
|
||||||
<Loader size="lg" color="blue" />
|
<Loader size="lg" color="blue" />
|
||||||
@@ -185,18 +129,7 @@ function Apbdes() {
|
|||||||
)}
|
)}
|
||||||
</SimpleGrid>
|
</SimpleGrid>
|
||||||
|
|
||||||
<Group justify="center" pb={"xl"}>
|
|
||||||
<Button
|
|
||||||
component={Link}
|
|
||||||
href="/darmasaba/apbdes"
|
|
||||||
radius="xl"
|
|
||||||
size="lg"
|
|
||||||
variant="gradient"
|
|
||||||
gradient={{ from: "#26667F", to: "#124170" }}
|
|
||||||
>
|
|
||||||
Lihat Semua Data
|
|
||||||
</Button>
|
|
||||||
</Group>
|
|
||||||
</Stack>
|
</Stack>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user