diff --git a/src/Utils/request/api.tsx b/src/Utils/request/api.tsx index 7424d4b1983..25a11d135fc 100644 --- a/src/Utils/request/api.tsx +++ b/src/Utils/request/api.tsx @@ -90,7 +90,13 @@ export interface LoginCredentials { password: string; } -type HttpMethod = "GET" | "POST" | "PUT" | "PATCH" | "DELETE"; +export enum HttpMethod { + GET = "GET", + POST = "POST", + PUT = "PUT", + PATCH = "PATCH", + DELETE = "DELETE", +} export const API = ( route: `${HttpMethod} ${string}`, @@ -923,26 +929,6 @@ const routes = { }, }, - // New user routes - user: { - list: { - path: "/api/v1/users/", - method: "GET", - TRes: Type>(), - }, - create: { - path: "/api/v1/users/", - method: "POST", - TRes: Type(), - TBody: Type(), - }, - get: { - path: "/api/v1/users/{username}/", - method: "GET", - TRes: Type(), - }, - }, - // OTP Routes otp: { sendOtp: { diff --git a/src/components/Common/UserSelector.tsx b/src/components/Common/UserSelector.tsx index b181c212022..986dfb5091d 100644 --- a/src/components/Common/UserSelector.tsx +++ b/src/components/Common/UserSelector.tsx @@ -23,10 +23,10 @@ import { Avatar } from "@/components/Common/Avatar"; import useDebouncedState from "@/hooks/useDebouncedState"; -import routes from "@/Utils/request/api"; import query from "@/Utils/request/query"; import { formatName } from "@/Utils/utils"; import { UserBase } from "@/types/user/user"; +import UserApi from "@/types/user/userApi"; interface Props { selected?: UserBase; @@ -47,7 +47,7 @@ export default function UserSelector({ const { data, isFetching } = useQuery({ queryKey: ["users", search], - queryFn: query(routes.user.list, { + queryFn: query(UserApi.list, { queryParams: { search_text: search }, }), }); diff --git a/src/components/Users/CreateUserForm.tsx b/src/components/Users/CreateUserForm.tsx index 09a3f5512f3..beafb4be950 100644 --- a/src/components/Users/CreateUserForm.tsx +++ b/src/components/Users/CreateUserForm.tsx @@ -26,10 +26,10 @@ import { import { GENDER_TYPES } from "@/common/constants"; import * as Notification from "@/Utils/Notifications"; -import routes from "@/Utils/request/api"; import request from "@/Utils/request/request"; import OrganizationSelector from "@/pages/Organization/components/OrganizationSelector"; import { UserBase } from "@/types/user/user"; +import UserApi from "@/types/user/userApi"; const userFormSchema = z .object({ @@ -122,7 +122,7 @@ export default function CreateUserForm({ onSubmitSuccess }: Props) { res, data: user, error, - } = await request(routes.user.create, { + } = await request(UserApi.create, { body: { ...data, // Omit c_password as it's not needed in the API diff --git a/src/pages/FacilityOrganization/components/LinkFacilityUserSheet.tsx b/src/pages/FacilityOrganization/components/LinkFacilityUserSheet.tsx index 29ea4b7db3b..0c1f999d251 100644 --- a/src/pages/FacilityOrganization/components/LinkFacilityUserSheet.tsx +++ b/src/pages/FacilityOrganization/components/LinkFacilityUserSheet.tsx @@ -29,6 +29,7 @@ import mutate from "@/Utils/request/mutate"; import query from "@/Utils/request/query"; import { formatName } from "@/Utils/utils"; import { UserBase } from "@/types/user/user"; +import UserApi from "@/types/user/userApi"; interface Props { organizationId: string; @@ -51,7 +52,7 @@ export default function LinkFacilityUserSheet({ const { data: preSelectedUser } = useQuery({ queryKey: ["user", preSelectedUsername], - queryFn: query(routes.user.get, { + queryFn: query(UserApi.get, { pathParams: { username: preSelectedUsername || "" }, }), enabled: !!preSelectedUsername, diff --git a/src/pages/Organization/components/LinkUserSheet.tsx b/src/pages/Organization/components/LinkUserSheet.tsx index 8c0634f8881..32cd87d2053 100644 --- a/src/pages/Organization/components/LinkUserSheet.tsx +++ b/src/pages/Organization/components/LinkUserSheet.tsx @@ -28,6 +28,7 @@ import routes from "@/Utils/request/api"; import mutate from "@/Utils/request/mutate"; import query from "@/Utils/request/query"; import { UserBase } from "@/types/user/user"; +import UserApi from "@/types/user/userApi"; interface Props { organizationId: string; @@ -48,7 +49,7 @@ export default function LinkUserSheet({ const { data: preSelectedUser } = useQuery({ queryKey: ["user", preSelectedUsername], - queryFn: query(routes.user.get, { + queryFn: query(UserApi.get, { pathParams: { username: preSelectedUsername || "" }, }), enabled: !!preSelectedUsername, diff --git a/src/types/user/userApi.ts b/src/types/user/userApi.ts new file mode 100644 index 00000000000..61292d5c473 --- /dev/null +++ b/src/types/user/userApi.ts @@ -0,0 +1,24 @@ +import { HttpMethod, Type } from "@/Utils/request/api"; +import { PaginatedResponse } from "@/Utils/request/types"; + +import { UserBase } from "./user"; + +export default { + list: { + path: "/api/v1/users/", + method: HttpMethod.GET, + TRes: Type>(), + }, + create: { + path: "/api/v1/users/", + method: HttpMethod.POST, + TRes: Type(), + TBody: Type(), + }, + get: { + path: "/api/v1/users/{username}/", + method: HttpMethod.GET, + + TRes: Type(), + }, +};