2023-03-15 05:30:05 +00:00
|
|
|
import { useEffect } from "react";
|
|
|
|
|
2023-07-26 12:21:26 +00:00
|
|
|
import { useRouter } from "next/router";
|
|
|
|
|
|
|
|
import useSWR from "swr";
|
|
|
|
|
|
|
|
// react-hook-form
|
2023-03-15 05:30:05 +00:00
|
|
|
import { useForm } from "react-hook-form";
|
2023-07-26 12:21:26 +00:00
|
|
|
// services
|
|
|
|
import stateService from "services/state.service";
|
|
|
|
// hooks
|
|
|
|
import useProjectMembers from "hooks/use-project-members";
|
|
|
|
// components
|
|
|
|
import { FiltersList } from "components/core";
|
|
|
|
import { SelectFilters } from "components/views";
|
2023-03-15 05:30:05 +00:00
|
|
|
// ui
|
2023-03-30 11:29:07 +00:00
|
|
|
import { Input, PrimaryButton, SecondaryButton, TextArea } from "components/ui";
|
2023-07-26 12:21:26 +00:00
|
|
|
// helpers
|
|
|
|
import { checkIfArraysHaveSameElements } from "helpers/array.helper";
|
|
|
|
import { getStatesList } from "helpers/state.helper";
|
2023-03-15 05:30:05 +00:00
|
|
|
// types
|
2023-07-26 12:21:26 +00:00
|
|
|
import { IQuery, IView } from "types";
|
|
|
|
import issuesService from "services/issues.service";
|
2023-09-25 06:17:06 +00:00
|
|
|
import { IWorkspaceView } from "types/workspace-view";
|
2023-07-26 12:21:26 +00:00
|
|
|
// fetch-keys
|
|
|
|
import { PROJECT_ISSUE_LABELS, STATES_LIST } from "constants/fetch-keys";
|
2023-03-15 05:30:05 +00:00
|
|
|
|
|
|
|
type Props = {
|
|
|
|
handleFormSubmit: (values: IView) => Promise<void>;
|
|
|
|
handleClose: () => void;
|
|
|
|
status: boolean;
|
2023-09-25 06:17:06 +00:00
|
|
|
data?: IView | IWorkspaceView | null;
|
2023-04-17 06:54:30 +00:00
|
|
|
preLoadedData?: Partial<IView> | null;
|
2023-03-15 05:30:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const defaultValues: Partial<IView> = {
|
|
|
|
name: "",
|
|
|
|
description: "",
|
|
|
|
};
|
|
|
|
|
2023-04-17 06:54:30 +00:00
|
|
|
export const ViewForm: React.FC<Props> = ({
|
|
|
|
handleFormSubmit,
|
|
|
|
handleClose,
|
|
|
|
status,
|
|
|
|
data,
|
|
|
|
preLoadedData,
|
|
|
|
}) => {
|
2023-07-26 12:21:26 +00:00
|
|
|
const router = useRouter();
|
|
|
|
const { workspaceSlug, projectId } = router.query;
|
|
|
|
|
2023-03-15 05:30:05 +00:00
|
|
|
const {
|
|
|
|
register,
|
|
|
|
formState: { errors, isSubmitting },
|
|
|
|
handleSubmit,
|
|
|
|
reset,
|
2023-03-18 06:04:29 +00:00
|
|
|
watch,
|
|
|
|
setValue,
|
2023-03-15 05:30:05 +00:00
|
|
|
} = useForm<IView>({
|
|
|
|
defaultValues,
|
|
|
|
});
|
2023-04-12 10:03:21 +00:00
|
|
|
const filters = watch("query");
|
2023-03-15 05:30:05 +00:00
|
|
|
|
2023-07-26 12:21:26 +00:00
|
|
|
const { data: stateGroups } = useSWR(
|
2023-07-27 16:08:16 +00:00
|
|
|
workspaceSlug && projectId && (filters?.state ?? []).length > 0
|
2023-07-26 12:21:26 +00:00
|
|
|
? STATES_LIST(projectId as string)
|
|
|
|
: null,
|
2023-07-27 16:08:16 +00:00
|
|
|
workspaceSlug && (filters?.state ?? []).length > 0
|
2023-07-26 12:21:26 +00:00
|
|
|
? () => stateService.getStates(workspaceSlug as string, projectId as string)
|
|
|
|
: null
|
|
|
|
);
|
2023-07-26 17:50:44 +00:00
|
|
|
const states = getStatesList(stateGroups);
|
2023-07-26 12:21:26 +00:00
|
|
|
|
|
|
|
const { data: labels } = useSWR(
|
2023-07-27 16:08:16 +00:00
|
|
|
workspaceSlug && projectId && (filters?.labels ?? []).length > 0
|
2023-07-26 12:21:26 +00:00
|
|
|
? PROJECT_ISSUE_LABELS(projectId.toString())
|
|
|
|
: null,
|
2023-07-27 16:08:16 +00:00
|
|
|
workspaceSlug && projectId && (filters?.labels ?? []).length > 0
|
2023-07-26 12:21:26 +00:00
|
|
|
? () => issuesService.getIssueLabels(workspaceSlug.toString(), projectId.toString())
|
|
|
|
: null
|
|
|
|
);
|
|
|
|
const { members } = useProjectMembers(workspaceSlug?.toString(), projectId?.toString());
|
|
|
|
|
2023-03-15 05:30:05 +00:00
|
|
|
const handleCreateUpdateView = async (formData: IView) => {
|
|
|
|
await handleFormSubmit(formData);
|
|
|
|
|
|
|
|
reset({
|
|
|
|
...defaultValues,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2023-07-26 12:21:26 +00:00
|
|
|
const clearAllFilters = () => {
|
|
|
|
setValue("query", {
|
|
|
|
assignees: null,
|
|
|
|
created_by: null,
|
|
|
|
labels: null,
|
|
|
|
priority: null,
|
|
|
|
state: null,
|
2023-08-24 14:15:23 +00:00
|
|
|
start_date: null,
|
2023-07-26 12:21:26 +00:00
|
|
|
target_date: null,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2023-03-15 05:30:05 +00:00
|
|
|
useEffect(() => {
|
|
|
|
reset({
|
|
|
|
...defaultValues,
|
2023-04-17 06:54:30 +00:00
|
|
|
...preLoadedData,
|
2023-03-15 05:30:05 +00:00
|
|
|
...data,
|
|
|
|
});
|
2023-04-17 06:54:30 +00:00
|
|
|
}, [data, preLoadedData, reset]);
|
2023-03-15 05:30:05 +00:00
|
|
|
|
2023-03-16 08:37:19 +00:00
|
|
|
useEffect(() => {
|
2023-04-12 10:03:21 +00:00
|
|
|
if (status && data) {
|
|
|
|
setValue("query", data.query_data);
|
|
|
|
}
|
|
|
|
}, [data, status, setValue]);
|
2023-03-18 06:04:29 +00:00
|
|
|
|
2023-03-15 05:30:05 +00:00
|
|
|
return (
|
|
|
|
<form onSubmit={handleSubmit(handleCreateUpdateView)}>
|
|
|
|
<div className="space-y-5">
|
2023-07-10 07:17:00 +00:00
|
|
|
<h3 className="text-lg font-medium leading-6 text-custom-text-100">
|
2023-03-15 05:30:05 +00:00
|
|
|
{status ? "Update" : "Create"} View
|
|
|
|
</h3>
|
|
|
|
<div className="space-y-3">
|
|
|
|
<div>
|
|
|
|
<Input
|
|
|
|
id="name"
|
|
|
|
name="name"
|
|
|
|
type="name"
|
2023-04-12 10:03:21 +00:00
|
|
|
placeholder="Title"
|
2023-03-15 05:30:05 +00:00
|
|
|
autoComplete="off"
|
2023-04-12 10:03:21 +00:00
|
|
|
className="resize-none text-xl"
|
2023-03-15 05:30:05 +00:00
|
|
|
error={errors.name}
|
|
|
|
register={register}
|
|
|
|
validations={{
|
2023-04-12 10:03:21 +00:00
|
|
|
required: "Title is required",
|
2023-03-15 05:30:05 +00:00
|
|
|
maxLength: {
|
|
|
|
value: 255,
|
2023-04-12 10:03:21 +00:00
|
|
|
message: "Title should be less than 255 characters",
|
2023-03-15 05:30:05 +00:00
|
|
|
},
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div>
|
|
|
|
<TextArea
|
|
|
|
id="description"
|
|
|
|
name="description"
|
2023-04-12 10:03:21 +00:00
|
|
|
placeholder="Description"
|
|
|
|
className="h-32 resize-none text-sm"
|
2023-03-15 05:30:05 +00:00
|
|
|
error={errors.description}
|
|
|
|
register={register}
|
|
|
|
/>
|
|
|
|
</div>
|
2023-03-18 06:04:29 +00:00
|
|
|
<div>
|
2023-03-23 06:31:50 +00:00
|
|
|
<SelectFilters
|
|
|
|
filters={filters}
|
2023-03-18 06:04:29 +00:00
|
|
|
onSelect={(option) => {
|
|
|
|
const key = option.key as keyof typeof filters;
|
|
|
|
|
2023-08-24 14:15:23 +00:00
|
|
|
if (key === "start_date" || key === "target_date") {
|
2023-07-26 12:21:26 +00:00
|
|
|
const valueExists = checkIfArraysHaveSameElements(
|
2023-08-24 14:15:23 +00:00
|
|
|
filters?.[key] ?? [],
|
2023-07-26 12:21:26 +00:00
|
|
|
option.value
|
|
|
|
);
|
|
|
|
|
2023-03-18 06:04:29 +00:00
|
|
|
setValue("query", {
|
2023-08-24 14:15:23 +00:00
|
|
|
...filters,
|
|
|
|
[key]: valueExists ? null : option.value,
|
2023-07-26 12:21:26 +00:00
|
|
|
} as IQuery);
|
|
|
|
} else {
|
|
|
|
if (!filters?.[key]?.includes(option.value))
|
|
|
|
setValue("query", {
|
|
|
|
...filters,
|
|
|
|
[key]: [...((filters?.[key] as any[]) ?? []), option.value],
|
|
|
|
});
|
|
|
|
else {
|
|
|
|
setValue("query", {
|
|
|
|
...filters,
|
|
|
|
[key]: (filters?.[key] as any[])?.filter((item) => item !== option.value),
|
|
|
|
});
|
|
|
|
}
|
2023-03-18 06:04:29 +00:00
|
|
|
}
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div>
|
2023-07-26 12:21:26 +00:00
|
|
|
<FiltersList
|
2023-03-30 11:29:07 +00:00
|
|
|
filters={filters}
|
2023-07-26 12:21:26 +00:00
|
|
|
labels={labels}
|
|
|
|
members={members?.map((m) => m.member)}
|
|
|
|
states={states}
|
|
|
|
clearAllFilters={clearAllFilters}
|
2023-03-30 11:29:07 +00:00
|
|
|
setFilters={(query: any) => {
|
|
|
|
setValue("query", {
|
|
|
|
...filters,
|
|
|
|
...query,
|
|
|
|
});
|
|
|
|
}}
|
|
|
|
/>
|
2023-03-18 06:04:29 +00:00
|
|
|
</div>
|
2023-03-15 05:30:05 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div className="mt-5 flex justify-end gap-2">
|
2023-03-17 05:10:38 +00:00
|
|
|
<SecondaryButton onClick={handleClose}>Cancel</SecondaryButton>
|
|
|
|
<PrimaryButton type="submit" loading={isSubmitting}>
|
2023-03-15 05:30:05 +00:00
|
|
|
{status
|
|
|
|
? isSubmitting
|
|
|
|
? "Updating View..."
|
|
|
|
: "Update View"
|
|
|
|
: isSubmitting
|
|
|
|
? "Creating View..."
|
|
|
|
: "Create View"}
|
2023-03-17 05:10:38 +00:00
|
|
|
</PrimaryButton>
|
2023-03-15 05:30:05 +00:00
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
);
|
|
|
|
};
|