Skip to content

Commit

Permalink
Merge pull request #138 from NUS-Fintech-Society/fix-the-bug-on-edit-…
Browse files Browse the repository at this point in the history
…user

Fix up the UIUX and the bug for edit modal
  • Loading branch information
woowenjun99 committed Jul 18, 2023
2 parents 068e81e + ee84b59 commit 16c4f45
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 19 deletions.
37 changes: 18 additions & 19 deletions src/components/users/EditModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
import { useForm } from 'react-hook-form'
import { useRouter } from 'next/router'
import { trpc } from '~/utils/trpc'
import { useContext, useCallback, useState } from 'react'
import { useContext, useCallback } from 'react'
import { ModalContext } from '~/context/ModalContext'
import type { User } from '~/server/db/models/User'
import { roles } from '~/constant/roles'
Expand All @@ -26,36 +26,43 @@ const EditModal = () => {
const modal = useContext(ModalContext)
const toast = useToast()
const router = useRouter()
const [department, setDepartment] = useState('')

const { register, handleSubmit } = useForm({
mode: 'onSubmit',
})

const { data, isLoading, refetch } = trpc.user.getUserProfile.useQuery(
const { data, isLoading } = trpc.user.getUserProfile.useQuery(
modal.id
)
const { mutateAsync, isLoading: loading } =
trpc.user.updateUserProfile.useMutation()
const { refetch } = trpc.user.getAllUsersForTable.useQuery()

const onSubmit = useCallback(
async (formData: Partial<User>) => {
try {
const projData = {
const filteredRole = roles.find(role => role.role === formData.role)

const department = filteredRole ? filteredRole.department : data?.department

await mutateAsync({
id: data?.id as string,
name: formData.name as string,
email: formData.email as string,
department,
department: department as string,
role: formData.role as string,
}
await mutateAsync(projData)
})

await refetch()

toast({
duration: 9000,
duration: 3000,
description: 'User has been successfully updated',
title: 'Successfully updated',
status: 'success',
})
router.push('./')

modal.onClose()
} catch (e) {
toast({
duration: 9000,
Expand All @@ -65,7 +72,7 @@ const EditModal = () => {
})
}
},
[mutateAsync, data?.id, toast, department, refetch, router]
[mutateAsync, data?.id, toast, data?.department, router, refetch]
)

if (!modal.id || isLoading) return null
Expand All @@ -88,7 +95,7 @@ const EditModal = () => {
defaultValue={(data as User).name}
/>

<FormLabel fontWeight={'semibold'}>Email address</FormLabel>
<FormLabel fontWeight={'semibold'}>NUS Email</FormLabel>
<Input
mb={'5'}
{...register('email')}
Expand All @@ -102,14 +109,6 @@ const EditModal = () => {
isRequired
defaultValue={(data as User).role}
{...register('role')}
onChange={(e) => {
if (!e.target.value) return
const element = roles.filter(
(role) => role.role === e.target.value
)
if (!element || !element.length || !element[0]) return
setDepartment(element[0].department)
}}
placeholder="Select role"
>
{roles.map((role, index) => {
Expand Down
2 changes: 2 additions & 0 deletions src/server/trpc/router/users/updateUserProfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export const updateUserProfile = protectedProcedure
z.object({
id: z.string(),
name: z.string(),
email: z.string(),
role: z.string(),
department: z.string(),
})
Expand All @@ -21,6 +22,7 @@ export const updateUserProfile = protectedProcedure
}

await userCollection.update(input.id, {
email: input.email,
department: input.department,
name: input.name,
role: input.role,
Expand Down

0 comments on commit 16c4f45

Please sign in to comment.