|
@@ -31,22 +31,25 @@ export const useSWRxPage = (
|
|
|
initialData?: IPagePopulatedToShowRevision|null,
|
|
initialData?: IPagePopulatedToShowRevision|null,
|
|
|
config?: SWRConfiguration,
|
|
config?: SWRConfiguration,
|
|
|
): SWRResponse<IPagePopulatedToShowRevision|null, Error> => {
|
|
): SWRResponse<IPagePopulatedToShowRevision|null, Error> => {
|
|
|
- const swrResponse = useSWRImmutable<IPagePopulatedToShowRevision|null, Error>(
|
|
|
|
|
|
|
+
|
|
|
|
|
+ const swrResponse = useSWRImmutable(
|
|
|
pageId != null ? ['/page', pageId, shareLinkId, revisionId] : null,
|
|
pageId != null ? ['/page', pageId, shareLinkId, revisionId] : null,
|
|
|
// TODO: upgrade SWR to v2 and use useSWRMutation
|
|
// TODO: upgrade SWR to v2 and use useSWRMutation
|
|
|
// in order to avoid complicated fetcher settings
|
|
// in order to avoid complicated fetcher settings
|
|
|
Object.assign({
|
|
Object.assign({
|
|
|
- fetcher: (endpoint, pageId, shareLinkId, revisionId) => apiv3Get<{ page: IPagePopulatedToShowRevision }>(endpoint, { pageId, shareLinkId, revisionId })
|
|
|
|
|
- .then(result => result.data.page)
|
|
|
|
|
- .catch((errs) => {
|
|
|
|
|
- if (!Array.isArray(errs)) { throw Error('error is not array') }
|
|
|
|
|
- const statusCode = errs[0].status;
|
|
|
|
|
- if (statusCode === 403 || statusCode === 404) {
|
|
|
|
|
- // for NotFoundPage
|
|
|
|
|
- return null;
|
|
|
|
|
- }
|
|
|
|
|
- throw Error('failed to get page');
|
|
|
|
|
- }),
|
|
|
|
|
|
|
+ fetcher: ([endpoint, pageId, shareLinkId, revisionId]: [string, string, string|undefined, string|undefined]) => {
|
|
|
|
|
+ return apiv3Get<{ page: IPagePopulatedToShowRevision }>(endpoint, { pageId, shareLinkId, revisionId })
|
|
|
|
|
+ .then(result => result.data.page)
|
|
|
|
|
+ .catch((errs) => {
|
|
|
|
|
+ if (!Array.isArray(errs)) { throw Error('error is not array') }
|
|
|
|
|
+ const statusCode = errs[0].status;
|
|
|
|
|
+ if (statusCode === 403 || statusCode === 404) {
|
|
|
|
|
+ // for NotFoundPage
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+ throw Error('failed to get page');
|
|
|
|
|
+ });
|
|
|
|
|
+ },
|
|
|
}, config ?? {}),
|
|
}, config ?? {}),
|
|
|
);
|
|
);
|
|
|
|
|
|
|
@@ -61,9 +64,9 @@ export const useSWRxPage = (
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
export const useSWRxPageByPath = (path?: string): SWRResponse<IPagePopulatedToShowRevision, Error> => {
|
|
export const useSWRxPageByPath = (path?: string): SWRResponse<IPagePopulatedToShowRevision, Error> => {
|
|
|
- return useSWR<IPagePopulatedToShowRevision, Error>(
|
|
|
|
|
|
|
+ return useSWR(
|
|
|
path != null ? ['/page', path] : null,
|
|
path != null ? ['/page', path] : null,
|
|
|
- (endpoint, path) => apiv3Get<{ page: IPagePopulatedToShowRevision }>(endpoint, { path }).then(result => result.data.page),
|
|
|
|
|
|
|
+ ([endpoint, path]) => apiv3Get<{ page: IPagePopulatedToShowRevision }>(endpoint, { path }).then(result => result.data.page),
|
|
|
);
|
|
);
|
|
|
};
|
|
};
|
|
|
|
|
|
|
@@ -99,9 +102,9 @@ export const useSWRxTagsInfo = (pageId: Nullable<string>): SWRResponse<IPageTags
|
|
|
|
|
|
|
|
const endpoint = `/pages.getPageTag?pageId=${pageId}`;
|
|
const endpoint = `/pages.getPageTag?pageId=${pageId}`;
|
|
|
|
|
|
|
|
- return useSWRImmutable<IPageTagsInfo | undefined, Error>(
|
|
|
|
|
|
|
+ return useSWRImmutable(
|
|
|
shareLinkId == null && pageId != null ? [endpoint, pageId] : null,
|
|
shareLinkId == null && pageId != null ? [endpoint, pageId] : null,
|
|
|
- (endpoint, pageId) => apiGet<IPageTagsInfo>(endpoint, { pageId }).then(result => result),
|
|
|
|
|
|
|
+ ([endpoint, pageId]) => apiGet<IPageTagsInfo>(endpoint, { pageId }).then(result => result),
|
|
|
);
|
|
);
|
|
|
};
|
|
};
|
|
|
|
|
|
|
@@ -120,9 +123,9 @@ export const useSWRxPageInfo = (
|
|
|
// assign null if shareLinkId is undefined in order to identify SWR key only by pageId
|
|
// assign null if shareLinkId is undefined in order to identify SWR key only by pageId
|
|
|
const fixedShareLinkId = shareLinkId ?? null;
|
|
const fixedShareLinkId = shareLinkId ?? null;
|
|
|
|
|
|
|
|
- const swrResult = useSWRImmutable<IPageInfo | IPageInfoForOperation, Error>(
|
|
|
|
|
|
|
+ const swrResult = useSWRImmutable(
|
|
|
pageId != null && termNumber != null ? ['/page/info', pageId, fixedShareLinkId, termNumber] : null,
|
|
pageId != null && termNumber != null ? ['/page/info', pageId, fixedShareLinkId, termNumber] : null,
|
|
|
- (endpoint, pageId, shareLinkId) => apiv3Get(endpoint, { pageId, shareLinkId }).then(response => response.data),
|
|
|
|
|
|
|
+ ([endpoint, pageId, shareLinkId]) => apiv3Get(endpoint, { pageId, shareLinkId }).then(response => response.data),
|
|
|
{ fallbackData: initialData },
|
|
{ fallbackData: initialData },
|
|
|
);
|
|
);
|
|
|
|
|
|
|
@@ -135,9 +138,9 @@ export const useSWRxPageRevisions = (
|
|
|
pageId: string | null | undefined,
|
|
pageId: string | null | undefined,
|
|
|
): SWRResponse<IRevisionsForPagination, Error> => {
|
|
): SWRResponse<IRevisionsForPagination, Error> => {
|
|
|
|
|
|
|
|
- return useSWRImmutable<IRevisionsForPagination, Error>(
|
|
|
|
|
|
|
+ return useSWRImmutable(
|
|
|
['/revisions/list', pageId, page, limit],
|
|
['/revisions/list', pageId, page, limit],
|
|
|
- (endpoint, pageId, page, limit) => {
|
|
|
|
|
|
|
+ ([endpoint, pageId, page, limit]) => {
|
|
|
return apiv3Get(endpoint, { pageId, page, limit }).then((response) => {
|
|
return apiv3Get(endpoint, { pageId, page, limit }).then((response) => {
|
|
|
const revisions = {
|
|
const revisions = {
|
|
|
revisions: response.data.docs,
|
|
revisions: response.data.docs,
|
|
@@ -162,7 +165,7 @@ export const useSWRxIsGrantNormalized = (
|
|
|
|
|
|
|
|
return useSWRImmutable(
|
|
return useSWRImmutable(
|
|
|
key,
|
|
key,
|
|
|
- (endpoint, pageId) => apiv3Get(endpoint, { pageId }).then(response => response.data),
|
|
|
|
|
|
|
+ ([endpoint, pageId]) => apiv3Get(endpoint, { pageId }).then(response => response.data),
|
|
|
);
|
|
);
|
|
|
};
|
|
};
|
|
|
|
|
|
|
@@ -172,7 +175,7 @@ export const useSWRxApplicableGrant = (
|
|
|
|
|
|
|
|
return useSWRImmutable(
|
|
return useSWRImmutable(
|
|
|
pageId != null ? ['/page/applicable-grant', pageId] : null,
|
|
pageId != null ? ['/page/applicable-grant', pageId] : null,
|
|
|
- (endpoint, pageId) => apiv3Get(endpoint, { pageId }).then(response => response.data),
|
|
|
|
|
|
|
+ ([endpoint, pageId]) => apiv3Get(endpoint, { pageId }).then(response => response.data),
|
|
|
);
|
|
);
|
|
|
};
|
|
};
|
|
|
|
|
|
|
@@ -187,7 +190,7 @@ export const useCurrentPagePath = (): SWRResponse<string | undefined, Error> =>
|
|
|
|
|
|
|
|
return useSWRImmutable(
|
|
return useSWRImmutable(
|
|
|
['currentPagePath', currentPage?.path, currentPathname],
|
|
['currentPagePath', currentPage?.path, currentPathname],
|
|
|
- (key: Key, pagePath: string|undefined, pathname: string|undefined) => {
|
|
|
|
|
|
|
+ ([, , pathname]) => {
|
|
|
if (currentPage?.path != null) {
|
|
if (currentPage?.path != null) {
|
|
|
return currentPage.path;
|
|
return currentPage.path;
|
|
|
}
|
|
}
|
|
@@ -206,7 +209,7 @@ export const useIsTrashPage = (): SWRResponse<boolean, Error> => {
|
|
|
|
|
|
|
|
return useSWRImmutable(
|
|
return useSWRImmutable(
|
|
|
pagePath == null ? null : ['isTrashPage', pagePath],
|
|
pagePath == null ? null : ['isTrashPage', pagePath],
|
|
|
- (key: Key, pagePath: string) => pagePathUtils.isTrashPage(pagePath),
|
|
|
|
|
|
|
+ ([, pagePath]) => pagePathUtils.isTrashPage(pagePath),
|
|
|
// TODO: set fallbackData
|
|
// TODO: set fallbackData
|
|
|
// { fallbackData: }
|
|
// { fallbackData: }
|
|
|
);
|
|
);
|