From 27cd84127f36376a9befe4058811d7f4c1c7797b Mon Sep 17 00:00:00 2001 From: swve Date: Fri, 26 May 2023 16:52:53 +0000 Subject: [PATCH] feat: init collectionid page --- .../collection/[collectionid]/error.tsx | 23 +++++++ .../collection/[collectionid]/loading.tsx | 8 +++ .../collection/[collectionid]/page.tsx | 62 +++++++++++++++++++ .../(withmenu)/course/[courseid]/page.tsx | 1 + front/app/orgs/[orgslug]/(withmenu)/page.tsx | 6 +- front/services/courses/collections.ts | 14 +++++ front/services/utils/ts/requests.ts | 2 +- src/services/courses/collections.py | 12 ++++ 8 files changed, 126 insertions(+), 2 deletions(-) create mode 100644 front/app/orgs/[orgslug]/(withmenu)/collection/[collectionid]/error.tsx create mode 100644 front/app/orgs/[orgslug]/(withmenu)/collection/[collectionid]/loading.tsx create mode 100644 front/app/orgs/[orgslug]/(withmenu)/collection/[collectionid]/page.tsx diff --git a/front/app/orgs/[orgslug]/(withmenu)/collection/[collectionid]/error.tsx b/front/app/orgs/[orgslug]/(withmenu)/collection/[collectionid]/error.tsx new file mode 100644 index 00000000..9e4c3efb --- /dev/null +++ b/front/app/orgs/[orgslug]/(withmenu)/collection/[collectionid]/error.tsx @@ -0,0 +1,23 @@ +'use client'; // Error components must be Client Components + +import ErrorUI from '@components/UI/Error/Error'; +import { useEffect } from 'react'; + +export default function Error({ + error, + reset, +}: { + error: Error; + reset: () => void; +}) { + useEffect(() => { + // Log the error to an error reporting service + console.error(error); + }, [error]); + + return ( +
+ +
+ ); +} \ No newline at end of file diff --git a/front/app/orgs/[orgslug]/(withmenu)/collection/[collectionid]/loading.tsx b/front/app/orgs/[orgslug]/(withmenu)/collection/[collectionid]/loading.tsx new file mode 100644 index 00000000..9a7cafe9 --- /dev/null +++ b/front/app/orgs/[orgslug]/(withmenu)/collection/[collectionid]/loading.tsx @@ -0,0 +1,8 @@ +import PageLoading from "@components/Pages/PageLoading"; + +export default function Loading() { + return ( + + ) + +} \ No newline at end of file diff --git a/front/app/orgs/[orgslug]/(withmenu)/collection/[collectionid]/page.tsx b/front/app/orgs/[orgslug]/(withmenu)/collection/[collectionid]/page.tsx new file mode 100644 index 00000000..0797c455 --- /dev/null +++ b/front/app/orgs/[orgslug]/(withmenu)/collection/[collectionid]/page.tsx @@ -0,0 +1,62 @@ +import { getBackendUrl, getUriWithOrg } from "@services/config/config"; +import { getCollectionByIdWithAuthHeader } from "@services/courses/collections"; +import { getOrganizationContextInfo } from "@services/organizations/orgs"; +import { Metadata } from "next"; +import { cookies } from "next/headers"; +import Link from "next/link"; + +type MetadataProps = { + params: { orgslug: string, courseid: string, collectionid: string }; + searchParams: { [key: string]: string | string[] | undefined }; +}; + +export async function generateMetadata( + { params }: MetadataProps, +): Promise { + const cookieStore = cookies(); + const access_token_cookie: any = cookieStore.get('access_token_cookie'); + // Get Org context information + const org = await getOrganizationContextInfo(params.orgslug, { revalidate: 1800, tags: ['organizations'] }); + const col = await getCollectionByIdWithAuthHeader(params.collectionid, access_token_cookie ? access_token_cookie.value : null, { revalidate: 0, tags: ['collections'] }); + + console.log(col) + + return { + title: `Collection : ${col.name} — ${org.name}`, + description: `${col.description} `, + }; +} + +const CollectionPage = async (params: any) => { + const cookieStore = cookies(); + const access_token_cookie: any = cookieStore.get('access_token_cookie'); + const orgslug = params.params.orgslug; + const col = await getCollectionByIdWithAuthHeader(params.params.collectionid, access_token_cookie ? access_token_cookie.value : null, { revalidate: 0, tags: ['collections'] }); + + const removeCoursePrefix = (courseid: string) => { + return courseid.replace("course_", "") + } + + + return
+

Collection

+

{col.name}

+
+
+ {col.courses.map((course: any) => ( +
+ +
+
+ +

{course.name}

+
+ ))} +
+ + + +
; +}; + +export default CollectionPage; \ No newline at end of file diff --git a/front/app/orgs/[orgslug]/(withmenu)/course/[courseid]/page.tsx b/front/app/orgs/[orgslug]/(withmenu)/course/[courseid]/page.tsx index e5494c84..2e07285e 100644 --- a/front/app/orgs/[orgslug]/(withmenu)/course/[courseid]/page.tsx +++ b/front/app/orgs/[orgslug]/(withmenu)/course/[courseid]/page.tsx @@ -15,6 +15,7 @@ export async function generateMetadata( ): Promise { const cookieStore = cookies(); const access_token_cookie: any = cookieStore.get('access_token_cookie'); + // Get Org context information const org = await getOrganizationContextInfo(params.orgslug, { revalidate: 1800, tags: ['organizations'] }); diff --git a/front/app/orgs/[orgslug]/(withmenu)/page.tsx b/front/app/orgs/[orgslug]/(withmenu)/page.tsx index 4c5def42..f4d48efc 100644 --- a/front/app/orgs/[orgslug]/(withmenu)/page.tsx +++ b/front/app/orgs/[orgslug]/(withmenu)/page.tsx @@ -43,6 +43,10 @@ const OrgHomePage = async (params: any) => { return course_id.replace("course_", ""); } + function removeCollectionPrefix(collection_id: string) { + return collection_id.replace("collection_", ""); + } + return (
@@ -51,7 +55,7 @@ const OrgHomePage = async (params: any) => {
{collections.map((collection: any) => (
- +

{collection.name}

diff --git a/front/services/courses/collections.ts b/front/services/courses/collections.ts index 73805141..4d99f19b 100644 --- a/front/services/courses/collections.ts +++ b/front/services/courses/collections.ts @@ -19,6 +19,20 @@ export async function createCollection(collection: any) { return res; } + +// Get a colletion by id +export async function getCollectionById(collection_id: any) { + const result: any = await fetch(`${getAPIUrl()}collections/${collection_id}`, { next: { revalidate: 10 } }); + const res = await errorHandling(result); + return res; +} + +export async function getCollectionByIdWithAuthHeader(collection_id: any, access_token: string, next: any) { + const result: any = await fetch(`${getAPIUrl()}collections/collection_${collection_id}`, RequestBodyWithAuthHeader("GET", null, next, access_token)); + const res = await errorHandling(result); + return res; +} + // Get collections // TODO : add per org filter export async function getOrgCollections() { diff --git a/front/services/utils/ts/requests.ts b/front/services/utils/ts/requests.ts index 06c88816..e5b9bf5a 100644 --- a/front/services/utils/ts/requests.ts +++ b/front/services/utils/ts/requests.ts @@ -78,7 +78,7 @@ export const swrFetcher = async (url: string, body: any, router?: AppRouterInsta export const errorHandling = (res: any) => { if (!res.ok) { - const error: any = new Error(`Error ${res.status}: ${res.statusText}`, {}); + const error: any = new Error(`${res.status}: ${res.statusText}`, {}); error.status = res.status; throw error; } diff --git a/src/services/courses/collections.py b/src/services/courses/collections.py index 3b5ed279..8f482f1c 100644 --- a/src/services/courses/collections.py +++ b/src/services/courses/collections.py @@ -38,6 +38,18 @@ async def get_collection(request: Request,collection_id: str, current_user: Publ status_code=status.HTTP_409_CONFLICT, detail="Collection does not exist") collection = Collection(**collection) + + # add courses to collection + courses = request.app.db["courses"] + courseids = [course for course in collection.courses] + + collection.courses = [] + collection.courses = courses.find( + {"course_id": {"$in": courseids}}, {'_id': 0}) + + collection.courses = [course for course in await collection.courses.to_list(length=100)] + + return collection