Skip to content

Commit

Permalink
Merge pull request #118 from wri/feat/TM-741-workdays-be
Browse files Browse the repository at this point in the history
[TM-741] Turn Workdays into a BE concern
  • Loading branch information
roguenet authored Apr 17, 2024
2 parents f630697 + 0150d92 commit 4589d31
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 193 deletions.
60 changes: 24 additions & 36 deletions src/components/elements/Inputs/DataTable/RHFWorkdaysTable.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { AccessorKeyColumnDef } from "@tanstack/react-table";
import { useT } from "@transifex/react";
import { PropsWithChildren } from "react";
import { remove } from "lodash";
import { PropsWithChildren, useCallback } from "react";
import { useController, UseControllerProps, UseFormReturn } from "react-hook-form";
import { v4 as uuidv4 } from "uuid";
import * as yup from "yup";

import { FieldType } from "@/components/extensive/WizardForm/types";
import { getAgeOptions } from "@/constants/options/age";
import { getGenderOptionsWithUndefined } from "@/constants/options/gender";
import { getIndigeneityOptionsWithUndefined } from "@/constants/options/indigeneity";
import { useDeleteV2WorkdaysUUID, usePostV2Workdays } from "@/generated/apiComponents";
import { Entity, Option } from "@/types/common";
import { formatOptionsList } from "@/utils/options";

