Skip to content

Commit

Permalink
feat: use own endpoint for requesting more information (#3523)
Browse files Browse the repository at this point in the history
  • Loading branch information
sirtawast authored Nov 12, 2024
1 parent 1a8467c commit 18406f6
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 15 deletions.
19 changes: 19 additions & 0 deletions backend/benefit/applications/api/v1/application_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -848,6 +848,25 @@ def clone_as_draft(self, request, pk) -> HttpResponse:
status=status.HTTP_201_CREATED,
)

@action(methods=["PATCH"], detail=True, url_path="require_additional_information")
@transaction.atomic
def require_additional_information(self, request, pk) -> HttpResponse:
application = self.get_object()
application_status = request.data["status"]
if application_status in [
ApplicationStatus.ADDITIONAL_INFORMATION_NEEDED,
ApplicationStatus.HANDLING,
]:
application.status = application_status
application.save()

return Response(
status=status.HTTP_200_OK,
)
return Response(
status=status.HTTP_400_BAD_REQUEST,
)

def _create_application_batch(self, status) -> QuerySet[Application]:
"""
Create a new application batch out of the existing applications in the given status
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { APPLICATION_ACTIONS } from 'benefit/handler/constants';
import { useApplicationActions } from 'benefit/handler/hooks/useApplicationActions';
import useRequireAdditionalInformation from 'benefit/handler/hooks/useRequireAdditionalInformation';
import { APPLICATION_STATUSES } from 'benefit-shared/constants';
import { Application } from 'benefit-shared/types/application';
import { Button, IconLock, IconPen } from 'hds-react';
Expand All @@ -13,23 +12,21 @@ export type Props = {
const EditAction: React.FC<Props> = ({ application }) => {
const translationsBase = 'common:review.actions';
const { t } = useTranslation();
const { updateStatus } = useApplicationActions(
application,
APPLICATION_ACTIONS.HANDLER_ALLOW_APPLICATION_EDIT
);

const [isUpdatingApplication, setIsUpdatingApplication] =
React.useState(false);
const {
mutate: requireAdditionalInformation,
isLoading: isUpdatingApplication,
} = useRequireAdditionalInformation();

const updateApplicationStatus = (status: APPLICATION_STATUSES): void => {
setIsUpdatingApplication(true);
updateStatus(status);
const updateApplicationStatus = (
status: APPLICATION_STATUSES.INFO_REQUIRED | APPLICATION_STATUSES.HANDLING
): void => {
requireAdditionalInformation({
id: application.id,
status,
});
};

React.useEffect(() => {
setIsUpdatingApplication(false);
}, [application.status]);

return (
<>
{application.status === APPLICATION_STATUSES.HANDLING && (
Expand All @@ -41,6 +38,7 @@ const EditAction: React.FC<Props> = ({ application }) => {
variant="secondary"
size="small"
iconLeft={<IconPen />}
loadingText={t(`${translationsBase}.handlingToInfoRequired`)}
isLoading={isUpdatingApplication}
>
{t(`${translationsBase}.handlingToInfoRequired`)}
Expand All @@ -53,6 +51,7 @@ const EditAction: React.FC<Props> = ({ application }) => {
variant="secondary"
size="small"
iconLeft={<IconLock />}
loadingText={t(`${translationsBase}.infoRequiredToHandling`)}
isLoading={isUpdatingApplication}
>
{t(`${translationsBase}.infoRequiredToHandling`)}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { AxiosError } from 'axios';
import { HandlerEndpoint } from 'benefit-shared/backend-api/backend-api';
import { APPLICATION_STATUSES } from 'benefit-shared/constants';
import { useTranslation } from 'next-i18next';
import { useMutation, UseMutationResult, useQueryClient } from 'react-query';
import showErrorToast from 'shared/components/toast/show-error-toast';
import useBackendAPI from 'shared/hooks/useBackendAPI';

type Payload = {
id: string;
status: APPLICATION_STATUSES.INFO_REQUIRED | APPLICATION_STATUSES.HANDLING;
};

const useRequireAdditionalInformation = (): UseMutationResult<
null,
Error,
Payload
> => {
const { axios, handleResponse } = useBackendAPI();
const queryClient = useQueryClient();
const { t } = useTranslation();

return useMutation<null, Error, Payload>(
'require_additional_information',
({ id, status }: Payload) =>
handleResponse(
axios.patch(
`${HandlerEndpoint.HANDLER_REQUIRE_ADDITIONAL_INFORMATION(id)}`,
{ status }
)
),
{
onSuccess: () => {
void queryClient.invalidateQueries('application');
void queryClient.invalidateQueries('applications');
},
onError: (error: AxiosError<Error, Record<string, string[]>>) => {
showErrorToast(
t('common:error.generic.label'),
t('common:error.generic.text')
);
// eslint-disable-next-line no-console
console.log(error);
},
}
);
};

export default useRequireAdditionalInformation;
2 changes: 2 additions & 0 deletions frontend/benefit/shared/src/backend-api/backend-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ export const HandlerEndpoint = {
`${BackendEndpoint.HANDLER_APPLICATIONS}batch_p2p_file?batch_id=${id}`,
HANDLER_APPLICATIONS_CLONE_AS_DRAFT: (id: string) =>
`${handlerApplicationsBase(id)}clone_as_draft/`,
HANDLER_REQUIRE_ADDITIONAL_INFORMATION: (id: string) =>
`${handlerApplicationsBase(id)}require_additional_information/`,
HANDLER_INSTALMENT_STATUS_TRANSITION: (id: string) =>
`${handlerInstalmentBase(id)}`,
} as const;
Expand Down

0 comments on commit 18406f6

Please sign in to comment.