mirror of
https://gitlab.com/mbugroup/lti-web-client.git
synced 2026-05-20 21:41:57 +00:00
feat(FE): Add week calculation utility and improve state resets
This commit is contained in:
@@ -243,6 +243,23 @@ const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => {
|
||||
const [isProductionStandardModalOpen, setIsProductionStandardModalOpen] =
|
||||
useState(false);
|
||||
|
||||
const calculateWeek = useCallback(
|
||||
(day: number): number => {
|
||||
if (
|
||||
productionStandards?.details &&
|
||||
productionStandards.details.length > 0
|
||||
) {
|
||||
const firstWeek = productionStandards.details[0].week;
|
||||
|
||||
const weekOffset = Math.ceil(day / 7) - 1;
|
||||
return firstWeek + weekOffset;
|
||||
}
|
||||
|
||||
return Math.ceil(day / 7);
|
||||
},
|
||||
[productionStandards]
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
const checkProductionStandardModalOpen = () => {
|
||||
const isOpen = productionStandardModal.ref.current?.open || false;
|
||||
@@ -441,13 +458,24 @@ const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => {
|
||||
() => ProductionStandardApi.getSingle(productionStandardId!)
|
||||
);
|
||||
|
||||
const { data: productionStandardForAdd } = useSWR(
|
||||
type === 'add' && productionStandardId
|
||||
? `production-standard-add-${productionStandardId}`
|
||||
: null,
|
||||
() => ProductionStandardApi.getSingle(productionStandardId!)
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
if (productionStandard?.status === 'success') {
|
||||
setProductionStandards(
|
||||
productionStandard.data as ProductionStandard | null
|
||||
);
|
||||
} else if (productionStandardForAdd?.status === 'success') {
|
||||
setProductionStandards(
|
||||
productionStandardForAdd.data as ProductionStandard | null
|
||||
);
|
||||
}
|
||||
}, [productionStandard]);
|
||||
}, [productionStandard, productionStandardForAdd]);
|
||||
|
||||
const projectFlockKandangDetailUrl = useMemo(() => {
|
||||
if (
|
||||
@@ -1378,6 +1406,8 @@ const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => {
|
||||
setSelectedLocation(location);
|
||||
setSelectedProjectFlock(null);
|
||||
setSelectedKandang(null);
|
||||
setProductionStandards(null);
|
||||
setNextDayRecording(null);
|
||||
if (duplicateErrorShown) {
|
||||
toast.dismiss();
|
||||
setDuplicateErrorShown(false);
|
||||
@@ -1402,6 +1432,8 @@ const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => {
|
||||
|
||||
setSelectedProjectFlock(projectFlock);
|
||||
setSelectedKandang(null);
|
||||
setProductionStandards(null);
|
||||
setNextDayRecording(null);
|
||||
if (duplicateErrorShown) {
|
||||
toast.dismiss();
|
||||
setDuplicateErrorShown(false);
|
||||
@@ -1422,6 +1454,8 @@ const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => {
|
||||
formik.setFieldValue('kandang_id', kandangId);
|
||||
|
||||
setSelectedKandang(kandang);
|
||||
setProductionStandards(null);
|
||||
setNextDayRecording(null);
|
||||
if (duplicateErrorShown) {
|
||||
toast.dismiss();
|
||||
setDuplicateErrorShown(false);
|
||||
@@ -1958,10 +1992,10 @@ const RecordingForm = ({ type = 'add', initialValues }: RecordingFormProps) => {
|
||||
<p className='font-semibold'>
|
||||
{type === 'add'
|
||||
? nextDayRecording
|
||||
? `Hari ke-${nextDayRecording.next_day} (Minggu ke-${Math.ceil(nextDayRecording.next_day / 7)})`
|
||||
? `Hari ke-${nextDayRecording.next_day} (Minggu ke-${calculateWeek(nextDayRecording.next_day)})`
|
||||
: '-'
|
||||
: initialValues?.day
|
||||
? `Hari ke-${initialValues.day} (Minggu ke-${Math.ceil(initialValues.day / 7)})`
|
||||
? `Hari ke-${initialValues.day} (Minggu ke-${calculateWeek(initialValues.day)})`
|
||||
: '-'}
|
||||
</p>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user