Skip to content

Commit

Permalink
feat: show added and removed communities in history
Browse files Browse the repository at this point in the history
  • Loading branch information
RRanath committed Nov 14, 2024
1 parent 4ca0c77 commit 0d49a39
Show file tree
Hide file tree
Showing 16 changed files with 350 additions and 99 deletions.
81 changes: 81 additions & 0 deletions app/components/Analyst/CBC/History/CbcHistoryCommunitiesTable.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import styled from 'styled-components';

interface Props {
action: string;
communities: any[];
}

const StyledCommunitiesContainer = styled.div`
display: flex;
align-items: center;
`;

const StyledLeftContainer = styled.div`
padding-right: 2%;
width: 250px;
`;

const StyledTable = styled.table`
th {
border: none;
}
tbody > tr {
border-bottom: thin dashed;
border-color: ${(props) => props.theme.color.borderGrey};
td {
width: 200px;
max-width: 200px;
border: none;
}
}
`;

const StyledIdCell = styled.td`
width: 100px !important;
max-width: 100px;
`;

const CbcHistoryCommunitiesTable: React.FC<Props> = ({
action,
communities,
}) => {
return (
<StyledCommunitiesContainer
style={{ display: 'flex', alignItems: 'center' }}
>
<StyledLeftContainer>{`${action} community location data`}</StyledLeftContainer>
<div>
<StyledTable>
<thead>
<tr>
<th>Economic Region</th>
<th>Regional District</th>
<th>Geographic Name</th>
<th>Type</th>
<th>ID</th>
</tr>
</thead>
<tbody>
{communities?.map((community, index) => (
<tr
// eslint-disable-next-line react/no-array-index-key
key={`${action}-${community.communities_source_data_id}-${index}`}
data-key={`${action}-row-${index}`}
>
<td>{community.economic_region}</td>
<td>{community.regional_district}</td>
<td>{community.bc_geographic_name}</td>
<td>{community.geographic_type}</td>
<StyledIdCell>
{community.communities_source_data_id}
</StyledIdCell>
</tr>
))}
</tbody>
</StyledTable>
</div>
</StyledCommunitiesContainer>
);
};

export default CbcHistoryCommunitiesTable;
14 changes: 14 additions & 0 deletions app/components/Analyst/CBC/History/CbcHistoryContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import HistoryDetails from 'components/Analyst/History/HistoryDetails';
import cbcData from 'formSchema/uiSchema/history/cbcData';
import { DateTime } from 'luxon';
import styled from 'styled-components';
import CbcHistoryCommunitiesTable from './CbcHistoryCommunitiesTable';

