Skip to content

Commit

Permalink
Show latest form values changes for local unit validation
Browse files Browse the repository at this point in the history
  • Loading branch information
shreeyash07 authored and samshara committed Dec 18, 2024
1 parent 285dbd7 commit a9037dd
Show file tree
Hide file tree
Showing 8 changed files with 363 additions and 6 deletions.
13 changes: 13 additions & 0 deletions app/src/components/domain/BaseMapPointInput/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ interface Props<NAME> extends BaseMapProps {
readOnly?: boolean;
required?: boolean;
error?: ObjectError<Value>;
baseMapFormFieldsChanges: {
lat: boolean | undefined;
lng: boolean | undefined;
} | undefined;
}

function BaseMapPointInput<NAME extends string>(props: Props<NAME>) {
Expand All @@ -74,6 +78,7 @@ function BaseMapPointInput<NAME extends string>(props: Props<NAME>) {
country,
required,
error,
baseMapFormFieldsChanges,
...otherProps
} = props;

Expand Down Expand Up @@ -183,6 +188,10 @@ function BaseMapPointInput<NAME extends string>(props: Props<NAME>) {
<div className={_cs(styles.baseMapPointInput, className)}>
<div className={styles.locationInputs}>
<NumberInput
inputSectionClassName={_cs(
baseMapFormFieldsChanges?.lat
&& styles.changes,
)}
className={styles.input}
name="lat"
label={strings.latitude}
Expand All @@ -193,6 +202,10 @@ function BaseMapPointInput<NAME extends string>(props: Props<NAME>) {
required={required}
/>
<NumberInput
inputSectionClassName={_cs(
baseMapFormFieldsChanges?.lng
&& styles.changes,
)}
className={styles.input}
name="lng"
label={strings.longitude}
Expand Down
4 changes: 4 additions & 0 deletions app/src/components/domain/BaseMapPointInput/styles.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
flex-direction: column;
gap: var(--go-ui-spacing-md);

.changes {
background-color: var(--go-ui-color-semantic-yellow) !important;
}

.location-inputs {
display: flex;
gap: var(--go-ui-spacing-sm);
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
background-color: var(--go-ui-color-background);
height: 32rem;
}

}

.changes {
background-color: var(--go-ui-color-semantic-yellow) !important;
}

/* NOTE: this element is portaled */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,22 @@ import { Modal } from '@ifrc-go/ui';
import { useTranslation } from '@ifrc-go/ui/hooks';
import { isDefined } from '@togglecorp/fujs';

import { GoApiResponse } from '#utils/restRequest';

import LocalUnitsForm from './LocalUnitsForm';

import i18n from './i18n.json';
import styles from './styles.module.css';

type LocalUnitResponse = GoApiResponse<'/api/v2/local-units/{id}/'>;

interface Props {
localUnitId?: number;
readOnly?: boolean;
setReadOnly?: React.Dispatch<React.SetStateAction<boolean>>;
onClose: (requestDone?: boolean) => void;
onDeleteActionSuccess?: () => void;
previousData?: LocalUnitResponse;
}

function LocalUnitsFormModal(props: Props) {
Expand All @@ -26,6 +31,7 @@ function LocalUnitsFormModal(props: Props) {
readOnly,
setReadOnly,
onDeleteActionSuccess,
previousData,
} = props;

const strings = useTranslation(i18n);
Expand Down Expand Up @@ -77,6 +83,7 @@ function LocalUnitsFormModal(props: Props) {
headingDescriptionRef={headingDescriptionRef}
headerDescriptionRef={headerDescriptionRef}
onDeleteActionSuccess={onDeleteActionSuccess}
previousData={previousData}
/>
</Modal>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,10 @@ import {
import { localUnitMapStyle } from '#utils/map';
import { type CountryOutletContext } from '#utils/outletContext';
import {
GoApiResponse,
GoApiUrlQuery,
type GoApiBody,
type GoApiResponse,
type GoApiUrlQuery,
useLazyRequest,
useRequest,
} from '#utils/restRequest';

Expand All @@ -74,6 +76,9 @@ import { TYPE_HEALTH_CARE } from '../LocalUnitsFormModal/LocalUnitsForm/schema';
import i18n from './i18n.json';
import styles from './styles.module.css';

type LocalUnitResponse = GoApiResponse<'/api/v2/local-units/{id}/'>;
type LocalUnitLatestChangesBody = GoApiBody<'/api/v2/local-units/{id}/latest-change-request/', 'POST'>

const LOCAL_UNIT_ICON_KEY = 'local-units';
const HEALTHCARE_ICON_KEY = 'healthcare';

Expand Down Expand Up @@ -253,6 +258,18 @@ function LocalUnitsMap(props: Props) {
}) : undefined,
});

const {
response: previousData,
trigger: latestChanges,
} = useLazyRequest({
url: '/api/v2/local-units/{id}/latest-change-request/',
method: 'POST',
pathVariables: isDefined(clickedPointProperties) ? ({
id: clickedPointProperties.localUnitId,
}) : undefined,
body: (ctx: LocalUnitLatestChangesBody) => ctx,
});

const localUnitDetail = requestType !== AUTHENTICATED
? publicLocalUnitDetailResponse
: superLocalUnitDetailResponse;
Expand Down Expand Up @@ -318,10 +335,11 @@ function LocalUnitsMap(props: Props) {

const handleLocalUnitHeadingClick = useCallback(
() => {
latestChanges(clickedPointProperties?.localUnitId as never);
setReadOnlyLocalUnitModal(true);
setShowLocalUnitViewModalTrue();
},
[setShowLocalUnitViewModalTrue],
[setShowLocalUnitViewModalTrue, latestChanges, clickedPointProperties?.localUnitId],
);

const handleLocalUnitsFormModalClose = useCallback(
Expand Down Expand Up @@ -585,6 +603,9 @@ function LocalUnitsMap(props: Props) {
readOnly={readOnlyLocalUnitModal}
setReadOnly={setReadOnlyLocalUnitModal}
onDeleteActionSuccess={refetchLocalUnits}
previousData={
previousData?.previous_data_details as unknown as LocalUnitResponse
}
/>
))}
</Container>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,21 @@ import { environment } from '#config';
import useAuth from '#hooks/domain/useAuth';
import useCountry from '#hooks/domain/useCountry';
import usePermissions from '#hooks/domain/usePermissions';
import {
type GoApiBody,
type GoApiResponse,
useLazyRequest,
} from '#utils/restRequest';

import LocalUnitDeleteModal from '../../LocalUnitDeleteModal';
import LocalUnitsFormModal from '../../LocalUnitsFormModal';
import LocalUnitValidateButton from '../../LocalUnitValidateButton';

import i18n from './i18n.json';

type LocalUnitLatestChangesBody = GoApiBody<'/api/v2/local-units/{id}/latest-change-request/', 'POST'>
type LocalUnitResponse = GoApiResponse<'/api/v2/local-units/{id}/'>;

export interface Props {
countryId: number;
localUnitName: string;
Expand Down Expand Up @@ -63,6 +71,16 @@ function LocalUnitsTableActions(props: Props) {

const hasDeletePermission = isAuthenticated && !isGuestUser;

const {
response: previousData,
trigger: latestChanges,
} = useLazyRequest({
url: '/api/v2/local-units/{id}/latest-change-request/',
method: 'POST',
pathVariables: { id: localUnitId },
body: (ctx: LocalUnitLatestChangesBody) => ctx,
});

const [readOnlyLocalUnitModal, setReadOnlyLocalUnitModal] = useState(false);

const [
Expand Down Expand Up @@ -91,21 +109,22 @@ function LocalUnitsTableActions(props: Props) {
},
[setShowLocalUnitModalFalse, onDeleteActionSuccess],
);

const handleViewLocalUnitClick = useCallback(
() => {
latestChanges(localUnitId as never);
setReadOnlyLocalUnitModal(true);
setShowLocalUnitModalTrue();
},
[setShowLocalUnitModalTrue],
[setShowLocalUnitModalTrue, latestChanges, localUnitId],
);

const handleEditLocalUnitClick = useCallback(
() => {
latestChanges(localUnitId as never);
setReadOnlyLocalUnitModal(false);
setShowLocalUnitModalTrue();
},
[setShowLocalUnitModalTrue],
[setShowLocalUnitModalTrue, latestChanges, localUnitId],
);

return (
Expand Down Expand Up @@ -170,6 +189,9 @@ function LocalUnitsTableActions(props: Props) {
readOnly={readOnlyLocalUnitModal}
setReadOnly={setReadOnlyLocalUnitModal}
onDeleteActionSuccess={onDeleteActionSuccess}
previousData={
previousData?.previous_data_details as unknown as LocalUnitResponse
}
/>
)}
{showDeleteLocalUnitModal && (
Expand Down
1 change: 1 addition & 0 deletions packages/ui/src/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@
--go-ui-color-blue: #3FA2F7;
--go-ui-color-red: #F75C65;
--go-ui-color-yellow: #d9b100;
--go-ui-color-semantic-yellow: #FFB443;
--go-ui-color-black: #000000;
--go-ui-color-white: #ffffff;

Expand Down

0 comments on commit a9037dd

Please sign in to comment.