personal-settings.tsx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import { useTranslation } from 'next-i18next';
  2. import useSWR, { SWRConfiguration, SWRResponse } from 'swr';
  3. import { IExternalAccount } from '~/interfaces/external-account';
  4. import { IUser } from '~/interfaces/user';
  5. import { useIsGuestUser } from '~/stores/context';
  6. import loggerFactory from '~/utils/logger';
  7. import { apiv3Get, apiv3Put } from '../client/util/apiv3-client';
  8. import { useStaticSWR } from './use-static-swr';
  9. const logger = loggerFactory('growi:stores:personal-settings');
  10. export const useSWRxPersonalSettings = (config?: SWRConfiguration): SWRResponse<IUser, Error> => {
  11. const { data: isGuestUser } = useIsGuestUser();
  12. const key = !isGuestUser ? '/personal-setting' : null;
  13. return useSWR(
  14. key,
  15. endpoint => apiv3Get(endpoint).then(response => response.data.currentUser),
  16. config,
  17. );
  18. };
  19. export type IPersonalSettingsInfoOption = {
  20. sync: () => void,
  21. updateBasicInfo: () => Promise<void>,
  22. associateLdapAccount: (account: { username: string, password: string }) => Promise<void>,
  23. disassociateLdapAccount: (account: { providerType: string, accountId: string }) => Promise<void>,
  24. }
  25. export const usePersonalSettings = (config?: SWRConfiguration): SWRResponse<IUser, Error> & IPersonalSettingsInfoOption => {
  26. const { i18n } = useTranslation();
  27. const { data: personalSettingsDataFromDB, mutate: revalidate } = useSWRxPersonalSettings(config);
  28. const key = personalSettingsDataFromDB != null ? 'personalSettingsInfo' : null;
  29. const swrResult = useStaticSWR<IUser, Error>(key, undefined, { fallbackData: personalSettingsDataFromDB });
  30. // Sync with database
  31. const sync = async(): Promise<void> => {
  32. const { mutate } = swrResult;
  33. const result = await revalidate();
  34. mutate(result);
  35. };
  36. const updateBasicInfo = async(): Promise<void> => {
  37. const { data } = swrResult;
  38. if (data == null) {
  39. return;
  40. }
  41. const updateData = {
  42. name: data.name,
  43. email: data.email,
  44. isEmailPublished: data.isEmailPublished,
  45. lang: data.lang,
  46. slackMemberId: data.slackMemberId,
  47. };
  48. // invoke API
  49. try {
  50. await apiv3Put('/personal-setting/', updateData);
  51. i18n.changeLanguage(updateData.lang);
  52. }
  53. catch (err) {
  54. logger.error(err);
  55. throw new Error('Failed to update personal data');
  56. }
  57. };
  58. const associateLdapAccount = async(account): Promise<void> => {
  59. try {
  60. await apiv3Put('/personal-setting/associate-ldap', account);
  61. }
  62. catch (err) {
  63. logger.error(err);
  64. throw new Error('Failed to associate ldap account');
  65. }
  66. };
  67. const disassociateLdapAccount = async(account): Promise<void> => {
  68. try {
  69. await apiv3Put('/personal-setting/disassociate-ldap', account);
  70. }
  71. catch (err) {
  72. logger.error(err);
  73. throw new Error('Failed to disassociate ldap account');
  74. }
  75. };
  76. return {
  77. ...swrResult,
  78. sync,
  79. updateBasicInfo,
  80. associateLdapAccount,
  81. disassociateLdapAccount,
  82. };
  83. };
  84. export const useSWRxPersonalExternalAccounts = (): SWRResponse<IExternalAccount[], Error> => {
  85. return useSWR(
  86. '/personal-setting/external-accounts',
  87. endpoint => apiv3Get(endpoint).then(response => response.data.externalAccounts),
  88. );
  89. };