feat: label grouping, fix: new states response (#254)

This commit is contained in:
Aaryan Khandelwal 2023-02-08 18:51:03 +05:30 committed by GitHub
parent c978632938
commit 166520dfda
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
17 changed files with 539 additions and 303 deletions

View file

@ -19,14 +19,14 @@ import { requiredAdmin } from "lib/auth";
// layouts
import AppLayout from "layouts/app-layout";
// components
import SingleLabel from "components/project/settings/single-label";
import { SingleLabel } from "components/labels";
// ui
import { Button, Input, Loader } from "components/ui";
import { BreadcrumbItem, Breadcrumbs } from "components/breadcrumbs";
// icons
import { PlusIcon } from "@heroicons/react/24/outline";
// types
import { IIssueLabels } from "types";
import { IIssueLabels, UserAuth } from "types";
import type { NextPageContext, NextPage } from "next";
// fetch-keys
import { PROJECT_DETAILS, PROJECT_ISSUE_LABELS, WORKSPACE_DETAILS } from "constants/fetch-keys";
@ -36,28 +36,15 @@ const defaultValues: Partial<IIssueLabels> = {
color: "#ff0000",
};
type TLabelSettingsProps = {
isMember: boolean;
isOwner: boolean;
isViewer: boolean;
isGuest: boolean;
};
const LabelsSettings: NextPage<TLabelSettingsProps> = (props) => {
const LabelsSettings: NextPage<UserAuth> = (props) => {
const { isMember, isOwner, isViewer, isGuest } = props;
const [newLabelForm, setNewLabelForm] = useState(false);
const [labelForm, setLabelForm] = useState(false);
const [isUpdating, setIsUpdating] = useState(false);
const [labelIdForUpdate, setLabelIdForUpdate] = useState<string | null>(null);
const {
query: { workspaceSlug, projectId },
} = useRouter();
const { data: activeWorkspace } = useSWR(
workspaceSlug ? WORKSPACE_DETAILS(workspaceSlug as string) : null,
() => (workspaceSlug ? workspaceService.getWorkspace(workspaceSlug as string) : null)
);
const router = useRouter();
const { workspaceSlug, projectId } = router.query;
const { data: projectDetails } = useSWR(
workspaceSlug && projectId ? PROJECT_DETAILS(projectId as string) : null,
@ -66,6 +53,13 @@ const LabelsSettings: NextPage<TLabelSettingsProps> = (props) => {
: null
);
const { data: issueLabels, mutate } = useSWR<IIssueLabels[]>(
workspaceSlug && projectId ? PROJECT_ISSUE_LABELS(projectId as string) : null,
workspaceSlug && projectId
? () => issuesService.getIssueLabels(workspaceSlug as string, projectId as string)
: null
);
const {
register,
handleSubmit,
@ -76,37 +70,37 @@ const LabelsSettings: NextPage<TLabelSettingsProps> = (props) => {
watch,
} = useForm<IIssueLabels>({ defaultValues });
const { data: issueLabels, mutate } = useSWR<IIssueLabels[]>(
workspaceSlug && projectId ? PROJECT_ISSUE_LABELS(projectId as string) : null,
workspaceSlug && projectId
? () => issuesService.getIssueLabels(workspaceSlug as string, projectId as string)
: null
);
const handleNewLabel: SubmitHandler<IIssueLabels> = async (formData) => {
if (!activeWorkspace || !projectDetails || isSubmitting) return;
await issuesService
.createIssueLabel(activeWorkspace.slug, projectDetails.id, formData)
.then((res) => {
reset(defaultValues);
mutate((prevData) => [...(prevData ?? []), res], false);
setNewLabelForm(false);
});
const newLabel = () => {
reset();
setIsUpdating(false);
setLabelForm(true);
};
const editLabel = (label: IIssueLabels) => {
setNewLabelForm(true);
setLabelForm(true);
setValue("color", label.color);
setValue("name", label.name);
setIsUpdating(true);
setLabelIdForUpdate(label.id);
};
const handleLabelUpdate: SubmitHandler<IIssueLabels> = async (formData) => {
if (!activeWorkspace || !projectDetails || isSubmitting) return;
const handleLabelCreate: SubmitHandler<IIssueLabels> = async (formData) => {
if (!workspaceSlug || !projectDetails || isSubmitting) return;
await issuesService
.patchIssueLabel(activeWorkspace.slug, projectDetails.id, labelIdForUpdate ?? "", formData)
.createIssueLabel(workspaceSlug as string, projectDetails.id, formData)
.then((res) => {
mutate((prevData) => [res, ...(prevData ?? [])], false);
reset(defaultValues);
setLabelForm(false);
});
};
const handleLabelUpdate: SubmitHandler<IIssueLabels> = async (formData) => {
if (!workspaceSlug || !projectDetails || isSubmitting) return;
await issuesService
.patchIssueLabel(workspaceSlug as string, projectDetails.id, labelIdForUpdate ?? "", formData)
.then((res) => {
console.log(res);
reset(defaultValues);
@ -115,15 +109,15 @@ const LabelsSettings: NextPage<TLabelSettingsProps> = (props) => {
prevData?.map((p) => (p.id === labelIdForUpdate ? { ...p, ...formData } : p)),
false
);
setNewLabelForm(false);
setLabelForm(false);
});
};
const handleLabelDelete = (labelId: string) => {
if (activeWorkspace && projectDetails) {
if (workspaceSlug && projectDetails) {
mutate((prevData) => prevData?.filter((p) => p.id !== labelId), false);
issuesService
.deleteIssueLabel(activeWorkspace.slug, projectDetails.id, labelId)
.deleteIssueLabel(workspaceSlug as string, projectDetails.id, labelId)
.then((res) => {
console.log(res);
})
@ -154,11 +148,7 @@ const LabelsSettings: NextPage<TLabelSettingsProps> = (props) => {
</div>
<div className="flex items-center justify-between gap-2 md:w-2/3">
<h4 className="text-md mb-1 leading-6 text-gray-900">Manage labels</h4>
<Button
theme="secondary"
className="flex items-center gap-x-1"
onClick={() => setNewLabelForm(true)}
>
<Button theme="secondary" className="flex items-center gap-x-1" onClick={newLabel}>
<PlusIcon className="h-4 w-4" />
New label
</Button>
@ -166,7 +156,7 @@ const LabelsSettings: NextPage<TLabelSettingsProps> = (props) => {
<div className="space-y-5">
<div
className={`flex items-center gap-2 rounded-md border p-3 md:w-2/3 ${
newLabelForm ? "" : "hidden"
labelForm ? "" : "hidden"
}`}
>
<div className="h-8 w-8 flex-shrink-0">
@ -227,7 +217,14 @@ const LabelsSettings: NextPage<TLabelSettingsProps> = (props) => {
error={errors.name}
/>
</div>
<Button type="button" theme="secondary" onClick={() => setNewLabelForm(false)}>
<Button
type="button"
theme="secondary"
onClick={() => {
reset();
setLabelForm(false);
}}
>
Cancel
</Button>
{isUpdating ? (
@ -239,7 +236,11 @@ const LabelsSettings: NextPage<TLabelSettingsProps> = (props) => {
{isSubmitting ? "Updating" : "Update"}
</Button>
) : (
<Button type="button" onClick={handleSubmit(handleNewLabel)} disabled={isSubmitting}>
<Button
type="button"
onClick={handleSubmit(handleLabelCreate)}
disabled={isSubmitting}
>
{isSubmitting ? "Adding" : "Add"}
</Button>
)}

View file

@ -1,10 +1,9 @@
import React, { useState } from "react";
import { useRouter } from "next/router";
import useSWR from "swr";
import { PencilSquareIcon, PlusIcon, TrashIcon } from "@heroicons/react/24/outline";
import { IState } from "types";
import useSWR from "swr";
// services
import stateService from "services/state.service";
import projectService from "services/project.service";
@ -17,22 +16,18 @@ import { CreateUpdateStateInline, DeleteStateModal, StateGroup } from "component
// ui
import { Loader } from "components/ui";
import { BreadcrumbItem, Breadcrumbs } from "components/breadcrumbs";
// icons
import { PencilSquareIcon, PlusIcon, TrashIcon } from "@heroicons/react/24/outline";
// helpers
import { addSpaceIfCamelCase } from "helpers/string.helper";
import { groupBy } from "helpers/array.helper";
import { getStatesList, orderStateGroups } from "helpers/state.helper";
// types
import { UserAuth } from "types";
import type { NextPage, NextPageContext } from "next";
// fetch-keys
import { PROJECT_DETAILS, STATE_LIST } from "constants/fetch-keys";
type TStateSettingsProps = {
isMember: boolean;
isOwner: boolean;
isViewer: boolean;
isGuest: boolean;
};
const StatesSettings: NextPage<TStateSettingsProps> = (props) => {
const StatesSettings: NextPage<UserAuth> = (props) => {
const { isMember, isOwner, isViewer, isGuest } = props;
const [activeGroup, setActiveGroup] = useState<StateGroup>(null);
@ -56,16 +51,15 @@ const StatesSettings: NextPage<TStateSettingsProps> = (props) => {
? () => stateService.getStates(workspaceSlug as string, projectId as string)
: null
);
const orderedStateGroups = orderStateGroups(states ?? {});
const groupedStates: {
[key: string]: IState[];
} = groupBy(states ?? [], "group");
const statesList = getStatesList(orderStateGroups ?? {});
return (
<>
<DeleteStateModal
isOpen={!!selectDeleteState}
data={states?.find((state) => state.id === selectDeleteState) ?? null}
data={statesList?.find((state) => state.id === selectDeleteState) ?? null}
onClose={() => setSelectDeleteState(null)}
/>
<AppLayout
@ -88,60 +82,23 @@ const StatesSettings: NextPage<TStateSettingsProps> = (props) => {
</div>
<div className="flex flex-col justify-between gap-4">
{states && projectDetails ? (
Object.keys(groupedStates).map((key) => (
<div key={key}>
<div className="mb-2 flex w-full justify-between md:w-2/3">
<p className="text-md capitalize leading-6 text-gray-900">{key} states</p>
<button
type="button"
onClick={() => setActiveGroup(key as keyof StateGroup)}
className="flex items-center gap-2 text-xs text-theme"
>
<PlusIcon className="h-3 w-3 text-theme" />
Add
</button>
</div>
<div className="space-y-1 rounded-xl border p-1 md:w-2/3">
{key === activeGroup && (
<CreateUpdateStateInline
projectId={projectDetails.id}
onClose={() => {
setActiveGroup(null);
setSelectedState(null);
}}
workspaceSlug={workspaceSlug as string}
data={null}
selectedGroup={key as keyof StateGroup}
/>
)}
{groupedStates[key]?.map((state) =>
state.id !== selectedState ? (
<div
key={state.id}
className={`flex items-center justify-between gap-2 border-b bg-gray-50 p-3 ${
Boolean(activeGroup !== key) ? "last:border-0" : ""
}`}
Object.keys(orderedStateGroups).map((key) => {
if (orderedStateGroups[key].length !== 0)
return (
<div key={key}>
<div className="mb-2 flex w-full justify-between md:w-2/3">
<p className="text-md capitalize leading-6 text-gray-900">{key} states</p>
<button
type="button"
onClick={() => setActiveGroup(key as keyof StateGroup)}
className="flex items-center gap-2 text-xs text-theme"
>
<div className="flex items-center gap-2">
<div
className="h-3 w-3 flex-shrink-0 rounded-full"
style={{
backgroundColor: state.color,
}}
/>
<h6 className="text-sm">{addSpaceIfCamelCase(state.name)}</h6>
</div>
<div className="flex items-center gap-2">
<button type="button" onClick={() => setSelectDeleteState(state.id)}>
<TrashIcon className="h-4 w-4 text-red-400" />
</button>
<button type="button" onClick={() => setSelectedState(state.id)}>
<PencilSquareIcon className="h-4 w-4 text-gray-400" />
</button>
</div>
</div>
) : (
<div className="border-b last:border-b-0" key={state.id}>
<PlusIcon className="h-3 w-3 text-theme" />
Add
</button>
</div>
<div className="space-y-1 rounded-xl border p-1 md:w-2/3">
{key === activeGroup && (
<CreateUpdateStateInline
projectId={projectDetails.id}
onClose={() => {
@ -149,15 +106,60 @@ const StatesSettings: NextPage<TStateSettingsProps> = (props) => {
setSelectedState(null);
}}
workspaceSlug={workspaceSlug as string}
data={states?.find((state) => state.id === selectedState) ?? null}
data={null}
selectedGroup={key as keyof StateGroup}
/>
</div>
)
)}
</div>
</div>
))
)}
{orderedStateGroups[key].map((state) =>
state.id !== selectedState ? (
<div
key={state.id}
className={`flex items-center justify-between gap-2 border-b bg-gray-50 p-3 ${
Boolean(activeGroup !== key) ? "last:border-0" : ""
}`}
>
<div className="flex items-center gap-2">
<div
className="h-3 w-3 flex-shrink-0 rounded-full"
style={{
backgroundColor: state.color,
}}
/>
<h6 className="text-sm">{addSpaceIfCamelCase(state.name)}</h6>
</div>
<div className="flex items-center gap-2">
<button
type="button"
onClick={() => setSelectDeleteState(state.id)}
>
<TrashIcon className="h-4 w-4 text-red-400" />
</button>
<button type="button" onClick={() => setSelectedState(state.id)}>
<PencilSquareIcon className="h-4 w-4 text-gray-400" />
</button>
</div>
</div>
) : (
<div className="border-b last:border-b-0" key={state.id}>
<CreateUpdateStateInline
projectId={projectDetails.id}
onClose={() => {
setActiveGroup(null);
setSelectedState(null);
}}
workspaceSlug={workspaceSlug as string}
data={
statesList?.find((state) => state.id === selectedState) ?? null
}
selectedGroup={key as keyof StateGroup}
/>
</div>
)
)}
</div>
</div>
);
})
) : (
<Loader className="space-y-5 md:w-2/3">
<Loader.Item height="40px" />