plane/web/components/views/form.tsx

242 lines
6.9 KiB
TypeScript
Raw Normal View History

2023-03-15 05:30:05 +00:00
import { useEffect } from "react";
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";
// services
import stateService from "services/state.service";
// hooks
import useProjectMembers from "hooks/use-project-members";
import useProjects from "hooks/use-projects";
import useWorkspaceMembers from "hooks/use-workspace-members";
// components
import { FiltersList } from "components/core";
import { SelectFilters } from "components/views";
2023-03-15 05:30:05 +00:00
// ui
import { Input, PrimaryButton, SecondaryButton, TextArea } from "components/ui";
// helpers
import { checkIfArraysHaveSameElements } from "helpers/array.helper";
import { getStatesList } from "helpers/state.helper";
2023-03-15 05:30:05 +00:00
// types
import { IQuery, IView } from "types";
import issuesService from "services/issues.service";
// fetch-keys
import { PROJECT_ISSUE_LABELS, STATES_LIST, WORKSPACE_LABELS } from "constants/fetch-keys";
2023-03-15 05:30:05 +00:00
type Props = {
handleFormSubmit: (values: IView) => Promise<void>;
handleClose: () => void;
status: boolean;
data?: IView | null;
viewType?: "workspace" | "project";
preLoadedData?: Partial<IView> | null;
2023-03-15 05:30:05 +00:00
};
const defaultValues: Partial<IView> = {
name: "",
description: "",
};
export const ViewForm: React.FC<Props> = ({
handleFormSubmit,
handleClose,
status,
data,
viewType,
preLoadedData,
}) => {
const router = useRouter();
const { workspaceSlug, projectId } = router.query;
2023-03-15 05:30:05 +00:00
const {
register,
formState: { errors, isSubmitting },
handleSubmit,
reset,
watch,
setValue,
2023-03-15 05:30:05 +00:00
} = useForm<IView>({
defaultValues,
});
const filters = watch("query");
2023-03-15 05:30:05 +00:00
const { data: stateGroups } = useSWR(
2023-07-27 16:08:16 +00:00
workspaceSlug && projectId && (filters?.state ?? []).length > 0
? STATES_LIST(projectId as string)
: null,
2023-07-27 16:08:16 +00:00
workspaceSlug && (filters?.state ?? []).length > 0
? () => stateService.getStates(workspaceSlug as string, projectId as string)
: null
);
const states = getStatesList(stateGroups);
const { data: labels } = useSWR(
2023-07-27 16:08:16 +00:00
workspaceSlug && projectId && (filters?.labels ?? []).length > 0
? PROJECT_ISSUE_LABELS(projectId.toString())
: null,
2023-07-27 16:08:16 +00:00
workspaceSlug && projectId && (filters?.labels ?? []).length > 0
? () => issuesService.getIssueLabels(workspaceSlug.toString(), projectId.toString())
: null
);
const { data: workspaceLabels } = useSWR(
workspaceSlug ? WORKSPACE_LABELS(workspaceSlug.toString()) : null,
workspaceSlug ? () => issuesService.getWorkspaceLabels(workspaceSlug.toString()) : null
);
const labelOptions = viewType === "workspace" ? workspaceLabels : labels;
const { members } = useProjectMembers(workspaceSlug?.toString(), projectId?.toString());
const { workspaceMembers } = useWorkspaceMembers(workspaceSlug?.toString() ?? "");
const memberOptions =
viewType === "workspace"
? workspaceMembers?.map((m) => m.member)
: members?.map((m) => m.member);
const { projects: allProjects } = useProjects();
const joinedProjects = allProjects?.filter((p) => p.is_member);
2023-03-15 05:30:05 +00:00
const handleCreateUpdateView = async (formData: IView) => {
await handleFormSubmit(formData);
reset({
...defaultValues,
});
};
const clearAllFilters = () => {
setValue("query", {
assignees: null,
created_by: null,
subscriber: null,
labels: null,
priority: null,
state: null,
state_group: null,
start_date: null,
target_date: null,
project: null,
});
};
2023-03-15 05:30:05 +00:00
useEffect(() => {
reset({
...defaultValues,
...preLoadedData,
2023-03-15 05:30:05 +00:00
...data,
});
}, [data, preLoadedData, reset]);
2023-03-15 05:30:05 +00:00
useEffect(() => {
if (status && data) {
setValue("query", data.query_data);
}
}, [data, status, setValue]);
2023-03-15 05:30:05 +00:00
return (
<form onSubmit={handleSubmit(handleCreateUpdateView)}>
<div className="space-y-5">
<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"
placeholder="Title"
2023-03-15 05:30:05 +00:00
autoComplete="off"
className="resize-none text-xl"
2023-03-15 05:30:05 +00:00
error={errors.name}
register={register}
validations={{
required: "Title is required",
2023-03-15 05:30:05 +00:00
maxLength: {
value: 255,
message: "Title should be less than 255 characters",
2023-03-15 05:30:05 +00:00
},
}}
/>
</div>
<div>
<TextArea
id="description"
name="description"
placeholder="Description"
className="h-32 resize-none text-sm"
2023-03-15 05:30:05 +00:00
error={errors.description}
register={register}
/>
</div>
<div>
<SelectFilters
filters={filters}
onSelect={(option) => {
const key = option.key as keyof typeof filters;
if (key === "start_date" || key === "target_date") {
const valueExists = checkIfArraysHaveSameElements(
filters?.[key] ?? [],
option.value
);
setValue("query", {
...filters,
[key]: valueExists ? null : option.value,
} 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),
});
}
}
}}
/>
</div>
<div>
<FiltersList
filters={filters}
labels={labelOptions}
members={memberOptions}
states={states}
project={joinedProjects}
clearAllFilters={clearAllFilters}
setFilters={(query: any) => {
setValue("query", {
...filters,
...query,
});
}}
/>
</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>
);
};