Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deploy Production 13/7/67 #699

Merged
merged 8 commits into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions .changeset/pre.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"mode": "exit",
"tag": "beta",
"initialVersions": {
"admin-api": "1.3.1",
"admin-web": "1.3.0",
"api": "1.9.1",
"reg-scraper": "1.5.1",
"web": "1.10.1",
"web-e2e": "0.3.0",
"@cgr/codegen": "1.5.0",
"@cgr/course-utils": "1.3.0",
"@cgr/project-config": "1.3.0",
"@cgr/schema": "1.5.0"
},
"changesets": [
"real-mails-beg",
"rich-grapes-kiss",
"shikanoko-nokonoko-koshitantan"
]
}
5 changes: 5 additions & 0 deletions .changeset/real-mails-beg.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"web": minor
---

fix: disable referrer check
5 changes: 5 additions & 0 deletions .changeset/rich-grapes-kiss.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"web": minor
---

feat: schedule import
5 changes: 5 additions & 0 deletions .changeset/shikanoko-nokonoko-koshitantan.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"web": minor
---

feat: schedule import part 2
18 changes: 18 additions & 0 deletions apps/web/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
# web

## 1.11.0-beta.2

### Minor Changes

- b80e54d: feat: schedule import part 2

## 1.11.0-beta.1

### Minor Changes

- cd80cac: feat: schedule import

## 1.11.0-beta.0

### Minor Changes

- ef01a7c: fix: disable referrer check

## 1.10.1

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion apps/web/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "web",
"version": "1.10.1",
"version": "1.11.0-beta.2",
"private": true,
"scripts": {
"dev": "next dev -p 4200",
Expand Down
119 changes: 71 additions & 48 deletions apps/web/src/pages/[studyProgram]/schedule/import/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useEffect } from 'react'

import { ApolloClient, NormalizedCacheObject, isApolloError } from '@apollo/client'
import { ApolloClient, NormalizedCacheObject } from '@apollo/client'
import { observer } from 'mobx-react'
import { GetServerSidePropsContext, GetServerSidePropsResult } from 'next'
import { useRouter } from 'next/router'
Expand Down Expand Up @@ -32,9 +32,10 @@ interface ScheduleItem {

interface ImportPageProps {
items: ScheduleItem[]
errorMessage: string[]
}

function ImportSchedulePage({ items }: ImportPageProps) {
function ImportSchedulePage({ items, errorMessage }: ImportPageProps) {
const router = useRouter()
const { buildLink } = useLinkBuilder()

Expand All @@ -46,64 +47,86 @@ function ImportSchedulePage({ items }: ImportPageProps) {
items.forEach(({ course, sectionNo }) => {
courseCartStore.addItem(course, sectionNo)
})
router.replace(buildLink(`/schedule`))

if (!errorMessage) router.replace(buildLink(`/schedule`))
}
fn()
}, [items, router, buildLink])
}, [items, router, buildLink, errorMessage])

return (
<>
<Loading loading />

{errorMessage && (
<div>
<p>ERROR</p>

return <Loading loading />
<ul>
{errorMessage.map((msg, i) => (
<li key={i}>{msg}</li>
))}
</ul>
</div>
)}
</>
)
}

// TODO: research security issues for importing from other 3rd party data sources
const whitelistedOrigins = ['https://esc.eng.chula.ac.th', 'http://localhost:8000']
// const whitelistedOrigins = ['https://esc.eng.chula.ac.th', 'http://localhost:8000']

export async function getServerSideProps(
context: GetServerSidePropsContext
): Promise<GetServerSidePropsResult<ImportPageProps>> {
const referer = context.req.headers.referer as string
const isFromWhitelistedOrigins = whitelistedOrigins.some((origin) => {
return typeof referer === 'string' && referer.startsWith(origin)
})
if (!isFromWhitelistedOrigins) {
return {
notFound: true,
}
}
try {
const client = createApolloServerClient()
const q = context.query
const courseGroup = parseCourseGroup(q)
const itemsQuery = (q.items as string) ?? ''
const rawItems = itemsQuery
.split(',')
.map((it) => {
if (it.length === 0) {
return null
}
const parts = it.split(':')
if (parts.length !== 2) {
throw new Error('expected 2 parts for each item')
}
return {
courseNo: parts[0],
sectionNo: parts[1],
}
})
.filter((it) => it !== null) as RawScheduleItem[]
const items = await Promise.all(rawItems.map((it) => fetchItem(client, courseGroup, it)))
return {
props: {
items,
},
}
} catch (e: unknown) {
if (isApolloError(e as Error)) {
// const referer = context.req.headers.referer as string
// const isFromWhitelistedOrigins = whitelistedOrigins.some((origin) => {
// return typeof referer === 'string' && referer.startsWith(origin)
// })
// if (!isFromWhitelistedOrigins) {
// return {
// notFound: true,
// }
// }
const client = createApolloServerClient()
const q = context.query
const courseGroup = parseCourseGroup(q)
const itemsQuery = (q.items as string) ?? ''

const errorMessage: string[] = []

const rawItems = itemsQuery
.split(',')
.map((it) => {
if (it.length === 0) {
return null
}
const parts = it.split(':')
if (parts.length !== 2) {
errorMessage.push(`expected 2 parts for each item: ${it}`)
}
return {
notFound: true,
courseNo: parts[0],
sectionNo: parts[1],
}
} else {
throw e
}
})
.filter((it) => it !== null) as RawScheduleItem[]

const items = (
await Promise.all(
rawItems.map((it) => {
return fetchItem(client, courseGroup, it).catch(() => {
errorMessage.push(`error fetching item: ${it.courseNo} ${it.sectionNo}`)
return null
})
})
)
).filter((x) => x !== null) as ScheduleItem[]

return {
props: {
items,
errorMessage,
},
}
}

Expand Down