Skip to content

Commit

Permalink
refactor: add username selection to question page & integrate new URLs (
Browse files Browse the repository at this point in the history
#16)

* refactor: add username selection to question page & integreate new URLs

* test: ensure they know to look for username when testing navbar
  • Loading branch information
Gum-Joe authored Jul 4, 2024
1 parent fa5bb8d commit 114d892
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const router = createBrowserRouter([
element: <FrontCover />,
},
{
path: 'questions/:questionId',
path: 'questions/:questionId/:username',
element: <QuestionPage />,
},
{
Expand Down
2 changes: 1 addition & 1 deletion src/api/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const routes = {
studentAnswers: (studentID: string) => `/${studentID}/answers`,
studentMarks: (studentID: string) => `/${studentID}/marks`,
question: (number: number) => `/questions/${number}`,
questionAnswers: (number: number) => `/questions/${number}/answer`,
questionAnswers: (number: number, username: string) => `/answers/${username}/question/${number}`,
}

export default routes
6 changes: 4 additions & 2 deletions src/components/NavBar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { Container, Section, TabNav } from '@radix-ui/themes'
import React, { FC } from 'react'
import { useLocation } from 'react-router-dom'

import { DEFAULT_TEST_USERNAME } from '../../utils/globalConstants'

interface NavBarProps {
questionCount: number
}
Expand All @@ -24,9 +26,9 @@ const NavBar: FC<NavBarProps> = ({ questionCount }) => {
</TabNav.Link>
{[...Array(questionCount).keys()].map((i) => (
<TabNav.Link
href={`/questions/${i + 1}`}
href={`/questions/${i + 1}/${DEFAULT_TEST_USERNAME}`}
key={i}
active={pathname === `/questions/${i + 1}`}
active={pathname.startsWith(`/questions/${i + 1}`)}
>
{`Question ${i + 1}`}
</TabNav.Link>
Expand Down
9 changes: 7 additions & 2 deletions src/hooks/exam.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { plainToInstance } from 'class-transformer'
import { useCallback, useEffect, useMemo, useState } from 'react'
import { useParams } from 'react-router-dom'

import axiosInstance from '../api/axiosInstance'
import routes from '../api/routes'
import { Answer, AnswerMap, Question, Summary } from '../types/exam'
import { buildResourceLookupTable } from '../utils/answers'
import { DEFAULT_TEST_USERNAME } from '../utils/globalConstants'

export const useQuestion = (number: number | undefined) => {
const [question, setQuestion] = useState<Question>()
Expand All @@ -22,10 +24,13 @@ export const useQuestion = (number: number | undefined) => {
export const useQuestionAnswers = (number: number | undefined) => {
const [answers, setAnswers] = useState<Answer[]>([])
const [answersAreLoaded, setAnswersAreLoaded] = useState(false)

const { username = DEFAULT_TEST_USERNAME } = useParams()

useEffect(() => {
if (number === undefined) return
axiosInstance
.get(routes.questionAnswers(number))
.get(routes.questionAnswers(number, username))
.then(({ data }) => setAnswers(data.map((d) => plainToInstance(Answer, d))))
.finally(() => setAnswersAreLoaded(true))
}, [number])
Expand Down Expand Up @@ -54,7 +59,7 @@ export const useQuestionAnswers = (number: number | undefined) => {

const saveAnswers = useCallback(() => {
if (number === undefined) return
axiosInstance.post(routes.questionAnswers(number), answers).then(() => {})
axiosInstance.post(routes.questionAnswers(number, username), answers).then(() => {})
}, [answers, number])

return { lookupAnswer, answersAreLoaded, setAnswer, saveAnswers }
Expand Down
4 changes: 3 additions & 1 deletion src/pages/QuestionPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ import { parseAnswer } from '../utils/answers'

const QuestionPage: FC = () => {
const { pathname } = useLocation()
const pathMatch = matchPath({ path: '/questions/:number' }, pathname)
const pathMatch = matchPath({ path: '/questions/:number/:username' }, pathname)

console.log(pathMatch)

const { question, questionIsLoaded } = useQuestion(Number(pathMatch?.params?.number))
const { lookupAnswer, setAnswer, saveAnswers } = useQuestionAnswers(
Expand Down
5 changes: 3 additions & 2 deletions src/tests/components/NavBar.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import React from 'react'
import { MemoryRouter, Route, Routes } from 'react-router-dom'

import NavBar from '../../components/NavBar'
import { DEFAULT_TEST_USERNAME } from '../../utils/globalConstants'

describe('NavBar', () => {
const renderWithRouter = (ui, { route = '/' } = {}) => {
Expand All @@ -29,12 +30,12 @@ describe('NavBar', () => {
for (let i = 1; i <= questionCount; i++) {
const questionLink = screen.getByRole('link', { name: new RegExp(`question ${i}`, 'i') })
expect(questionLink).toBeInTheDocument()
expect(questionLink).toHaveAttribute('href', `/questions/${i}`)
expect(questionLink).toHaveAttribute('href', `/questions/${i}/${DEFAULT_TEST_USERNAME}`)
}
})

it('highlights the correct active link based on the current path', () => {
const activeRoute = '/questions/3'
const activeRoute = `/questions/3/${DEFAULT_TEST_USERNAME}`
renderWithRouter(<NavBar questionCount={5} />, { route: activeRoute })

// Check for active class on the correct link
Expand Down
2 changes: 2 additions & 0 deletions src/utils/globalConstants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// TODO: Remove this for production
export const DEFAULT_TEST_USERNAME = 'hpotter'

0 comments on commit 114d892

Please sign in to comment.