const StyledContent = styled.span`
display: flex;
Expand Down Expand Up @@ -72,10 +73,23 @@ const HistoryContent = ({
'updated_at',
'change_reason',
'cbc_data_id',
'locations',
]}
diffSchema={cbcData}
overrideParent="cbcData"
/>
{json?.locations?.added?.length > 0 && (
<CbcHistoryCommunitiesTable
action="Added"
communities={json?.locations?.added}
/>
)}
{json?.locations?.removed?.length > 0 && (
<CbcHistoryCommunitiesTable
action="Deleted"
communities={json?.locations?.removed}
/>
)}
{op === 'UPDATE' && changeReason !== '' && (
<ChangeReason reason={changeReason} />
)}
Expand Down
4 changes: 4 additions & 0 deletions app/components/Analyst/CBC/History/CbcHistoryTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ const CbcHistoryTable: React.FC<Props> = ({ query }) => {
json={{
...historyItem.record?.json_data,
project_number: historyItem.record?.project_number,
locations: {
added: historyItem.record?.added_communities,
removed: historyItem.record?.deleted_communities,
},
}}
prevJson={{
...historyItem.oldRecord?.json_data,
Expand Down
48 changes: 14 additions & 34 deletions app/pages/analyst/cbc/[cbcId].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import CbcForm from 'components/Analyst/CBC/CbcForm';
import { ChangeModal } from 'components/Analyst';
import styled from 'styled-components';
import ReviewTheme from 'components/Review/ReviewTheme';
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
import { useEffect, useMemo, useRef, useState } from 'react';
import { useUpdateCbcDataAndInsertChangeRequest } from 'schema/mutations/cbc/updateCbcDataAndInsertChangeReason';
import review from 'formSchema/analyst/cbc/review';
import reviewUiSchema from 'formSchema/uiSchema/cbc/reviewUiSchema';
Expand All @@ -25,7 +25,6 @@ import {
import customValidate, { CBC_WARN_COLOR } from 'utils/cbcCustomValidator';
import CbcRecordLock from 'components/Analyst/CBC/CbcRecordLock';
import useModal from 'lib/helpers/useModal';
import { useUpdateCbcCommunityDataMutationMutation } from 'schema/mutations/cbc/updateCbcCommunityData';

const getCbcQuery = graphql`
query CbcIdQuery($rowId: Int!) {
Expand Down Expand Up @@ -248,42 +247,12 @@ const Cbc = ({
}, [query, isCbcAdmin, editFeatureEnabled, cbcCommunitiesData]);

const [updateFormData] = useUpdateCbcDataAndInsertChangeRequest();
const [updateCbcCommunitySourceData] =
useUpdateCbcCommunityDataMutationMutation();

const handleChangeRequestModal = () => {
setChangeReason(null);
changeModal.open();
};

const handleUpdateCommunitySource = useCallback(() => {
updateCbcCommunitySourceData({
variables: {
input: {
_projectId: rowId,
_communityIdsToAdd: addedCommunities,
_communityIdsToArchive: removedCommunities,
},
},
debounceKey: 'cbc_update_community_source_data',
onCompleted: (response) => {
setAddedCommunities([]);
setRemovedCommunities([]);
setResponseCommunityData(
response.editCbcProjectCommunities.cbcProjectCommunities.map(
(proj) => proj.communitiesSourceDataByCommunitiesSourceDataId
)
);
},
});
}, [
addedCommunities,
removedCommunities,
updateCbcCommunitySourceData,
rowId,
setResponseCommunityData,
]);

const handleSubmit = () => {
const {
geographicNames,
Expand Down Expand Up @@ -317,12 +286,23 @@ const Cbc = ({
query?.cbcByRowId?.cbcDataByCbcId?.edges[0].node.rowId || null,
},
},
inputCbcProjectCommunities: {
_projectId: rowId,
_communityIdsToAdd: addedCommunities,
_communityIdsToArchive: removedCommunities,
},
},
debounceKey: 'cbc_update_form_data',
onCompleted: () => {
onCompleted: (response) => {
setEditMode(false);
changeModal.close();
handleUpdateCommunitySource();
setAddedCommunities([]);
setRemovedCommunities([]);
setResponseCommunityData(
response.editCbcProjectCommunities.cbcProjectCommunities.map(
(proj) => proj.communitiesSourceDataByCommunitiesSourceDataId
)
);
setAllowEdit(isCbcAdmin && editFeatureEnabled);
},
});
Expand Down
34 changes: 7 additions & 27 deletions app/pages/analyst/cbc/[cbcId]/edit/[section].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import {
getAllEconomicRegionNames,
} from 'utils/schemaUtils';
import ArrayLocationFieldTemplate from 'lib/theme/fields/ArrayLocationDataField';
import { useUpdateCbcCommunityDataMutationMutation } from 'schema/mutations/cbc/updateCbcCommunityData';
import customValidate, { CBC_WARN_COLOR } from 'utils/cbcCustomValidator';

const getCbcSectionQuery = graphql`
Expand Down Expand Up @@ -152,9 +151,6 @@ const EditCbcSection = ({
return formPayload;
};

const [updateCbcCommunitySourceData] =
useUpdateCbcCommunityDataMutationMutation();

const handleOnChange = (e) => {
if (section === 'locations') {
setFormData({
Expand All @@ -164,28 +160,6 @@ const EditCbcSection = ({
} else setFormData({ ...formData, [section]: e.formData });
};

const handleUpdateCommunitySource = useCallback(() => {
updateCbcCommunitySourceData({
variables: {
input: {
_projectId: rowId,
_communityIdsToAdd: addedCommunities,
_communityIdsToArchive: removedCommunities,
},
},
debounceKey: 'cbc_update_community_source_data',
onCompleted: () => {
setAddedCommunities([]);
setRemovedCommunities([]);
},
});
}, [
addedCommunities,
removedCommunities,
updateCbcCommunitySourceData,
rowId,
]);

const handleChangeRequestModal = (e) => {
changeModal.open();
setFormData({ ...formData, [section]: e.formData });
Expand Down Expand Up @@ -248,10 +222,16 @@ const EditCbcSection = ({
cbcDataId: cbcDataRowId,
},
},
inputCbcProjectCommunities: {
_projectId: rowId,
_communityIdsToAdd: addedCommunities,
_communityIdsToArchive: removedCommunities,
},
},
debounceKey: 'cbc_update_section_data',
onCompleted: () => {
handleUpdateCommunitySource();
setAddedCommunities([]);
setRemovedCommunities([]);
router.push(`/analyst/cbc/${rowId}`);
},
});
Expand Down
15 changes: 15 additions & 0 deletions app/schema/mutations/cbc/updateCbcDataAndInsertChangeReason.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const mutation = graphql`
mutation updateCbcDataAndInsertChangeReasonMutation(
$inputCbcData: UpdateCbcDataByRowIdInput!
$inputCbcChangeReason: CreateCbcDataChangeReasonInput!
$inputCbcProjectCommunities: EditCbcProjectCommunitiesInput!
) {
updateCbcDataByRowId(input: $inputCbcData) {
cbcData {
Expand All @@ -24,6 +25,20 @@ const mutation = graphql`
createdAt
}
}
editCbcProjectCommunities(input: $inputCbcProjectCommunities) {
cbcProjectCommunities {
communitiesSourceDataId
cbcId
communitiesSourceDataByCommunitiesSourceDataId {
geographicNameId
economicRegion
regionalDistrict
bcGeographicName
geographicType
rowId
}
}
}
}
`;

Expand Down
23 changes: 6 additions & 17 deletions app/tests/pages/analyst/cbc/[cbcId].test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,11 @@ describe('Cbc', () => {
cbcDataId: 1,
},
},
inputCbcProjectCommunities: {
_projectId: 1,
_communityIdsToAdd: [],
_communityIdsToArchive: [],
},
}
);

Expand Down Expand Up @@ -770,23 +775,7 @@ describe('Cbc', () => {
cbcDataId: 1,
},
},
}
);

