Skip to content

Commit

Permalink
implement redirect to onboarding
Browse files Browse the repository at this point in the history
  • Loading branch information
wwsalmon committed Feb 12, 2023
1 parent 3f266b5 commit 774ea44
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions pages/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { GetServerSidePropsContext } from "next";
import { getSession } from "next-auth/react";
import mongoose, { HydratedDocument } from "mongoose";
import { IUser, IVaxEvent, UserModel } from "../models/models";
import { IUser, IVaxEvent, UserModel, VaxEventModel } from "../models/models";
import { createAccount } from "../utils/createAccount";
import { cleanForJSON } from "../utils/cleanForJson";
import { useEffect, useRef, useState } from "react";
Expand Down Expand Up @@ -123,10 +123,11 @@ function addImmunity(svg: d3.Selection<SVGSVGElement | null, unknown, null, unde
seriesGroups.selectAll("circle.immunity" + type).data(d => d).join("circle").attr("class", "immunity" + type).attr("r", 5).attr("fill", color).attr("cx", d => xScale(d.date) + chartPadding.left).attr("cy", d => yScale(d.VE) + chartPadding.top);
}

export default function Index({ thisUser }: {
export default function Index({ thisUser, initVaxEvents }: {
thisUser: HydratedDocument<IUser>,
initVaxEvents: HydratedDocument<IVaxEvent>[],
}) {
const [vaxEvents, setVaxEvents] = useState<HydratedDocument<IVaxEvent>[]>([]);
const [vaxEvents, setVaxEvents] = useState<HydratedDocument<IVaxEvent>[]>(initVaxEvents);
const [date, setDate] = useState<string>("");
const [vaxId, setVaxId] = useState<string>("Pfizer 1st and 2nd doses"); // temporarily fixed;
const [iter, setIter] = useState<number>(0);
Expand Down Expand Up @@ -313,7 +314,11 @@ export async function getServerSideProps(context: GetServerSidePropsContext) {

if (!thisUser) thisUser = await createAccount(session.user);

return {props: {thisUser: cleanForJSON(thisUser)}};
const vaxEvents = await VaxEventModel.find({userId: thisUser._id});

if (!vaxEvents.length) return { redirect: {permanent: false, destination: "/onboarding"}};

return {props: {thisUser: cleanForJSON(thisUser), initVaxEvents: cleanForJSON(vaxEvents)}};
} catch (e) {
console.log(e);
return { notFound: true };
Expand Down

0 comments on commit 774ea44

Please sign in to comment.