Expand Down Expand Up @@ -64,46 +65,33 @@ const RHFWorkdaysTable = ({
...props
}: PropsWithChildren<RHFWorkdaysTableProps>) => {
const t = useT();
const { field } = useController(props);
const value = field?.value || [];
const {
field: { value, onChange }
} = useController(props);

const { mutate: createStrata } = usePostV2Workdays({
onSuccess(data) {
const _tmp = [...value];
//@ts-ignore
_tmp.push(data.data);
field.onChange(_tmp);
}
});
const createWorkday = useCallback(
(data: any) => {
onChange([...(value ?? []), { ...data, uuid: uuidv4(), collection }]);
},
[value, onChange]
);

const { mutate: removeStrata } = useDeleteV2WorkdaysUUID({
onSuccess(data, variables) {
//@ts-ignore
_.remove(value, v => v.uuid === variables.pathParams.uuid);
field.onChange(value);
}
});
const deleteWorkday = useCallback(
(uuid: string | undefined) => {
if (uuid != null) {
remove(value, (v: any) => v.uuid === uuid);
onChange(value);
}
},
[value, onChange]
);

return (
<DataTable
{...props}
value={value}
handleCreate={data => {
createStrata({
body: {
...data,
collection,
model_type: entity?.entityName,
//@ts-ignore
model_uuid: entity?.entityUUID
}
});
}}
handleDelete={uuid => {
if (uuid) {
removeStrata({ pathParams: { uuid } });
}
}}
value={value ?? []}
handleCreate={createWorkday}
handleDelete={deleteWorkday}
addButtonCaption={t("Add Workdays")}
tableColumns={getWorkdaysTableColumns(t, ethnicityOptions)}
fields={[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ const RHFTreeSpeciesInput = (props: PropsWithChildren<RHFTreeSpeciesInputProps>)
const createTreeSpecies = useCallback(
(treeValue: TreeSpeciesValue) => {
onChange([...(value ?? []), treeValue]);
// Specifically avoiding `onChangeCapture()` here because it's too easy to lose progress
// typing a name or amount when the server response comes back.
formHook?.clearErrors(props.name);
},
[value, onChange, formHook]
Expand Down
135 changes: 0 additions & 135 deletions src/generated/apiComponents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8119,141 +8119,6 @@ export const useGetV2UpdateRequestsENTITYUUID = <TData = GetV2UpdateRequestsENTI
);
};

export type PostV2WorkdaysError = Fetcher.ErrorWrapper<undefined>;

export type PostV2WorkdaysResponse = {
uuid?: string;
amount?: number;
collection?: string;
gender?: string;
age?: string;
ethnicity?: string;
indigeneity?: string;
};

export type PostV2WorkdaysRequestBody = {
model_type?: string;
model_uuid?: string;
amount?: number;
collection?: string;
gender?: string;
age?: string;
ethnicity?: string;
indigeneity?: string;
};

export type PostV2WorkdaysVariables = {
body?: PostV2WorkdaysRequestBody;
} & ApiContext["fetcherOptions"];

export const fetchPostV2Workdays = (variables: PostV2WorkdaysVariables, signal?: AbortSignal) =>
apiFetch<PostV2WorkdaysResponse, PostV2WorkdaysError, PostV2WorkdaysRequestBody, {}, {}, {}>({
url: "/v2/workdays",
method: "post",
...variables,
signal
});

export const usePostV2Workdays = (
options?: Omit<
reactQuery.UseMutationOptions<PostV2WorkdaysResponse, PostV2WorkdaysError, PostV2WorkdaysVariables>,
"mutationFn"
>
) => {
const { fetcherOptions } = useApiContext();
return reactQuery.useMutation<PostV2WorkdaysResponse, PostV2WorkdaysError, PostV2WorkdaysVariables>(
(variables: PostV2WorkdaysVariables) => fetchPostV2Workdays({ ...fetcherOptions, ...variables }),
options
);
};

export type PatchV2WorkdaysUUIDPathParams = {
uuid: string;
};

export type PatchV2WorkdaysUUIDError = Fetcher.ErrorWrapper<undefined>;

export type PatchV2WorkdaysUUIDResponse = {
data?: {
uuid?: string;
amount?: number;
collection?: string;
gender?: string;
age?: string;
ethnicity?: string;
indigeneity?: string;
}[];
links?: {
first?: string;
last?: string;
prev?: string;
next?: string;
};
meta?: {
current_page?: number;
from?: number;
last_page?: number;
next?: number;
unfiltered_total?: number;
};
};

export type PatchV2WorkdaysUUIDVariables = {
pathParams: PatchV2WorkdaysUUIDPathParams;
} & ApiContext["fetcherOptions"];

export const fetchPatchV2WorkdaysUUID = (variables: PatchV2WorkdaysUUIDVariables, signal?: AbortSignal) =>
apiFetch<PatchV2WorkdaysUUIDResponse, PatchV2WorkdaysUUIDError, undefined, {}, {}, PatchV2WorkdaysUUIDPathParams>({
url: "/v2/workdays/{uuid}",
method: "patch",
...variables,
signal
});

export const usePatchV2WorkdaysUUID = (
options?: Omit<
reactQuery.UseMutationOptions<PatchV2WorkdaysUUIDResponse, PatchV2WorkdaysUUIDError, PatchV2WorkdaysUUIDVariables>,
"mutationFn"
>
) => {
const { fetcherOptions } = useApiContext();
return reactQuery.useMutation<PatchV2WorkdaysUUIDResponse, PatchV2WorkdaysUUIDError, PatchV2WorkdaysUUIDVariables>(
(variables: PatchV2WorkdaysUUIDVariables) => fetchPatchV2WorkdaysUUID({ ...fetcherOptions, ...variables }),
options
);
};

export type DeleteV2WorkdaysUUIDPathParams = {
uuid: string;
};

export type DeleteV2WorkdaysUUIDError = Fetcher.ErrorWrapper<undefined>;

export type DeleteV2WorkdaysUUIDVariables = {
pathParams: DeleteV2WorkdaysUUIDPathParams;
} & ApiContext["fetcherOptions"];

export const fetchDeleteV2WorkdaysUUID = (variables: DeleteV2WorkdaysUUIDVariables, signal?: AbortSignal) =>
apiFetch<undefined, DeleteV2WorkdaysUUIDError, undefined, {}, {}, DeleteV2WorkdaysUUIDPathParams>({
url: "/v2/workdays/{uuid}",
method: "delete",
...variables,
signal
});

export const useDeleteV2WorkdaysUUID = (
options?: Omit<
reactQuery.UseMutationOptions<undefined, DeleteV2WorkdaysUUIDError, DeleteV2WorkdaysUUIDVariables>,
"mutationFn"
>
) => {
const { fetcherOptions } = useApiContext();
return reactQuery.useMutation<undefined, DeleteV2WorkdaysUUIDError, DeleteV2WorkdaysUUIDVariables>(
(variables: DeleteV2WorkdaysUUIDVariables) => fetchDeleteV2WorkdaysUUID({ ...fetcherOptions, ...variables }),
options
);
};

export type GetV2WorkdaysENTITYUUIDPathParams = {
/**
* allowed values project/site/nursery/project-reports/site-reports/nursery-reports
Expand Down
20 changes: 0 additions & 20 deletions src/generated/apiSchemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4941,26 +4941,6 @@ export type V2WorkdayRead = {
indigeneity?: string;
};

export type V2WorkdayCreate = {
model_type?: string;
model_uuid?: string;
amount?: number;
collection?: string;
gender?: string;
age?: string;
ethnicity?: string;
indigeneity?: string;
};

export type V2WorkdayUpdate = {
amount?: number;
collection?: string;
gender?: string;
age?: string;
ethnicity?: string;
indigeneity?: string;
};

export type V2WorkdaysPaginated = {
data?: {
uuid?: string;
Expand Down

0 comments on commit 4589d31

Please sign in to comment.