|
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, |
@@ -182,7 +184,6 @@ function LocalUnitsForm(props: Props) { |
182 | 184 | validate, |
183 | 185 | setError, |
184 | 186 | setValue, |
185 | | - pristine, |
186 | 187 | } = useForm( |
187 | 188 | schema, |
188 | 189 | { |
@@ -223,7 +224,9 @@ function LocalUnitsForm(props: Props) { |
223 | 224 | }, |
224 | 225 | }); |
225 | 226 |
|
226 | | - const { response: localUnitPreviousResponse } = useRequest({ |
| 227 | + const { |
| 228 | + response: localUnitPreviousResponse, |
| 229 | + } = useRequest({ |
227 | 230 | url: '/api/v2/local-units/{id}/latest-change-request/', |
228 | 231 | pathVariables: isDefined(localUnitId) ? { id: localUnitId } : undefined, |
229 | 232 | /* |
@@ -443,7 +446,14 @@ function LocalUnitsForm(props: Props) { |
443 | 446 | const previousData = ( |
444 | 447 | localUnitPreviousResponse?.previous_data_details as unknown as LocalUnitResponse |
445 | 448 | ); |
446 | | - const isNewLocalUnit = isNotDefined(previousData); |
| 449 | + const isNewLocalUnit = useMemo(() => { |
| 450 | + if (isObject(previousData)) { |
| 451 | + if (Object.keys(previousData).length <= 0) { |
| 452 | + return true; |
| 453 | + } |
| 454 | + } |
| 455 | + return false; |
| 456 | + }, [previousData]); |
447 | 457 | const showChanges = !isNewLocalUnit && !!localUnitDetailsResponse?.is_locked; |
448 | 458 |
|
449 | 459 | return ( |
@@ -559,7 +569,6 @@ function LocalUnitsForm(props: Props) { |
559 | 569 | {hasValidatePermission && ( |
560 | 570 | <LocalUnitValidateButton |
561 | 571 | onClick={setShowValidateLocalUnitModalTrue} |
562 | | - readOnly={pristine} |
563 | 572 | isValidated={localUnitDetailsResponse.validated} |
564 | 573 | hasValidatePermission={hasValidatePermission} |
565 | 574 | /> |
|
0 commit comments