global-notification.ts 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import { type SWRResponseWithUtils, withUtils } from '@growi/core/dist/swr';
  2. import useSWRImmutable from 'swr/immutable';
  3. import type { 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 = (
  9. globalNotificationId: string,
  10. ): SWRResponseWithUtils<Util, any, Error> => {
  11. const swrResult = useSWRImmutable(
  12. globalNotificationId != null
  13. ? `/notification-setting/global-notification/${globalNotificationId}`
  14. : null,
  15. (endpoint) =>
  16. apiv3Get(endpoint).then((response) => {
  17. return {
  18. globalNotification: response.data.globalNotification,
  19. };
  20. }),
  21. );
  22. const update = async (updateData: IGlobalNotification) => {
  23. const { data } = swrResult;
  24. if (data == null) {
  25. return;
  26. }
  27. // invoke API
  28. await apiv3Put(
  29. `/notification-setting/global-notification/${globalNotificationId}`,
  30. updateData,
  31. );
  32. };
  33. return withUtils<Util, any, Error>(swrResult, { update });
  34. };