Skip to content

Commit

Permalink
Hide knowledge of GetQuestionSetError in component
Browse files Browse the repository at this point in the history
  • Loading branch information
leung018 committed Jul 29, 2024
1 parent fd3782e commit ea2d930
Showing 1 changed file with 19 additions and 18 deletions.
37 changes: 19 additions & 18 deletions src/app/components/editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,16 @@ export class QuestionSetEditorUIService {
return (
<QuestionSetEditor
saveQuestionSet={this.saveQuestionSet}
fetchQuestionSet={() =>
this.questionSetRepo.getQuestionSetById(questionSetId)
}
fetchQuestionSet={() => {
try {
return this.questionSetRepo.getQuestionSetById(questionSetId)
} catch (e) {
if (e instanceof GetQuestionSetError) {
return null
}
throw e
}
}}
deleteQuestionSet={(id) => this.questionSetRepo.deleteQuestionSet(id)}
/>
)
Expand Down Expand Up @@ -215,7 +222,7 @@ function QuestionSetEditor({
fetchQuestionSet,
deleteQuestionSet,
}: {
fetchQuestionSet: () => QuestionSet
fetchQuestionSet: () => QuestionSet | null
saveQuestionSet: (questionSet: QuestionSet) => OperationResult
deleteQuestionSet?: (questionSetId: string) => void
}) {
Expand All @@ -233,21 +240,15 @@ function QuestionSetEditor({
const [isConfirmDelete, setIsConfirmDelete] = useState<boolean>(false)

useEffect(() => {
try {
const questionSet = fetchQuestionSet()
questionSetIdRef.current = questionSet.id
setQuestionSetInput(mapQuestionSetToQuestionSetInput(questionSet))
setLoading(false)
} catch (e) {
if (
e instanceof GetQuestionSetError &&
e.cause.code === 'QUESTION_SET_NOT_FOUND'
) {
setNotFound(true)
return
}
throw e
const questionSet = fetchQuestionSet()
if (questionSet == null) {
setNotFound(true)
return
}

questionSetIdRef.current = questionSet.id
setQuestionSetInput(mapQuestionSetToQuestionSetInput(questionSet))
setLoading(false)
}, [fetchQuestionSet])

const handleQuestionUpdate = (newQuestion: QuestionInput) => {
Expand Down

0 comments on commit ea2d930

Please sign in to comment.