From 6eb257705f90e6a2883c930ca5284ae28a42ccd3 Mon Sep 17 00:00:00 2001 From: Adnan Zahir Date: Tue, 28 Apr 2026 10:50:46 +0700 Subject: [PATCH] feat: konfigurasi sistem toggle pemakaian pakan ovk negatif --- src/app/master-data/system-config/page.tsx | 11 ++ src/config/constant.ts | 6 + src/config/route-permission.ts | 2 + .../system-config/SystemConfigContent.tsx | 176 ++++++++++++++++++ src/services/api/system-settings.ts | 48 +++++ .../api/system-settings/system-setting.d.ts | 11 ++ 6 files changed, 254 insertions(+) create mode 100644 src/app/master-data/system-config/page.tsx create mode 100644 src/figma-make/components/pages/master-data/system-config/SystemConfigContent.tsx create mode 100644 src/services/api/system-settings.ts create mode 100644 src/types/api/system-settings/system-setting.d.ts diff --git a/src/app/master-data/system-config/page.tsx b/src/app/master-data/system-config/page.tsx new file mode 100644 index 00000000..c156280c --- /dev/null +++ b/src/app/master-data/system-config/page.tsx @@ -0,0 +1,11 @@ +import { SystemConfigContent } from '@/figma-make/components/pages/master-data/system-config/SystemConfigContent'; + +const SystemConfigPage = () => { + return ( +
+ +
+ ); +}; + +export default SystemConfigPage; diff --git a/src/config/constant.ts b/src/config/constant.ts index 99594b65..0f06d499 100644 --- a/src/config/constant.ts +++ b/src/config/constant.ts @@ -236,6 +236,7 @@ export const MAIN_DRAWER_LINKS: SidebarMenuItem[] = [ 'lti.master.uoms.list', 'lti.master.warehouses.list', 'lti.master.production_standards.list', + 'lti.system_settings.update', ], submenu: [ { @@ -303,6 +304,11 @@ export const MAIN_DRAWER_LINKS: SidebarMenuItem[] = [ link: '/master-data/production-standard', permission: ['lti.master.production_standards.list'], }, + { + text: 'Konfigurasi Sistem', + link: '/master-data/system-config', + permission: ['lti.system_settings.update'], + }, ], }, ] as const; diff --git a/src/config/route-permission.ts b/src/config/route-permission.ts index 8c65a611..ee03d4a2 100644 --- a/src/config/route-permission.ts +++ b/src/config/route-permission.ts @@ -218,4 +218,6 @@ export const ROUTE_PERMISSIONS: Record = { '/master-data/production-standard/detail/edit/': [ 'lti.master.production_standards.update', ], + + '/master-data/system-config/': ['lti.system_settings.update'], }; diff --git a/src/figma-make/components/pages/master-data/system-config/SystemConfigContent.tsx b/src/figma-make/components/pages/master-data/system-config/SystemConfigContent.tsx new file mode 100644 index 00000000..935e38b6 --- /dev/null +++ b/src/figma-make/components/pages/master-data/system-config/SystemConfigContent.tsx @@ -0,0 +1,176 @@ +'use client'; + +import { useState } from 'react'; +import { Card, CardContent } from '@/figma-make/components/base/card'; +import { toast } from 'sonner'; +import useSWR from 'swr'; +import { isResponseError, isResponseSuccess } from '@/lib/api-helper'; +import { SystemSettingsApi } from '@/services/api/system-settings'; +import { SystemSetting } from '@/types/api/system-settings/system-setting'; + +const ALLOW_NEGATIVE_PAKAN_OVK_KEY = 'allow_negative_pakan_ovk'; + +function SettingToggle({ + setting, + onToggle, + loading, +}: { + setting: SystemSetting; + onToggle: (key: string, currentValue: boolean) => void; + loading: boolean; +}) { + const isEnabled = setting.value === 'true'; + + return ( +
+
+

+ {setting.key === ALLOW_NEGATIVE_PAKAN_OVK_KEY + ? 'Mode Migrasi PAKAN & OVK' + : setting.key} +

+ {setting.description && ( +

{setting.description}

+ )} + + {isEnabled ? 'Aktif' : 'Nonaktif'} + +
+ +
+ ); +} + +export function SystemConfigContent() { + const [toggling, setToggling] = useState(null); + + const { + data: settingsResponse, + isLoading, + mutate: refreshSettings, + } = useSWR(SystemSettingsApi.basePath, SystemSettingsApi.getAllFetcher, { + keepPreviousData: true, + }); + + const handleToggle = async (key: string, currentValue: boolean) => { + if (key !== ALLOW_NEGATIVE_PAKAN_OVK_KEY) return; + + setToggling(key); + try { + const res = await SystemSettingsApi.setAllowNegativePakanOvk({ + value: !currentValue, + }); + + if (isResponseError(res)) { + toast.error(res.message || 'Gagal mengubah pengaturan'); + return; + } + + await refreshSettings(); + toast.success( + !currentValue + ? 'Mode migrasi PAKAN & OVK diaktifkan' + : 'Mode migrasi PAKAN & OVK dinonaktifkan' + ); + } catch { + toast.error('Terjadi kesalahan saat mengubah pengaturan'); + } finally { + setToggling(null); + } + }; + + const settings = isResponseSuccess(settingsResponse) + ? settingsResponse.data + : []; + + if (isLoading && !settingsResponse) { + return ( +
+
+
+

+ Konfigurasi Sistem +

+

+ Master Data •{' '} + Konfigurasi Sistem +

+
+ + + Memuat data... + + +
+
+ ); + } + + return ( +
+
+
+

+ Konfigurasi Sistem +

+

+ Master Data •{' '} + Konfigurasi Sistem +

+
+ + + +
+

+ Pengaturan Global +

+

+ Pengaturan ini berlaku untuk seluruh sistem. +

+
+ +
+ {settings.length === 0 ? ( +

+ Tidak ada pengaturan tersedia. +

+ ) : ( + settings.map((setting) => ( + + )) + )} +
+
+
+
+
+ ); +} diff --git a/src/services/api/system-settings.ts b/src/services/api/system-settings.ts new file mode 100644 index 00000000..06ef4ace --- /dev/null +++ b/src/services/api/system-settings.ts @@ -0,0 +1,48 @@ +import axios from 'axios'; +import { httpClient, httpClientFetcher } from '@/services/http/client'; +import { BaseApiResponse } from '@/types/api/api-general'; +import { + SetAllowNegativePakanOvkPayload, + SystemSetting, +} from '@/types/api/system-settings/system-setting'; + +const BASE_PATH = '/system-settings'; + +export const SystemSettingsApi = { + basePath: BASE_PATH, + + getAllFetcher: ( + endpoint: string + ): Promise> => + httpClientFetcher>(endpoint), + + async getAll(): Promise | undefined> { + try { + return await httpClient>(BASE_PATH); + } catch (error) { + if (axios.isAxiosError>(error)) { + return error.response?.data; + } + return undefined; + } + }, + + async setAllowNegativePakanOvk( + payload: SetAllowNegativePakanOvkPayload + ): Promise { + try { + return await httpClient( + `${BASE_PATH}/allow-negative-pakan-ovk`, + { + method: 'PATCH', + body: payload, + } + ); + } catch (error) { + if (axios.isAxiosError(error)) { + return error.response?.data; + } + return undefined; + } + }, +}; diff --git a/src/types/api/system-settings/system-setting.d.ts b/src/types/api/system-settings/system-setting.d.ts new file mode 100644 index 00000000..98b71f6b --- /dev/null +++ b/src/types/api/system-settings/system-setting.d.ts @@ -0,0 +1,11 @@ +export type SystemSetting = { + key: string; + value: string; + description: string; + created_at: string; + updated_at: string; +}; + +export type SetAllowNegativePakanOvkPayload = { + value: boolean; +};