Skip to content

Commit 88f1aa0

Browse files
feat: implemented addOrRemoveLabel mutation on CardPage #266 (#270)
* feat: implemented addOrRemoveLabel mutation on CardPage to automatically add new labels #266 * Remove .env.backup
1 parent 06ca6e3 commit 88f1aa0

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

apps/web/src/views/card/index.tsx

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ export default function CardPage({ isTemplate }: { isTemplate?: boolean }) {
147147
getModalState,
148148
clearModalState,
149149
isOpen,
150+
modalStates,
150151
} = useModal();
151152
const { showPopup } = usePopup();
152153
const { workspace } = useWorkspace();
@@ -183,6 +184,21 @@ export default function CardPage({ isTemplate }: { isTemplate?: boolean }) {
183184
},
184185
});
185186

187+
const addOrRemoveLabel = api.card.addOrRemoveLabel.useMutation({
188+
onError: () => {
189+
showPopup({
190+
header: t`Unable to add label`,
191+
message: t`Please try again later, or contact customer support.`,
192+
icon: "error",
193+
});
194+
},
195+
onSettled: async () => {
196+
if (cardId) {
197+
await utils.card.byId.invalidate({ cardPublicId: cardId });
198+
}
199+
},
200+
});
201+
186202
const { register, handleSubmit, setValue, watch } = useForm<FormValues>({
187203
values: {
188204
cardId: cardId ?? "",
@@ -199,6 +215,24 @@ export default function CardPage({ isTemplate }: { isTemplate?: boolean }) {
199215
});
200216
};
201217

218+
// this adds the new created label to selected labels
219+
useEffect(() => {
220+
const newLabelId = modalStates.NEW_LABEL_CREATED;
221+
if (newLabelId && cardId) {
222+
const isAlreadyAdded = card?.labels.some(
223+
(label) => label.publicId === newLabelId,
224+
);
225+
226+
if (!isAlreadyAdded) {
227+
addOrRemoveLabel.mutate({
228+
cardPublicId: cardId,
229+
labelPublicId: newLabelId,
230+
});
231+
}
232+
clearModalState("NEW_LABEL_CREATED");
233+
}
234+
}, [modalStates.NEW_LABEL_CREATED, card, cardId]);
235+
202236
// Open the new item form after creating a new checklist
203237
useEffect(() => {
204238
if (!card) return;

0 commit comments

Comments
 (0)