feat: assign labels on new card
This commit is contained in:
@@ -14,7 +14,9 @@ import { useModal } from "~/app/providers/modal";
|
|||||||
interface FormData {
|
interface FormData {
|
||||||
title: string;
|
title: string;
|
||||||
listPublicId: string;
|
listPublicId: string;
|
||||||
|
labelPublicIds: string[];
|
||||||
memberPublicIds: string[];
|
memberPublicIds: string[];
|
||||||
|
isCreateAnotherEnabled: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface NewCardFormProps {
|
interface NewCardFormProps {
|
||||||
@@ -29,17 +31,20 @@ export function NewCardForm({ listPublicId }: NewCardFormProps) {
|
|||||||
const utils = api.useUtils();
|
const utils = api.useUtils();
|
||||||
const { boardData } = useBoard();
|
const { boardData } = useBoard();
|
||||||
const { closeModal } = useModal();
|
const { closeModal } = useModal();
|
||||||
const [isCreateAnotherEnabled, setIsCreateAnotherEnabled] = useState(false);
|
|
||||||
|
|
||||||
const { register, handleSubmit, reset, setValue, watch } = useForm<FormData>({
|
const { register, handleSubmit, reset, setValue, watch } = useForm<FormData>({
|
||||||
defaultValues: {
|
defaultValues: {
|
||||||
title: "",
|
title: "",
|
||||||
listPublicId,
|
listPublicId,
|
||||||
|
labelPublicIds: [],
|
||||||
memberPublicIds: [],
|
memberPublicIds: [],
|
||||||
|
isCreateAnotherEnabled: false,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const labelPublicIds = watch("labelPublicIds") || [];
|
||||||
const memberPublicIds = watch("memberPublicIds") || [];
|
const memberPublicIds = watch("memberPublicIds") || [];
|
||||||
|
const isCreateAnotherEnabled = watch("isCreateAnotherEnabled");
|
||||||
|
|
||||||
const refetchBoard = () =>
|
const refetchBoard = () =>
|
||||||
utils.board.byId.refetch({ id: boardData.publicId });
|
utils.board.byId.refetch({ id: boardData.publicId });
|
||||||
@@ -49,6 +54,13 @@ export function NewCardForm({ listPublicId }: NewCardFormProps) {
|
|||||||
try {
|
try {
|
||||||
await refetchBoard();
|
await refetchBoard();
|
||||||
if (!isCreateAnotherEnabled) closeModal();
|
if (!isCreateAnotherEnabled) closeModal();
|
||||||
|
reset({
|
||||||
|
title: "",
|
||||||
|
listPublicId: watch("listPublicId"),
|
||||||
|
labelPublicIds: [],
|
||||||
|
memberPublicIds: [],
|
||||||
|
isCreateAnotherEnabled: watch("isCreateAnotherEnabled"),
|
||||||
|
});
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log(e);
|
console.log(e);
|
||||||
}
|
}
|
||||||
@@ -61,6 +73,13 @@ export function NewCardForm({ listPublicId }: NewCardFormProps) {
|
|||||||
if (titleElement) titleElement.focus();
|
if (titleElement) titleElement.focus();
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
const formattedLabels =
|
||||||
|
boardData?.labels.map((label) => ({
|
||||||
|
key: label.publicId,
|
||||||
|
value: label.name,
|
||||||
|
selected: labelPublicIds.includes(label.publicId),
|
||||||
|
})) ?? [];
|
||||||
|
|
||||||
const formattedLists =
|
const formattedLists =
|
||||||
boardData?.lists.map((list) => ({
|
boardData?.lists.map((list) => ({
|
||||||
key: list.publicId,
|
key: list.publicId,
|
||||||
@@ -79,9 +98,13 @@ export function NewCardForm({ listPublicId }: NewCardFormProps) {
|
|||||||
createCard.mutate({
|
createCard.mutate({
|
||||||
title: data.title,
|
title: data.title,
|
||||||
listPublicId: data.listPublicId,
|
listPublicId: data.listPublicId,
|
||||||
|
labelsPublicIds: data.labelPublicIds,
|
||||||
memberPublicIds: data.memberPublicIds,
|
memberPublicIds: data.memberPublicIds,
|
||||||
});
|
});
|
||||||
reset();
|
};
|
||||||
|
|
||||||
|
const handleToggleCreateAnother = (): void => {
|
||||||
|
setValue("isCreateAnotherEnabled", !isCreateAnotherEnabled);
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleSelectList = (listPublicId: string): void => {
|
const handleSelectList = (listPublicId: string): void => {
|
||||||
@@ -99,6 +122,17 @@ export function NewCardForm({ listPublicId }: NewCardFormProps) {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const handleSelectLabels = (labelPublicId: string): void => {
|
||||||
|
const currentIndex = labelPublicIds.indexOf(labelPublicId);
|
||||||
|
if (currentIndex === -1) {
|
||||||
|
setValue("labelPublicIds", [...labelPublicIds, labelPublicId]);
|
||||||
|
} else {
|
||||||
|
const newLabelPublicIds = [...labelPublicIds];
|
||||||
|
newLabelPublicIds.splice(currentIndex, 1);
|
||||||
|
setValue("labelPublicIds", newLabelPublicIds);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
const selectedList = formattedLists.find((item) => item.selected);
|
const selectedList = formattedLists.find((item) => item.selected);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@@ -152,7 +186,7 @@ export function NewCardForm({ listPublicId }: NewCardFormProps) {
|
|||||||
{!memberPublicIds.length ? (
|
{!memberPublicIds.length ? (
|
||||||
"Members"
|
"Members"
|
||||||
) : (
|
) : (
|
||||||
<>
|
<div className="flex -space-x-1 overflow-hidden">
|
||||||
{memberPublicIds.map((memberPublicId) => {
|
{memberPublicIds.map((memberPublicId) => {
|
||||||
const member = formattedMembers.find(
|
const member = formattedMembers.find(
|
||||||
(member) => member.key === memberPublicId,
|
(member) => member.key === memberPublicId,
|
||||||
@@ -174,6 +208,55 @@ export function NewCardForm({ listPublicId }: NewCardFormProps) {
|
|||||||
</span>
|
</span>
|
||||||
);
|
);
|
||||||
})}
|
})}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</CheckboxDropdown>
|
||||||
|
</div>
|
||||||
|
<div className="w-fit">
|
||||||
|
<CheckboxDropdown
|
||||||
|
items={formattedLabels}
|
||||||
|
handleSelect={(list: { key: string }) =>
|
||||||
|
handleSelectLabels(list.key)
|
||||||
|
}
|
||||||
|
>
|
||||||
|
<div className="flex h-full w-full items-center rounded-[5px] border-[1px] border-dark-600 bg-dark-400 px-2 py-1 text-left text-xs text-dark-1000 hover:bg-dark-500">
|
||||||
|
{!labelPublicIds.length ? (
|
||||||
|
"Labels"
|
||||||
|
) : (
|
||||||
|
<>
|
||||||
|
<div
|
||||||
|
className={
|
||||||
|
labelPublicIds.length > 1
|
||||||
|
? "flex -space-x-[2px] overflow-hidden"
|
||||||
|
: "flex items-center"
|
||||||
|
}
|
||||||
|
>
|
||||||
|
{labelPublicIds.map((labelPublicId) => {
|
||||||
|
const label = boardData?.labels.find(
|
||||||
|
(label) => label.publicId === labelPublicId,
|
||||||
|
);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
<svg
|
||||||
|
fill={label?.colourCode}
|
||||||
|
className="h-2 w-2"
|
||||||
|
viewBox="0 0 6 6"
|
||||||
|
aria-hidden="true"
|
||||||
|
>
|
||||||
|
<circle cx={3} cy={3} r={3} />
|
||||||
|
</svg>
|
||||||
|
{labelPublicIds.length === 1 && (
|
||||||
|
<div className="ml-1">{label?.name}</div>
|
||||||
|
)}
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
})}
|
||||||
|
</div>
|
||||||
|
{labelPublicIds.length > 1 && (
|
||||||
|
<div className="ml-1">{`${labelPublicIds.length} labels`}</div>
|
||||||
|
)}
|
||||||
</>
|
</>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
@@ -185,7 +268,7 @@ export function NewCardForm({ listPublicId }: NewCardFormProps) {
|
|||||||
<span className="mr-2 text-xs text-dark-900">Create more</span>
|
<span className="mr-2 text-xs text-dark-900">Create more</span>
|
||||||
<Switch
|
<Switch
|
||||||
checked={isCreateAnotherEnabled}
|
checked={isCreateAnotherEnabled}
|
||||||
onChange={setIsCreateAnotherEnabled}
|
onChange={handleToggleCreateAnother}
|
||||||
className={classNames(
|
className={classNames(
|
||||||
isCreateAnotherEnabled ? "bg-indigo-600" : "bg-dark-800",
|
isCreateAnotherEnabled ? "bg-indigo-600" : "bg-dark-800",
|
||||||
"relative inline-flex h-4 w-6 flex-shrink-0 cursor-pointer rounded-full border-2 border-transparent transition-colors duration-200 ease-in-out focus:outline-none",
|
"relative inline-flex h-4 w-6 flex-shrink-0 cursor-pointer rounded-full border-2 border-transparent transition-colors duration-200 ease-in-out focus:outline-none",
|
||||||
|
|||||||
@@ -19,12 +19,19 @@ interface BoardContextProps {
|
|||||||
interface BoardData {
|
interface BoardData {
|
||||||
name: string;
|
name: string;
|
||||||
publicId: string;
|
publicId: string;
|
||||||
|
labels: Label[];
|
||||||
lists: List[];
|
lists: List[];
|
||||||
workspace: {
|
workspace: {
|
||||||
members: Members[];
|
members: Members[];
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface Label {
|
||||||
|
publicId: string;
|
||||||
|
name: string;
|
||||||
|
colourCode: string;
|
||||||
|
}
|
||||||
|
|
||||||
interface List {
|
interface List {
|
||||||
publicId: string;
|
publicId: string;
|
||||||
name: string;
|
name: string;
|
||||||
@@ -62,6 +69,7 @@ const initialBoardData: BoardData = {
|
|||||||
name: "",
|
name: "",
|
||||||
publicId: "",
|
publicId: "",
|
||||||
lists: [],
|
lists: [],
|
||||||
|
labels: [],
|
||||||
workspace: {
|
workspace: {
|
||||||
members: [],
|
members: [],
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -62,6 +62,13 @@ export const boardRouter = createTRPCRouter({
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
labels: {
|
||||||
|
columns: {
|
||||||
|
publicId: true,
|
||||||
|
colourCode: true,
|
||||||
|
name: true,
|
||||||
|
}
|
||||||
|
},
|
||||||
lists: {
|
lists: {
|
||||||
orderBy: [asc(lists.index)],
|
orderBy: [asc(lists.index)],
|
||||||
where: isNull(cards.deletedAt),
|
where: isNull(cards.deletedAt),
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ export const cardRouter = createTRPCRouter({
|
|||||||
z.object({
|
z.object({
|
||||||
title: z.string().min(1),
|
title: z.string().min(1),
|
||||||
listPublicId: z.string().min(12),
|
listPublicId: z.string().min(12),
|
||||||
|
labelsPublicIds: z.array(z.string().min(12)),
|
||||||
memberPublicIds: z.array(z.string().min(12))
|
memberPublicIds: z.array(z.string().min(12))
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
@@ -49,6 +50,18 @@ export const cardRouter = createTRPCRouter({
|
|||||||
index: latestCard ? latestCard.index + 1 : 0
|
index: latestCard ? latestCard.index + 1 : 0
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (newCard.insertId && input.labelsPublicIds.length) {
|
||||||
|
const labels = await tx.query.labels.findMany({
|
||||||
|
where: inArray(cards.publicId, input.labelsPublicIds),
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!labels.length) return;
|
||||||
|
|
||||||
|
const labelsInsert = labels.map((label) => ({ cardId: Number(newCard.insertId), labelId: label.id }))
|
||||||
|
|
||||||
|
await tx.insert(cardsToLabels).values(labelsInsert);
|
||||||
|
}
|
||||||
|
|
||||||
if (newCard.insertId && input.memberPublicIds.length) {
|
if (newCard.insertId && input.memberPublicIds.length) {
|
||||||
const members = await tx.query.workspaceMembers.findMany({
|
const members = await tx.query.workspaceMembers.findMany({
|
||||||
where: inArray(workspaceMembers.publicId, input.memberPublicIds),
|
where: inArray(workspaceMembers.publicId, input.memberPublicIds),
|
||||||
|
|||||||
Reference in New Issue
Block a user