2025-01-21 12:00:09 +03:00
|
|
|
import { queryOptions } from '@tanstack/react-query';
|
2025-02-03 13:13:11 +03:00
|
|
|
import { z } from 'zod';
|
2025-01-21 12:00:09 +03:00
|
|
|
|
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-02-03 13:13:11 +03:00
|
|
|
import { IUserInfo, IUserProfile } from '@/models/user';
|
|
|
|
import { patterns } from '@/utils/constants';
|
|
|
|
import { errors, information } from '@/utils/labels';
|
2025-01-28 19:45:31 +03:00
|
|
|
|
2025-01-31 21:04:21 +03:00
|
|
|
/**
|
|
|
|
* Represents signup data, used to create new users.
|
|
|
|
*/
|
2025-02-03 13:13:11 +03:00
|
|
|
export const UserSignupSchema = z
|
|
|
|
.object({
|
|
|
|
username: z.string().nonempty(errors.requiredField).regex(RegExp(patterns.login), errors.loginFormat),
|
|
|
|
email: z.string().email(errors.emailField),
|
|
|
|
first_name: z.string(),
|
|
|
|
last_name: z.string(),
|
|
|
|
|
|
|
|
password: z.string().nonempty(errors.requiredField),
|
|
|
|
password2: z.string().nonempty(errors.requiredField)
|
|
|
|
})
|
|
|
|
.refine(schema => schema.password === schema.password2, { path: ['password2'], message: errors.passwordsMismatch });
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Represents signup data, used to create new users.
|
|
|
|
*/
|
|
|
|
export type IUserSignupDTO = z.infer<typeof UserSignupSchema>;
|
2025-01-31 21:04:21 +03:00
|
|
|
|
2025-01-21 20:33:05 +03:00
|
|
|
/**
|
|
|
|
* Represents user data, intended to update user profile in persistent storage.
|
|
|
|
*/
|
2025-02-03 18:48:29 +03:00
|
|
|
export const UpdateProfileSchema = z.object({
|
|
|
|
email: z.string().email(errors.emailField),
|
|
|
|
first_name: z.string(),
|
|
|
|
last_name: z.string()
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Represents user data, intended to update user profile in persistent storage.
|
|
|
|
*/
|
|
|
|
export type IUpdateProfileDTO = z.infer<typeof UpdateProfileSchema>;
|
2025-01-21 20:33:05 +03:00
|
|
|
|
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 }
|
|
|
|
})
|
|
|
|
}),
|
|
|
|
|
2025-02-03 13:13:11 +03:00
|
|
|
signup: (data: IUserSignupDTO) =>
|
|
|
|
axiosPost<IUserSignupDTO, IUserProfile>({
|
2025-01-28 19:45:31 +03:00
|
|
|
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
|
|
|
};
|