From 8f7ce0ccfea73746343e6253d0fce1ac8d43007a Mon Sep 17 00:00:00 2001 From: aishikanandi Date: Fri, 5 Jan 2024 18:29:20 +0530 Subject: [PATCH] All set to migrate backend --- .gitignore | 3 + package-lock.json | 45 ++++----- src/components/Common/UpvoteButton.tsx | 10 +- src/components/Notes/Table.tsx | 62 +++++++++++-- src/components/PYQs/Table.tsx | 70 +++++++++++--- src/pages/courses/[...id].tsx | 124 ++++++++++++++++++++++--- src/pages/courses/index.tsx | 93 +++++++++++++++++-- src/utils/api.ts | 12 +-- src/utils/firebaseAdminInit.ts | 56 +++++------ src/utils/firebaseInit.ts | 24 ++--- 10 files changed, 379 insertions(+), 120 deletions(-) diff --git a/.gitignore b/.gitignore index ecda0f53..f9e447eb 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,8 @@ # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. .env +next/cache/ +next/ +.next/ # dependencies /node_modules /.pnp diff --git a/package-lock.json b/package-lock.json index c33054a7..3be48b0a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -7,6 +7,7 @@ "": { "name": "knowledge-sharing-app", "version": "0.1.0", + "hasInstallScript": true, "dependencies": { "@lottiefiles/react-lottie-player": "^3.5.0", "@next/font": "13.0.7", @@ -1107,7 +1108,7 @@ "version": "4.8.0", "resolved": "https://registry.npmjs.org/@prisma/engines/-/engines-4.8.0.tgz", "integrity": "sha512-A1Asn2rxZMlLAj1HTyfaCv0VQrLUv034jVay05QlqZg1qiHPeA3/pGTfNMijbsMYCsGVxfWEJuaZZuNxXGMCrA==", - "devOptional": true, + "dev": true, "hasInstallScript": true }, "node_modules/@prisma/engines-version": { @@ -5042,7 +5043,7 @@ "version": "4.8.0", "resolved": "https://registry.npmjs.org/prisma/-/prisma-4.8.0.tgz", "integrity": "sha512-DWIhxvxt8f4h6MDd35mz7BJff+fu7HItW3WPDIEpCR3RzcOWyiHBbLQW5/DOgmf+pRLTjwXQob7kuTZVYUAw5w==", - "devOptional": true, + "dev": true, "hasInstallScript": true, "dependencies": { "@prisma/engines": "4.8.0" @@ -6334,14 +6335,12 @@ "@firebase/auth-interop-types": { "version": "0.2.0", "resolved": "https://registry.npmjs.org/@firebase/auth-interop-types/-/auth-interop-types-0.2.0.tgz", - "integrity": "sha512-7Mt2qzwvu5X3Qxz24gjj0qITrBsMmy1W4vGBP8TZRuQrjA4OTlGVCTG8ysvweZ3xpdl1XGhBsIjo2KjfOPg0xA==", - "requires": {} + "integrity": "sha512-7Mt2qzwvu5X3Qxz24gjj0qITrBsMmy1W4vGBP8TZRuQrjA4OTlGVCTG8ysvweZ3xpdl1XGhBsIjo2KjfOPg0xA==" }, "@firebase/auth-types": { "version": "0.12.0", "resolved": "https://registry.npmjs.org/@firebase/auth-types/-/auth-types-0.12.0.tgz", - "integrity": "sha512-pPwaZt+SPOshK8xNoiQlK5XIrS97kFYc3Rc7xmy373QsOJ9MmqXxLaYssP5Kcds4wd2qK//amx/c+A8O2fVeZA==", - "requires": {} + "integrity": "sha512-pPwaZt+SPOshK8xNoiQlK5XIrS97kFYc3Rc7xmy373QsOJ9MmqXxLaYssP5Kcds4wd2qK//amx/c+A8O2fVeZA==" }, "@firebase/component": { "version": "0.6.0", @@ -6417,8 +6416,7 @@ "@firebase/firestore-types": { "version": "2.5.1", "resolved": "https://registry.npmjs.org/@firebase/firestore-types/-/firestore-types-2.5.1.tgz", - "integrity": "sha512-xG0CA6EMfYo8YeUxC8FeDzf6W3FX1cLlcAGBYV6Cku12sZRI81oWcu61RSKM66K6kUENP+78Qm8mvroBcm1whw==", - "requires": {} + "integrity": "sha512-xG0CA6EMfYo8YeUxC8FeDzf6W3FX1cLlcAGBYV6Cku12sZRI81oWcu61RSKM66K6kUENP+78Qm8mvroBcm1whw==" }, "@firebase/functions": { "version": "0.9.0", @@ -6477,8 +6475,7 @@ "@firebase/installations-types": { "version": "0.5.0", "resolved": "https://registry.npmjs.org/@firebase/installations-types/-/installations-types-0.5.0.tgz", - "integrity": "sha512-9DP+RGfzoI2jH7gY4SlzqvZ+hr7gYzPODrbzVD82Y12kScZ6ZpRg/i3j6rleto8vTFC8n6Len4560FnV1w2IRg==", - "requires": {} + "integrity": "sha512-9DP+RGfzoI2jH7gY4SlzqvZ+hr7gYzPODrbzVD82Y12kScZ6ZpRg/i3j6rleto8vTFC8n6Len4560FnV1w2IRg==" }, "@firebase/logger": { "version": "0.4.0", @@ -6603,8 +6600,7 @@ "@firebase/storage-types": { "version": "0.7.0", "resolved": "https://registry.npmjs.org/@firebase/storage-types/-/storage-types-0.7.0.tgz", - "integrity": "sha512-n/8pYd82hc9XItV3Pa2KGpnuJ/2h/n/oTAaBberhe6GeyWQPnsmwwRK94W3GxUwBA/ZsszBAYZd7w7tTE+6XXA==", - "requires": {} + "integrity": "sha512-n/8pYd82hc9XItV3Pa2KGpnuJ/2h/n/oTAaBberhe6GeyWQPnsmwwRK94W3GxUwBA/ZsszBAYZd7w7tTE+6XXA==" }, "@firebase/util": { "version": "1.8.0", @@ -6944,7 +6940,7 @@ "version": "4.8.0", "resolved": "https://registry.npmjs.org/@prisma/engines/-/engines-4.8.0.tgz", "integrity": "sha512-A1Asn2rxZMlLAj1HTyfaCv0VQrLUv034jVay05QlqZg1qiHPeA3/pGTfNMijbsMYCsGVxfWEJuaZZuNxXGMCrA==", - "devOptional": true + "dev": true }, "@prisma/engines-version": { "version": "4.8.0-61.d6e67a83f971b175a593ccc12e15c4a757f93ffe", @@ -7253,8 +7249,7 @@ "acorn-jsx": { "version": "5.3.2", "resolved": "https://registry.npmjs.org/acorn-jsx/-/acorn-jsx-5.3.2.tgz", - "integrity": "sha512-rq9s+JNhf0IChjtDXxllJ7g41oZk5SlXtp0LHwyA5cejwn7vKmKp4pPri6YEePv2PU65sAsegbXtIinmDFDXgQ==", - "requires": {} + "integrity": "sha512-rq9s+JNhf0IChjtDXxllJ7g41oZk5SlXtp0LHwyA5cejwn7vKmKp4pPri6YEePv2PU65sAsegbXtIinmDFDXgQ==" }, "acorn-node": { "version": "1.8.2", @@ -8178,8 +8173,7 @@ "eslint-plugin-react-hooks": { "version": "4.6.0", "resolved": "https://registry.npmjs.org/eslint-plugin-react-hooks/-/eslint-plugin-react-hooks-4.6.0.tgz", - "integrity": "sha512-oFc7Itz9Qxh2x4gNHStv3BqJq54ExXmfC+a1NjAta66IAN87Wu0R/QArgIS9qKzX3dXKPI9H5crl9QchNMY9+g==", - "requires": {} + "integrity": "sha512-oFc7Itz9Qxh2x4gNHStv3BqJq54ExXmfC+a1NjAta66IAN87Wu0R/QArgIS9qKzX3dXKPI9H5crl9QchNMY9+g==" }, "eslint-scope": { "version": "7.1.1", @@ -8406,8 +8400,7 @@ "@firebase/auth-interop-types": { "version": "0.1.7", "resolved": "https://registry.npmjs.org/@firebase/auth-interop-types/-/auth-interop-types-0.1.7.tgz", - "integrity": "sha512-yA/dTveGGPcc85JP8ZE/KZqfGQyQTBCV10THdI8HTlP1GDvNrhr//J5jAt58MlsCOaO3XmC4DqScPBbtIsR/EA==", - "requires": {} + "integrity": "sha512-yA/dTveGGPcc85JP8ZE/KZqfGQyQTBCV10THdI8HTlP1GDvNrhr//J5jAt58MlsCOaO3XmC4DqScPBbtIsR/EA==" }, "@firebase/component": { "version": "0.5.21", @@ -8656,8 +8649,7 @@ "goober": { "version": "2.1.11", "resolved": "https://registry.npmjs.org/goober/-/goober-2.1.11.tgz", - "integrity": "sha512-5SS2lmxbhqH0u9ABEWq7WPU69a4i2pYcHeCxqaNq6Cw3mnrF0ghWNM4tEGid4dKy8XNIAUbuThuozDHHKJVh3A==", - "requires": {} + "integrity": "sha512-5SS2lmxbhqH0u9ABEWq7WPU69a4i2pYcHeCxqaNq6Cw3mnrF0ghWNM4tEGid4dKy8XNIAUbuThuozDHHKJVh3A==" }, "google-auth-library": { "version": "8.7.0", @@ -9422,8 +9414,7 @@ "version": "8.6.6", "resolved": "https://registry.npmjs.org/markdown-it-anchor/-/markdown-it-anchor-8.6.6.tgz", "integrity": "sha512-jRW30YGywD2ESXDc+l17AiritL0uVaSnWsb26f+68qaW9zgbIIr1f4v2Nsvc0+s0Z2N3uX6t/yAw7BwCQ1wMsA==", - "optional": true, - "requires": {} + "optional": true }, "marked": { "version": "4.2.4", @@ -9829,7 +9820,7 @@ "version": "4.8.0", "resolved": "https://registry.npmjs.org/prisma/-/prisma-4.8.0.tgz", "integrity": "sha512-DWIhxvxt8f4h6MDd35mz7BJff+fu7HItW3WPDIEpCR3RzcOWyiHBbLQW5/DOgmf+pRLTjwXQob7kuTZVYUAw5w==", - "devOptional": true, + "dev": true, "requires": { "@prisma/engines": "4.8.0" } @@ -9955,8 +9946,7 @@ "react-icons": { "version": "4.7.1", "resolved": "https://registry.npmjs.org/react-icons/-/react-icons-4.7.1.tgz", - "integrity": "sha512-yHd3oKGMgm7zxo3EA7H2n7vxSoiGmHk5t6Ou4bXsfcgWyhfDKMpyKfhHR6Bjnn63c+YXBLBPUql9H4wPJM6sXw==", - "requires": {} + "integrity": "sha512-yHd3oKGMgm7zxo3EA7H2n7vxSoiGmHk5t6Ou4bXsfcgWyhfDKMpyKfhHR6Bjnn63c+YXBLBPUql9H4wPJM6sXw==" }, "react-is": { "version": "16.13.1", @@ -9971,8 +9961,7 @@ "react-table": { "version": "7.8.0", "resolved": "https://registry.npmjs.org/react-table/-/react-table-7.8.0.tgz", - "integrity": "sha512-hNaz4ygkZO4bESeFfnfOft73iBUj8K5oKi1EcSHPAibEydfsX2MyU6Z8KCr3mv3C9Kqqh71U+DhZkFvibbnPbA==", - "requires": {} + "integrity": "sha512-hNaz4ygkZO4bESeFfnfOft73iBUj8K5oKi1EcSHPAibEydfsX2MyU6Z8KCr3mv3C9Kqqh71U+DhZkFvibbnPbA==" }, "read-cache": { "version": "1.0.0", diff --git a/src/components/Common/UpvoteButton.tsx b/src/components/Common/UpvoteButton.tsx index 73beb746..ab30cf78 100644 --- a/src/components/Common/UpvoteButton.tsx +++ b/src/components/Common/UpvoteButton.tsx @@ -22,10 +22,12 @@ export const UpvoteButton: FC<{ }, [upvotesCount]) useEffect(() => { - users.find((user: any) => currentUser?.user_id === user.user_id) - ? setIsUpvoted(true) - : setIsUpvoted(false) - }, [users, currentUser]) + if (Array.isArray(users)) { + users.find((user: any) => currentUser?.user_id === user.user_id) + ? setIsUpvoted(true) + : setIsUpvoted(false); + } + }, [users, currentUser]); return (
diff --git a/src/components/Notes/Table.tsx b/src/components/Notes/Table.tsx index d483da29..489e0963 100644 --- a/src/components/Notes/Table.tsx +++ b/src/components/Notes/Table.tsx @@ -25,6 +25,48 @@ import { removeNotesUpvote } from '../../services/db/notes/removeNotesUpvote' import { upvoteNotes } from '../../services/db/notes/upvoteNotes' import { IoMdArrowDropdown, IoMdArrowDropup } from 'react-icons/io' import { GlobalFilter } from '../Common/GlobalFilter' +const dummyNotesData: notesColumnData[] = [ + { + sno: '1.', + upvotes: { + count: 10, + users: [ + { user_id: 'user1' }, + { user_id: 'user2' } + ], + }, + title: 'Introduction to React', + subjectCode: 'CS101', + subjectName: 'Web Development', + url: 'https://example.com/notes/react-intro', + semester: 'Semester 3', + instructor: 'Dr. Smith', + branch: 'Computer Science', + uploadedBy: 'User 1', + actions: , + }, + { + sno: '2.', + upvotes: { + count: 10, + users: [ + { user_id: 'user1' }, + { user_id: 'user2' } + ], + }, + title: 'Data Structures Basics', + subjectCode: 'CS201', + subjectName: 'Algorithms and Data Structures', + url: 'https://example.com/notes/data-structures', + semester: 'Semester 4', + instructor: 'Prof. Johnson', + branch: 'Computer Science', + uploadedBy: 'User 2', + actions: , + }, + // Add more dummy data as needed + ]; + export const Table: FC<{ notes: notesColumnData[] @@ -306,28 +348,28 @@ export const Table: FC<{ () => isDataFetching ? Array(8).fill({}) - : notes && - notes.map((note: any, index) => { + : dummyNotesData && + dummyNotesData.map((note: any, index) => { return { sno: `${index + 1}.`, id: note.id, upvotes: { - count: note._count.upvotes, - users: note.upvotes, + count: note.upvotes.count, + users: note.upvotes.users, }, title: note.title, - subjectCode: note.subject_code, - subjectName: note.subject.name, + subjectCode: note.subjectCode, + subjectName: note.subjectName, url: note.url, semester: note.semester, - instructor: note.instructor.name, + instructor: note.instructor, branch: note.branch, - uploadedBy: note.created_by.name, + uploadedBy: note.uploadedBy, anonymous: note.anonymous, actions: note, } }), - [isDataFetching, notes] + [isDataFetching, dummyNotesData] ) const { @@ -354,7 +396,7 @@ export const Table: FC<{ } }, [loading, setHiddenColumns, user]) - return isDataFetching || notes.length ? ( + return isDataFetching || dummyNotesData.length ? (
console.log('Edit')}>Edit, + }, + { + sno: '2.', + upvotes: { + count: 5, + users: [ + { user_id: 'user3' }, + { user_id: 'user4' } + ], + }, + title: 'Sample Title 2', + subjectCode: 'SC002', + subjectName: 'Sample Subject 2', + url: 'https://example.com/sample2', + semester: 'Semester 2', + instructor: 'Instructor 2', + branch: 'Branch 2', + uploadedBy: 'User 2', + actions: , + }, +]; + + export const Table: FC<{ pyqs: pyqsColumnData[] setSelectedPYQ: Dispatch> @@ -299,29 +341,33 @@ export const Table: FC<{ () => isDataFetching ? Array(8).fill({}) - : pyqs && - pyqs.map((pyq: any, index) => { + : dummyPyqsData + ? dummyPyqsData.map((pyq: any, index) => { return { sno: `${index + 1}.`, id: pyq.id, title: pyq.title, upvotes: { - count: pyq._count.upvotes, + count: // Replace this with a static value or your logic + pyq.upvotes ? pyq.upvotes.length : 0, // For example, counting the length of upvotes array users: pyq.upvotes, }, - subjectCode: pyq.subject_code, - subjectName: pyq.subject.name, + subjectCode: pyq.subjectCode, + subjectName: pyq.subjectName ? pyq.subjectName : '', url: pyq.url, semester: pyq.semester, - instructor: pyq.instructor.name, + instructor: pyq.instructor ? pyq.instructor: '', branch: pyq.branch, - uploadedBy: pyq.created_by.name, + uploadedBy: pyq.created_by ? pyq.uploadedBy : '', anonymous: pyq.anonymous, actions: pyq, - } - }), - [isDataFetching, pyqs] - ) + }; + }) + : [], + [isDataFetching, dummyPyqsData] + ); + + const { getTableProps, @@ -347,7 +393,7 @@ export const Table: FC<{ } }, [loading, setHiddenColumns, user]) - return isDataFetching || pyqs.length ? ( + return isDataFetching || dummyPyqsData.length ? (
{ //? router const router = useRouter() @@ -51,13 +149,14 @@ const Course: NextPage = ({}) => { return [...userReviews, ...restReviews] } else return reviews } + const refetchReviews = () => { setIsDataFetching(true) if (router.query.id) { const id = parseInt(router.query.id[0]) getCourseById({ id }).then((res) => { - setReviews(res.reviews) + setReviews(dummyCourse.reviews) setIsDataFetching(false) }) } @@ -65,16 +164,19 @@ const Course: NextPage = ({}) => { //? effects useEffect(() => { - setIsDataFetching(true) + setIsDataFetching(true); if (router.query.id) { - const id = parseInt(router.query.id[0]) - getCourseById({ id }).then((res) => { - setCourse(res) - setReviews(res.reviews) - setIsDataFetching(false) - }) + const id = parseInt(router.query.id[0]); + if (id === 1) { + setCourse(dummyCourse); + setReviews(dummyCourse.reviews); + } else if (id === 2) { + setCourse(dummyCourse2); + setReviews(dummyCourse2.reviews); + } + setIsDataFetching(false); } - }, [router]) + }, [router]); useEffect(() => { if (selectedCourseReview) setShowUpdateReviewModal(true) @@ -269,9 +371,7 @@ const Course: NextPage = ({}) => { + + +
+ ), + }, + { + sno: '2.', + upvotes: 5, + title: 'Web Development Advanced', + code: 'WD102', + created_by_id: 2, + instructor: { name: 'Mrs. Advanced' }, + reviews: [ + { + id: 3, + rating: 5, + comment: 'Excellent content!', + upvotes: [], + anonymous: false, + user: { + name: 'Advanced User', + }, + }, + { + id: 4, + rating: 3, + comment: 'Needs improvement', + upvotes: [], + anonymous: true, + user: null, + }, + ], + actions: ( +
+ + +
+ ),} + // Add more dummy courses as needed + ]; + export default function Courses() { //? router const router = useRouter() @@ -30,6 +103,9 @@ export default function Courses() { const [searchInput, setSearchInput] = useState('') //? functions + + + const refetchCourses = () => { setIsDataFetching(true) getCourses().then((res) => { @@ -40,12 +116,13 @@ export default function Courses() { //? effects useEffect(() => { - setIsDataFetching(true) - getCourses().then((res) => { - setCourses(res) - setIsDataFetching(false) - }) - }, []) + setIsDataFetching(true); + // getCourses().then((res) => { + // setCourses(res); + setCourses(dummyCourses); // Use dummy data instead + setIsDataFetching(false); + // }); + }, []); useEffect(() => { if (selectedCourse) setShowUpdateCourseModal(true) @@ -191,7 +268,7 @@ export default function Courses() {

router.push( - '/courses/' + course.id + '/courses/' + course.sno ) } className="font-semibold cursor-pointer text-primary hover:underline duration-150 transition-all text-xl" @@ -242,7 +319,7 @@ export default function Courses() {

Total Reviews:

-

{course._count.reviews}

+

{course.reviews.length}

) diff --git a/src/utils/api.ts b/src/utils/api.ts index 0cc57db8..48de6ece 100644 --- a/src/utils/api.ts +++ b/src/utils/api.ts @@ -1,13 +1,13 @@ import axios from 'axios' -let urls = { - test: `http://localhost:3000`, - development: `http://localhost:3000`, - production: `https://knowledge-sharing-platform.vercel.app`, -} +// let urls = { +// test: `http://localhost:3000`, +// development: `http://localhost:3000`, +// production: `https://knowledge-sharing-platform.vercel.app`, +// } export const api = axios.create({ - baseURL: urls[process.env.NODE_ENV], + baseURL: `https://knowledge-sharing-platform.vercel.app`, headers: { Accept: 'application/json', 'Content-Type': 'application/json', diff --git a/src/utils/firebaseAdminInit.ts b/src/utils/firebaseAdminInit.ts index 6cb08670..e17574a4 100644 --- a/src/utils/firebaseAdminInit.ts +++ b/src/utils/firebaseAdminInit.ts @@ -1,33 +1,33 @@ -import { - AppOptions, - cert, - getApp, - getApps, - initializeApp, - ServiceAccount, -} from 'firebase-admin/app' -import { getAuth } from 'firebase-admin/auth' +// import { +// AppOptions, +// cert, +// getApp, +// getApps, +// initializeApp, +// ServiceAccount, +// } from 'firebase-admin/app' +// import { getAuth } from 'firebase-admin/auth' -const { privateKey } = JSON.parse(process.env.PRIVATE_KEY!) +// const { privateKey } = JSON.parse(process.env.PRIVATE_KEY!) -const credentials: ServiceAccount = { - projectId: process.env.PROJECT_ID, - privateKey: privateKey, - clientEmail: process.env.CLIENT_EMAIL, -} +// const credentials: ServiceAccount = { +// projectId: process.env.PROJECT_ID, +// privateKey: privateKey, +// clientEmail: process.env.CLIENT_EMAIL, +// } -const options: AppOptions = { - credential: cert(credentials), - databaseURL: process.env.databaseURL, -} +// const options: AppOptions = { +// credential: cert(credentials), +// databaseURL: process.env.databaseURL, +// } -function createFirebaseAdminApp(config: AppOptions) { - if (getApps().length === 0) { - return initializeApp(config) - } else { - return getApp() - } -} +// function createFirebaseAdminApp(config: AppOptions) { +// if (getApps().length === 0) { +// return initializeApp(config) +// } else { +// return getApp() +// } +// } -const firebaseAdmin = createFirebaseAdminApp(options) -export const adminAuth = getAuth(firebaseAdmin) +// const firebaseAdmin = createFirebaseAdminApp(options) +// export const adminAuth = getAuth(firebaseAdmin) diff --git a/src/utils/firebaseInit.ts b/src/utils/firebaseInit.ts index 18327bb1..b10bf239 100644 --- a/src/utils/firebaseInit.ts +++ b/src/utils/firebaseInit.ts @@ -1,14 +1,14 @@ -import { FirebaseOptions, getApp, initializeApp } from 'firebase/app' -import { getAuth } from 'firebase/auth' -import { FIREBASE_CONFIG } from '../config' +// import { FirebaseOptions, getApp, initializeApp } from 'firebase/app' +// import { getAuth } from 'firebase/auth' +// import { FIREBASE_CONFIG } from '../config' -function createFirebaseApp(config: FirebaseOptions) { - try { - return getApp() - } catch { - return initializeApp(config) - } -} +// function createFirebaseApp(config: FirebaseOptions) { +// try { +// return getApp() +// } catch { +// return initializeApp(config) +// } +// } -const firebaseApp = createFirebaseApp(FIREBASE_CONFIG) -export const firebaseAuth = getAuth(firebaseApp) +// const firebaseApp = createFirebaseApp(FIREBASE_CONFIG) +// export const firebaseAuth = getAuth(firebaseApp)