@@ -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