@@ -5,6 +5,7 @@ import * as articleActions from "@/backend/services/article.actions";
55import * as tagActions from "@/backend/services/tag.action" ;
66import { ArticleRepositoryInput } from "@/backend/services/inputs/article.input" ;
77import MultipleSelector from "@/components/ui/multi-select" ;
8+ import { useImmer } from "use-immer" ;
89import { useDebouncedCallback } from "@/hooks/use-debounced-callback" ;
910import { useTranslation } from "@/i18n/use-translation" ;
1011import { useSession } from "@/store/session.atom" ;
@@ -53,7 +54,7 @@ const ArticleEditorDrawer: React.FC<Props> = ({ article, open, onClose }) => {
5354 } ,
5455 } ) ;
5556
56- const [ selectedTags , setSelectedTags ] = useState < Tag [ ] > ( article . tags ?? [ ] ) ;
57+ const [ selectedTags , setSelectedTags ] = useImmer < Tag [ ] > ( article . tags ?? [ ] ) ;
5758
5859 const setDebounceHandler = useDebouncedCallback ( async ( slug : string ) => {
5960 const handle = await articleActions . getUniqueArticleHandle ( slug ) ;
@@ -176,20 +177,41 @@ const ArticleEditorDrawer: React.FC<Props> = ({ article, open, onClose }) => {
176177 page : 1 ,
177178 search : searchTerm ,
178179 } ) ;
179-
180- const searchResult = res ?? [ ] ;
181- return searchResult ?. map ( ( tag ) => ( {
182- label : tag . name ,
183- value : tag . id ,
184- } ) ) ;
180+ return (
181+ res ?. map ( ( tag ) => ( {
182+ label : tag . name ,
183+ value : tag . id ,
184+ } ) ) ?? [ ]
185+ ) ;
185186 } }
186187 value = {
187188 selectedTags ?. map ( ( option ) => ( {
188189 label : option . name ,
189190 value : option . id ,
190191 } ) ) ?? [ ]
191192 }
193+ creatable
194+ onCreate = { async ( data ) => {
195+ const createdResponse = await tagActions . createTag ( {
196+ name : data ,
197+ } ) ;
198+ const old_tags = form . watch ( "tag_ids" ) ?? [ ] ;
199+
200+ setSelectedTags ( function ( draft ) {
201+ draft . push ( {
202+ id : createdResponse ?. id ! ,
203+ name : data ,
204+ created_at : new Date ( ) ,
205+ updated_at : new Date ( ) ,
206+ } ) ;
207+ } ) ;
208+ form . setValue ( "tag_ids" , [
209+ ...old_tags ,
210+ createdResponse ?. id ! as string ,
211+ ] ) ;
212+ } }
192213 onChange = { ( e ) => {
214+ console . log ( e ) ;
193215 setSelectedTags (
194216 e . map ( ( option ) => ( {
195217 id : option . value ,
0 commit comments