-
-
Notifications
You must be signed in to change notification settings - Fork 394
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
96ba72d
commit f792a90
Showing
14 changed files
with
341 additions
and
468 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} | ||
/> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.