Skip to content

Commit

Permalink
feat: communication requests fixes (#158)
Browse files Browse the repository at this point in the history
* feat : displaying com requests from the most recent one to the oldest one by sorting by least recent status date com requests

* fix : using initialization timestamp instead of generating a new one

* fix : updated confirmation form adress format

* fix : fixed order and added delivery point

* fix: removed log

* fix: removed Fragment tags for each line

* fix: simplified algorithm for sorting com requests

* fix: removed useless comparator

* fix: sonar reliability issue, react components should not render non-boolean condition values
  • Loading branch information
prwozny authored Oct 18, 2024
1 parent 7f76da5 commit 80ffe4c
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 17 deletions.
36 changes: 25 additions & 11 deletions src/ui/SurveyUnit/Communication/CommunicationConfirmation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,12 @@ const CommunicationConfirmation = ({
saveCommunicationRequest,
bypassReasonLabel,
}: CommunicationConfirmationProps) => {
const address = getAddressData(surveyUnit.address);
const addressLines = Object.values(address).filter(v => !!v);
let address = getAddressData(surveyUnit.address);
const recipient = getprivilegedPerson(surveyUnit);
const { user } = useUser();

const userError = userSchema.safeParse(user);
const recipientError = recipientSchema.safeParse({ ...address, ...recipient });
const communicationError = communicationSchema.safeParse(communication);

const isValid = userError.success && recipientError.success && communicationError.success;

return (
Expand Down Expand Up @@ -89,12 +86,29 @@ const CommunicationConfirmation = ({
>
{getTitle(recipient.title)} {recipient.firstName} {recipient.lastName}
<br />
{addressLines.map(line => (
<Fragment key={line.toString()}>
{line}
<br />
</Fragment>
))}
<Fragment>
{address.deliveryPoint.length > 0 && (
<>
{address.deliveryPoint} <br />
</>
)}
{address.additionalAddress.length > 0 && (
<>
{address.additionalAddress} <br />
</>
)}
<>
{address.streetName} <br />
</>
{address.locality.length > 0 && (
<>
{address.locality} <br />
</>
)}
<>
{address.postCode}, {address.cityName}
</>
</Fragment>
</Typography>
{!recipientError.success && <ValidationError error={recipientError.error} mt={1} />}
</div>
Expand All @@ -115,7 +129,7 @@ const CommunicationConfirmation = ({
</Box>
</DialogContent>
<DialogActions>
<Button color="secondary" variant="contained" onClick={() => previousStep()}>
<Button color="white" variant="contained" onClick={() => previousStep()}>
{D.previousButton}
</Button>
<Button variant="contained" onClick={() => saveCommunicationRequest()} disabled={!isValid}>
Expand Down
23 changes: 18 additions & 5 deletions src/ui/SurveyUnit/Communication/CommunicationsCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Card from '@mui/material/Card/Card';
import CardContent from '@mui/material/CardContent';
import Stack from '@mui/material/Stack';
import D from 'i18n';
import { communicationMediumEnum } from 'utils/enum/CommunicationEnums';
import { communicationMediumEnum, communicationStatusEnum } from 'utils/enum/CommunicationEnums';
import { useToggle } from '../../../utils/hooks/useToggle';
import { Row } from '../../Row';
import { Typography } from '../../Typography';
Expand Down Expand Up @@ -40,6 +40,21 @@ export function CommunicationsCard({ surveyUnit }: Readonly<CommunicationsCardPr
surveyUnit.useLetterCommunication
);

// Sorting com requests by initialization date
const surveyUnitCommunicationRequests = surveyUnit.communicationRequests
?.map(comReq => {
const template = communicationTemplates.find(c => c.id === comReq.communicationTemplateId);
return { ...comReq, template };
})
.sort((a, b) => {
const dateA =
a.status.find(s => s.status === communicationStatusEnum.INITIATED.value)?.date ?? 0;
const dateB =
b.status.find(s => s.status === communicationStatusEnum.INITIATED.value)?.date ?? 0;

return dateB - dateA;
});

return (
<>
<Card>
Expand All @@ -60,11 +75,9 @@ export function CommunicationsCard({ surveyUnit }: Readonly<CommunicationsCardPr
{D.sendCommunication}
</Button>
<Stack gap={2}>
{surveyUnit.communicationRequests?.map(comReq => (
{surveyUnitCommunicationRequests?.map(comReq => (
<CommunicationItem
surveyUnitCommunicationTemplate={communicationTemplates.find(
c => c.id === comReq.communicationTemplateId
)}
surveyUnitCommunicationTemplate={comReq.template}
communication={comReq}
key={comReq.status[0].date ?? 1}
/>
Expand Down
2 changes: 1 addition & 1 deletion src/utils/api/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const formatSurveyUnitForPut = async (su: SurveyUnit) => {
comReq =>
<SurveyUnitNewCommunicationRequest>{
communicationTemplateId: comReq.communicationTemplateId,
creationTimestamp: new Date().getTime(),
creationTimestamp: comReq.status[0].date,
reason: comReq.reason,
}
);
Expand Down

0 comments on commit 80ffe4c

Please sign in to comment.