style: made each state group capsule

This commit is contained in:
Dakshesh Jain 2022-12-14 22:46:41 +05:30
parent c9b1a2590a
commit 53c3aec8b2
2 changed files with 247 additions and 228 deletions

View File

@ -0,0 +1,184 @@
import React, { useEffect } from "react";
// swr
import { mutate } from "swr";
// react hook form
import { useForm, Controller } from "react-hook-form";
// react color
import { TwitterPicker } from "react-color";
// headless
import { Popover, Transition } from "@headlessui/react";
// constants
import { STATE_LIST } from "constants/fetch-keys";
// services
import stateService from "lib/services/state.service";
// ui
import { Button, Input } from "ui";
// types
import type { IState } from "types";
type Props = {
workspaceSlug?: string;
projectId?: string;
data: IState | null;
onClose: () => void;
selectedGroup: StateGroup | null;
};
export type StateGroup = "backlog" | "unstarted" | "started" | "completed" | "cancelled" | null;
export const CreateUpdateStateInline: React.FC<Props> = ({
workspaceSlug,
projectId,
data,
onClose,
selectedGroup,
}) => {
const {
register,
handleSubmit,
formState: { errors },
setError,
watch,
reset,
control,
} = useForm<IState>({
defaultValues: {
name: "",
color: "#000000",
group: "backlog",
},
});
const handleClose = () => {
onClose();
reset({ name: "", color: "#000000", group: "backlog" });
};
const onSubmit = async (formData: IState) => {
if (!workspaceSlug || !projectId) return;
const payload: IState = {
...formData,
};
if (!data) {
await stateService
.createState(workspaceSlug, projectId, { ...payload, group: selectedGroup })
.then((res) => {
mutate<IState[]>(STATE_LIST(projectId), (prevData) => [...(prevData ?? []), res], false);
handleClose();
})
.catch((err) => {
Object.keys(err).map((key) => {
setError(key as keyof IState, {
message: err[key].join(", "),
});
});
});
} else {
await stateService
.updateState(workspaceSlug, projectId, data.id, {
...payload,
group: selectedGroup ?? "backlog",
})
.then((res) => {
mutate<IState[]>(
STATE_LIST(projectId),
(prevData) => {
const newData = prevData?.map((item) => {
if (item.id === res.id) {
return res;
}
return item;
});
return newData;
},
false
);
handleClose();
})
.catch((err) => {
Object.keys(err).map((key) => {
setError(key as keyof IState, {
message: err[key].join(", "),
});
});
});
}
};
useEffect(() => {
if (data === null) return;
reset(data);
}, [data]);
return (
<div className="flex items-center gap-x-2 p-2 bg-gray-50">
<div className="w-8 h-8 shrink-0">
<Popover className="relative w-full h-full flex justify-center items-center bg-gray-200 rounded-xl">
{({ open }) => (
<>
<Popover.Button
className={`group inline-flex items-center text-base font-medium focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500 ${
open ? "text-gray-900" : "text-gray-500"
}`}
>
{watch("color") && watch("color") !== "" && (
<span
className="w-4 h-4 rounded"
style={{
backgroundColor: watch("color") ?? "green",
}}
/>
)}
</Popover.Button>
<Transition
as={React.Fragment}
enter="transition ease-out duration-200"
enterFrom="opacity-0 translate-y-1"
enterTo="opacity-100 translate-y-0"
leave="transition ease-in duration-150"
leaveFrom="opacity-100 translate-y-0"
leaveTo="opacity-0 translate-y-1"
>
<Popover.Panel className="absolute top-full z-50 left-0 mt-3 px-2 w-screen max-w-xs sm:px-0">
<Controller
name="color"
control={control}
render={({ field: { value, onChange } }) => (
<TwitterPicker color={value} onChange={(value) => onChange(value.hex)} />
)}
/>
</Popover.Panel>
</Transition>
</>
)}
</Popover>
</div>
<Input
id="name"
name="name"
register={register}
placeholder="Enter state name"
validations={{
required: "Name is required",
}}
error={errors.name}
autoComplete="off"
/>
<Input
id="description"
name="description"
register={register}
placeholder="Enter state description"
error={errors.description}
autoComplete="off"
/>
<Button theme="secondary" onClick={handleClose}>
Cancel
</Button>
<Button theme="primary" onClick={handleSubmit(onSubmit)}>
Save
</Button>
</div>
);
};

View File

