/* eslint-disable react-hooks/set-state-in-effect */ import { useEffect, useState } from "react"; import { saveMerchant, getMerchant, clearDevice } from "../../utils/storage"; import { useNavigate } from "@tanstack/react-router"; export default function SettingsFeature() { const navigate = useNavigate(); const [form, setForm] = useState({ merchantName: "", tower: "", floor: "", unit: "", credential: "", }); const [loading, setLoading] = useState(false); function isObjectValueDifferent(a: MerchantForm, b: MerchantForm): boolean { return (Object.keys(a) as (keyof MerchantForm)[]).some( (key) => a[key] !== b[key], ); } function handleChange(e: React.ChangeEvent) { const { name, value } = e.target; setForm((prev) => ({ ...prev, [name]: value, })); } function handleSubmit(e: React.FormEvent) { e.preventDefault(); const merchant = getMerchant(); if (merchant) { const isDifferent = isObjectValueDifferent(merchant, form); if (isDifferent) { clearDevice(); // reset device kalau merchant berubah } } setLoading(true); saveMerchant(form); setTimeout(() => { setLoading(false); navigate({ to: "/" }); }, 200); } useEffect(() => { const merchant = getMerchant(); if (merchant) { setForm(merchant); } }, []); const isFormValid = form.merchantName?.trim() && form.tower?.trim() && form.floor?.trim() && form.unit?.trim(); return (
); }