Skip to content

Commit

Permalink
extra treatments are accessible from the main page
Browse files Browse the repository at this point in the history
  • Loading branch information
emrergin committed Feb 22, 2024
1 parent 1b83ab4 commit 305c1d2
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 37 deletions.
9 changes: 4 additions & 5 deletions src/components/Experiment.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import styles from "@/styles/Home.module.css";

import Intro from "@/components/Intro";
import Intro2 from "@/components/Intro2";
import Intro2 from "@/components/IntroBayesian";
import TopBar from "@/components/TopBar";

import { Participant } from "@prisma/client";
Expand All @@ -26,7 +26,6 @@ function Experiment({ data }: { data: SessionType }) {
}

const [phase, setPhase] = useState<Phase>(Phase.Intro);
const randomizedDraws = useRef(data.drawn_balls);
const [points, setPoints] = useState(0);
const [currentRound, setCurrentRound] = useState(0);
const [gpsQuestion, setGpsQuestion] = useState<GpsQuestion>("generalrisk");
Expand All @@ -37,7 +36,7 @@ function Experiment({ data }: { data: SessionType }) {
phase={phase}
points={points}
currentRound={currentRound}
lastRound={data.drawn_balls.length}
lastRound={data.round_parameters.length}
currentQuestion={gpsQuestion}
/>
{phase === Phase.Intro && <Intro nameFunction={generateNewParticipant} />}
Expand All @@ -48,13 +47,13 @@ function Experiment({ data }: { data: SessionType }) {
priors={data.prior as [number, number]}
treatment={data.treatment}
phaseFunction={setPhase}
numberOfRounds={data.drawn_balls.length}
numberOfRounds={data.round_parameters.length}
/>
)}
{phase === Phase.Main && (
<Round
bsr={data.treatment === "BSR"}
arrayOfDraws={randomizedDraws.current}
arrayOfDraws={data.round_parameters}
priors={data.prior}
aBlue={data.num_of_blue_a}
bBlue={data.num_of_blue_b}
Expand Down
28 changes: 13 additions & 15 deletions src/components/Experiment2.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import styles from "@/styles/Home.module.css";

import Intro from "@/components/Intro";
import Intro3 from "./Intro3";
import Intro3 from "./IntroGuess";
import TopBar from "@/components/TopBar";

import { Participant } from "@prisma/client";
Expand All @@ -10,23 +10,22 @@ import { useState } from "react";
import Demographics from "@/components/Demographics";
import Gps from "@/components/Gps";

import { Phase, GpsQuestion } from "@/utilities/types";
import { Phase, GpsQuestion, SessionType2 } from "@/utilities/types";
import Round2 from "./Round2";

function Experiment2() {
function Experiment2({ data }: { data: SessionType2 }) {
const [participant, setParticipant] = useState<Participant | {}>({});

async function generateNewParticipant(name: string) {
// const respond = await fetch("./api/participant", {
// method: "POST",
// body: JSON.stringify({ name_surname: name, sessionId: data.id }),
// });
// setParticipant(await respond.json());
const respond = await fetch("./api/participant", {
method: "POST",
body: JSON.stringify({ name_surname: name, sessionId: data.id }),
});
setParticipant(await respond.json());
setPhase(Phase.Intro2);
}

const [phase, setPhase] = useState<Phase>(Phase.Intro);
// const [phase, setPhase] = useState<Phase>(Phase.Main);
const [points, setPoints] = useState(0);
const [currentRound, setCurrentRound] = useState(0);
const [gpsQuestion, setGpsQuestion] = useState<GpsQuestion>("generalrisk");
Expand All @@ -37,24 +36,23 @@ function Experiment2() {
phase={phase}
points={points}
currentRound={currentRound}
lastRound={20}
// lastRound={data.drawn_balls.length}
lastRound={data.round_parameters.length}
currentQuestion={gpsQuestion}
/>
{phase === Phase.Intro && <Intro nameFunction={generateNewParticipant} />}

{phase === Phase.Intro2 && (
<Intro3
treatment={"BSR2"}
treatment={data.treatment}
phaseFunction={setPhase}
numberOfRounds={10}
numberOfRounds={data.round_parameters.length}
/>
)}

{phase === Phase.Main && (
<Round2
bsr={true}
arrayOfBags={[12, 34, 22]}
bsr={data.treatment === "BSR2"}
arrayOfBags={data.round_parameters}
phaseFunction={setPhase}
pointFunction={setPoints}
participantId={"id" in participant ? participant.id : "no-id-given"}
Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion src/pages/2/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const defaultSession: Omit<
name: "alpha_1",
location: null,
treatment: "QSR2",
drawn_balls: [10, 25, 50, 75, 90, 10],
round_parameters: [10, 25, 50, 75, 90, 10],
};

export default function Home() {
Expand Down
2 changes: 1 addition & 1 deletion src/pages/admin/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export default function Home({
"num_of_blue_a",
"num_of_blue_b",
"treatment",
"drawn_balls",
"round_parameters",
"prior",
"decision_time",
"chosen_probability",
Expand Down
41 changes: 27 additions & 14 deletions src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@ import { InferGetServerSidePropsType } from "next";
import { GetServerSideProps } from "next";
import { prisma } from "@/database";
import { Session } from "@prisma/client";
import { SessionType } from "@/utilities/types";
import { SessionType, SessionType2 } from "@/utilities/types";
import Experiment2 from "@/components/Experiment2";
export default function Home({
data,
}: InferGetServerSidePropsType<typeof getServerSideProps>) {
return <Experiment data={data} />;
if (data.treatment === "QSR" || data.treatment === "BSR") {
return <Experiment data={data} />;
} else {
return <Experiment2 data={data as SessionType2} />;
}
}

const defaultSession: Omit<Session, "id"> = {
Expand All @@ -19,33 +24,41 @@ const defaultSession: Omit<Session, "id"> = {
num_of_blue_a: 30,
num_of_blue_b: 70,
treatment: "QSR",
drawn_balls: [1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6],
round_parameters: [1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6],
prior: [3, 3],
};

export const getServerSideProps: GetServerSideProps<{
data: SessionType;
data: SessionType | SessionType2;
}> = async () => {
let sessionData = await prisma.session.findFirst({
let sessionData = (await prisma.session.findFirst({
orderBy: {
start_time: "desc",
},
});
})) as SessionType | SessionType2 | null;

if (sessionData === null) {
sessionData = await prisma.session.create({
sessionData = (await prisma.session.create({
data: { ...defaultSession },
});
})) as SessionType;
}
sessionData.start_time = JSON.parse(JSON.stringify(sessionData?.start_time));
sessionData.end_time = JSON.parse(JSON.stringify(sessionData?.end_time));
sessionData.drawn_balls = shuffle(sessionData.drawn_balls);
sessionData.round_parameters = shuffle(sessionData.round_parameters);

return {
props: {
data: sessionData as SessionType,
},
};
if (sessionData.treatment === "QSR" || sessionData.treatment === "BSR") {
return {
props: {
data: sessionData as SessionType,
},
};
} else {
return {
props: {
data: sessionData as SessionType2,
},
};
}
};

function shuffle(array: number[]) {
Expand Down
8 changes: 7 additions & 1 deletion src/utilities/types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import { Round, Session } from "@prisma/client";

export interface SessionType extends Omit<Session, "prior" | "treatment"> {
export interface SessionType
extends Omit<
Session,
"prior" | "treatment" | "num_of_blue_a" | "num_of_blue_b"
> {
prior: [number, number];
treatment: "QSR" | "BSR";
num_of_blue_a: number;
num_of_blue_b: number;
}

export interface SessionType2
Expand Down

0 comments on commit 305c1d2

Please sign in to comment.