Skip to content

Commit

Permalink
🚅 Save wave to db (#217)
Browse files Browse the repository at this point in the history
  • Loading branch information
nezouse authored Mar 22, 2024
1 parent 5ed453f commit bd36582
Show file tree
Hide file tree
Showing 15 changed files with 1,019 additions and 107 deletions.
6 changes: 1 addition & 5 deletions src/app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import Link from "next/link";
import { getWaves } from "@/drizzle/queries/waves";

import { formatDateRange } from "@/lib/dates";
import { Button } from "@/components/ui/button";
import {
Card,
Expand Down Expand Up @@ -30,10 +29,7 @@ export default async function Home() {
<CardHeader>
<CardTitle>{wave.name}</CardTitle>
</CardHeader>
<CardContent>
This is a wave of grants
<br /> Duration: {formatDateRange(wave.startsAt, wave.endsAt)}
</CardContent>
<CardContent>This is a wave of grants</CardContent>
<CardFooter className="flex justify-end">
<Button asChild>
<Link href={`/waves/${wave.id}`}>Go to details</Link>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"use server";

import { revalidatePath } from "next/cache";
import { UnauthenticatedError } from "@/constants/errors";
import {
deleteApplicationValue,
insertApplicationValue,
Expand All @@ -24,7 +25,7 @@ export async function applicationValueAction({
value,
}: ApplicationValueActionPayload) {
if (!session?.user?.id) {
throw new Error("Unauthorized");
throw new UnauthenticatedError();
}

if (isChecked) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import { useRouter } from "next/navigation";
import { User } from "next-auth";

import { LOCAL_STORAGE_KEYS } from "@/lib/localStorage";
import { WaveParamsSchema } from "@/lib/paramsValidation";
import { ApplicationPreview } from "@/components/ui/applicationPreview/applicationPreview";
import { BackButton } from "@/components/ui/backButton";
Expand All @@ -12,11 +13,7 @@ import { PageTitle } from "@/components/ui/pageTitle";
import { SaveIcon } from "@/components/icons/saveIcon";

import { createApplicationAction } from "../steps/createApplicationAction";
import {
applicationDataSchema,
useStepsContext,
useStepsDispatchContext,
} from "../stepsProvider";
import { applicationDataSchema, useStepsContext } from "../stepsProvider";

interface PreviewApplicationProps extends WaveParamsSchema {
user: User;
Expand All @@ -28,7 +25,6 @@ export default function PreviewApplication({
}: PreviewApplicationProps) {
const router = useRouter();
const { applicationData } = useStepsContext();
const dispatch = useStepsDispatchContext();
const validationResult = applicationDataSchema.safeParse(applicationData);
if (!validationResult.success) {
router.replace(`/waves/${waveId}/applications/create`);
Expand All @@ -53,7 +49,7 @@ export default function PreviewApplication({
className="px-14"
onClick={async () => {
await createApplicationAction(validatedApplicationData, waveId);
dispatch({ type: "RESET_STEPS" });
localStorage.removeItem(LOCAL_STORAGE_KEYS.applicationStepsData);
}}
>
Submit
Expand Down
41 changes: 12 additions & 29 deletions src/app/waves/[waveId]/applications/create/stepsProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,18 @@ import {
} from "react";
import { z } from "zod";

import {
getLocalStorageValue,
LOCAL_STORAGE_KEYS,
saveToLocalStorage,
} from "@/lib/localStorage";

import { grantScopingSchema } from "./steps/grantScoping";
import { mainDetailsSchema } from "./steps/mainDetails";
import { resourcesSchema } from "./steps/resources";
import { roadmapSchema } from "./steps/roadmap";
import { teamInformationSchema } from "./steps/teamInformation/teamInformation";

const LOCAL_STORAGE_KEY = "stepsState";

export const applicationDataSchema = mainDetailsSchema
.merge(teamInformationSchema)
.merge(grantScopingSchema)
Expand All @@ -40,9 +44,6 @@ type StepsAction =
| {
type: "UPDATE_APPLICATION_DATA";
payload: Partial<ApplicationData>;
}
| {
type: "RESET_STEPS";
};

function stepsReducer(state: StepsState, action: StepsAction): StepsState {
Expand All @@ -68,9 +69,6 @@ function stepsReducer(state: StepsState, action: StepsAction): StepsState {
currentStep: state.currentStep - 1,
};
}
case "RESET_STEPS": {
return initialStepsState;
}
}
}

Expand All @@ -79,31 +77,16 @@ function localStorageStepsReducer(
action: StepsAction,
): StepsState {
const newState = stepsReducer(state, action);

if (typeof localStorage !== "undefined") {
try {
localStorage.setItem(LOCAL_STORAGE_KEY, JSON.stringify(newState));
} catch (error) {
console.error("Error saving to local storage", error);
}
}

saveToLocalStorage(LOCAL_STORAGE_KEYS.applicationStepsData, newState);
return newState;
}

function getInitialState(initialArgs: StepsState): StepsState {
if (typeof localStorage !== "undefined") {
try {
const localStorageValue = localStorage.getItem(LOCAL_STORAGE_KEY);
if (localStorageValue) {
return stepsStateSchema.parse(JSON.parse(localStorageValue));
}
} catch (err) {
console.error(err);
}
}

return initialArgs;
return getLocalStorageValue(
stepsStateSchema,
LOCAL_STORAGE_KEYS.applicationStepsData,
initialArgs,
);
}

const initialStepsState: StepsState = {
Expand Down
21 changes: 17 additions & 4 deletions src/app/waves/create/createWaveAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,26 @@

import { revalidatePath } from "next/cache";
import { redirect } from "next/navigation";
import { UnauthenticatedError } from "@/constants/errors";
import { insertWave } from "@/drizzle/queries/waves";

export async function createWaveAction(data: any) {
import { auth } from "@/lib/auth";

import { WaveData } from "./stepsProvider";

export async function createWaveAction(data: WaveData) {
const session = await auth();
if (!session) {
throw new UnauthenticatedError();
}

const [{ id }] = await insertWave({
name: data.waveName,
startsAt: data.duration.from,
endsAt: data.duration.to,
name: data.name,
summary: data.summary,
openStartDate: data.openStartDate,
denoisingStartDate: data.denoisingStartDate,
assesmentStartDate: data.assesmentStartDate,
closeDate: data.closeDate,
});

revalidatePath("/");
Expand Down
12 changes: 5 additions & 7 deletions src/app/waves/create/preview/wavePreview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { useRouter } from "next/navigation";

import { cn } from "@/lib/cn";
import { formatDate } from "@/lib/dates";
import { LOCAL_STORAGE_KEYS } from "@/lib/localStorage";
import { BackButton } from "@/components/ui/backButton";
import { Button, buttonVariants } from "@/components/ui/button";
import {
Expand All @@ -16,16 +17,12 @@ import {
import { PageTitle } from "@/components/ui/pageTitle";
import { CalendarIcon } from "@/components/icons/calendarIcon";

import {
useWaveStepsContext,
useWaveStepsDispatchContext,
waveDataSchema,
} from "../stepsProvider";
import { createWaveAction } from "../createWaveAction";
import { useWaveStepsContext, waveDataSchema } from "../stepsProvider";

export default function PreviewApplication() {
const router = useRouter();
const { waveData } = useWaveStepsContext();
const dispatch = useWaveStepsDispatchContext();
const validationResult = waveDataSchema.safeParse(waveData);
if (!validationResult.success) {
router.replace("/waves/create");
Expand All @@ -43,7 +40,8 @@ export default function PreviewApplication() {
<Button
className="px-14"
onClick={async () => {
dispatch({ type: "RESET_STEPS" });
await createWaveAction(validatedWaveData);
localStorage.removeItem(LOCAL_STORAGE_KEYS.waveStepsData);
}}
>
Submit
Expand Down
41 changes: 12 additions & 29 deletions src/app/waves/create/stepsProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,15 @@ import {
} from "react";
import { z } from "zod";

import {
getLocalStorageValue,
LOCAL_STORAGE_KEYS,
saveToLocalStorage,
} from "@/lib/localStorage";

import { mainDetailsSchema } from "./steps/mainDetails.schema";
import { timelineSchema } from "./steps/timeline.schema";

const LOCAL_STORAGE_KEY = "waveStepsState";

export const waveDataSchema = mainDetailsSchema.merge(timelineSchema);
export type WaveData = z.infer<typeof waveDataSchema>;

Expand All @@ -33,9 +37,6 @@ type StepsAction =
| {
type: "UPDATE_WAVE_DATA";
payload: Partial<WaveData>;
}
| {
type: "RESET_STEPS";
};

function stepsReducer(state: StepsState, action: StepsAction): StepsState {
Expand All @@ -61,9 +62,6 @@ function stepsReducer(state: StepsState, action: StepsAction): StepsState {
currentStep: state.currentStep - 1,
};
}
case "RESET_STEPS": {
return initialStepsState;
}
}
}

Expand All @@ -72,31 +70,16 @@ function localStorageStepsReducer(
action: StepsAction,
): StepsState {
const newState = stepsReducer(state, action);

if (typeof localStorage !== "undefined") {
try {
localStorage.setItem(LOCAL_STORAGE_KEY, JSON.stringify(newState));
} catch (error) {
console.error("Error saving to local storage", error);
}
}

saveToLocalStorage(LOCAL_STORAGE_KEYS.waveStepsData, newState);
return newState;
}

function getInitialState(initialArgs: StepsState): StepsState {
if (typeof localStorage !== "undefined") {
try {
const localStorageValue = localStorage.getItem(LOCAL_STORAGE_KEY);
if (localStorageValue) {
return stepsStateSchema.parse(JSON.parse(localStorageValue));
}
} catch (err) {
console.error(err);
}
}

return initialArgs;
return getLocalStorageValue(
stepsStateSchema,
LOCAL_STORAGE_KEYS.waveStepsData,
initialArgs,
);
}

const initialStepsState: StepsState = {
Expand Down
7 changes: 7 additions & 0 deletions src/drizzle/migrations/0006_famous_tiger_shark.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
ALTER TABLE "wave" ADD COLUMN "summary" text NOT NULL;
ALTER TABLE "wave" ADD COLUMN "openStartDate" timestamp with time zone NOT NULL;
ALTER TABLE "wave" ADD COLUMN "denoisingStartDate" timestamp with time zone NOT NULL;
ALTER TABLE "wave" ADD COLUMN "assesmentStartDate" timestamp with time zone NOT NULL;
ALTER TABLE "wave" ADD COLUMN "closeDate" timestamp with time zone NOT NULL;
ALTER TABLE "wave" DROP COLUMN IF EXISTS "startsAt";
ALTER TABLE "wave" DROP COLUMN IF EXISTS "endsAt";
Loading

0 comments on commit bd36582

Please sign in to comment.