Skip to content

Commit

Permalink
- only admins can edit or delete takes when the file is Active or Draft
Browse files Browse the repository at this point in the history
  • Loading branch information
Herrera committed Apr 4, 2024
1 parent 868b1b3 commit ad0c2b7
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 45 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { ApiGen_CodeTypes_AcquisitionStatusTypes } from '@/models/api/generated/ApiGen_CodeTypes_AcquisitionStatusTypes';
import { ApiGen_CodeTypes_AcquisitionTakeStatusTypes } from '@/models/api/generated/ApiGen_CodeTypes_AcquisitionTakeStatusTypes';
import { ApiGen_CodeTypes_AgreementStatusTypes } from '@/models/api/generated/ApiGen_CodeTypes_AgreementStatusTypes';
import { ApiGen_Concepts_AcquisitionFile } from '@/models/api/generated/ApiGen_Concepts_AcquisitionFile';

Expand Down Expand Up @@ -64,28 +63,6 @@ class StatusUpdateSolver {
return canEdit;
}

canDeleteTake(takeStatus: ApiGen_CodeTypes_AcquisitionTakeStatusTypes): boolean {
if (this.acquisitionFile === null) {
return false;
}

let canDelete = false;
switch (takeStatus) {
case ApiGen_CodeTypes_AcquisitionTakeStatusTypes.CANCELLED:
case ApiGen_CodeTypes_AcquisitionTakeStatusTypes.INPROGRESS:
canDelete = true;
break;
case ApiGen_CodeTypes_AcquisitionTakeStatusTypes.COMPLETE:
canDelete = false;
break;
default:
canDelete = false;
break;
}

return canDelete;
}

