1- import { VariantType , useToastProvider } from '@openfun/cunningham-react' ;
2- import { useMutation , useQueryClient } from '@tanstack/react-query' ;
3- import { useTranslation } from 'react-i18next' ;
1+ import {
2+ UseMutationOptions ,
3+ useMutation ,
4+ useQueryClient ,
5+ } from '@tanstack/react-query' ;
46
57import { APIError , errorCauses , fetchAPI } from '@/api' ;
6- import { Doc } from '@/docs/doc-management' ;
8+ import { Doc , LinkReach , LinkRole } from '@/docs/doc-management' ;
79
810export type UpdateDocLinkParams = Pick < Doc , 'id' | 'link_reach' > &
911 Partial < Pick < Doc , 'link_role' > > ;
1012
13+ type UpdateDocLinkResponse = { link_role : LinkRole ; link_reach : LinkReach } ;
14+
1115export const updateDocLink = async ( {
1216 id,
1317 ...params
14- } : UpdateDocLinkParams ) : Promise < Doc > => {
18+ } : UpdateDocLinkParams ) : Promise < UpdateDocLinkResponse > => {
1519 const response = await fetchAPI ( `documents/${ id } /link-configuration/` , {
1620 method : 'PUT' ,
1721 body : JSON . stringify ( {
@@ -26,40 +30,31 @@ export const updateDocLink = async ({
2630 ) ;
2731 }
2832
29- return response . json ( ) as Promise < Doc > ;
33+ return response . json ( ) as Promise < UpdateDocLinkResponse > ;
3034} ;
3135
32- interface UpdateDocLinkProps {
33- onSuccess ?: ( data : Doc ) => void ;
36+ type UseUpdateDocLinkOptions = UseMutationOptions <
37+ UpdateDocLinkResponse ,
38+ APIError ,
39+ UpdateDocLinkParams
40+ > & {
3441 listInvalidQueries ?: string [ ] ;
35- }
42+ } ;
3643
37- export function useUpdateDocLink ( {
38- onSuccess,
39- listInvalidQueries,
40- } : UpdateDocLinkProps = { } ) {
44+ export function useUpdateDocLink ( options ?: UseUpdateDocLinkOptions ) {
4145 const queryClient = useQueryClient ( ) ;
42- const { toast } = useToastProvider ( ) ;
43- const { t } = useTranslation ( ) ;
4446
45- return useMutation < Doc , APIError , UpdateDocLinkParams > ( {
47+ return useMutation < UpdateDocLinkResponse , APIError , UpdateDocLinkParams > ( {
4648 mutationFn : updateDocLink ,
47- onSuccess : ( data ) => {
48- listInvalidQueries ?. forEach ( ( queryKey ) => {
49+ ...options ,
50+ onSuccess : ( data , variables , onMutateResult , context ) => {
51+ options ?. listInvalidQueries ?. forEach ( ( queryKey ) => {
4952 void queryClient . invalidateQueries ( {
5053 queryKey : [ queryKey ] ,
5154 } ) ;
5255 } ) ;
5356
54- toast (
55- t ( 'The document visibility has been updated.' ) ,
56- VariantType . SUCCESS ,
57- {
58- duration : 2000 ,
59- } ,
60- ) ;
61-
62- onSuccess ?.( data ) ;
57+ options ?. onSuccess ?.( data , variables , onMutateResult , context ) ;
6358 } ,
6459 } ) ;
6560}
0 commit comments