pageTestingHelper.environment.mock.resolveMostRecentOperation({
data: {
updateCbcDataAndInsertChangeReason: {
cbcData: {
rowId: 1,
},
},
},
});

pageTestingHelper.expectMutationToBeCalled(
'updateCbcCommunityDataMutation',
{
input: {
inputCbcProjectCommunities: {
_projectId: 1,
_communityIdsToAdd: [],
_communityIdsToArchive: [1],
Expand Down
21 changes: 10 additions & 11 deletions app/tests/pages/analyst/cbc/[cbcId]/[section].test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,11 @@ describe('EditCbcSection', () => {
cbcDataId: 20,
},
},
inputCbcProjectCommunities: {
_projectId: 1,
_communityIdsToAdd: expect.anything(),
_communityIdsToArchive: expect.anything(),
},
}
);
});
Expand Down Expand Up @@ -428,6 +433,11 @@ describe('EditCbcSection', () => {
cbcDataId: 20,
},
},
inputCbcProjectCommunities: {
_projectId: 1,
_communityIdsToAdd: expect.anything(),
_communityIdsToArchive: [],
},
}
);

Expand All @@ -440,16 +450,5 @@ describe('EditCbcSection', () => {
},
},
});

pageTestingHelper.expectMutationToBeCalled(
'updateCbcCommunityDataMutation',
{
input: {
_projectId: 1,
_communityIdsToAdd: expect.anything(),
_communityIdsToArchive: [],
},
}
);
});
});
Loading

0 comments on commit 0d49a39

Please sign in to comment.