canEditOrDeleteCompensation(isDraftCompensation: boolean | null): boolean {
if (this.acquisitionFile === null) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ import { RadioGroup, yesNoRadioGroupValues } from '@/components/common/form/Radi
import { Section } from '@/components/common/Section/Section';
import { SectionField } from '@/components/common/Section/SectionField';
import AreaContainer from '@/components/measurements/AreaContainer';
import { Roles } from '@/constants';
import * as API from '@/constants/API';
import useKeycloakWrapper from '@/hooks/useKeycloakWrapper';
import useLookupCodeHelpers from '@/hooks/useLookupCodeHelpers';
import { getDeleteModalProps, useModalContext } from '@/hooks/useModalContext';
import { ApiGen_CodeTypes_AcquisitionTakeStatusTypes } from '@/models/api/generated/ApiGen_CodeTypes_AcquisitionTakeStatusTypes';
import { withNameSpace } from '@/utils/formUtils';

import { StyledBorderSection, StyledNoTabSection } from '../styles';
Expand All @@ -31,6 +34,7 @@ const TakeSubForm: React.FunctionComponent<ITakeSubFormProps> = ({
const currentTake = getIn(values, withNameSpace(nameSpace));
const { getOptionsByType } = useLookupCodeHelpers();
const { setModalContent, setDisplayModal } = useModalContext();
const { hasRole } = useKeycloakWrapper();

const takeTypeOptions = getOptionsByType(API.TAKE_TYPES);
const takeStatusTypeOptions = getOptionsByType(API.TAKE_STATUS_TYPES);
Expand All @@ -48,6 +52,7 @@ const TakeSubForm: React.FunctionComponent<ITakeSubFormProps> = ({
values,
withNameSpace(nameSpace, 'isNewLicenseToConstruct'),
);
const takeStatusTypeCode = getIn(values, withNameSpace(nameSpace, 'takeStatusTypeCode'));

const getModalWarning = (onOk: () => void) => {
return (e: React.ChangeEvent<any>) => {
Expand All @@ -70,51 +75,61 @@ const TakeSubForm: React.FunctionComponent<ITakeSubFormProps> = ({
};
};

const canEditTake =
currentTake?.id === 0 ||
takeStatusTypeCode !== ApiGen_CodeTypes_AcquisitionTakeStatusTypes.COMPLETE ||
hasRole(Roles.SYSTEM_ADMINISTRATOR);

return (
<Section
className="position-relative"
header={currentTake?.id ? `Take ${takeIndex + 1}` : 'New Take'}
isCollapsable={true}
initiallyExpanded={true}
>
<StyledRemoveLinkButton
style={{ position: 'absolute', right: '1rem' }}
title="delete take"
data-testid="take-delete-button"
icon={<FaTrash size={24} id={`take-delete-${takeIndex}`} title="delete take" />}
onClick={() => {
setModalContent({
...getDeleteModalProps(),
handleOk: () => {
onRemove(takeIndex);
setDisplayModal(false);
},
});
setDisplayModal(true);
}}
></StyledRemoveLinkButton>
{canEditTake && (
<StyledRemoveLinkButton
style={{ position: 'absolute', right: '1rem' }}
title="delete take"
data-testid="take-delete-button"
icon={<FaTrash size={24} id={`take-delete-${takeIndex}`} title="delete take icon" />}
onClick={() => {
setModalContent({
...getDeleteModalProps(),
handleOk: () => {
onRemove(takeIndex);
setDisplayModal(false);
},
});
setDisplayModal(true);
}}
></StyledRemoveLinkButton>
)}

<SectionField label="Take type" required labelWidth="4" contentWidth="5">
<Select
field={withNameSpace(nameSpace, 'takeTypeCode')}
options={takeTypeOptions}
placeholder="Select take type"
disabled={!canEditTake}
/>
</SectionField>
<SectionField label="Take status" required labelWidth="4" contentWidth="5">
<Select
field={withNameSpace(nameSpace, 'takeStatusTypeCode')}
options={takeStatusTypeOptions}
disabled={!canEditTake}
/>
</SectionField>
<SectionField label="Site contamination" labelWidth="4" contentWidth="5">
<Select
field={withNameSpace(nameSpace, 'takeSiteContamTypeCode')}
options={takeSiteContamTypeOptions}
disabled={!canEditTake}
/>
</SectionField>
<SectionField label="Description of this Take" labelWidth="12">
<TextArea field={withNameSpace(nameSpace, 'description')} />
<TextArea field={withNameSpace(nameSpace, 'description')} disabled={!canEditTake} />
</SectionField>
<StyledNoTabSection header="Area">
<StyledBorderSection>
Expand All @@ -131,6 +146,7 @@ const TakeSubForm: React.FunctionComponent<ITakeSubFormProps> = ({
setFieldValue(withNameSpace(nameSpace, 'isNewHighwayDedication'), 'false');
setFieldValue(withNameSpace(nameSpace, 'newHighwayDedicationArea'), 0);
})}
disabled={!canEditTake}
/>
</SectionField>
{isNewHighwayDedication === 'true' && (
Expand Down Expand Up @@ -168,6 +184,7 @@ const TakeSubForm: React.FunctionComponent<ITakeSubFormProps> = ({
field={withNameSpace(nameSpace, 'isAcquiredForInventory')}
radioValues={yesNoRadioGroupValues}
flexDirection="row"
disabled={!canEditTake}
/>
</SectionField>
</StyledBorderSection>
Expand All @@ -185,6 +202,7 @@ const TakeSubForm: React.FunctionComponent<ITakeSubFormProps> = ({
setFieldValue(withNameSpace(nameSpace, 'statutoryRightOfWayArea'), 0);
setFieldValue(withNameSpace(nameSpace, 'srwEndDt'), '');
})}
disabled={!canEditTake}
/>
</SectionField>
{isNewInterestInSrw === 'true' && (
Expand All @@ -201,7 +219,7 @@ const TakeSubForm: React.FunctionComponent<ITakeSubFormProps> = ({
areaUnitTypeCode,
);
}}
isEditable
isEditable={canEditTake}
unitCode={getIn(
values,
withNameSpace(nameSpace, 'statutoryRightOfWayAreaUnitTypeCode'),
Expand Down Expand Up @@ -231,6 +249,7 @@ const TakeSubForm: React.FunctionComponent<ITakeSubFormProps> = ({
setFieldValue(withNameSpace(nameSpace, 'landActEndDt'), '');
setFieldValue(withNameSpace(nameSpace, 'landActTypeCode'), '');
})}
disabled={!canEditTake}
/>
</SectionField>
{isNewLandAct === 'true' && (
Expand All @@ -240,6 +259,7 @@ const TakeSubForm: React.FunctionComponent<ITakeSubFormProps> = ({
field={withNameSpace(nameSpace, 'landActTypeCode')}
placeholder="Select Land Act"
options={takeLandActTypeOptions}
disabled={!canEditTake}
/>
</SectionField>
<SectionField label="Area" labelWidth="12">
Expand All @@ -251,7 +271,7 @@ const TakeSubForm: React.FunctionComponent<ITakeSubFormProps> = ({
areaUnitTypeCode,
);
}}
isEditable
isEditable={canEditTake}
unitCode={getIn(values, withNameSpace(nameSpace, 'landActAreaUnitTypeCode'))}
landArea={currentTake.landActArea}
field={withNameSpace(nameSpace, 'landActArea')}
Expand All @@ -261,6 +281,7 @@ const TakeSubForm: React.FunctionComponent<ITakeSubFormProps> = ({
<FastDatePicker
field={withNameSpace(nameSpace, 'landActEndDt')}
formikProps={formikProps}
disabled={!canEditTake}
/>
</SectionField>
</>
Expand All @@ -280,6 +301,7 @@ const TakeSubForm: React.FunctionComponent<ITakeSubFormProps> = ({
setFieldValue(withNameSpace(nameSpace, 'licenseToConstructArea'), 0);
setFieldValue(withNameSpace(nameSpace, 'ltcEndDt'), '');
})}
disabled={!canEditTake}
/>
</SectionField>
{isNewLicenseToConstruct === 'true' && (
Expand All @@ -296,7 +318,7 @@ const TakeSubForm: React.FunctionComponent<ITakeSubFormProps> = ({
areaUnitTypeCode,
);
}}
isEditable
isEditable={canEditTake}
unitCode={getIn(
values,
withNameSpace(nameSpace, 'licenseToConstructAreaUnitTypeCode'),
Expand All @@ -310,6 +332,7 @@ const TakeSubForm: React.FunctionComponent<ITakeSubFormProps> = ({
<FastDatePicker
field={withNameSpace(nameSpace, 'ltcEndDt')}
formikProps={formikProps}
disabled={!canEditTake}
/>
</SectionField>
</>
Expand All @@ -327,6 +350,7 @@ const TakeSubForm: React.FunctionComponent<ITakeSubFormProps> = ({
setFieldValue(withNameSpace(nameSpace, 'isThereSurplus'), 'false');
setFieldValue(withNameSpace(nameSpace, 'surplusArea'), 0);
})}
disabled={!canEditTake}
/>
</SectionField>
{isThereSurplus === 'true' && (
Expand All @@ -340,7 +364,7 @@ const TakeSubForm: React.FunctionComponent<ITakeSubFormProps> = ({
areaUnitTypeCode,
);
}}
isEditable
isEditable={canEditTake}
unitCode={getIn(values, withNameSpace(nameSpace, 'surplusAreaUnitTypeCode'))}
landArea={currentTake.surplusArea}
field={withNameSpace(nameSpace, 'surplusArea')}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import { act, render, RenderOptions, screen, userEvent } from '@/utils/test-util

import { TakeModel } from './models';
import TakesUpdateForm, { ITakesUpdateFormProps } from './TakesUpdateForm';
import { ApiGen_CodeTypes_AcquisitionTakeStatusTypes } from '@/models/api/generated/ApiGen_CodeTypes_AcquisitionTakeStatusTypes';
import { Claims, Roles } from '@/constants';

jest.mock('@react-keycloak/web');

const history = createMemoryHistory();
const storeState = {
Expand All @@ -31,6 +35,7 @@ describe('TakesUpdateForm component', () => {
...renderOptions,
store: storeState,
history,
useMockAuthentication: true,
},
);

Expand Down Expand Up @@ -171,4 +176,37 @@ describe('TakesUpdateForm component', () => {

expect(queryByDisplayValue('20234.28')).toBeNull();
});

it('hides the delete button when the take has been completed', () => {
let completeTake = getMockApiTakes()[0];
const takeModel = new TakeModel(completeTake);
takeModel.takeStatusTypeCode = ApiGen_CodeTypes_AcquisitionTakeStatusTypes.COMPLETE;

const { queryByTitle, getByTestId } = setup({props: {
takes: [takeModel],
}});

const deleteButton = queryByTitle('delete take');
expect(deleteButton).toBeNull();

const noButton = getByTestId('radio-takes.0.istheresurplus-no');
expect(noButton).toBeDisabled();
});

it('shows the edit button when the take has been completed for Admin users', () => {
let completeTake = getMockApiTakes()[0];
const takeModel = new TakeModel(completeTake);
takeModel.takeStatusTypeCode = ApiGen_CodeTypes_AcquisitionTakeStatusTypes.COMPLETE;

const { queryByTitle } = setup({
props: {
takes: [takeModel],
},
claims: [Claims.ACQUISITION_EDIT],
roles: [Roles.SYSTEM_ADMINISTRATOR]
});

const deleteButton = queryByTitle('delete take');
expect(deleteButton).toBeInTheDocument();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ exports[`TakesUpdateForm component renders as expected 1`] = `
xmlns="http://www.w3.org/2000/svg"
>
<title>
delete take
delete take icon
</title>
<path
d="M432 32H312l-9.4-18.7A24 24 0 0 0 281.1 0H166.8a23.72 23.72 0 0 0-21.4 13.3L136 32H16A16 16 0 0 0 0 48v32a16 16 0 0 0 16 16h416a16 16 0 0 0 16-16V48a16 16 0 0 0-16-16zM53.2 467a48 48 0 0 0 47.9 45h245.8a48 48 0 0 0 47.9-45L416 128H32z"
Expand Down

0 comments on commit ad0c2b7

Please sign in to comment.