Skip to content

Commit

Permalink
JobFunctionForm use React Query
Browse files Browse the repository at this point in the history
Signed-off-by: Gilles Dubreuil <[email protected]>
  • Loading branch information
gildub committed Jul 26, 2023
1 parent 14a8766 commit caefe27
Show file tree
Hide file tree
Showing 10 changed files with 166 additions and 195 deletions.
55 changes: 24 additions & 31 deletions client/src/app/api/rest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,37 +163,6 @@ export const getBusinessServiceById = (
return APIClient.get(`${BUSINESS_SERVICES}/${id}`);
};

// Job functions

export enum JobFunctionSortBy {
NAME,
}

export interface JobFunctionSortByQuery {
field: JobFunctionSortBy;
direction?: Direction;
}

export const getJobFunctions = (): AxiosPromise<JobFunction[]> => {
return APIClient.get(`${JOB_FUNCTIONS}`, jsonHeaders);
};

export const createJobFunction = (
obj: New<JobFunction>
): AxiosPromise<JobFunction> => {
return APIClient.post(`${JOB_FUNCTIONS}`, obj);
};

export const updateJobFunction = (
obj: JobFunction
): AxiosPromise<JobFunction> => {
return APIClient.put(`${JOB_FUNCTIONS}/${obj.id}`, obj);
};

export const deleteJobFunction = (id: number): AxiosPromise => {
return APIClient.delete(`${JOB_FUNCTIONS}/${id}`);
};

// App inventory

export const updateAllApplications = (
Expand Down Expand Up @@ -721,6 +690,30 @@ export const updateStakeholderGroup = (
): Promise<StakeholderGroup> =>
axios.put(`${STAKEHOLDER_GROUPS}/${obj.id}`, obj);

// Job functions

export enum JobFunctionSortBy {
NAME,
}

export interface JobFunctionSortByQuery {
field: JobFunctionSortBy;
direction?: Direction;
}

export const getJobFunctions = (): Promise<JobFunction[]> =>
axios.get(JOB_FUNCTIONS).then((response) => response.data);

Check warning on line 705 in client/src/app/api/rest.ts

View check run for this annotation

Codecov / codecov/patch

client/src/app/api/rest.ts#L705

Added line #L705 was not covered by tests

export const createJobFunction = (
obj: New<JobFunction>
): Promise<JobFunction> => axios.post(JOB_FUNCTIONS, obj);

Check warning on line 709 in client/src/app/api/rest.ts

View check run for this annotation

Codecov / codecov/patch

client/src/app/api/rest.ts#L709

Added line #L709 was not covered by tests

export const updateJobFunction = (obj: JobFunction): Promise<JobFunction> =>
axios.put(`${JOB_FUNCTIONS}/${obj.id}`, obj);

Check warning on line 712 in client/src/app/api/rest.ts

View check run for this annotation

Codecov / codecov/patch

client/src/app/api/rest.ts#L712

Added line #L712 was not covered by tests

export const deleteJobFunction = (id: number): Promise<JobFunction> =>
axios.delete(`${JOB_FUNCTIONS}/${id}`);

Check warning on line 715 in client/src/app/api/rest.ts

View check run for this annotation

Codecov / codecov/patch

client/src/app/api/rest.ts#L715

Added line #L715 was not covered by tests

// Tags

export const getTags = (): Promise<Tag[]> =>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,41 +1,42 @@
import React, { useState } from "react";
import React from "react";
import { useTranslation } from "react-i18next";
import { AxiosError, AxiosPromise, AxiosResponse } from "axios";
import { AxiosError, AxiosResponse } from "axios";
import { object, string } from "yup";

import {
ActionGroup,
Alert,
Button,
ButtonVariant,
Form,
} from "@patternfly/react-core";

import { createJobFunction, updateJobFunction } from "@app/api/rest";
import { JobFunction, New } from "@app/api/models";
import { duplicateNameCheck, getAxiosErrorMessage } from "@app/utils/utils";
import { useFetchJobFunctions } from "@app/queries/jobfunctions";
import { duplicateNameCheck } from "@app/utils/utils";
import {
useCreateJobFunctionMutation,
useFetchJobFunctions,
useUpdateJobFunctionMutation,
} from "@app/queries/jobfunctions";
import { yupResolver } from "@hookform/resolvers/yup";
import { useForm } from "react-hook-form";
import { HookFormPFTextInput } from "@app/shared/components/hook-form-pf-fields";
import { NotificationsContext } from "@app/shared/notifications-context";

export interface FormValues {
name: string;
}

export interface JobFunctionFormProps {
jobFunction?: JobFunction;
onSaved: (response: AxiosResponse<JobFunction>) => void;
onCancel: () => void;
onClose: () => void;
}

export const JobFunctionForm: React.FC<JobFunctionFormProps> = ({
jobFunction,
onSaved,
onCancel,
onClose,
}) => {
const { t } = useTranslation();
const [error, setError] = useState<AxiosError>();
const { pushNotification } = React.useContext(NotificationsContext);
const { jobFunctions } = useFetchJobFunctions();

const validationSchema = object().shape({
Expand Down Expand Up @@ -69,35 +70,70 @@ export const JobFunctionForm: React.FC<JobFunctionFormProps> = ({
mode: "onChange",
});

const onCreateJobFunctionSuccess = (response: AxiosResponse<JobFunction>) => {
pushNotification({
title: t("toastr.success.createWhat", {
type: t("terms.jobFunction"),
what: response.data.name,
}),
variant: "success",
});
onClose();
};

const onUpdateJobFunctionSuccess = (response: AxiosResponse<JobFunction>) => {
pushNotification({
title: t("toastr.success.save", {
type: t("terms.jobFunction"),
}),
variant: "success",
});
onClose();
};

const onCreateJobFunctionError = (error: AxiosError) => {
pushNotification({
title: t("toastr.fail.create", {
type: t("terms.jobFunction").toLowerCase(),
}),
variant: "danger",
});
};

const { mutate: createJobFunction } = useCreateJobFunctionMutation(
onCreateJobFunctionSuccess,
onCreateJobFunctionError
);

const onUpdateJobFunctionError = (error: AxiosError) => {
pushNotification({
title: t("toastr.fail.save", {
type: t("terms.jobFunction").toLowerCase(),
}),
variant: "danger",
});
};

const { mutate: updateJobFunction } = useUpdateJobFunctionMutation(
onUpdateJobFunctionSuccess,
onUpdateJobFunctionError
);

const onSubmit = (formValues: FormValues) => {
const payload: New<JobFunction> = {
name: formValues.name.trim(),
};

let promise: AxiosPromise<JobFunction>;
if (jobFunction) {
promise = updateJobFunction({
id: jobFunction.id,
...payload,
});
updateJobFunction({ id: jobFunction.id, ...payload });
} else {
promise = createJobFunction(payload);
createJobFunction(payload);
}

promise
.then((response) => {
onSaved(response);
})
.catch((error) => {
setError(error);
});
onClose();
};

return (
<Form onSubmit={handleSubmit(onSubmit)}>
{error && (
<Alert variant="danger" isInline title={getAxiosErrorMessage(error)} />
)}
<HookFormPFTextInput
control={control}
name="name"
Expand All @@ -121,7 +157,7 @@ export const JobFunctionForm: React.FC<JobFunctionFormProps> = ({
aria-label="cancel"
variant={ButtonVariant.link}
isDisabled={isSubmitting || isValidating}
onClick={onCancel}
onClick={onClose}
>
{t("actions.cancel")}
</Button>
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit caefe27

Please sign in to comment.