Skip to content

Commit

Permalink
Finish App Router Migration (#615)
Browse files Browse the repository at this point in the history
* Migrate to App Router (#600)

* * Create app router directory structure
* Added pages and root layout
* Temp made all components client as 1st migration step
* Renamed conflicting pages from pages router to *2

Ran locally and checked all pages load.

migrate all linked pages to app router

* Create a SC route for products (#602)

* Uploaded hardware db to vercel postgres
* Created producs-sc page with SC

Co-authored-by: Luke Munro <[email protected]>

* added vercel tracing target

* added vercel tracing target

* updated sentry import to nextjs

* removed withProfiler from about page

* Reconfigure layout (#606)

* Migrate layout to server

* fix a bunch of crap (#607)

* upgrade to next 15

* commented out session storage

* commented out crasher

* Moved some files and implemented naming conventions

* moved some css around to fix styling

* more refactor

* accidently removed dynamic export

* trying to disable vercel cache

---------

Co-authored-by: Luke Munro <[email protected]>
Co-authored-by: Luca Forstner <[email protected]>
Co-authored-by: Aidan Landen <[email protected]>

---------

Co-authored-by: Luke Munro <[email protected]>
Co-authored-by: Aidan Landen <[email protected]>
Co-authored-by: Luca Forstner <[email protected]>
Co-authored-by: Aidan Landen <[email protected]>

* Migrate about to app router
- Reorganize files
- Call the flask backend from server side to show external spans
- Remove unused code

* Migrate checkout flow to server actions (#617)

- Refactor directory structure to next js standards
- Add json.config for navigation
- Created server component for checkout with client form that accepts a server action
- Configured backend checkout to always throw out of inventory error

* fixed loader

* added logging statement for test

* testing sentry sdk

* testing sentry sdk

* ugraded sdk version

* added server action instrumentation

---------

Co-authored-by: Luke Munro <[email protected]>
Co-authored-by: Aidan Landen <[email protected]>
Co-authored-by: Luca Forstner <[email protected]>
Co-authored-by: Aidan Landen <[email protected]>
Co-authored-by: Aidan Landen <[email protected]>
  • Loading branch information
6 people authored Nov 11, 2024
1 parent 89d9e8f commit 4082b48
Show file tree
Hide file tree
Showing 60 changed files with 1,417 additions and 1,770 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ venv
npm-debug.log*
yarn-debug.log*
yarn-error.log*
next/config-overrides.js

notes.txt
flask/.virtualenv
Expand Down
40 changes: 40 additions & 0 deletions next/jsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"compilerOptions": {
"target": "es5",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"skipLibCheck": true,
"strict": true,
"noEmit": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "bundler",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve",
"incremental": true,
"plugins": [
{
"name": "next"
}
],
"paths": {
"@/*": [
"./*",
"./src/*",
]
}
},
"include": [
"next-env.d.ts",
"**/*.js",
"**/*.jsx",
],
"exclude": [
"node_modules"
]
}
141 changes: 110 additions & 31 deletions next/lib/data.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,11 @@
import { sql } from "@vercel/postgres";

'use server'
import { PrismaClient } from '@prisma/client';
import { determineBackendUrl } from '@/src/utils/backendrouter';
import { isOddReleaseWeek, busy_sleep } from '@/src/utils/time';
import * as Sentry from '@sentry/nextjs';

const prisma = new PrismaClient();

// export type Product = {
// id: number;
// description: string;
// descriptionfull: string;
// price: number;
// img: string;
// imgcropped: string;
// reviews: Review[] | [];
// }

// export type Review = {
// id: number;
// productid: number;
// rating: number;
// customerid: string;
// description: string;
// created: string;
// }


export default async function getProducts() {
try {
Expand All @@ -47,20 +30,116 @@ export default async function getProducts() {
console.error("Database Error:", error)
// do sentry stuff
}
}

export async function getProduct(index) {
const i = Number(index);
try {
console.log("Fetching product...");
console.log(i);
const product = await prisma.products.findUnique({
where: { id: i }
});

const product_reviews = await prisma.reviews.findMany({
where: { id: i },
});

product.reviews = product_reviews;
return product;
} catch (error) {
console.error("Database Error:", error);
}
}

async function getReview(i) {
try {
const data = await sql`SELECT * from reviews WHERE productId=${i}`;
console.log(data.rows);
return data.rows;
} catch (error) {
console.error("Db error", error);
export async function checkoutAction(cart) {
console.log("cart ", cart);
const inventory = await getInventory(cart);

console.log("> /checkout inventory", inventory)


if (inventory.length === 0 || cart.quantities.length === 0) {
const error = new Error("Not enough inventory for product")
Sentry.captureException(error);
throw error;
}

for (let inventoryItem of inventory) {
let id = inventoryItem.id;
console.log(inventoryItem.count, cart.quantities[id]);
if (inventoryItem.count < cart.quantities[id] || cart.quantities[id] >= inventoryItem.count) {
const error = new Error("Not enough inventory for product")
Sentry.captureException(error);
throw error;
}
}

return {status : 200, message: "success"}
}

export async function getProductsPrisma() {
const products = await prisma.products.findMany();
console.log("prisma", products);

export async function getInventory(cart) {
console.log("> getInventory");

const quantities = cart['quantities'];
console.log("> quantities", quantities);

let productIds = []

Object.entries(quantities).forEach(([key, value]) => productIds.push(Number(key)));
console.log("> product ids", productIds);
let inventory;
try {
inventory = await prisma.inventory.findMany({
where: { id : { in : productIds } }
});
} catch (error) {
console.log("Database Error:", error);
}
console.log("inventory: ", inventory);
return inventory;
}



export async function getAbout(backend) {
if (!isOddReleaseWeek()) {
// can't have async sleep in a constructor
busy_sleep(Math.random(25) + 100);
}

const url = determineBackendUrl(backend);
// Http requests to make in parallel, so the Transaction has more Spans
let request1 = fetch(url + '/api', {
method: 'GET',
});
let request2 = fetch(url + '/organization', {
method: 'GET',
});
let request3 = fetch(url + '/connect', {
method: 'GET',
});

// Need Safari13 in tests/config.py in order for this modern javascript to work in Safari Browser
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/allSettled#browser_compatibility
// let response = await Promise.allSettled([request1, request2, request3])

const [response1, response2, response3] = await Promise.all([request1, request2, request3]);

console.log([response1, response2, response3]);
const responses = [response1, response2, response3];
// Error Handling
responses.forEach((r) => {
if (!r.ok) {
Sentry.withScope((scope) => {
scope.setContext('response', r);
Sentry.captureException(
new Error(
r.status + ' - ' + (r.statusText || 'Server Error for API')
)
);
});
}
});
}
7 changes: 1 addition & 6 deletions next/next.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@ import { withSentryConfig } from '@sentry/nextjs';
/** @type {import('next').NextConfig} */
const nextConfig = {
distDir: './dist', // Changes the build output directory to `./dist/`.
// This should be removed during a refactor, following these directions:
// https://nextjs.org/docs/messages/missing-suspense-with-csr-bailout
experimental: {
missingSuspenseWithCSRBailout: false,
},
};

export default withSentryConfig(nextConfig, {
Expand Down Expand Up @@ -47,5 +42,5 @@ export default withSentryConfig(nextConfig, {
// See the following for more information:
// https://docs.sentry.io/product/crons/
// https://vercel.com/docs/cron-jobs
automaticVercelMonitors: true,
automaticVercelMonitors: false,
});
Loading

0 comments on commit 4082b48

Please sign in to comment.