2023-03-15 05:30:05 +00:00
|
|
|
import { useEffect } from "react";
|
|
|
|
|
2023-03-18 06:04:29 +00:00
|
|
|
import { useRouter } from "next/router";
|
|
|
|
|
|
|
|
import useSWR from "swr";
|
|
|
|
|
2023-03-15 05:30:05 +00:00
|
|
|
import { useForm } from "react-hook-form";
|
|
|
|
// ui
|
2023-03-23 06:31:50 +00:00
|
|
|
import { Avatar, Input, PrimaryButton, SecondaryButton, TextArea } from "components/ui";
|
2023-03-15 05:30:05 +00:00
|
|
|
// types
|
|
|
|
import { IView } from "types";
|
2023-03-18 06:04:29 +00:00
|
|
|
// constant
|
2023-03-29 19:38:14 +00:00
|
|
|
import { PROJECT_ISSUE_LABELS, PROJECT_MEMBERS, STATE_LIST } from "constants/fetch-keys";
|
2023-03-18 06:04:29 +00:00
|
|
|
// helpers
|
|
|
|
import { getStatesList } from "helpers/state.helper";
|
|
|
|
// services
|
|
|
|
import stateService from "services/state.service";
|
|
|
|
import projectService from "services/project.service";
|
2023-03-29 19:38:14 +00:00
|
|
|
import issuesService from "services/issues.service";
|
2023-03-23 06:31:50 +00:00
|
|
|
// components
|
|
|
|
import { SelectFilters } from "components/views";
|
2023-03-18 06:04:29 +00:00
|
|
|
// icons
|
|
|
|
import { getStateGroupIcon } from "components/icons";
|
|
|
|
import { getPriorityIcon } from "components/icons/priority-icon";
|
|
|
|
// components
|
2023-03-15 05:30:05 +00:00
|
|
|
|
|
|
|
type Props = {
|
|
|
|
handleFormSubmit: (values: IView) => Promise<void>;
|
|
|
|
handleClose: () => void;
|
|
|
|
status: boolean;
|
|
|
|
data?: IView;
|
2023-03-16 08:37:19 +00:00
|
|
|
preLoadedData?: Partial<IView> | null;
|
2023-03-15 05:30:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const defaultValues: Partial<IView> = {
|
|
|
|
name: "",
|
|
|
|
description: "",
|
|
|
|
};
|
|
|
|
|
2023-03-16 08:37:19 +00:00
|
|
|
export const ViewForm: React.FC<Props> = ({
|
|
|
|
handleFormSubmit,
|
|
|
|
handleClose,
|
|
|
|
status,
|
|
|
|
data,
|
|
|
|
preLoadedData,
|
|
|
|
}) => {
|
2023-03-18 06:04:29 +00:00
|
|
|
const router = useRouter();
|
|
|
|
const { workspaceSlug, projectId } = router.query;
|
|
|
|
|
|
|
|
const { data: states } = useSWR(
|
|
|
|
workspaceSlug && projectId ? STATE_LIST(projectId as string) : null,
|
|
|
|
workspaceSlug && projectId
|
|
|
|
? () => stateService.getStates(workspaceSlug as string, projectId as string)
|
|
|
|
: null
|
|
|
|
);
|
|
|
|
const statesList = getStatesList(states ?? {});
|
|
|
|
|
|
|
|
const { data: members } = useSWR(
|
|
|
|
projectId ? PROJECT_MEMBERS(projectId as string) : null,
|
|
|
|
workspaceSlug && projectId
|
|
|
|
? () => projectService.projectMembers(workspaceSlug as string, projectId as string)
|
|
|
|
: null
|
|
|
|
);
|
|
|
|
|
2023-03-29 19:38:14 +00:00
|
|
|
const { data: issueLabels } = useSWR(
|
|
|
|
projectId ? PROJECT_ISSUE_LABELS(projectId.toString()) : null,
|
|
|
|
workspaceSlug && projectId
|
|
|
|
? () => issuesService.getIssueLabels(workspaceSlug as string, projectId.toString())
|
|
|
|
: null
|
|
|
|
);
|
|
|
|
|
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,
|
|
|
|
});
|
|
|
|
|
|
|
|
const handleCreateUpdateView = async (formData: IView) => {
|
|
|
|
await handleFormSubmit(formData);
|
|
|
|
|
|
|
|
reset({
|
|
|
|
...defaultValues,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
reset({
|
|
|
|
...defaultValues,
|
|
|
|
...data,
|
|
|
|
});
|
|
|
|
}, [data, reset]);
|
|
|
|
|
2023-03-16 08:37:19 +00:00
|
|
|
useEffect(() => {
|
|
|
|
reset({
|
|
|
|
...defaultValues,
|
|
|
|
...preLoadedData,
|
|
|
|
});
|
|
|
|
}, [preLoadedData, reset]);
|
|
|
|
|
2023-03-18 06:04:29 +00:00
|
|
|
const filters = watch("query");
|
|
|
|
|
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-gray-900">
|
|
|
|
{status ? "Update" : "Create"} View
|
|
|
|
</h3>
|
|
|
|
<div className="space-y-3">
|
|
|
|
<div>
|
|
|
|
<Input
|
|
|
|
id="name"
|
|
|
|
label="Name"
|
|
|
|
name="name"
|
|
|
|
type="name"
|
|
|
|
placeholder="Enter name"
|
|
|
|
autoComplete="off"
|
|
|
|
error={errors.name}
|
|
|
|
register={register}
|
|
|
|
validations={{
|
|
|
|
required: "Name is required",
|
|
|
|
maxLength: {
|
|
|
|
value: 255,
|
|
|
|
message: "Name should be less than 255 characters",
|
|
|
|
},
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div>
|
|
|
|
<TextArea
|
|
|
|
id="description"
|
|
|
|
name="description"
|
|
|
|
label="Description"
|
|
|
|
placeholder="Enter description"
|
|
|
|
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;
|
|
|
|
|
|
|
|
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>
|
2023-03-29 19:38:14 +00:00
|
|
|
<div className="flex flex-wrap items-center gap-4">
|
2023-03-18 06:04:29 +00:00
|
|
|
{Object.keys(filters ?? {}).map((key) => {
|
|
|
|
const queryKey = key as keyof typeof filters;
|
|
|
|
if (queryKey === "state")
|
|
|
|
return (
|
|
|
|
<div className="flex gap-3" key={key}>
|
|
|
|
{filters.state?.map((stateID) => {
|
|
|
|
const state = statesList.find((state) => state.id === stateID);
|
|
|
|
if (!state) return null;
|
|
|
|
return (
|
2023-03-29 19:38:14 +00:00
|
|
|
<div
|
|
|
|
className="flex items-center gap-2 rounded-full border bg-white px-2 py-1.5 text-xs"
|
|
|
|
key={state.id}
|
|
|
|
>
|
2023-03-18 06:04:29 +00:00
|
|
|
{getStateGroupIcon(state?.group, "16", "16", state?.color)}
|
|
|
|
{state?.name}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
})}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
else if (queryKey === "priority")
|
|
|
|
return (
|
|
|
|
<div className="flex gap-3" key={key}>
|
|
|
|
{filters.priority?.map((priority) => (
|
2023-03-29 19:38:14 +00:00
|
|
|
<div
|
|
|
|
className="flex items-center gap-2 rounded-full border bg-white px-2 py-1.5 text-xs"
|
|
|
|
key={priority}
|
|
|
|
>
|
2023-03-18 06:04:29 +00:00
|
|
|
{getPriorityIcon(priority)}
|
|
|
|
{priority}
|
|
|
|
</div>
|
|
|
|
))}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
else if (queryKey === "assignees")
|
|
|
|
return (
|
|
|
|
<div className="flex gap-3" key={key}>
|
2023-03-23 06:31:50 +00:00
|
|
|
{filters.assignees?.map((assigneeId) => {
|
|
|
|
const member = members?.find((member) => member.member.id === assigneeId);
|
|
|
|
|
2023-03-18 06:04:29 +00:00
|
|
|
if (!member) return null;
|
|
|
|
return (
|
2023-03-29 19:38:14 +00:00
|
|
|
<div
|
|
|
|
className="flex items-center gap-2 rounded-full border bg-white px-1.5 py-1 text-xs"
|
|
|
|
key={member.member.id}
|
|
|
|
>
|
2023-03-18 06:04:29 +00:00
|
|
|
<Avatar user={member.member} />
|
|
|
|
{member.member.first_name && member.member.first_name !== ""
|
|
|
|
? member.member.first_name
|
|
|
|
: member.member.email}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
})}
|
|
|
|
</div>
|
|
|
|
);
|
2023-03-29 19:38:14 +00:00
|
|
|
else if (queryKey === "labels")
|
|
|
|
return (
|
|
|
|
<div className="flex gap-3" key={key}>
|
|
|
|
<div className="flex items-center gap-x-1">
|
|
|
|
{filters.labels?.map((labelId: string) => {
|
|
|
|
const label = issueLabels?.find((l) => l.id === labelId);
|
|
|
|
if (!label) return null;
|
|
|
|
return (
|
2023-03-29 20:01:43 +00:00
|
|
|
<div className="flex items-center gap-1 rounded-full border bg-white px-1.5 py-1 text-xs">
|
2023-03-29 19:38:14 +00:00
|
|
|
<div
|
2023-03-29 20:01:43 +00:00
|
|
|
className="h-2 w-2 rounded-full"
|
|
|
|
style={{
|
|
|
|
backgroundColor:
|
|
|
|
label.color && label.color !== "" ? label.color : "#000000",
|
|
|
|
}}
|
2023-03-29 19:38:14 +00:00
|
|
|
/>
|
|
|
|
{label.name}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
})}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
2023-03-18 06:04:29 +00:00
|
|
|
})}
|
|
|
|
</div>
|
|
|
|
</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>
|
|
|
|
);
|
|
|
|
};
|