-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
7 changed files
with
128 additions
and
3 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
46 changes: 46 additions & 0 deletions
46
frontend/src/components/admin/assessment-status/EditStatusButtons/PreviewButton.tsx
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 |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import React from "react"; | ||
import { useHistory } from "react-router-dom"; | ||
import { useQuery } from "@apollo/client"; | ||
|
||
import { GET_TEST } from "../../../../APIClients/queries/TestQueries"; | ||
import type { TestResponse } from "../../../../APIClients/types/TestClientTypes"; | ||
import * as Routes from "../../../../constants/Routes"; | ||
import useToast from "../../../common/info/useToast"; | ||
import PopoverButton from "../../../common/popover/PopoverButton"; | ||
|
||
interface PreviewButtonProps { | ||
assessmentId: string; | ||
} | ||
|
||
const PreviewButton = ({ | ||
assessmentId, | ||
}: PreviewButtonProps): React.ReactElement => { | ||
const history = useHistory(); | ||
const { data } = useQuery<{ test: TestResponse }>(GET_TEST, { | ||
fetchPolicy: "cache-and-network", | ||
variables: { id: assessmentId }, | ||
}); | ||
const { showToast } = useToast(); | ||
|
||
return ( | ||
<PopoverButton | ||
name="Preview" | ||
onClick={async () => { | ||
if (data) { | ||
history.push({ | ||
pathname: Routes.ASESESSMENT_PREVIEW_PAGE({ assessmentId }), | ||
state: data.test, | ||
}); | ||
} else { | ||
showToast({ | ||
message: | ||
"This assessment cannot be previewed at this time. Please try again.", | ||
status: "error", | ||
}); | ||
} | ||
}} | ||
/> | ||
); | ||
}; | ||
|
||
export default PreviewButton; |
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
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
56 changes: 56 additions & 0 deletions
56
frontend/src/components/pages/admin/AssessmentPreviewPage.tsx
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 |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import React, { useMemo } from "react"; | ||
import { useLocation, useParams } from "react-router-dom"; | ||
import { useHistory } from "react-router-dom"; | ||
import { useQuery } from "@apollo/client"; | ||
import { Box } from "@chakra-ui/react"; | ||
|
||
import { GET_TEST } from "../../../APIClients/queries/TestQueries"; | ||
import type { TestResponse } from "../../../APIClients/types/TestClientTypes"; | ||
import { ASSESSMENTS_PAGE } from "../../../constants/Routes"; | ||
import { formatQuestionsResponse } from "../../../utils/QuestionUtils"; | ||
import AssessmentPreview from "../../admin/assessment-creation/AssessmentPreview"; | ||
import QueryStateHandler from "../../common/QueryStateHandler"; | ||
|
||
const AssessmentPreviewPage = () => { | ||
const history = useHistory(); | ||
|
||
// Data could come from the previous page. | ||
const { state: locationState } = useLocation<TestResponse | undefined>(); | ||
|
||
// If data is not available from the previous page, then we have to fetch it. | ||
const { assessmentId } = useParams<{ assessmentId?: string }>(); | ||
const { | ||
data: testData, | ||
loading, | ||
error, | ||
} = useQuery<{ | ||
test: TestResponse; | ||
}>(GET_TEST, { | ||
variables: { id: assessmentId }, | ||
skip: !!locationState || !assessmentId, | ||
}); | ||
|
||
const test = locationState || testData?.test; | ||
const state = useMemo( | ||
() => | ||
test && { | ||
...test, | ||
questions: formatQuestionsResponse(test.questions), | ||
}, | ||
[test], | ||
); | ||
|
||
return ( | ||
<Box mx={4}> | ||
<QueryStateHandler error={error} loading={loading}> | ||
<AssessmentPreview | ||
backButtonText="Return to Assessments" | ||
goBack={() => history.push(ASSESSMENTS_PAGE)} | ||
questions={state?.questions || []} | ||
/> | ||
</QueryStateHandler> | ||
</Box> | ||
); | ||
}; | ||
|
||
export default AssessmentPreviewPage; |
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