Skip to content
This repository has been archived by the owner on Nov 20, 2024. It is now read-only.

Commit

Permalink
fix: preparing dynamic pages and routes
Browse files Browse the repository at this point in the history
  • Loading branch information
Firgrep committed May 18, 2024
1 parent 7d6ac3d commit 434f90b
Show file tree
Hide file tree
Showing 14 changed files with 85 additions and 67 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ yarn-debug.log*
yarn-error.log*

# local env files
_.env
.env*.local
.env.dev
.env
Expand Down
2 changes: 2 additions & 0 deletions src/app/(public)/courses/[courseSlug]/[lessonSlug]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { dbGetUserPurchasedCourses } from "@/server/controllers/dbController";
import { redirect } from "next/navigation";
import { Suspense } from "react";

export const dynamic = "force-dynamic";

export default async function LessonFrontPageRoute({
params,
}: {
Expand Down
2 changes: 2 additions & 0 deletions src/app/(public)/courses/[courseSlug]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import FadeIn from "@/components/animations/FadeIn";
import { errorMessages } from "@/config/errorMessages";
import { redirect } from "next/navigation";

export const dynamic = "force-dynamic";

export default async function CourseFrontPageRoute({
params,
}: {
Expand Down
2 changes: 2 additions & 0 deletions src/app/(public)/courses/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { PageWrapper } from "@/components/PageWrapper";
import FadeIn from "@/components/animations/FadeIn";
import { Suspense } from "react";

export const dynamic = "force-dynamic";

export default async function PublishedCourses() {
return (
<PageWrapper>
Expand Down
4 changes: 3 additions & 1 deletion src/app/(user)/billing/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import Image from "next/image";
import Link from "next/link";
import { redirect } from "next/navigation";

export default async function Admin() {
export const dynamic = "force-dynamic";

export default async function Billing() {
const session = await getServerAuthSession();

if (!session) {
Expand Down
13 changes: 4 additions & 9 deletions src/app/(user)/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,13 @@ import { redirect } from "next/navigation";
export default async function UserLayout({
children,
}: {
children: React.ReactNode
children: React.ReactNode;
}) {
const user = await getServerAuthSession();

if (!user) {
/**
* TODO fix redirect properly
*/
return redirect("/?missing=login");;
return redirect("/?missing=login");
}

return (
<>{children}</>
);
}
return <>{children}</>;
}
2 changes: 2 additions & 0 deletions src/app/(user)/purchase-success/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { getServerAuthSession } from "@/server/auth";
import { redirect } from "next/navigation";
import { Suspense } from "react";

export const dynamic = "force-dynamic";

export default async function PublishedCourses({
searchParams,
}: {
Expand Down
22 changes: 12 additions & 10 deletions src/app/api/image-upload/route.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import sharp from 'sharp'
import { NextRequest, NextResponse } from 'next/server'
import { requireAdminAuth } from '@/server/auth'
import { gcPipeImageUpload } from '@/server/controllers/gcController';
import sharp from "sharp";
import { NextRequest, NextResponse } from "next/server";
import { requireAdminAuth } from "@/server/auth";
import { gcPipeImageUpload } from "@/server/controllers/gcController";

export const dynamic = "force-dynamic";

/**
* Thanks to these resources for helping me understand how to do this!
Expand All @@ -12,7 +14,7 @@ export async function POST(request: NextRequest) {
requireAdminAuth();

const formData = await request.formData();
const imageFile = formData.get('image') as unknown as File | null;
const imageFile = formData.get("image") as unknown as File | null;

if (!imageFile) return NextResponse.json(null, { status: 400 });

Expand All @@ -28,13 +30,13 @@ export async function POST(request: NextRequest) {
fileName: `${imageFileName}`,
});

return NextResponse.json({ imageUrl })
return NextResponse.json({ imageUrl });
}

// Export types for API Routes
export type UploadProfileImageResponse = ExtractGenericFromNextResponse<
Awaited<ReturnType<typeof POST>>
>
Awaited<ReturnType<typeof POST>>
>;
type ExtractGenericFromNextResponse<Type> = Type extends NextResponse<infer X>
? X
: never
? X
: never;
14 changes: 9 additions & 5 deletions src/app/api/stripe-webhook/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ import { stripe } from "@/lib/stripe/stripeClient";
import { handleSessionCompleted } from "@/server/controllers/stripeWebhookController";
import { dbCreateStripeEventRecord } from "@/server/controllers/dbController";

export const dynamic = "force-dynamic";

const webhookSecret = process.env.STRIPE_WEBHOOK_SECRET ?? "";

export async function POST (req: NextRequest) {
export async function POST(req: NextRequest) {
const event = await stripe.webhooks.constructEvent(
await req.text(),
req.headers.get("stripe-signature") as string,
webhookSecret as string
)
);

// Handle the event
switch (event.type) {
Expand All @@ -26,13 +28,15 @@ export async function POST (req: NextRequest) {
case "payment_intent.requires_action":
break;
case "product.updated":
console.log("===Product updated event received")
console.log("===Product updated event received");
break;
case "product.created":
break;
default:
console.log(`===Unhandled event type: ${event.type}`);
// Unexpected event type
if (process.env.NODE_ENV === "development") {
console.log(`===Unhandled event type: ${event.type}`);
}
// Unexpected event type
}

// Record the event in the database (unless development mode)
Expand Down
26 changes: 16 additions & 10 deletions src/app/test/[courseSlug]/[lessonSlug]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
import LoadingBars from "@/components/LoadingBars";
import { MDXRenderer } from "@/components/MDXRenderer";
import { DBGetCompiledMdxBySlugsProps, dbGetCompiledMdxBySlugs } from "@/server/controllers/dbController";
import {
DBGetCompiledMdxBySlugsProps,
dbGetCompiledMdxBySlugs,
} from "@/server/controllers/dbController";
import { Suspense } from "react";

export const dynamic = "force-dynamic";
/**
* * TEST ROUTE
* * /test/first-course-updated/logic-introduction
*/
export default async function TestPage2({ params }: { params: { courseSlug: string, lessonSlug: string}}) {
export default async function TestPage2({
params,
}: {
params: { courseSlug: string; lessonSlug: string };
}) {
const courseSlug = params.courseSlug;
const lessonSlug = params.lessonSlug;
/**
Expand All @@ -18,22 +26,21 @@ export default async function TestPage2({ params }: { params: { courseSlug: stri
lessonSlug: lessonSlug,
lessonType: "CONTENT",
access: "PUBLIC",
}
const compiledMdx = await dbGetCompiledMdxBySlugs(mdxGetArgs)
};
const compiledMdx = await dbGetCompiledMdxBySlugs(mdxGetArgs);

const mdxGetArgs2: DBGetCompiledMdxBySlugsProps = {
courseSlug: courseSlug,
lessonSlug: lessonSlug,
lessonType: "TRANSCRIPT",
access: "PUBLIC",
}
const compiledMdx2 = await dbGetCompiledMdxBySlugs(mdxGetArgs2)
};
const compiledMdx2 = await dbGetCompiledMdxBySlugs(mdxGetArgs2);

return (
<main className="h-full flex flex-col justify-front items-center gap-4 bg-slate-200">
<p>Test page with 2 dynamic retrieval from db</p>
<div className="container">

<p className="bg-red-400 w-full p-2">Content:</p>
<div className="border-black border-2 border-dashed">
<Suspense fallback={<LoadingBars />}>
Expand All @@ -47,8 +54,7 @@ export default async function TestPage2({ params }: { params: { courseSlug: stri
<MDXRenderer data={compiledMdx2} />
</Suspense>
</div>

</div>
</main>
)
}
);
}
18 changes: 13 additions & 5 deletions src/app/test/[courseSlug]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
import LoadingBars from "@/components/LoadingBars";
import { MDXRenderer } from "@/components/MDXRenderer";
import { DBGetCompiledMdxBySlugsProps, dbGetCompiledMdxBySlugs } from "@/server/controllers/dbController";
import {
DBGetCompiledMdxBySlugsProps,
dbGetCompiledMdxBySlugs,
} from "@/server/controllers/dbController";
import { Suspense } from "react";

export const dynamic = "force-dynamic";
/**
* * TEST ROUTE
* * /test/first-course-updated
*/
export default async function TestPage1({ params }: { params: { courseSlug: string }}) {
export default async function TestPage1({
params,
}: {
params: { courseSlug: string };
}) {
const courseSlug = params.courseSlug;
/**
* TODO Why must I add the Props here for TS not to yell at me!?
*/
const mdxGetArgs: DBGetCompiledMdxBySlugsProps = {
courseSlug: courseSlug,
access: "PUBLIC",
}
};

// const compiledMdx = await mdxGetCompiledSource(mdxGetArgs)
const mdxCompiled = await dbGetCompiledMdxBySlugs(mdxGetArgs);
Expand All @@ -32,5 +40,5 @@ export default async function TestPage1({ params }: { params: { courseSlug: stri
</div>
</div>
</main>
)
}
);
}
24 changes: 10 additions & 14 deletions src/app/test/page.tsx
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@
import Link from "next/link";



export default async function TestPage() {



return (
<main className="h-screen flex flex-col justify-front items-center gap-4 bg-slate-200">
<p>Test page with hardcoded retrieval from db</p>
<div className="container">
<p>Scheduled for deletion</p>
<br></br>
<Link href="/test/buddy/"><button className="btn btn-accent">1 dynamic path</button></Link>
<p>Scheduled for deletion</p>
<br></br>
<Link href="/test/buddy/">
<button className="btn btn-accent">1 dynamic path</button>
</Link>
<br></br>
<Link href="/test/buddy/loving-buddy"><button className="btn btn-accent">2 dynamic path</button></Link>

<Link href="/test/buddy/loving-buddy">
<button className="btn btn-accent">2 dynamic path</button>
</Link>
</div>


</main>
)
}
);
}
16 changes: 6 additions & 10 deletions src/server/bucket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ import { Storage } from "@google-cloud/storage";
import { env } from "process";

const creds = {
type: 'service_account',
type: "service_account",
project_id: env.GCP_PROJECT_ID,
private_key_id: env.GCP_PRIVATE_KEY_ID,
private_key: env.GCP_PRIVATE_KEY,
client_email: env.GCP_CLIENT_EMAIL,
client_id: env.GCP_CLIENT_ID,
auth_uri: 'https://accounts.google.com/o/oauth2/auth',
token_uri: 'https://oauth2.googleapis.com/token',
auth_provider_x509_cert_url: 'https://www.googleapis.com/oauth2/v1/certs',
auth_uri: "https://accounts.google.com/o/oauth2/auth",
token_uri: "https://oauth2.googleapis.com/token",
auth_provider_x509_cert_url: "https://www.googleapis.com/oauth2/v1/certs",
client_x509_cert_url: env.GCP_CLIENT_X509_CERT_URL,
universe_domain: "googleapis.com",
}
};

const storage = new Storage({
projectId: env.GCP_PROJECT_ID,
Expand All @@ -23,13 +23,9 @@ const storage = new Storage({
const PRIMARY_BUCKET_NAME = env.GCP_PRIMARY_BUCKET_NAME ?? "invalid";
const SECONDARY_BUCKET_NAME = env.GCP_SECONDARY_BUCKET_NAME ?? "invalid";

if (PRIMARY_BUCKET_NAME === "invalid" || SECONDARY_BUCKET_NAME === "invalid") {
throw new Error("A GCP bucket name is not set");
}

/**
* Storage bucket reference instance. Call methods on this object to interact with the bucket.
* @see https://cloud.google.com/nodejs/docs/reference/storage/latest
*/
export const primaryBucket = storage.bucket(PRIMARY_BUCKET_NAME);
export const secondaryBucket = storage.bucket(SECONDARY_BUCKET_NAME);
export const secondaryBucket = storage.bucket(SECONDARY_BUCKET_NAME);
6 changes: 3 additions & 3 deletions src/server/email.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Resend } from 'resend';
import { Resend } from "resend";

const RESEND_API_KEY = process.env.RESEND_API_KEY;
const RESEND_API_KEY = process.env.RESEND_API_KEY ?? "invalid";

export const resend = new Resend(RESEND_API_KEY);
export const resend = new Resend(RESEND_API_KEY);

0 comments on commit 434f90b

Please sign in to comment.