userStore.ts 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import { create } from 'zustand'
  2. import { getMyInfo, getMyEntList as _getMyEntList, getMyPrivacy } from '@/service/user'
  3. import { TEntItem, TUserInfo, TMyPrivacy } from '@/types/user'
  4. import { isSuccess } from '@/utils'
  5. export interface UserState {
  6. whoami: TUserInfo | null
  7. entList: TEntItem[]
  8. privacy: TMyPrivacy
  9. fetchtMyInfo: ()=> Promise<void>
  10. fetchMyPrivacy: ()=> Promise<void>
  11. fetchMyEntList: ()=> Promise<void>
  12. }
  13. export const useUserStore = create<UserState>((set) => ({
  14. whoami: null,
  15. privacy: {
  16. contactVisibleMyInfo: false,
  17. directVisibleMyInfo: false,
  18. forwardVisibleMyInfo: false
  19. },
  20. entList: [],
  21. fetchtMyInfo: async () => {
  22. const res = await getMyInfo()
  23. if(isSuccess(res.status)){
  24. set({ whoami: res.data})
  25. }
  26. },
  27. fetchMyPrivacy: async () => {
  28. const res = await getMyPrivacy()
  29. if(isSuccess(res.status)){
  30. set({ privacy: res.data})
  31. }
  32. },
  33. fetchMyEntList: async () => {
  34. const res = await _getMyEntList()
  35. if(isSuccess(res.status)){
  36. set({ entList: res.data})
  37. }
  38. },
  39. }))