2025-01-21 12:00:09 +03:00
|
|
|
import { queryOptions } from '@tanstack/react-query';
|
|
|
|
|
2025-01-28 23:23:03 +03:00
|
|
|
import { axiosGet, axiosPatch, axiosPost } from '@/backend/apiTransport';
|
2025-01-23 19:41:31 +03:00
|
|
|
import { DELAYS } from '@/backend/configuration';
|
2025-01-31 21:04:21 +03:00
|
|
|
import { IUser, IUserInfo, IUserProfile } from '@/models/user';
|
2025-01-28 19:45:31 +03:00
|
|
|
import { information } from '@/utils/labels';
|
|
|
|
|
2025-01-31 21:04:21 +03:00
|
|
|
/**
|
|
|
|
* Represents signup data, used to create new users.
|
|
|
|
*/
|
|
|
|
export interface IUserSignupData extends Omit<IUser, 'is_staff' | 'id'> {
|
|
|
|
password: string;
|
|
|
|
password2: string;
|
|
|
|
}
|
|
|
|
|
2025-01-21 20:33:05 +03:00
|
|
|
/**
|
|
|
|
* Represents user data, intended to update user profile in persistent storage.
|
|
|
|
*/
|
|
|
|
export interface IUpdateProfileDTO extends Omit<IUser, 'is_staff' | 'id'> {}
|
|
|
|
|
2025-01-21 12:00:09 +03:00
|
|
|
export const usersApi = {
|
|
|
|
baseKey: 'users',
|
2025-01-21 20:33:05 +03:00
|
|
|
getUsersQueryOptions: () =>
|
|
|
|
queryOptions({
|
2025-01-21 12:00:09 +03:00
|
|
|
queryKey: [usersApi.baseKey, 'list'],
|
2025-01-23 19:41:31 +03:00
|
|
|
staleTime: DELAYS.staleMedium,
|
2025-01-21 12:00:09 +03:00
|
|
|
queryFn: meta =>
|
2025-01-28 19:45:31 +03:00
|
|
|
axiosGet<IUserInfo[]>({
|
|
|
|
endpoint: '/users/api/active-users',
|
|
|
|
options: { signal: meta.signal }
|
|
|
|
})
|
2025-01-21 20:33:05 +03:00
|
|
|
}),
|
|
|
|
getProfileQueryOptions: () =>
|
|
|
|
queryOptions({
|
|
|
|
queryKey: [usersApi.baseKey, 'profile'],
|
2025-01-23 19:41:31 +03:00
|
|
|
staleTime: DELAYS.staleShort,
|
2025-01-21 20:33:05 +03:00
|
|
|
queryFn: meta =>
|
2025-01-28 19:45:31 +03:00
|
|
|
axiosGet<IUserProfile>({
|
|
|
|
endpoint: '/users/api/profile',
|
|
|
|
options: { signal: meta.signal }
|
|
|
|
})
|
|
|
|
}),
|
|
|
|
|
|
|
|
signup: (data: IUserSignupData) =>
|
|
|
|
axiosPost<IUserSignupData, IUserProfile>({
|
|
|
|
endpoint: '/users/api/signup',
|
|
|
|
request: {
|
|
|
|
data: data,
|
|
|
|
successMessage: createdUser => information.newUser(createdUser.username)
|
|
|
|
}
|
2025-01-21 20:33:05 +03:00
|
|
|
}),
|
|
|
|
|
2025-01-28 19:45:31 +03:00
|
|
|
updateProfile: (data: IUpdateProfileDTO) =>
|
|
|
|
axiosPatch<IUpdateProfileDTO, IUserProfile>({
|
|
|
|
endpoint: '/users/api/profile',
|
|
|
|
request: {
|
|
|
|
data: data,
|
|
|
|
successMessage: information.changesSaved
|
|
|
|
}
|
|
|
|
})
|
2025-01-21 12:00:09 +03:00
|
|
|
};
|