@ -1,22 +1,12 @@
import React, { useEffect, useState } from "react"; import React, { useState } from "react";
// swr
import { mutate } from "swr";
// react hook form
import { useForm, Controller } from "react-hook-form";
// react color
import { TwitterPicker } from "react-color";
// headless
import { Popover, Transition } from "@headlessui/react";
// hooks // hooks
import useUser from "lib/hooks/useUser"; import useUser from "lib/hooks/useUser";
// constants
import { STATE_LIST } from "constants/fetch-keys";
// services
import stateService from "lib/services/state.service";
// components // components
import {
StateGroup,
CreateUpdateStateInline,
} from "components/project/issues/BoardView/state/create-update-state-inline";
import ConfirmStateDeletion from "components/project/issues/BoardView/state/confirm-state-delete"; import ConfirmStateDeletion from "components/project/issues/BoardView/state/confirm-state-delete";
// ui
import { Button, Input } from "ui";
// icons // icons
import { PencilSquareIcon, PlusIcon, TrashIcon } from "@heroicons/react/24/outline"; import { PencilSquareIcon, PlusIcon, TrashIcon } from "@heroicons/react/24/outline";
// constants // constants
@ -28,173 +18,6 @@ type Props = {
projectId: string | string[] | undefined; projectId: string | string[] | undefined;
}; };
type CreateUpdateStateProps = {
workspaceSlug?: string;
projectId?: string;
data: IState | null;
onClose: () => void;
selectedGroup: StateGroup | null;
};
type StateGroup = "backlog" | "unstarted" | "started" | "completed" | "cancelled" | null;
const CreateUpdateState: React.FC<CreateUpdateStateProps> = ({
workspaceSlug,
projectId,
data,
onClose,
selectedGroup,
}) => {
const {
register,
handleSubmit,
formState: { errors },
setError,
watch,
reset,
control,
} = useForm<IState>({
defaultValues: {
name: "",
color: "#000000",
group: "backlog",
},
});
const handleClose = () => {
onClose();
reset({ name: "", color: "#000000", group: "backlog" });
};
const onSubmit = async (formData: IState) => {
if (!workspaceSlug || !projectId) return;
const payload: IState = {
...formData,
};
if (!data) {
await stateService
.createState(workspaceSlug, projectId, { ...payload, group: selectedGroup })
.then((res) => {
mutate<IState[]>(STATE_LIST(projectId), (prevData) => [...(prevData ?? []), res], false);
handleClose();
})
.catch((err) => {
Object.keys(err).map((key) => {
setError(key as keyof IState, {
message: err[key].join(", "),
});
});
});
} else {
await stateService
.updateState(workspaceSlug, projectId, data.id, {
...payload,
group: selectedGroup ?? "backlog",
})
.then((res) => {
mutate<IState[]>(
STATE_LIST(projectId),
(prevData) => {
const newData = prevData?.map((item) => {
if (item.id === res.id) {
return res;
}
return item;
});
return newData;
},
false
);
handleClose();
})
.catch((err) => {
Object.keys(err).map((key) => {
setError(key as keyof IState, {
message: err[key].join(", "),
});
});
});
}
};
useEffect(() => {
if (data === null) return;
reset(data);
}, [data]);
return (
<div className="flex items-center gap-x-2 p-2 bg-white">
<div className="w-8 h-8 border shrink-0">
<Popover className="relative w-full h-full flex justify-center items-center">
{({ open }) => (
<>
<Popover.Button
className={`group bg-white rounded-md inline-flex items-center text-base font-medium hover:text-gray-900 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500 ${
open ? "text-gray-900" : "text-gray-500"
}`}
>
{watch("color") && watch("color") !== "" && (
<span
className="w-4 h-4 rounded"
style={{
backgroundColor: watch("color") ?? "green",
}}
></span>
)}
</Popover.Button>
<Transition
as={React.Fragment}
enter="transition ease-out duration-200"
enterFrom="opacity-0 translate-y-1"
enterTo="opacity-100 translate-y-0"
leave="transition ease-in duration-150"
leaveFrom="opacity-100 translate-y-0"
leaveTo="opacity-0 translate-y-1"
>
<Popover.Panel className="absolute top-full z-50 left-0 mt-3 px-2 w-screen max-w-xs sm:px-0">
<Controller
name="color"
control={control}
render={({ field: { value, onChange } }) => (
<TwitterPicker color={value} onChange={(value) => onChange(value.hex)} />
)}
/>
</Popover.Panel>
</Transition>
</>
)}
</Popover>
</div>
<Input
id="name"
name="name"
register={register}
placeholder="Enter state name"
validations={{
required: "Name is required",
}}
error={errors.name}
autoComplete="off"
/>
<Input
id="description"
name="description"
register={register}
placeholder="Enter state description"
error={errors.description}
autoComplete="off"
/>
<Button theme="secondary" onClick={handleClose}>
Cancel
</Button>
<Button theme="primary" onClick={handleSubmit(onSubmit)}>
Save
</Button>
</div>
);
};
const StatesSettings: React.FC<Props> = ({ projectId }) => { const StatesSettings: React.FC<Props> = ({ projectId }) => {
const [activeGroup, setActiveGroup] = useState<StateGroup>(null); const [activeGroup, setActiveGroup] = useState<StateGroup>(null);
const [selectedState, setSelectedState] = useState<string | null>(null); const [selectedState, setSelectedState] = useState<string | null>(null);
@ -221,64 +44,76 @@ const StatesSettings: React.FC<Props> = ({ projectId }) => {
</div> </div>
<div className="flex flex-col justify-between gap-3"> <div className="flex flex-col justify-between gap-3">
{Object.keys(groupedStates).map((key) => ( {Object.keys(groupedStates).map((key) => (
<div className="w-full space-y-1" key={key}> <React.Fragment key={key}>
<div className="flex justify-between"> <div className="flex justify-between w-full md:w-2/3">
<p className="font-medium capitalize">{key} states</p> <p className="font-medium capitalize">{key} states</p>
<button type="button" onClick={() => setActiveGroup(key as keyof StateGroup)}> <button
<PlusIcon className="h-4 w-4 text-gray-600" /> type="button"
onClick={() => setActiveGroup(key as keyof StateGroup)}
className="flex items-center gap-x-2 text-theme"
>
<PlusIcon className="h-4 w-4 text-theme" />
<span>Add</span>
</button> </button>
</div> </div>
{groupedStates[key]?.map((state) => <div className="w-full md:w-2/3 space-y-1 border p-1 rounded-xl bg-gray-50">
state.id !== selectedState ? ( <div className="w-full">
<div {groupedStates[key]?.map((state) =>
key={state.id} state.id !== selectedState ? (
className="bg-white px-4 py-2 rounded flex justify-between items-center border"
>
<div className="flex items-center gap-x-2">
<div <div
className="w-3 h-3 rounded-full" key={state.id}
style={{ className={`bg-gray-50 px-5 py-4 flex justify-between items-center border-b ${
backgroundColor: state.color, Boolean(activeGroup !== key) ? "last:border-0" : ""
}} }`}
></div> >
<h4>{addSpaceIfCamelCase(state.name)}</h4> <div className="flex items-center gap-x-8">
</div> <div
<div className="flex gap-x-2"> className="w-6 h-6 rounded-full"
<button type="button" onClick={() => setSelectDeleteState(state.id)}> style={{
<TrashIcon className="h-5 w-5 text-red-400" /> backgroundColor: state.color,
</button> }}
<button type="button" onClick={() => setSelectedState(state.id)}> ></div>
<PencilSquareIcon className="h-5 w-5 text-gray-400" /> <h4>{addSpaceIfCamelCase(state.name)}</h4>
</button> </div>
</div> <div className="flex gap-x-2">
</div> <button type="button" onClick={() => setSelectDeleteState(state.id)}>
) : ( <TrashIcon className="h-5 w-5 text-red-400" />
<CreateUpdateState </button>
key={state.id} <button type="button" onClick={() => setSelectedState(state.id)}>
<PencilSquareIcon className="h-5 w-5 text-gray-400" />
</button>
</div>
</div>
) : (
<div className={`border-b last:border-b-0`} key={state.id}>
<CreateUpdateStateInline
projectId={projectId as string}
onClose={() => {
setActiveGroup(null);
setSelectedState(null);
}}
workspaceSlug={activeWorkspace?.slug}
data={states?.find((state) => state.id === selectedState) ?? null}
selectedGroup={key as keyof StateGroup}
/>
</div>
)
)}
</div>
{key === activeGroup && (
<CreateUpdateStateInline
projectId={projectId as string} projectId={projectId as string}
onClose={() => { onClose={() => {
setActiveGroup(null); setActiveGroup(null);
setSelectedState(null); setSelectedState(null);
}} }}
workspaceSlug={activeWorkspace?.slug} workspaceSlug={activeWorkspace?.slug}
data={states?.find((state) => state.id === selectedState) ?? null} data={null}
selectedGroup={key as keyof StateGroup} selectedGroup={key as keyof StateGroup}
/> />
) )}
)} </div>
{key === activeGroup && ( </React.Fragment>
<CreateUpdateState
projectId={projectId as string}
onClose={() => {
setActiveGroup(null);
setSelectedState(null);
}}
workspaceSlug={activeWorkspace?.slug}
data={null}
selectedGroup={key as keyof StateGroup}
/>
)}
</div>
))} ))}
</div> </div>
</section> </section>