Skip to content

Commit

Permalink
refactor-krishnaacharyaa#418: replaced the rest of type Role
Browse files Browse the repository at this point in the history
  • Loading branch information
AMS003010 committed Jun 19, 2024
1 parent 18b8e16 commit e762c3a
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 54 deletions.
2 changes: 1 addition & 1 deletion backend/controllers/user-controller.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { HTTP_STATUS, RESPONSE_MESSAGES } from '../utils/constants.js';
import User from '../models/user.js';
import { Role } from '../types/role-type.js';
import { Role } from '../types/role-type.tsx';

export const getAllUserHandler = async (req, res) => {
try {
Expand Down
2 changes: 1 addition & 1 deletion backend/middlewares/auth-middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { JWT_SECRET } from '../config/utils.js';
import { ApiError } from '../utils/api-error.js';
import { HTTP_STATUS, RESPONSE_MESSAGES } from '../utils/constants.js';
import jwt from 'jsonwebtoken';
import { Role } from '../types/role-type.js';
import { Role } from '../types/role-type.tsx';

export const authMiddleware = async (req, res, next) => {
const token = req.cookies?.access_token;
Expand Down
5 changes: 3 additions & 2 deletions backend/models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import JWT from 'jsonwebtoken';
import bcrypt from 'bcryptjs';
import crypto from 'crypto';
import { ACCESS_TOKEN_EXPIRES_IN, JWT_SECRET, REFRESH_TOKEN_EXPIRES_IN } from '../config/utils.js';
import { Role } from '../types/role-type.tsx';

const userSchema = new Schema(
{
Expand Down Expand Up @@ -47,8 +48,8 @@ const userSchema = new Schema(
},
role: {
type: String,
default: 'USER',
enum: ['USER', 'ADMIN'],
default: Role.User,
enum: [Role.User, Role.Admin],
},
posts: [
{
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { useLayoutEffect } from 'react';
import RequireAuth from './components/require-auth';
import useThemeClass from './utils/theme-changer';
import AdminContainer from './components/admin-container';
import { Role } from './types/role-type';
import { Role } from './types/role-type.tsx';

function App() {
useLayoutEffect(() => {
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/layouts/header-layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import Loader from '@/components/skeletons/loader';
import useAuthData from '@/hooks/useAuthData';
import userState from '@/utils/user-state';
import { Link } from 'react-router-dom';
import { Role } from '@/types/role-type';
import { Role } from '@/types/role-type.tsx';

function header() {
const navigate = useNavigate();
Expand Down
88 changes: 40 additions & 48 deletions frontend/src/pages/admin-users.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,12 @@ import axiosInstance from '@/helpers/axios-instance';
import { useEffect, useState } from 'react';
import { toast } from 'react-toastify';
import 'react-toastify/dist/ReactToastify.css';

enum role {
admin = 'ADMIN',
user = 'USER',
}
import { Role } from '@/types/role-type';

type User = {
_id: string;
fullName: string;
role: role;
role: Role;
email: string;
};

Expand All @@ -27,9 +23,9 @@ const AdminUsers = () => {
}
};

const handleClick = async (userId: string, role: role) => {
const handleClick = async (userId: string, role: Role) => {
try {
const response = await axiosInstance.patch('/api/user/' + userId, { role: role });
const response = await axiosInstance.patch('/api/user/' + userId, { role });
if (response.status === 200) {
fetchData();
toast.success('User updated successfully!');
Expand All @@ -44,48 +40,44 @@ const AdminUsers = () => {
}, []);

return (
<>
<div className="w-full p-3 px-5 sm:p-12">
<h1 className="absolute left-16 top-3 text-2xl font-bold text-light-title dark:text-dark-title sm:static">
Users
</h1>
<div className="mt-2 sm:mt-12">
{users?.map((user: User) => {
return (
<div
key={user?._id}
className="mb-3 flex w-full flex-row items-center justify-between gap-5 rounded-lg border-b border-gray-300 bg-light px-3 py-4 shadow-md dark:border-gray-700 dark:bg-dark-card"
<div className="w-full p-3 px-5 sm:p-12">
<h1 className="absolute left-16 top-3 text-2xl font-bold text-light-title dark:text-dark-title sm:static">
Users
</h1>
<div className="mt-2 sm:mt-12">
{users?.map((user: User) => (
<div
key={user?._id}
className="mb-3 flex w-full flex-row items-center justify-between gap-5 rounded-lg border-b border-gray-300 bg-light px-3 py-4 shadow-md dark:border-gray-700 dark:bg-dark-card"
>
<div className="flex flex-col gap-[10px]">
<p className="text-base font-medium text-light-title dark:text-dark-title">
{user?.fullName}
</p>
<p className="text-base font-medium text-light-description dark:text-dark-description">
{user?.email}
</p>
</div>
{user.role === Role.Admin && (
<button
onClick={() => handleClick(user._id, Role.User)}
className="h-fit rounded-xl border border-black bg-black px-4 py-2 text-sm font-semibold text-white"
>
Admin
</button>
)}
{user.role === Role.User && (
<button
onClick={() => handleClick(user._id, Role.Admin)}
className="h-fit rounded-xl border border-black bg-transparent px-4 py-2 text-sm font-semibold text-black dark:text-white"
>
<div className="flex flex-col gap-[10px] ">
<p className="text-base font-medium text-light-title dark:text-dark-title">
{user?.fullName}
</p>
<p className="text-base font-medium text-light-description dark:text-dark-description">
{user?.email}
</p>
</div>
{user.role === role.admin && (
<button
onClick={() => handleClick(user._id, role.user)}
className="h-fit rounded-xl border border-black bg-black px-4 py-2 text-sm font-semibold text-white"
>
Admin
</button>
)}
{user.role === role.user && (
<button
onClick={() => handleClick(user._id, role.admin)}
className="h-fit rounded-xl border border-black bg-transparent px-4 py-2 text-sm font-semibold text-white"
>
Admin
</button>
)}
</div>
);
})}
</div>
User
</button>
)}
</div>
))}
</div>
</>
</div>
);
};

Expand Down

0 comments on commit e762c3a

Please sign in to comment.