Skip to content

Commit

Permalink
fix: role init + sharing types backend/frontend
Browse files Browse the repository at this point in the history
  • Loading branch information
thomhickey committed Nov 16, 2024
1 parent a59419e commit e6dd57e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/backend/routers/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { hasAuthenticated, hasAdmin, router } from "../trpc";
import { z } from "zod";
import { UserType, ROLE_OPTIONS } from "@/types/auth";

const sortOrderSchema = z.enum(["asc", "desc"]).default("asc");
const sortBySchema = z
export const sortOrderSchema = z.enum(["asc", "desc"]).default("asc");
export const sortBySchema = z
.enum(["first_name", "last_name", "email", "role"])
.default("first_name");

Expand Down
13 changes: 9 additions & 4 deletions src/pages/admin/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,23 @@ import {
} from "@mui/material";
import { SelectChangeEvent } from "@mui/material/Select";
import { getRoleLabel } from "@/types/auth";
import { sortBySchema, sortOrderSchema } from "@/backend/routers/user";
import { z } from "zod";

interface User extends BaseEntity {
user_id: string;
role: string;
}

type SortBy = z.infer<typeof sortBySchema>;
type SortOrder = z.infer<typeof sortOrderSchema>;

const AdminHome: React.FC = () => {
const utils = trpc.useContext();
const router = useRouter();
const [page, setPage] = useState(1);
const [sortBy, setSortBy] = useState<keyof User>("first_name");
const [sortOrder, setSortOrder] = useState<"asc" | "desc">("asc");
const [sortBy, setSortBy] = useState<SortBy>("first_name");
const [sortOrder, setSortOrder] = useState<SortOrder>("asc");
const [searchTerm, setSearchTerm] = useState("");
const pageSize = 2;

Expand All @@ -46,14 +51,14 @@ const AdminHome: React.FC = () => {
try {
await createUserMutation.mutateAsync({
...userData,
role: userData.role || "PARA", // Set default role if needed
role: userData.role || "PARA",

Check failure on line 54 in src/pages/admin/index.tsx

View workflow job for this annotation

GitHub Actions / type-check

Type 'string | number' is not assignable to type '"PARA" | "CASE_MANAGER" | "ADMIN"'.
});
} catch (error) {
console.error(error);
}
};

const handleSort = (newSortBy: keyof User, newSortOrder: "asc" | "desc") => {
const handleSort = (newSortBy: SortBy, newSortOrder: SortOrder) => {
setSortBy(newSortBy);
setSortOrder(newSortOrder);
};
Expand Down
12 changes: 8 additions & 4 deletions src/pages/users/[user_id].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { trpc } from "@/client/lib/trpc";
import { Box, Button, Container, Modal, Stack, TextField } from "@mui/material";
import Typography from "@mui/material/Typography";
import { useRouter } from "next/router";
import { useState } from "react";
import { useState, useEffect } from "react";
import { UserType, ROLE_OPTIONS } from "@/types/auth";
import $CompassModal from "@/components/design_system/modal/CompassModal.module.css";
import $button from "@/components/design_system/button/Button.module.css";
Expand Down Expand Up @@ -44,9 +44,13 @@ const ViewUserPage = () => {
onSuccess: () => utils.user.getUserById.invalidate(),
});

const [selectedRole, setSelectedRole] = useState(
user?.role.toUpperCase() || ""
);
const [selectedRole, setSelectedRole] = useState("");

useEffect(() => {
if (user?.role) {
setSelectedRole(user.role.toUpperCase());
}
}, [user?.role]);

const handleEditUser = (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault();
Expand Down

0 comments on commit e6dd57e

Please sign in to comment.