Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,6 @@ import {
renderFormHydrationField,
} from "@/utils/formHydration";
import { updateDarApplicationAnswersAction } from "@/app/actions/updateDarApplicationAnswers";
import { updateDarApplicationTeamAction } from "@/app/actions/updateDarApplicationTeam";
import { updateDarApplicationUserAction } from "@/app/actions/updateDarApplicationUser";
import notFound from "@/app/not-found";
import { DarActionBar } from "./DarActionBar";
import DarFieldArray from "./DarFieldArray";
Expand Down Expand Up @@ -212,7 +210,9 @@ const ApplicationSection = ({
? formData[PROJECT_TITLE_FIELD]
: getValues(PROJECT_TITLE_FIELD),
applicant_id: data.applicant_id,
submission_status: DarApplicationStatus.DRAFT,
submission_status: formData
? DarApplicationStatus.SUBMITTED
: DarApplicationStatus.DRAFT,
};

const values = formData ?? getValues();
Expand All @@ -224,48 +224,34 @@ const ApplicationSection = ({
excludedQuestionFields
);

const resAnswers = await updateDarApplicationAnswersAction(
applicationId,
userId,
{
...applicationData,
answers,
}
);

if (formData) {
const [resAnswers, resApplication] = await Promise.all([
updateDarApplicationAnswersAction(applicationId, userId, {
...applicationData,
answers,
}),
isResearcher
? updateDarApplicationUserAction(applicationId, userId, {
submission_status: DarApplicationStatus.SUBMITTED,
})
: teamId &&
updateDarApplicationTeamAction(applicationId, teamId, {
submission_status: DarApplicationStatus.SUBMITTED,
}),
]);

if (resAnswers && resApplication) {
if (resAnswers) {
notificationService.apiSuccess(
"Data Access Request submitted successfully"
);
push(
`/${RouteName.ACCOUNT}/${RouteName.PROFILE}/${RouteName.DATA_ACCESS_REQUESTS}/${RouteName.APPLICATIONS}`
);
} else {
notificationService.apiError("Failed to submit application");
}
} else if (resAnswers) {
notificationService.apiSuccess(
"Successfully updated Data Access Request"
);
} else {
const resAnswers = await updateDarApplicationAnswersAction(
applicationId,
userId,
{
...applicationData,
answers,
}
notificationService.apiError(
"Failed to update Data Access Request"
);

if (resAnswers) {
notificationService.apiSuccess(
"Successfully updated Data Access Request"
);
} else {
notificationService.apiError(
"Failed to update Data Access Request"
);
}
}
};

Expand All @@ -277,6 +263,10 @@ const ApplicationSection = ({
await saveApplication();
};

const handleInvalidSubmit = () => {
notificationService.apiError(t("missingRequiredFields"));
};

const handleManageApplication = () => {
showDialog(DarManageDialog, { darApplicationEndpoint, applicationId });
};
Expand All @@ -287,6 +277,12 @@ const ApplicationSection = ({
teamApplication &&
teamApplication?.approval_status !== null);

const isApplicationEditable =
!teamApplication ||
(teamApplication?.approval_status === null &&
teamApplication.submission_status !==
DarApplicationStatus.SUBMITTED);

const renderSectionHeader = (field: DarFormattedField) => (
<>
<Box sx={{ pl: 3, pr: 3 }}>
Expand Down Expand Up @@ -566,8 +562,12 @@ const ApplicationSection = ({
teamId={teamId}
userId={userId}
saveDraftOnClick={handleSaveAsDraft}
submitOnClick={handleSubmit(handleSave)}
submitOnClick={handleSubmit(
handleSave,
handleInvalidSubmit
)}
isResearcher={isResearcher}
showSaveDraft={isApplicationEditable}
manageApplicationOnStatus={handleManageApplication}
/>

Expand Down Expand Up @@ -799,14 +799,12 @@ const ApplicationSection = ({
)}

<Box sx={{ gap: 1, p: 0, display: "flex" }}>
{isResearcher &&
(!teamApplication ||
(teamApplication?.approval_status ===
null &&
teamApplication.submission_status !==
DarApplicationStatus.SUBMITTED)) && (
{isResearcher && isApplicationEditable && (
<Button
onClick={handleSubmit(handleSave)}
onClick={handleSubmit(
handleSave,
handleInvalidSubmit
)}
type="submit"
variant="outlined"
color="secondary">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const defaultProps = {
submitOnClick: jest.fn(),
manageApplicationOnStatus: jest.fn(),
isResearcher: true,
showSaveDraft: true,
};

const mockData = {
Expand Down Expand Up @@ -55,4 +56,21 @@ describe("DarActionBar", () => {
expect(await screen.findByText("Dataset Alpha")).toBeInTheDocument();
expect(await screen.findByText("Dataset Beta")).toBeInTheDocument();
});

it("renders the save draft button when showSaveDraft is true", async () => {
mockFetch(mockData);
render(<DarActionBar {...defaultProps} />);
expect(
await screen.findByRole("button", { name: "Save draft" })
).toBeInTheDocument();
});

it("does not render the save draft button when showSaveDraft is false", async () => {
mockFetch(mockData);
render(<DarActionBar {...defaultProps} showSaveDraft={false} />);
expect(await screen.findByText("My Research Project")).toBeInTheDocument();
expect(
screen.queryByRole("button", { name: "Save draft" })
).not.toBeInTheDocument();
});
})
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ interface DarFormHeaderProps {
submitOnClick: () => Promise<void> | void | undefined;
manageApplicationOnStatus: () => Promise<void> | void | undefined;
isResearcher: boolean;
showSaveDraft: boolean;
}

const DarActionBar = ({
Expand All @@ -44,6 +45,7 @@ const DarActionBar = ({
submitOnClick,
manageApplicationOnStatus,
isResearcher,
showSaveDraft,
}: DarFormHeaderProps) => {
const idTitle = `DAR Application ${applicationId}`;
const t = useTranslations(TRANSLATION_PATH);
Expand Down Expand Up @@ -151,9 +153,13 @@ const DarActionBar = ({
sx={{ my: 2, ml: 2 }}>
{isResearcher ? (
<>
<Button color="greyCustom" onClick={saveDraftOnClick}>
{t("saveDraft")}
</Button>
{showSaveDraft && (
<Button
color="greyCustom"
onClick={saveDraftOnClick}>
{t("saveDraft")}
</Button>
)}
<Button color="primary" onClick={submitOnClick}>
{t("submitApplication")}
</Button>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ const requests = [
generateCohortRequestV1({
request_status: "APPROVED",
nhse_sde_request_status: "IN PROCESS",
created_at: "2025-01-15T00:00:00.000Z",
updated_at: "2025-06-20T00:00:00.000Z",
}),
generateCohortRequestV1({
request_status: "REJECTED",
Expand Down
10 changes: 9 additions & 1 deletion src/components/DarStatusTracker/DarStatusTracker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,12 @@ export default function DarStatusTracker({
statuses,
}: DarStatusTrackerProps) {
const t = useTranslations(TRANSLATION_PATH);
const orderedStatuses = statuses.includes(DarApplicationStatus.DRAFT)
? statuses
: [DarApplicationStatus.DRAFT, ...statuses];

const formattedStatuses = [
...statuses,
...orderedStatuses,
approvalStatus &&
approvalStatus !== DarApplicationApprovalStatus.FEEDBACK
? approvalStatus
Expand Down Expand Up @@ -53,6 +57,10 @@ export default function DarStatusTracker({
mb: 3,
}}>
{formattedStatuses.map((status, index) => {
if (status === DarApplicationStatus.DRAFT) {
return null;
}

const isActive = index === activeIndex;
const isFuture = index > activeIndex;

Expand Down
4 changes: 3 additions & 1 deletion src/config/messages/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -2112,7 +2112,9 @@
"rejectedButtonText": "Reject",
"changeStatus": "Change Application Status",
"intro": "Use the options below to log your decision about this application.",
"defaultDraftMessage": "This is an automated notification that the application has been changed from 'In review' to 'Draft' status. No comment has been added by the Data Custodian."
"defaultDraftMessage": "This is an automated notification that the application has been changed from 'In review' to 'Draft' status. No comment has been added by the Data Custodian.",
"statusUpdateSuccess": "Application status updated successfully",
"statusUpdateError": "Failed to update application status"
},
"DarActionDialog": {
"actionPermanent": "I understand this action is permanent",
Expand Down
6 changes: 5 additions & 1 deletion src/modules/DarManageDialog/DarManageDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import Form from "@/components/Form";
import InputWrapper from "@/components/InputWrapper";
import Typography from "@/components/Typography";
import useModal from "@/hooks/useModal";
import notificationService from "@/services/notification";
import { inputComponents } from "@/config/forms";
import { colors } from "@/config/theme";
import { CACHE_DAR_REVIEWS } from "@/consts/cache";
Expand Down Expand Up @@ -86,12 +87,15 @@ const DarManageDialog = ({ applicationId }: DarManageDialogProps) => {
payload
);

revalidateCacheAction(`${CACHE_DAR_REVIEWS}${applicationId}`);
await revalidateCacheAction(`${CACHE_DAR_REVIEWS}${applicationId}`);

hideModal();

if (updateResponse) {
notificationService.apiSuccess(t("statusUpdateSuccess"));
push(redirectUrl);
} else {
notificationService.apiError(t("statusUpdateError"));
}
};

Expand Down
Loading