Skip to content

Commit

Permalink
Verifying project names from management portal
Browse files Browse the repository at this point in the history
  • Loading branch information
this-Aditya committed Aug 21, 2024
1 parent 716f90f commit af669e9
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 6 deletions.
36 changes: 31 additions & 5 deletions pages/study.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<StudyPageProps> = ({definitions}) => {
const Study: NextPage<StudyPageProps> = ({definitions, projectExists}) => {
const router = useRouter()
const [projectId, setProjectId] = useState<string | null>(null)
const studyInfo: MutableRefObject<Definition[]> = useRef([])
Expand All @@ -33,6 +35,16 @@ const Study: NextPage<StudyPageProps> = ({definitions}) => {
}
}, [router.query, router.isReady, definitions])

if (!projectExists) {
return (
<MarginCard>
<CardTitle>Project Not Found</CardTitle>
<p>The project with the name {projectId} does not exist in the Management Portal.</p>
<p>Please enter a valid project name.</p>
</MarginCard>
)
}

return (
<>
<Head>
Expand Down Expand Up @@ -77,17 +89,31 @@ const StudyInfo: React.FC<any> = ({ 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
14 changes: 14 additions & 0 deletions services/mp-projects-fetcher.ts
Original file line number Diff line number Diff line change
@@ -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<Project[]> {
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[];
}
2 changes: 1 addition & 1 deletion utils/structures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export type Definition = {
};

export type Project = {
projectName?: string;
projectName: string;
description?: string;
sourceTypes: SourceType[];
}
Expand Down

0 comments on commit af669e9

Please sign in to comment.