Skip to content

Commit

Permalink
Refactor user API routes and update components to use new structure (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
bodhish authored Jan 2, 2025
1 parent 3a9c7fe commit f450310
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 27 deletions.
28 changes: 7 additions & 21 deletions src/Utils/request/api.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <TResponse = undefined, TBody = undefined>(
route: `${HttpMethod} ${string}`,
Expand Down Expand Up @@ -923,26 +929,6 @@ const routes = {
},
},

// New user routes
user: {
list: {
path: "/api/v1/users/",
method: "GET",
TRes: Type<PaginatedResponse<UserBase>>(),
},
create: {
path: "/api/v1/users/",
method: "POST",
TRes: Type<UserBase>(),
TBody: Type<UserBase>(),
},
get: {
path: "/api/v1/users/{username}/",
method: "GET",
TRes: Type<UserBase>(),
},
},

// OTP Routes
otp: {
sendOtp: {
Expand Down
4 changes: 2 additions & 2 deletions src/components/Common/UserSelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 },
}),
});
Expand Down
4 changes: 2 additions & 2 deletions src/components/Users/CreateUserForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion src/pages/Organization/components/LinkUserSheet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand Down
24 changes: 24 additions & 0 deletions src/types/user/userApi.ts
Original file line number Diff line number Diff line change
@@ -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<PaginatedResponse<UserBase>>(),
},
create: {
path: "/api/v1/users/",
method: HttpMethod.POST,
TRes: Type<UserBase>(),
TBody: Type<UserBase>(),
},
get: {
path: "/api/v1/users/{username}/",
method: HttpMethod.GET,

TRes: Type<UserBase>(),
},
};

0 comments on commit f450310

Please sign in to comment.