|
1 | 1 | import { |
2 | 2 | type RefObject, |
3 | 3 | useCallback, |
| 4 | + useMemo, |
4 | 5 | useRef, |
5 | 6 | } from 'react'; |
6 | 7 | import { useOutletContext } from 'react-router-dom'; |
@@ -31,6 +32,7 @@ import { |
31 | 32 | import { |
32 | 33 | isDefined, |
33 | 34 | isNotDefined, |
| 35 | + isObject, |
34 | 36 | } from '@togglecorp/fujs'; |
35 | 37 | import { |
36 | 38 | createSubmitHandler, |
@@ -183,7 +185,6 @@ function LocalUnitsForm(props: Props) { |
183 | 185 | validate, |
184 | 186 | setError, |
185 | 187 | setValue, |
186 | | - pristine, |
187 | 188 | } = useForm( |
188 | 189 | schema, |
189 | 190 | { |
@@ -224,7 +225,9 @@ function LocalUnitsForm(props: Props) { |
224 | 225 | }, |
225 | 226 | }); |
226 | 227 |
|
227 | | - const { response: localUnitPreviousResponse } = useRequest({ |
| 228 | + const { |
| 229 | + response: localUnitPreviousResponse, |
| 230 | + } = useRequest({ |
228 | 231 | url: '/api/v2/local-units/{id}/latest-change-request/', |
229 | 232 | pathVariables: isDefined(localUnitId) ? { id: localUnitId } : undefined, |
230 | 233 | /* |
@@ -444,7 +447,14 @@ function LocalUnitsForm(props: Props) { |
444 | 447 | const previousData = ( |
445 | 448 | localUnitPreviousResponse?.previous_data_details as unknown as LocalUnitResponse |
446 | 449 | ); |
447 | | - const isNewLocalUnit = isNotDefined(previousData); |
| 450 | + const isNewLocalUnit = useMemo(() => { |
| 451 | + if (isObject(previousData)) { |
| 452 | + if (Object.keys(previousData).length <= 0) { |
| 453 | + return true; |
| 454 | + } |
| 455 | + } |
| 456 | + return false; |
| 457 | + }, [previousData]); |
448 | 458 | const showChanges = !isNewLocalUnit && !!localUnitDetailsResponse?.is_locked; |
449 | 459 |
|
450 | 460 | return ( |
@@ -560,7 +570,6 @@ function LocalUnitsForm(props: Props) { |
560 | 570 | {hasValidatePermission && ( |
561 | 571 | <LocalUnitValidateButton |
562 | 572 | onClick={setShowValidateLocalUnitModalTrue} |
563 | | - readOnly={pristine} |
564 | 573 | isValidated={localUnitDetailsResponse.validated} |
565 | 574 | hasValidatePermission={hasValidatePermission} |
566 | 575 | /> |
|
0 commit comments