@@ -347,13 +347,16 @@ const usePushCollection = () => {
347347 const repo = useRepoContext ( )
348348 const { collection } = useCollectionContext ( )
349349 const octokit = useOctokit ( )
350+ const onSettled = useInvalidateCollection ( )
350351
351- return ( update : ( c : Collection ) => void , message : string ) => {
352+ const mutate = ( update : ( c : Collection ) => void , message : string ) => {
352353 const copy = clone ( CollectionSchema , collection )
353354
354355 update ( copy )
355356 copy . sha = ""
356357
358+ client . setQueryData ( [ "repo" , repo . id , "collection" ] , copy )
359+
357360 return octokit . rest . repos . createOrUpdateFileContents ( {
358361 path : "collection.json" ,
359362 owner : account . login ,
@@ -364,6 +367,8 @@ const usePushCollection = () => {
364367 headers : { "If-None-Match" : "" } ,
365368 } )
366369 }
370+
371+ return { mutate, onSettled }
367372}
368373
369374const useInvalidateCollection = ( ) => {
@@ -374,41 +379,37 @@ const useInvalidateCollection = () => {
374379}
375380
376381export const useDeleteTags = ( ) => {
377- const pushCollection = usePushCollection ( )
378- const invalidateCollection = useInvalidateCollection ( )
382+ const { mutate, onSettled } = usePushCollection ( )
379383
380384 return useMutation ( {
381385 mutationFn : ( tags : string [ ] ) =>
382- pushCollection ( ( c ) => {
386+ mutate ( ( c ) => {
383387 for ( const name of tags ) {
384388 delete c . tags [ name ]
385389 }
386390 } , `deleted ${ tags . length } tag(s)` ) ,
387- onSettled : invalidateCollection ,
391+ onSettled,
388392 retry : handleError ( "Failed to delete tags" , { maxFailures : 0 } ) ,
389393 } )
390394}
391395
392396export const useNewTag = ( ) => {
393- const pushCollection = usePushCollection ( )
394- const invalidateCollection = useInvalidateCollection ( )
397+ const { mutate, onSettled } = usePushCollection ( )
395398
396399 return useMutation ( {
397400 mutationFn : ( tag : Tag ) =>
398- pushCollection ( ( c ) => ( c . tags [ tag . name ] = tag ) , `created tag - ${ tag . name } ` ) ,
399- onSettled : invalidateCollection ,
401+ mutate ( ( c ) => ( c . tags [ tag . name ] = tag ) , `created tag - ${ tag . name } ` ) ,
402+ onSettled,
400403 retry : handleError ( "Failed to create tag" , { maxFailures : 0 } ) ,
401404 } )
402405}
403406
404407export const useUpdateOrder = ( ) => {
405- const pushCollection = usePushCollection ( )
406- const invalidateCollection = useInvalidateCollection ( )
408+ const { mutate, onSettled } = usePushCollection ( )
407409
408410 return useMutation ( {
409- mutationFn : ( order : string [ ] ) =>
410- pushCollection ( ( c ) => ( c . order = order ) , `reordered habits` ) ,
411- onSettled : invalidateCollection ,
411+ mutationFn : ( order : string [ ] ) => mutate ( ( c ) => ( c . order = order ) , `reordered habits` ) ,
412+ onSettled,
412413 retry : handleError ( "Failed to update order" , { maxFailures : 0 } ) ,
413414 } )
414415}
0 commit comments