fix: add new created label to selected label (#136)

This commit is contained in:
Ridham Khandar
2025-08-10 01:05:29 +05:30
committed by GitHub
parent d555546782
commit 4a67afdd3a
4 changed files with 54 additions and 6 deletions

View File

@@ -12,11 +12,14 @@ export function DeleteLabelConfirmation({
labelPublicId: string; labelPublicId: string;
refetch: () => void; refetch: () => void;
}) { }) {
const { closeModal } = useModal(); const { closeModal, closeModals } = useModal();
const { showPopup } = usePopup(); const { showPopup } = usePopup();
const deleteLabelMutation = api.label.delete.useMutation({ const deleteLabelMutation = api.label.delete.useMutation({
onSuccess: () => refetch(), onSuccess: () => {
refetch();
closeModals(2);
},
onError: () => onError: () =>
showPopup({ showPopup({
header: t`Error deleting label`, header: t`Error deleting label`,
@@ -26,7 +29,6 @@ export function DeleteLabelConfirmation({
}); });
const handleDeleteLabel = () => { const handleDeleteLabel = () => {
closeModal();
deleteLabelMutation.mutate({ deleteLabelMutation.mutate({
labelPublicId, labelPublicId,
}); });

View File

@@ -32,7 +32,7 @@ export function LabelForm({
refetch: () => void; refetch: () => void;
isEdit?: boolean; isEdit?: boolean;
}) { }) {
const { closeModal, entityId, openModal } = useModal(); const { closeModal, entityId, openModal, setModalState } = useModal();
const label = api.label.byPublicId.useQuery( const label = api.label.byPublicId.useQuery(
{ {
@@ -57,12 +57,13 @@ export function LabelForm({
const isCreateAnotherEnabled = watch("isCreateAnotherEnabled"); const isCreateAnotherEnabled = watch("isCreateAnotherEnabled");
const createLabel = api.label.create.useMutation({ const createLabel = api.label.create.useMutation({
onSuccess: () => { onSuccess: (newLabel) => {
const currentColourIndex = colours.findIndex( const currentColourIndex = colours.findIndex(
(c) => c.code === watch("colour").code, (c) => c.code === watch("colour").code,
); );
try { try {
refetch(); refetch();
setModalState("NEW_LABEL_CREATED", newLabel.publicId);
if (!isCreateAnotherEnabled) { if (!isCreateAnotherEnabled) {
closeModal(); closeModal();
} else { } else {
@@ -220,6 +221,7 @@ export function LabelForm({
<div className="space-x-2"> <div className="space-x-2">
{isEdit && ( {isEdit && (
<Button <Button
type="button"
variant="secondary" variant="secondary"
onClick={() => openModal("DELETE_LABEL", entityId)} onClick={() => openModal("DELETE_LABEL", entityId)}
> >

View File

@@ -18,6 +18,8 @@ type ModalContextType = {
entityLabel?: string, entityLabel?: string,
) => void; ) => void;
closeModal: () => void; closeModal: () => void;
closeModals: (count: number) => void;
clearAllModals: () => void;
modalContentType: string; modalContentType: string;
entityId: string; entityId: string;
entityLabel: string; entityLabel: string;
@@ -58,6 +60,17 @@ export const ModalProvider: React.FC<Props> = ({ children }) => {
}); });
}; };
const closeModals = (count: number) => {
setModalStack(prev => {
const newLength = Math.max(0, prev.length - count);
return prev.slice(0, newLength);
});
};
const clearAllModals = () => {
setModalStack([]);
};
const setModalState = (modalType: string, state: any) => { const setModalState = (modalType: string, state: any) => {
setModalStates(prev => ({ setModalStates(prev => ({
...prev, ...prev,
@@ -87,6 +100,8 @@ export const ModalProvider: React.FC<Props> = ({ children }) => {
isOpen, isOpen,
openModal, openModal,
closeModal, closeModal,
closeModals,
clearAllModals,
modalContentType, modalContentType,
entityId, entityId,
entityLabel, entityLabel,

View File

@@ -46,7 +46,7 @@ export function NewCardForm({
queryParams, queryParams,
}: NewCardFormProps) { }: NewCardFormProps) {
const { showPopup } = usePopup(); const { showPopup } = usePopup();
const { closeModal, openModal } = useModal(); const { closeModal, openModal, modalStates, clearModalState } = useModal();
const utils = api.useUtils(); const utils = api.useUtils();
@@ -85,10 +85,39 @@ export function NewCardForm({
return () => subscription.unsubscribe(); return () => subscription.unsubscribe();
}, [watch, saveFormState]); }, [watch, saveFormState]);
const { data: boardData } = api.board.byId.useQuery(queryParams, { const { data: boardData } = api.board.byId.useQuery(queryParams, {
enabled: !!boardPublicId, enabled: !!boardPublicId,
}); });
// this adds the new created label to selected labels
useEffect(() => {
const newLabelId = modalStates["NEW_LABEL_CREATED"];
if (newLabelId !== undefined && !labelPublicIds.includes(newLabelId)) {
setValue("labelPublicIds", [...labelPublicIds, newLabelId]);
}
}, [modalStates, labelPublicIds]);
// this removes the deleted label from selected labels if it is selected
useEffect(() => {
if (boardData?.labels) {
const availableLabelIds = boardData.labels.map(label => label.publicId);
const newLabelId = modalStates["NEW_LABEL_CREATED"];
if (newLabelId && availableLabelIds.includes(newLabelId)) {
clearModalState("NEW_LABEL_CREATED");
}
const validLabelIds = labelPublicIds.filter(id =>
availableLabelIds.includes(id) || id === newLabelId
);
if (validLabelIds.length !== labelPublicIds.length) {
setValue("labelPublicIds", validLabelIds);
}
}
}, [boardData?.labels, labelPublicIds, modalStates["NEW_LABEL_CREATED"]]);
const createCard = api.card.create.useMutation({ const createCard = api.card.create.useMutation({
onMutate: async (args) => { onMutate: async (args) => {
await utils.board.byId.cancel(); await utils.board.byId.cancel();