diff --git a/src/features/IndexPage/IndexPage.tsx b/src/features/IndexPage/IndexPage.tsx index f4037c6..2b1c33f 100644 --- a/src/features/IndexPage/IndexPage.tsx +++ b/src/features/IndexPage/IndexPage.tsx @@ -1,4 +1,5 @@ import { GetStaticProps } from 'next'; +import * as Sentry from '@sentry/nextjs'; import { createClient, Profession, @@ -17,7 +18,6 @@ import AboutExam from './components/AboutExam/AboutExam'; import ExamParts from './components/ExamParts/ExamParts'; import CheckMobileApp from './components/CheckMobileApp/CheckMobileApp'; import Professions from './components/Professions/Professions'; -import * as Sentry from '@sentry/nextjs'; interface IndexPageProps { professions: Profession[]; @@ -77,7 +77,9 @@ export const getStaticProps: GetStaticProps = async () => { resp.professions.items ); } - } catch (e) {} + } catch (e) { + Sentry.captureException(e); + } return { props: pageProps, diff --git a/src/features/QualificationPage/features/TestPage/TestPage.tsx b/src/features/QualificationPage/features/TestPage/TestPage.tsx index db69a52..7357e30 100644 --- a/src/features/QualificationPage/features/TestPage/TestPage.tsx +++ b/src/features/QualificationPage/features/TestPage/TestPage.tsx @@ -1,4 +1,5 @@ import { GetStaticPaths, GetStaticProps } from 'next'; +import * as Sentry from '@sentry/nextjs'; import { isString, isNil } from 'lodash'; import { polishPlurals } from 'polish-plurals'; import { @@ -58,74 +59,77 @@ const SUGGESTIONS_LIMIT = 6; const REVALIDATE_ERROR = 600; const REVALIDATE_SUCCESS = 10; -export const getStaticProps: GetStaticProps< - TestPageProps, - TestPageParams -> = async ({ params }) => { - const props: TestPageProps = { - suggestions: [], - questions: [], - qualification: { - id: 0, - slug: '', - name: '', - code: '', - createdAt: new Date(0), - }, +export const getStaticProps: GetStaticProps = + async ({ params }) => { + const props: TestPageProps = { + suggestions: [], + questions: [], + qualification: { + id: 0, + slug: '', + name: '', + code: '', + createdAt: new Date(0), + }, + }; + + if (!params || isNil(params.limit) || !isString(params.slug)) + return { notFound: true, revalidate: REVALIDATE_ERROR }; + const limit = parseInt(params.limit); + const slug = params.slug.trim(); + if ( + isNaN(limit) || + !QUESTIONS.some(numOfQuestions => numOfQuestions === limit) + ) { + return { + props, + redirect: { + destination: resolveAs({ + pathname: Route.TestPage, + query: { ...params, limit: QUESTIONS[QUESTIONS.length - 1] }, + }), + }, + revalidate: REVALIDATE_ERROR, + }; + } + + const client = createClient(); + try { + const { qualification } = await client.request< + Pick, + QueryQualificationArgs + >(QUERY_QUALIFICATION, { slug }); + if (!qualification) { + throw new Error('Qualification not found: slug=' + slug); + } + props.qualification = qualification; + + const { generateTest, similarQualifications } = await client.request< + Pick, + QueryGenerateTestSimilarQualificationsArgs + >(QUERY_GENERATE_TEST_SIMILAR_QUALIFICATIONS, { + limitSuggestions: SUGGESTIONS_LIMIT, + qualificationID: qualification.id, + limitTest: limit, + skipSuggestions: false, + }); + + if (Array.isArray(generateTest)) { + props.questions = generateTest; + } + + if (Array.isArray(similarQualifications.items)) { + props.suggestions = similarQualifications.items; + } + + return { + props, + revalidate: REVALIDATE_SUCCESS, + }; + } catch (e) { + Sentry.captureException(e); + return { notFound: true, revalidate: REVALIDATE_ERROR }; + } }; - if (!params || isNil(params.limit) || !isString(params.slug)) - return { notFound: true, revalidate: REVALIDATE_ERROR }; - const limit = parseInt(params.limit); - const slug = params.slug.trim(); - if ( - isNaN(limit) || - !QUESTIONS.some(numOfQuestions => numOfQuestions === limit) - ) { - return { - props, - redirect: { - destination: resolveAs({ - pathname: Route.TestPage, - query: { ...params, limit: QUESTIONS[QUESTIONS.length - 1] }, - }), - }, - revalidate: REVALIDATE_ERROR, - }; - } - - const client = createClient(); - try { - const { qualification } = await client.request< - Pick, - QueryQualificationArgs - >(QUERY_QUALIFICATION, { slug }); - if (!qualification) { - throw new Error('404'); - } - props.qualification = qualification; - const { generateTest, similarQualifications } = await client.request< - Pick, - QueryGenerateTestSimilarQualificationsArgs - >(QUERY_GENERATE_TEST_SIMILAR_QUALIFICATIONS, { - limitSuggestions: SUGGESTIONS_LIMIT, - qualificationID: qualification.id, - limitTest: limit, - skipSuggestions: false, - }); - if (Array.isArray(generateTest)) { - props.questions = generateTest; - } - if (Array.isArray(similarQualifications.items)) { - props.suggestions = similarQualifications.items; - } - return { - props, - revalidate: REVALIDATE_SUCCESS, - }; - } catch (e) { - return { notFound: true, revalidate: REVALIDATE_ERROR }; - } -}; - export default TestPage; diff --git a/src/features/QualificationPage/features/TestPage/components/Test/Test.tsx b/src/features/QualificationPage/features/TestPage/components/Test/Test.tsx index 93c00d0..e602832 100644 --- a/src/features/QualificationPage/features/TestPage/components/Test/Test.tsx +++ b/src/features/QualificationPage/features/TestPage/components/Test/Test.tsx @@ -1,5 +1,6 @@ import { Fragment, useRef, useState } from 'react'; import { useUpdateEffect } from 'react-use'; +import * as Sentry from '@sentry/nextjs'; import clsx from 'clsx'; import { usePrompt } from 'libs/hooks'; import { @@ -97,6 +98,7 @@ const Test = ({ initialQuestions, qualification }: TestProps) => { try { setIsFetching(true); + const { generateTest: newQuestions } = await createClient().request< Pick, QueryGenerateTestSimilarQualificationsArgs @@ -106,8 +108,11 @@ const Test = ({ initialQuestions, qualification }: TestProps) => { skipSuggestions: true, qualificationID: qualification.id, }); + resetValues(newQuestions); - } catch (e) {} + } catch (e) { + Sentry.captureException(e); + } setIsFetching(false); };