Skip to content

Commit

Permalink
fix: render button only for admins
Browse files Browse the repository at this point in the history
  • Loading branch information
shishirbychapur committed Oct 1, 2024
1 parent 4a41547 commit 26b8e27
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 25 deletions.
11 changes: 6 additions & 5 deletions frontend/components/auth/Login.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
'use client'

import { tokenState, userState } from '@/atoms/auth'
import validateInput, { initialFormValues } from '@/util/input-validation'

import { Button } from '../ui/button'
import { InputField } from '../customs/custom-input'
import { PasswordReset } from './PasswordReset'
import React from 'react'
import { loginRequest } from '@/services/user-service-api'
import { toast } from 'sonner'
import usePasswordToggle from '../../hooks/UsePasswordToggle'
import { useState } from 'react'
import { loginRequest } from '@/services/user-service-api'
import { useSetRecoilState } from 'recoil'
import { tokenState, userState } from '@/atoms/auth'
import { useRouter } from 'next/router'
import React from 'react'
import { useSetRecoilState } from 'recoil'
import { useState } from 'react'

export default function Login() {
const [formValues, setFormValues] = useState({ ...initialFormValues })
Expand Down Expand Up @@ -55,6 +55,7 @@ export default function Login() {
sessionStorage.setItem('email', res.email)
sessionStorage.setItem('TTL', new Date().toString())
sessionStorage.setItem('isAuth', 'true')
sessionStorage.setItem('role', res.role)
setIsAuth(true)
setIsValid(true)
router.push('/')
Expand Down
48 changes: 28 additions & 20 deletions frontend/pages/questions/index.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
'use client'

import { Button } from '@/components/ui/button'
import { useEffect, useState } from 'react'
import Datatable from '@/components/customs/datatable'
import {
Difficulty,
IGetQuestions,
Expand All @@ -14,20 +11,25 @@ import {
SortDirection,
} from '@/types'
import { columns, formFields } from './props'
import CustomModal from '@/components/customs/custom-modal'
import CustomForm from '@/components/customs/custom-form'
import ConfirmDialog from '@/components/customs/confirm-dialog'
import { capitalizeFirst } from '@/util/string-modification'
import {
createQuestionRequest,
deleteQuestionById,
getQuestionbyIDRequest,
getQuestionsRequest,
updateQuestionRequest,
} from '@/services/question-service-api'
import { useEffect, useState } from 'react'

import { Button } from '@/components/ui/button'
import ConfirmDialog from '@/components/customs/confirm-dialog'
import CustomForm from '@/components/customs/custom-form'
import CustomModal from '@/components/customs/custom-modal'
import Datatable from '@/components/customs/datatable'
import { capitalizeFirst } from '@/util/string-modification'
import { toast } from 'sonner'

export default function Questions() {
const [isAdmin, setIsAdmin] = useState(false)
const [data, setData] = useState<IQuestion[]>([])
const [isLoading, setLoading] = useState<boolean>(false)
const [pagination, setPagination] = useState<IPagination>({
Expand Down Expand Up @@ -102,6 +104,10 @@ export default function Questions() {
setIsInit(true)
}, [isInit])

useEffect(() => {
setIsAdmin(sessionStorage.getItem('role') === 'ADMIN')
}, [])

const sortHandler = (sortBy: ISortBy) => {
setSortBy(sortBy)
const body: IGetQuestions = {
Expand Down Expand Up @@ -235,19 +241,21 @@ export default function Questions() {
<div className="m-8">
<div className="flex items-center justify-between mb-4">
<h2 className="text-xl font-bold">Questions</h2>
<Button
variant={'primary'}
onClick={() => {
setModalData({
...modalData,
title: 'Create new question',
isOpen: true,
})
setModificationType(Modification.CREATE)
}}
>
Create
</Button>
{isAdmin && (
<Button
variant={'primary'}
onClick={() => {
setModalData({
...modalData,
title: 'Create new question',
isOpen: true,
})
setModificationType(Modification.CREATE)
}}
>
Create
</Button>
)}
</div>
<Datatable
data={data}
Expand Down

0 comments on commit 26b8e27

Please sign in to comment.