Skip to content

Commit

Permalink
Formatted code and removed log
Browse files Browse the repository at this point in the history
  • Loading branch information
this-Aditya committed Aug 23, 2024
1 parent 5c6b151 commit aaf01be
Show file tree
Hide file tree
Showing 7 changed files with 338 additions and 252 deletions.
42 changes: 25 additions & 17 deletions config/github-config.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,37 @@
export const GITHUB_CONFIG = {
ORGANIZATION_NAME: 'RADAR-base',
REPOSITORY_NAME: process.env.GITHUB_REPO_NAME || 'radar-self-enrolment-definitions',
DEFINITIONS_BRANCH: process.env.GITHUB_REPO_BRANCH_NAME || 'test',
MAX_CONTENT_LENGTH: parseInt(process.env.GITHUB_RESPONSE_CONTENT_LENGTH || '1000000', 10),
CACHE_DURATION: parseInt(process.env.GITHUB_RESPONSE_CACHE_DURATION || '180000', 10),
CACHE_SIZE: parseInt(process.env.GITHUB_RESPONSE_CACHE_SIZE || '50', 10),
API_URL: 'https://api.github.com',
ACCEPT_HEADER: 'application/vnd.github.v3+json',
ORGANIZATION_NAME: "RADAR-base",
REPOSITORY_NAME:
process.env.GITHUB_REPO_NAME || "radar-self-enrolment-definitions",
DEFINITIONS_BRANCH: process.env.GITHUB_REPO_BRANCH_NAME || "test",
MAX_CONTENT_LENGTH: parseInt(
process.env.GITHUB_RESPONSE_CONTENT_LENGTH || "1000000",
10,
),
CACHE_DURATION: parseInt(
process.env.GITHUB_RESPONSE_CACHE_DURATION || "180000",
10,
),
CACHE_SIZE: parseInt(process.env.GITHUB_RESPONSE_CACHE_SIZE || "50", 10),
API_URL: "https://api.github.com",
ACCEPT_HEADER: "application/vnd.github.v3+json",
}

export const GITHUB_AUTH_CONFIG = {
GITHUB_AUTH_TOKEN: process.env.GITHUB_AUTH_TOKEN || '',
GITHUB_AUTH_TOKEN: process.env.GITHUB_AUTH_TOKEN || "",
}

export const REMOTE_DEFINITIONS_CONFIG = {
CONSENT_VERSION: 'v1',
ELIGIBILITY_VERSION: 'v1',
STUDY_INFO_VERSION: 'v1',
CONSENT_VERSION: "v1",
ELIGIBILITY_VERSION: "v1",
STUDY_INFO_VERSION: "v1",

CONSENT_DEFINITION_FILE_NAME_CONTENT: 'consent',
ELIGIBILITY_DEFINITION_FILE_NAME_CONTENT: 'eligibility',
STUDY_INFO_DEFINITION_FILE_NAME_CONTENT: 'study_info'
CONSENT_DEFINITION_FILE_NAME_CONTENT: "consent",
ELIGIBILITY_DEFINITION_FILE_NAME_CONTENT: "eligibility",
STUDY_INFO_DEFINITION_FILE_NAME_CONTENT: "study_info",
}

export const MP_CONFIG = {
BASE_URL: process.env.MP_CONFIG_BASE_URL || 'http://127.0.1.1:8080/managementportal',
PROJECTS_ENDPOINT: process.env.MP_PROJECTS_ENDPOINT || 'api/public/projects',
BASE_URL:
process.env.MP_CONFIG_BASE_URL || "http://127.0.1.1:8080/managementportal",
PROJECTS_ENDPOINT: process.env.MP_PROJECTS_ENDPOINT || "api/public/projects",
}
115 changes: 67 additions & 48 deletions pages/study.tsx
Original file line number Diff line number Diff line change
@@ -1,36 +1,41 @@
import type {GetServerSideProps, NextPage} from "next"
import type { GetServerSideProps, NextPage } from "next"
import Head from "next/head"
import { useRouter } from "next/router"
import {MutableRefObject, useEffect, useRef, useState} from "react"
import { MutableRefObject, useEffect, useRef, useState } from "react"

