Browse Source

mutation for rich attachment rendering

ryoji-s 2 years ago
parent
commit
e773b162fe
1 changed files with 6 additions and 3 deletions
  1. 6 3
      apps/app/src/stores/attachment.tsx

+ 6 - 3
apps/app/src/stores/attachment.tsx

@@ -4,7 +4,7 @@ import {
   IAttachmentHasId, Nullable, type SWRResponseWithUtils, withUtils,
   IAttachmentHasId, Nullable, type SWRResponseWithUtils, withUtils,
 } from '@growi/core';
 } from '@growi/core';
 import { Util } from 'reactstrap';
 import { Util } from 'reactstrap';
-import useSWR from 'swr';
+import useSWR, { useSWRConfig } from 'swr';
 
 
 import { apiPost } from '~/client/util/apiv1-client';
 import { apiPost } from '~/client/util/apiv1-client';
 import { apiv3Get } from '~/client/util/apiv3-client';
 import { apiv3Get } from '~/client/util/apiv3-client';
@@ -15,7 +15,7 @@ type Util = {
 };
 };
 
 
 type IDataAttachmentList = {
 type IDataAttachmentList = {
-  attachments: (IAttachmentHasId)[]
+  attachments: IAttachmentHasId[]
   totalAttachments: number
   totalAttachments: number
   limit: number
   limit: number
 };
 };
@@ -47,6 +47,7 @@ export const useSWRxAttachment = (attachmentId: string): SWRResponseWithUtils<Ut
 };
 };
 
 
 export const useSWRxAttachments = (pageId?: Nullable<string>, pageNumber?: number): SWRResponseWithUtils<Util, IDataAttachmentList, Error> => {
 export const useSWRxAttachments = (pageId?: Nullable<string>, pageNumber?: number): SWRResponseWithUtils<Util, IDataAttachmentList, Error> => {
+  const { mutate: mutateUseSWRxAttachment } = useSWRConfig();
   const shouldFetch = pageId != null && pageNumber != null;
   const shouldFetch = pageId != null && pageNumber != null;
 
 
   const fetcher = useCallback(async([endpoint, pageId, pageNumber]) => {
   const fetcher = useCallback(async([endpoint, pageId, pageNumber]) => {
@@ -72,11 +73,13 @@ export const useSWRxAttachments = (pageId?: Nullable<string>, pageNumber?: numbe
     try {
     try {
       await apiPost('/attachments.remove', body);
       await apiPost('/attachments.remove', body);
       mutate();
       mutate();
+      // Mutation for rich attachment rendering
+      mutateUseSWRxAttachment(['/attachment', body.attachment_id], body.attachment_id);
     }
     }
     catch (err) {
     catch (err) {
       throw err;
       throw err;
     }
     }
-  }, [swrResponse]);
+  }, [mutateUseSWRxAttachment, swrResponse]);
 
 
   return withUtils<Util, IDataAttachmentList, Error>(swrResponse, { remove });
   return withUtils<Util, IDataAttachmentList, Error>(swrResponse, { remove });
 };
 };