From dbdea30d79fc6d74f372c785e28a957ef0e38261 Mon Sep 17 00:00:00 2001 From: Prathamsahu52 Date: Wed, 27 Dec 2023 01:41:46 +0530 Subject: [PATCH] Fixed(partially to buildable with warnings) type and IndexedDB environment errors --- Dockerfile | 67 +++++++++++++++++++ .../components/layout/navigation.tsx | 10 +-- .../components/mobile-nav/mobile-nav.tsx | 4 +- components/Hearts.tsx | 3 - components/matchedResults.tsx | 5 +- next.config.js | 10 ++- package-lock.json | 10 +++ package.json | 3 +- pages/_document.tsx | 1 + pages/credits.tsx | 60 ++++++++--------- pages/dashboard.tsx | 2 +- pages/result.tsx | 34 +++++++++- utils/API_Calls/Send_Heart.tsx | 2 +- utils/API_Calls/get_results.tsx | 1 + utils/API_Calls/search.tsx | 42 ++++++++---- utils/UserData.tsx | 4 +- 16 files changed, 195 insertions(+), 63 deletions(-) create mode 100644 Dockerfile diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..7d8396e --- /dev/null +++ b/Dockerfile @@ -0,0 +1,67 @@ +FROM node:18-alpine AS base + +# Install dependencies only when needed +FROM base AS deps +# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed. +RUN apk add --no-cache libc6-compat +WORKDIR /app + +# Install dependencies based on the preferred package manager +COPY package.json yarn.lock* package-lock.json* pnpm-lock.yaml* ./ +RUN \ + if [ -f yarn.lock ]; then yarn --frozen-lockfile; \ + elif [ -f package-lock.json ]; then npm ci; \ + elif [ -f pnpm-lock.yaml ]; then yarn global add pnpm && pnpm i --frozen-lockfile; \ + else echo "Lockfile not found." && exit 1; \ + fi + + +# Rebuild the source code only when needed +FROM base AS builder +WORKDIR /app +COPY --from=deps /app/node_modules ./node_modules +COPY . . + +# Next.js collects completely anonymous telemetry data about general usage. +# Learn more here: https://nextjs.org/telemetry +# Uncomment the following line in case you want to disable telemetry during the build. +# ENV NEXT_TELEMETRY_DISABLED 1 + +# RUN yarn build + +# If using npm comment out above and use below instead +RUN npm run build + +# Production image, copy all the files and run next +FROM base AS runner +WORKDIR /app + +ENV NODE_ENV production +# Uncomment the following line in case you want to disable telemetry during runtime. +# ENV NEXT_TELEMETRY_DISABLED 1 + +RUN addgroup --system --gid 1001 nodejs +RUN adduser --system --uid 1001 nextjs + +COPY --from=builder /app/public ./public + +# Set the correct permission for prerender cache +RUN mkdir .next +RUN chown nextjs:nodejs .next + +# Automatically leverage output traces to reduce image size +# https://nextjs.org/docs/advanced-features/output-file-tracing +COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./ +COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static + +USER nextjs + +EXPOSE 3000 + +ENV PORT 3000 +# set hostname to localhost +ENV HOSTNAME "0.0.0.0" + +# server.js is created by next build from the standalone output +# https://nextjs.org/docs/pages/api-reference/next-config-js/output +CMD ["node", "server.js"] \ No newline at end of file diff --git a/app/(landing)/components/layout/navigation.tsx b/app/(landing)/components/layout/navigation.tsx index b2419a3..f15503f 100644 --- a/app/(landing)/components/layout/navigation.tsx +++ b/app/(landing)/components/layout/navigation.tsx @@ -35,16 +35,16 @@ const Navigation: React.FC = () => { return ( - {siteConfig.header.links.map(({ href, id, ...props }, i) => { + {/* {siteConfig.header.links.map(({ id, ...props }, i) => { return ( { {props.label} ); - })} + })} */} diff --git a/app/(landing)/components/mobile-nav/mobile-nav.tsx b/app/(landing)/components/mobile-nav/mobile-nav.tsx index eb8a52e..2f82272 100644 --- a/app/(landing)/components/mobile-nav/mobile-nav.tsx +++ b/app/(landing)/components/mobile-nav/mobile-nav.tsx @@ -129,7 +129,7 @@ export function MobileNavContent(props: MobileNavContentProps) { - + {/* {siteConfig.header.links.map( ({ href, id, label, ...props }, i) => { return ( @@ -143,7 +143,7 @@ export function MobileNavContent(props: MobileNavContentProps) { ) } )} - + */} diff --git a/components/Hearts.tsx b/components/Hearts.tsx index 9f171ed..7110f44 100644 --- a/components/Hearts.tsx +++ b/components/Hearts.tsx @@ -24,9 +24,6 @@ function Hearts() { ))} - - - ); } diff --git a/components/matchedResults.tsx b/components/matchedResults.tsx index 682237b..5ad4386 100644 --- a/components/matchedResults.tsx +++ b/components/matchedResults.tsx @@ -10,13 +10,16 @@ const Results = () => { return (
- {Matches.map((student) => ( + {/* {Matches.map((student) => ( //
{student.i}
+ ))} */} + {Matches.map((student) => ( +
{student.i}
))}
); diff --git a/next.config.js b/next.config.js index 884e7d6..2a05ae8 100644 --- a/next.config.js +++ b/next.config.js @@ -1,5 +1,10 @@ /** @type {import('next').NextConfig} */ -const nextConfig = {} +const nextConfig = { + eslint: { + ignoreDuringBuilds: true, + }, + output: 'standalone' +} module.exports = nextConfig @@ -13,4 +18,5 @@ module.exports = { env: { SERVER_IP: SERVER_IP, }, -}; \ No newline at end of file +}; + diff --git a/package-lock.json b/package-lock.json index 7ecf38a..bb89f28 100644 --- a/package-lock.json +++ b/package-lock.json @@ -37,6 +37,7 @@ "autoprefixer": "latest", "eslint": "latest", "eslint-config-next": "latest", + "fake-indexeddb": "^5.0.1", "postcss": "latest", "tailwindcss": "latest", "typescript": "latest" @@ -4085,6 +4086,15 @@ "node": ">=0.10.0" } }, + "node_modules/fake-indexeddb": { + "version": "5.0.1", + "resolved": "https://registry.npmjs.org/fake-indexeddb/-/fake-indexeddb-5.0.1.tgz", + "integrity": "sha512-vxybH29Owtc6khV/Usy47B1g+eKwyhFiX8nwpCC4td320jvwrKQDH6vNtcJZgUzVxmfsSIlHzLKQzT76JMCO7A==", + "dev": true, + "engines": { + "node": ">=18" + } + }, "node_modules/fast-deep-equal": { "version": "3.1.3", "resolved": "https://registry.npmjs.org/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz", diff --git a/package.json b/package.json index 422f250..6b1a434 100644 --- a/package.json +++ b/package.json @@ -37,6 +37,7 @@ "autoprefixer": "latest", "eslint": "latest", "eslint-config-next": "latest", + "fake-indexeddb": "^5.0.1", "postcss": "latest", "tailwindcss": "latest", "typescript": "latest" @@ -45,4 +46,4 @@ "main": "next.config.js", "author": "", "license": "ISC" -} \ No newline at end of file +} diff --git a/pages/_document.tsx b/pages/_document.tsx index 325ef56..72b5068 100644 --- a/pages/_document.tsx +++ b/pages/_document.tsx @@ -8,6 +8,7 @@ export default class Document extends NextDocument { + diff --git a/pages/credits.tsx b/pages/credits.tsx index cd71aeb..c5ed8c6 100644 --- a/pages/credits.tsx +++ b/pages/credits.tsx @@ -2,32 +2,32 @@ import Image from "next/image" import { BsLinkedin, BsGithub } from "react-icons/bs" import "../styles/credits.css" import Clear from "@/components/clear" -const CreditCard = ({ check }) => { - return ( -
-
- Profile -
-
-

Pratham Sahu

- {check &&

Coordinator

} - -
-
- - -
- - -
- ) -} +// const CreditCard = ({ check }) => { +// return ( +//
+//
+// Profile +//
+//
+//

Pratham Sahu

+// {check &&

Coordinator

} + +//
+//
+// +// +//
+ + +//
+// ) +// } const Credits = () => { return (
@@ -36,8 +36,8 @@ const Credits = () => {
- - + {/* + */}
@@ -46,13 +46,13 @@ const Credits = () => {

Other Contributors

- + {/* - + */}
diff --git a/pages/dashboard.tsx b/pages/dashboard.tsx index 41addba..9fbecfb 100644 --- a/pages/dashboard.tsx +++ b/pages/dashboard.tsx @@ -243,7 +243,7 @@ const New = () => {
{user &&
{user?.n}
-
{user?.d}
+ {/*
{user?.d}
*/}
{user?.i}
{!hearts_submitted ? ( { + useEffect(() => { + const show_result = async() => { + await get_result(); + for(let j=0; j < Matched_Ids.length; j++) { + const data: Array = search_students(Matched_Ids[j]); + if(!data.length) { + return; + } + const student = data[0]; + setMatches(student) + } + } + show_result(); + }, []) + + const router = useRouter() + const stylesss = { backgroundImage: `url("https://home.iitk.ac.in/~${user?.u}/dp"), url("https://oa.cc.iitk.ac.in/Oa/Jsp/Photo/${user?.i}_0.jpg"), url("/dummy.png")`, }; @@ -30,7 +51,7 @@ const ResultPage = () => {
{user &&
{user?.n}
-
{user?.d}
+ {/*
{user?.d}
*/}
{user?.i}
} @@ -53,7 +74,16 @@ const ResultPage = () => {

Results Yet to be Published

} - Back + router.push(`/dashboard`)} + style={{ color: "black" }} + > + Back + + diff --git a/utils/API_Calls/Send_Heart.tsx b/utils/API_Calls/Send_Heart.tsx index 387e4cb..402a436 100644 --- a/utils/API_Calls/Send_Heart.tsx +++ b/utils/API_Calls/Send_Heart.tsx @@ -3,7 +3,7 @@ import { PubK, Gender, ReturnHearts, Set_Submit } from "../UserData" import { returnHearts } from "./returnHearts" const SERVER_IP = process.env.SERVER_IP -let PublicKeys; +let PublicKeys: any[] = []; let isPubliKAvail = false; diff --git a/utils/API_Calls/get_results.tsx b/utils/API_Calls/get_results.tsx index 44ad49d..4a08468 100644 --- a/utils/API_Calls/get_results.tsx +++ b/utils/API_Calls/get_results.tsx @@ -14,6 +14,7 @@ export const get_result = async() => { throw new Error(`HTTP Error: ${res.status} - ${res.statusText}`); } const res_json = await res.json() + console.log(res_json) if(res_json.matches){ setAdminPublished(true) matchedId = res_json.matches as string[] diff --git a/utils/API_Calls/search.tsx b/utils/API_Calls/search.tsx index 22079bd..01a7171 100644 --- a/utils/API_Calls/search.tsx +++ b/utils/API_Calls/search.tsx @@ -21,7 +21,7 @@ interface Query { -var students: any[] = [] +var students: Student[] = []; var new_students: any[] | undefined = undefined; var config = { "APP_ID": "data-rgzxa", @@ -84,6 +84,7 @@ async function fetch_student_data() { //WILL throw errors when something goes wr async function start_IDB() { //if this resolves, the global variable 'db' should contain a reference to the database - otherwise it should remain an empty string return new Promise((resolve, reject) => { db = ""; + const openRequest = indexedDB.open("students", 1); openRequest.addEventListener("error", (error) => { // console.error("Failed to access local database."); @@ -95,7 +96,7 @@ async function start_IDB() { //if this resolves, the global variable 'db' should db = openRequest.result; resolve("Success"); }, {once: true}); - openRequest.addEventListener("upgradeneeded", (event) => { + openRequest.addEventListener("upgradeneeded", (event : any) => { //set up the DB, and if nothing goes wrong (i.e. no errors) then resolve successfully // console.log("Setting up IDB"); db = event.target?.result; @@ -108,14 +109,14 @@ async function start_IDB() { //if this resolves, the global variable 'db' should } //both of these assume that 'db' has the reference to the IndexedDB after 'start_IDB()' finishes - otherwise, will throw errors -async function update_IDB(students) { +async function update_IDB(students: any[]) { await start_IDB(); //first: we get a cursor to see if there is already a record - if so, we delete it //then, we store "students" into the DB //this should all happen in one transaction so that if trxn fails, we don't end up with an empty DB or two items let trxn = db.transaction(["students"], "readwrite"); // console.log("Opened transaction"); - trxn.objectStore("students").openCursor().onsuccess = (event) => { + trxn.objectStore("students").openCursor().onsuccess = (event: any) => { let cursor = event.target?.result; if (cursor) { // console.log("Deleting an entry"); @@ -133,18 +134,18 @@ async function update_IDB(students) { trxn.oncomplete = () => { // console.log("Student data successfully saved locally."); } - trxn.onerror = (error) => { + trxn.onerror = (error : any) => { // console.error("Something went wrong when trying to update the local database."); // console.error(error); } } -async function check_IDB() { +async function check_IDB() : Promise{ return new Promise(async (resolve, reject) => { await start_IDB(); //just get a cursor and see if the record is there - if yes, put it into the global variable "students" - if not, throw an error let trxn = db.transaction(["students"],"readwrite") - trxn.objectStore("students").openCursor().onsuccess = (event) => { + trxn.objectStore("students").openCursor().onsuccess = (event : any) => { let cursor = event.target?.result; if (cursor) {//if there is an entry if (!Array.isArray(cursor.value.students)) {reject("IDB entry is improper")} @@ -156,7 +157,7 @@ async function check_IDB() { reject("IDB is empty."); } } - trxn.onerror = (error) => { + trxn.onerror = (error: any) => { // console.error("Error occurred when trying to access IDB."); reject(error); } @@ -202,13 +203,24 @@ function prepare_worker() {//student data should be in a global variable called postMessage(["Options", options]); //when worker processes everything it should send out options headers again } +if (typeof window !== 'undefined' && 'indexedDB' in window) { (async function () { let error_count = 0; try { - console.log("Grabbing data locally..."); - students = await check_IDB(); - console.log("Preparing worker using local data..."); - prepare_worker(); + // if (typeof window !== 'undefined' && 'indexedDB' in window) { + // // indexedDB is available in the browser environment, so you can use it here + console.log("Grabbing data locally..."); + students = await check_IDB(); + console.log("Preparing worker using local data..."); + prepare_worker(); + // Perform your indexedDB operations here + // } else { + // Handle the case where indexedDB is not available + // console.error('indexedDB is not available in this environment.'); + // You can provide a fallback or alternative storage mechanism here + // } + + } catch (error) { console.error("Failed to find data locally"); console.error(error); @@ -240,7 +252,11 @@ function prepare_worker() {//student data should be in a global variable called console.error("Could not find data locally or fetch it. This web app will not work."); } -})(); //execute immediately +})(); //execute immediately} +}else { + console.error("IndexedDB not supported in this environment."); +} + function rollToYear(roll: string): string { //take a student's roll number, and output the batch they were in. diff --git a/utils/UserData.tsx b/utils/UserData.tsx index ba637d9..b2e4009 100644 --- a/utils/UserData.tsx +++ b/utils/UserData.tsx @@ -12,12 +12,12 @@ export let receiverIds: string[] = [] export let Matched_Ids: string[] = [] export let Matches: Student[] = [] export let admin_pulished : boolean = false; -export let user = "" +export let user : Student = {} as Student; export const setMatches = ( student_matched : any) => { Matches.push(student_matched); }; -export const setUser = ( student_user : any) => { +export const setUser = ( student_user : Student) => { user = student_user; };