global-notification.ts 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. import { type SWRResponseWithUtils, withUtils } from '@growi/core/dist/swr';
  2. import useSWRImmutable from 'swr/immutable';
  3. import { IGlobalNotification } from '~/client/interfaces/global-notification';
  4. import { apiv3Get, apiv3Put } from '../client/util/apiv3-client';
  5. type Util = {
  6. update(updateData: any): Promise<void>
  7. };
  8. export const useSWRxGlobalNotification = (globalNotificationId: string): SWRResponseWithUtils<Util, any, Error> => {
  9. const swrResult = useSWRImmutable(
  10. globalNotificationId != null ? `/notification-setting/global-notification/${globalNotificationId}` : null,
  11. endpoint => apiv3Get(endpoint).then((response) => {
  12. return {
  13. globalNotification: response.data.globalNotification,
  14. };
  15. }),
  16. );
  17. const update = async(updateData: IGlobalNotification) => {
  18. const { data } = swrResult;
  19. if (data == null) {
  20. return;
  21. }
  22. // invoke API
  23. await apiv3Put(`/notification-setting/global-notification/${globalNotificationId}`, updateData);
  24. };
  25. return withUtils<Util, any, Error>(swrResult, { update });
  26. };