Skip to content

Commit

Permalink
feat: question ssr (#3977)
Browse files Browse the repository at this point in the history
  • Loading branch information
mariojsnunes authored Nov 4, 2024
1 parent 96ba72d commit f792a90
Show file tree
Hide file tree
Showing 14 changed files with 341 additions and 468 deletions.
4 changes: 2 additions & 2 deletions src/pages/Howto/Content/Howto/Howto.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ export const Howto = observer(({ howto }: HowtoParams) => {
commentsCount={totalCommentsCount}
votedUsefulCount={usefulCount}
hasUserVotedUseful={voted}
onUsefulClick={async () =>
await onUsefulClick(voted ? 'delete' : 'add', 'HowtoDescription')
onUsefulClick={() =>
onUsefulClick(voted ? 'delete' : 'add', 'HowtoDescription')
}
/>
<Box sx={{ mt: 9 }}>
Expand Down
65 changes: 19 additions & 46 deletions src/pages/Question/QuestionEdit.tsx
Original file line number Diff line number Diff line change
@@ -1,62 +1,35 @@
import { useEffect, useState } from 'react'
import { useEffect } from 'react'
import { useNavigate, useParams } from '@remix-run/react'
import { toJS } from 'mobx'
import { Loader } from 'oa-components'
import { UserRole } from 'oa-shared'
import { logger } from 'src/logger'
import { QuestionForm } from 'src/pages/Question/Content/Common/QuestionForm'
import { useQuestionStore } from 'src/stores/Question/question.store'

import type { IQuestion } from 'oa-shared'
import type { IQuestionDB } from 'oa-shared'

export const QuestionEdit = () => {
type QuestionEdit = {
question: IQuestionDB
}

export const QuestionEdit = ({ question }: QuestionEdit) => {
const { slug } = useParams()
const navigate = useNavigate()
const store = useQuestionStore()
const [isLoading, setIsLoading] = useState(true)
const [initialValues, setInitialValues] = useState<Partial<IQuestion.Item>>(
{},
)

useEffect(() => {
const fetchQuestion = async () => {
logger.debug(`fetchQuestion`, slug)

if (slug) {
const questionDoc = await store.fetchQuestionBySlug(slug)
logger.debug(`fetchQuestion.questionDoc`, questionDoc)

if (questionDoc === null) {
navigate('/questions')
}

if (
questionDoc?._createdBy !== store.activeUser?.userName &&
!store.activeUser?.userRoles?.includes(UserRole.ADMIN)
) {
navigate(`/questions/${slug}`)
return
}

setInitialValues(toJS(questionDoc || {}))
}
setIsLoading(false)
if (
question?._createdBy !== store.activeUser?.userName &&
!store.activeUser?.userRoles?.includes(UserRole.ADMIN)
) {
navigate(`/questions/${slug}`)
return
}

fetchQuestion()
}, [slug])
}, [slug, store.activeUser?.userName])

return (
<>
{isLoading ? (
<Loader />
) : (
<QuestionForm
data-testid="question-create-form"
parentType="edit"
formValues={initialValues}
/>
)}
</>
<QuestionForm
data-testid="question-create-form"
parentType="edit"
formValues={question}
/>
)
}
33 changes: 9 additions & 24 deletions src/pages/Question/QuestionPage.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'

import { QuestionPage } from './QuestionPage'

import type { IQuestionDB } from 'oa-shared'
import type { Mock } from 'vitest'

const Theme = testingThemeStyles
Expand Down Expand Up @@ -62,12 +63,6 @@ describe('Questions', () => {
beforeEach(() => {
// Setup a fresh instance of the mock store before each test
mockQuestionStore = {
activeQuestionItem: mockQuestionItem,
incrementViewCount: vi.fn(),
fetchQuestionBySlug: vi.fn(() => {
return mockQuestionItem
}),
upsertQuestion: vi.fn(),
toggleSubscriberStatusByUserName: vi.fn(),
toggleUsefulByUser: vi.fn(),
}
Expand All @@ -92,18 +87,11 @@ describe('Questions', () => {
_deleted: faker.datatype.boolean(),
_contentModifiedTimestamp: faker.date.past().toString(),
}
;(useQuestionStore as Mock).mockReturnValue({
...mockQuestionStore,
activeQuestionItem: mockQuestionItem,
fetchQuestionBySlug: vi.fn(() => {
return mockQuestionItem
}),
})

// Act
let wrapper
act(() => {
wrapper = getWrapper()
wrapper = getWrapper(mockQuestionItem)
})

// Assert: Check the breadcrumb items and chevrons
Expand Down Expand Up @@ -133,18 +121,11 @@ describe('Questions', () => {
mockQuestionItem.title =
'Do you prefer camping near a lake or in a forest?'
mockQuestionItem.questionCategory = undefined
;(useQuestionStore as Mock).mockReturnValue({
...mockQuestionStore,
activeQuestionItem: mockQuestionItem,
fetchQuestionBySlug: vi.fn(() => {
return mockQuestionItem
}),
})

// Act
let wrapper
act(() => {
wrapper = getWrapper()
wrapper = getWrapper(mockQuestionItem)
})

// Assert: Check the breadcrumb items and chevrons
Expand All @@ -168,10 +149,14 @@ describe('Questions', () => {
})
})

const getWrapper = () => {
const getWrapper = (question: IQuestionDB) => {
const router = createMemoryRouter(
createRoutesFromElements(
<Route path="/questions/:slug" key={1} element={<QuestionPage />} />,
<Route
path="/questions/:slug"
key={1}
element={<QuestionPage question={question} />}
/>,
),
{
initialEntries: ['/questions/question'],
Expand Down
Loading

0 comments on commit f792a90

Please sign in to comment.