-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathuseUsers.ts
More file actions
117 lines (101 loc) · 2.72 KB
/
Copy pathuseUsers.ts
File metadata and controls
117 lines (101 loc) · 2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/**
* @AngelaMos | 2026
* useUsers.ts
*/
import {
type UseMutationResult,
type UseQueryResult,
useMutation,
useQuery,
useQueryClient,
} from '@tanstack/react-query'
import {
isValidUserResponse,
USER_ERROR_MESSAGES,
type UserCreateRequest,
type UserResponse,
UserResponseError,
type UserUpdateRequest,
} from '@/api/types'
import { apiClient, QUERY_STRATEGIES } from '@/core/api'
import { API_ENDPOINTS, QUERY_KEYS } from '@/core/config'
import { useAuthStore } from '@/core/lib'
import { authQueries } from './useAuth'
export const userQueries = {
all: () => QUERY_KEYS.USERS.ALL,
byId: (id: string) => QUERY_KEYS.USERS.BY_ID(id),
me: () => QUERY_KEYS.USERS.ME(),
} as const
const fetchUserById = async (id: string): Promise<UserResponse> => {
const response = await apiClient.get<unknown>(API_ENDPOINTS.USERS.BY_ID(id))
const data: unknown = response.data
if (!isValidUserResponse(data)) {
throw new UserResponseError(
USER_ERROR_MESSAGES.INVALID_USER_RESPONSE,
API_ENDPOINTS.USERS.BY_ID(id)
)
}
return data
}
export const useUser = (id: string): UseQueryResult<UserResponse, Error> => {
return useQuery({
queryKey: userQueries.byId(id),
queryFn: () => fetchUserById(id),
enabled: id.length > 0,
...QUERY_STRATEGIES.standard,
})
}
const performRegister = async (
data: UserCreateRequest
): Promise<UserResponse> => {
const response = await apiClient.post<unknown>(
API_ENDPOINTS.USERS.REGISTER,
data
)
const responseData: unknown = response.data
if (!isValidUserResponse(responseData)) {
throw new UserResponseError(
USER_ERROR_MESSAGES.INVALID_USER_RESPONSE,
API_ENDPOINTS.USERS.REGISTER
)
}
return responseData
}
export const useRegister = (): UseMutationResult<
UserResponse,
Error,
UserCreateRequest
> => {
return useMutation({
mutationFn: performRegister,
})
}
const performUpdateProfile = async (
data: UserUpdateRequest
): Promise<UserResponse> => {
const response = await apiClient.patch<unknown>(API_ENDPOINTS.USERS.ME, data)
const responseData: unknown = response.data
if (!isValidUserResponse(responseData)) {
throw new UserResponseError(
USER_ERROR_MESSAGES.INVALID_USER_RESPONSE,
API_ENDPOINTS.USERS.ME
)
}
return responseData
}
export const useUpdateProfile = (): UseMutationResult<
UserResponse,
Error,
UserUpdateRequest
> => {
const queryClient = useQueryClient()
const updateUser = useAuthStore((s) => s.updateUser)
return useMutation({
mutationFn: performUpdateProfile,
onSuccess: (data: UserResponse): void => {
updateUser(data)
queryClient.setQueryData(authQueries.me(), data)
queryClient.setQueryData(userQueries.me(), data)
},
})
}