39 lines
910 B
TypeScript
39 lines
910 B
TypeScript
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
|
|
}
|
|
}
|
|
}
|