import { REMOTE_DEFINITIONS_CONFIG } from "../config/github-config"
// Import render helpers
import { MarginCard, CardTitle, TextCenterButton, InnerCard } from "../pkg"
import githubService from "../services/github-service";
import {REMOTE_DEFINITIONS_CONFIG} from "../config/github-config";
import {Definition, Project} from "../utils/structures";
import fetchProjectsFromMp from "../services/mp-projects-fetcher";
import {MPFetchError} from "../utils/errors/MPFetchError";
import {ContentLengthError} from "../utils/errors/ContentLengthError";
import {GithubApiError} from "../utils/errors/GithubApiError";
import {NoContentError} from "../utils/errors/NoContentError";
import githubService from "../services/github-service"
import fetchProjectsFromMp from "../services/mp-projects-fetcher"
import { ContentLengthError } from "../utils/errors/ContentLengthError"
import { GithubApiError } from "../utils/errors/GithubApiError"
import { MPFetchError } from "../utils/errors/MPFetchError"
import { NoContentError } from "../utils/errors/NoContentError"
import { Definition, Project } from "../utils/structures"

interface StudyPageProps {
definitions: string;
projectExists: boolean;
exceptionMessage: string;
exceptionStatusCode: number;
definitions: string
projectExists: boolean
exceptionMessage: string
exceptionStatusCode: number
}

// Renders the eligibility page
const Study: NextPage<StudyPageProps> = ({definitions, projectExists, exceptionMessage, exceptionStatusCode}) => {
const Study: NextPage<StudyPageProps> = ({
definitions,
projectExists,
exceptionMessage,
exceptionStatusCode,
}) => {
const router = useRouter()
const [projectId, setProjectId] = useState<string | null>(null)
const studyInfo: MutableRefObject<Definition[]> = useRef([])

useEffect(() => {
// If the router is not ready yet, or we already have a flow, do nothing.
if (router.isReady) {
const {projectId} = router.query
const { projectId } = router.query
if (definitions != null) {
studyInfo.current = JSON.parse(definitions) as Definition[]
}
Expand All @@ -41,23 +46,28 @@ const Study: NextPage<StudyPageProps> = ({definitions, projectExists, exceptionM
}
}, [router.query, router.isReady, definitions])

if(exceptionMessage) {
if (exceptionMessage) {
return (
<MarginCard>
<CardTitle>An exception occurred while fetching the project or definitions</CardTitle>
<p>{exceptionMessage}</p>
{exceptionStatusCode && <p>Status Code: {exceptionStatusCode}</p>}
</MarginCard>
<MarginCard>
<CardTitle>
An exception occurred while fetching the project or definitions
</CardTitle>
<p>{exceptionMessage}</p>
{exceptionStatusCode && <p>Status Code: {exceptionStatusCode}</p>}
</MarginCard>
)
}

if (projectExists === false) {
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>
<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>
)
}

Expand Down Expand Up @@ -103,50 +113,59 @@ const StudyInfo: React.FC<any> = ({ questions }) => {
}

export const getServerSideProps: GetServerSideProps = async (context) => {
const {projectId} = context.query
const { projectId } = context.query
if (typeof projectId === "string") {
try {
const fetchedProjects: Project[] = await fetchProjectsFromMp()
console.log(fetchedProjects)
const mpProjectNames: string[] = fetchedProjects.map((project: Project) => project.projectName)
const mpProjectNames: string[] = fetchedProjects.map(
(project: Project) => project.projectName,
)
if (!mpProjectNames.includes(projectId)) {
console.log("No project found")
return {
props: {
projectExists: false
}
};
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)
const studyDefinitions: string | undefined =
await githubService.initiateFetch(
projectId,
REMOTE_DEFINITIONS_CONFIG.STUDY_INFO_DEFINITION_FILE_NAME_CONTENT,
REMOTE_DEFINITIONS_CONFIG.STUDY_INFO_VERSION,
)

return studyDefinitions === undefined
? { props: { projectExists: true } }
: { props: { definitions: studyDefinitions, projectExists: true } }
return studyDefinitions === undefined
? { props: { projectExists: true } }
: { props: { definitions: studyDefinitions, projectExists: true } }
} catch (error: any) {
if (error instanceof MPFetchError || error instanceof ContentLengthError || error instanceof NoContentError) {
if (
error instanceof MPFetchError ||
error instanceof ContentLengthError ||
error instanceof NoContentError
) {
return {
props: {
exceptionMessage: error.message
}
exceptionMessage: error.message,
},
}
} else if (error instanceof GithubApiError) {
} else if (error instanceof GithubApiError) {
return {
props: {
exceptionMessage: error.message,
exceptionStatusCode: error.statusCode
}
exceptionStatusCode: error.statusCode,
},
}
} else {
return {
props: {
exceptionMessage: error.message
}
exceptionMessage: error.message,
},
}
}
}
} else return {props: {projectExists: false}}
} else return { props: { projectExists: false } }
}

export default Study
export default Study
Loading

0 comments on commit aaf01be

Please sign in to comment.