Added SSR
This commit is contained in:
38
src/getServerSideProps.ts
Normal file
38
src/getServerSideProps.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
import {GetServerSideProps} from "next";
|
||||
import {getGroup, setGroups} from "./database/groups";
|
||||
import {Dict, Group} from "./types";
|
||||
|
||||
export interface PropsGroupProvider {
|
||||
id: string
|
||||
groups: Dict<Group>
|
||||
ignored: string[]
|
||||
base: string[]
|
||||
}
|
||||
|
||||
export const getServerSidePropsGroupProvider: GetServerSideProps<PropsGroupProvider> = async ({query}) => {
|
||||
const id = Array.isArray(query?.id) ? query.id[0] : query?.id
|
||||
const data = id && await getGroup(id)
|
||||
if (data) {
|
||||
return {
|
||||
props: {
|
||||
id: data._id.toString(),
|
||||
groups: data.groups,
|
||||
ignored: data.ignored,
|
||||
base: data.base
|
||||
}
|
||||
}
|
||||
} else if (!id) {
|
||||
const newId = await setGroups({groups: {}, base: [], ignored: []})
|
||||
return {
|
||||
redirect: {
|
||||
destination: `?id=${newId}`,
|
||||
permanent: false,
|
||||
}
|
||||
}
|
||||
} else {
|
||||
console.log(data)
|
||||
return {
|
||||
notFound: true
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user