diff --git a/pages/study.tsx b/pages/study.tsx index e7cceed..59935f1 100644 --- a/pages/study.tsx +++ b/pages/study.tsx @@ -7,14 +7,16 @@ import {MutableRefObject, useEffect, useRef, useState} from "react" import { MarginCard, CardTitle, TextCenterButton, InnerCard } from "../pkg" import githubService from "../services/github-service"; import {REMOTE_DEFINITIONS_CONFIG} from "../config/github-config"; -import {Definition} from "../utils/structures"; +import {Definition, Project} from "../utils/structures"; +import fetchProjectsFromMp from "../services/mp-projects-fetcher"; interface StudyPageProps { definitions: string + projectExists: boolean } // Renders the eligibility page -const Study: NextPage = ({definitions}) => { +const Study: NextPage = ({definitions, projectExists}) => { const router = useRouter() const [projectId, setProjectId] = useState(null) const studyInfo: MutableRefObject = useRef([]) @@ -33,6 +35,16 @@ const Study: NextPage = ({definitions}) => { } }, [router.query, router.isReady, definitions]) + if (!projectExists) { + return ( + + Project Not Found +

The project with the name {projectId} does not exist in the Management Portal.

+

Please enter a valid project name.

+
+ ) + } + return ( <> @@ -77,17 +89,31 @@ const StudyInfo: React.FC = ({ questions }) => { export const getServerSideProps: GetServerSideProps = async (context) => { const {projectId} = context.query if (typeof projectId === "string") { + + const fetchedProjects: Project[] = await fetchProjectsFromMp() + console.log(fetchedProjects) + const mpProjectNames: string[] = fetchedProjects.map((project: Project) => project.projectName) + if (!mpProjectNames.includes(projectId)) { + console.log("No project found") + return { + props: { + projectExists: false + } + }; + } + const studyDefinitions: string | undefined = await githubService.initiateFetch(projectId, REMOTE_DEFINITIONS_CONFIG.STUDY_INFO_DEFINITION_FILE_NAME_CONTENT, REMOTE_DEFINITIONS_CONFIG.STUDY_INFO_VERSION) - if (studyDefinitions == undefined) return {props: {}} + if (studyDefinitions == undefined) return {props: {projectExists: true}} return { props: { definitions: studyDefinitions, + projectExists: true } } - } else return {props: {}} + } else return {props: {projectExists: false}} } -export default Study +export default Study \ No newline at end of file diff --git a/services/mp-projects-fetcher.ts b/services/mp-projects-fetcher.ts new file mode 100644 index 0000000..a6cf855 --- /dev/null +++ b/services/mp-projects-fetcher.ts @@ -0,0 +1,14 @@ +import {MP_CONFIG} from "../config/github-config"; +import {Project} from "../utils/structures"; + +const MP_PROJECTS_URL = `${MP_CONFIG.BASE_URL}/${MP_CONFIG.PROJECTS_ENDPOINT}` + +export default async function fetchProjectsFromMp(): Promise { + const response = await fetch(MP_PROJECTS_URL) + if (!response.ok) { + const errorText = await response.text(); + throw new Error(`Error while fetching projects: ${errorText}`); + } + const responseBody = await response.text(); + return JSON.parse(responseBody) as Project[]; +} diff --git a/utils/structures.ts b/utils/structures.ts index 10f314f..45f3a93 100644 --- a/utils/structures.ts +++ b/utils/structures.ts @@ -25,7 +25,7 @@ export type Definition = { }; export type Project = { - projectName?: string; + projectName: string; description?: string; sourceTypes: SourceType[]; }