Skip to content

Commit 717f93e

Browse files
authored
Merge pull request #54 from OpenLXP/feature/course-info-mappings
course mapping connection to backend
2 parents e00d635 + 262b968 commit 717f93e

File tree

5 files changed

+59
-8
lines changed

5 files changed

+59
-8
lines changed

src/config/endpoints.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ export const login_url = `${host}${api}auth/login`
1313
export const register_url = `${host}${api}auth/register`
1414

1515
//configs
16-
1716
export const configUrl = `${host}${api}config/catalogs/`
1817

1918
//sso configs
2019
export const ssoURL = `${host}${api}config/sso/`
20+
21+
//UI information mappings
22+
export const uiConfigUrl = `${host}${api}config/info-mapping/`;

src/hooks/useInfoMappings.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { axiosInstance } from '../config/axiosInstance';
2+
import { uiConfigUrl } from '../config/endpoints';
3+
import { twentyFourHours } from '../config/timeConstants';
4+
import { useQuery } from 'react-query';
5+
6+
/**
7+
* @description Reaches out to the backend and gets the configuration data required for the application. Valid for 24hrs
8+
*/
9+
10+
export function useInfoMappings() {
11+
return useQuery(
12+
'ui-config',
13+
() => axiosInstance.get(uiConfigUrl).then((res) => res.data),
14+
{
15+
staleTime: twentyFourHours,
16+
cacheTime: twentyFourHours,
17+
}
18+
);
19+
}

src/pages/dashboard/Courses/CourseList/CourseList.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22

33
import CourseListHeader from "../CourseHeader/CourseListHeader";
44
import { useRouter } from "next/router";
5+
import { useInfoMappings } from "@/hooks/useInfoMappings";
6+
import { getDeeplyNestedData } from "@/utils/getDeeplyNestedData";
7+
import { data } from "browserslist";
58

69
const CourseList = (props) => {
710
const router = useRouter();
11+
const config = useInfoMappings();
812

913
const courses = props.data?.experiences.results || [];
1014
// Creates the individual rows of the table.
@@ -16,10 +20,10 @@ const CourseList = (props) => {
1620
onClick={() => router.push(`/dashboard/${data.provider_name}/${data.metadata_key_hash}`)} >
1721
<td className="px-6 py-4 text-sm text-gray-900">
1822
<div className="font-medium">
19-
{data.metadata?.Course?.CourseTitle}
23+
{getDeeplyNestedData(config.data?.course_title, data)}
2024
</div>
2125
<div className="font-light">
22-
{data.metadata?.Course?.CourseCode}
26+
{getDeeplyNestedData(config.data?.course_code, data)}
2327
</div>
2428
</td>
2529

src/pages/dashboard/[catalogTitle]/[courseMetadataKey].js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
'use strict';
22

33
import { Dialog, Transition } from "@headlessui/react";
4-
import { Fragment, useEffect, useState } from "react";
4+
import { Fragment, useEffect, useMemo, useState } from "react";
55
import { useParams } from "react-router-dom";
66
import { updateDeeplyNestedJson } from "../../../utils/utils";
77
import { catalog_courses_url } from "../../../config/endpoints";
88
import { axiosInstance } from "../../../config/axiosInstance";
99
import { TrashIcon } from '@heroicons/react/outline';
1010
import DefaultLayout from "@/components/layouts/DefaultLayout";
11+
import { getDeeplyNestedData } from "@/utils/getDeeplyNestedData";
12+
import { useInfoMappings } from "@/hooks/useInfoMappings";
1113

1214
export function getServerSideProps(context) {
1315
const { catalogTitle, courseMetadataKey } = context.query;
@@ -121,17 +123,19 @@ export default function CourseDataContainerV2({catalogTitle, courseMetadataKey})
121123
});
122124
}
123125

126+
127+
124128
// title and basic info
125-
function courseHeader() {
126-
const title = course.data?.metadata?.Metadata_Ledger.Course.CourseTitle;
129+
function courseHeader() {
130+
127131
return (
128132
<div
129-
title={title}
133+
title={getDeeplyNestedData(config.data?.course_title.replace("metadata.", "metadata.Metadata_Ledger."), course.data)}
130134
className={
131135
"w-full flex flex-row my-2 py-2 px-2 space-x-1 justify-start "
132136
}
133137
>
134-
<div className={"text-xl font-bold w-full"}>{title}</div>
138+
<div className={"text-xl font-bold w-full"}>{getDeeplyNestedData(config.data?.course_title.replace("metadata.", "metadata.Metadata_Ledger."), course.data)}</div>
135139
<div
136140
className={
137141
"px-2 mt-1 text-xs leading-5 self-center font-semibold rounded-full bg-green-100 text-green-800"
@@ -376,6 +380,8 @@ export default function CourseDataContainerV2({catalogTitle, courseMetadataKey})
376380
};
377381
}, [id]);
378382

383+
const config = useInfoMappings();
384+
379385
return (
380386
<DefaultLayout>
381387
<div className="bg-white shadow rounded-md pb-4 mb-6">

src/utils/getDeeplyNestedData.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Functions to render data
2+
export const getDeeplyNestedData = (strKey, data) => {
3+
if (!strKey) return null;
4+
5+
// gets the keys for the data mapping
6+
const objKeys = strKey.split('.');
7+
8+
// inits with all the data
9+
let valueToReturn = data;
10+
11+
// Reduces it down to the specific value
12+
objKeys.forEach((key) => {
13+
if (valueToReturn) {
14+
valueToReturn = valueToReturn[key];
15+
}
16+
});
17+
18+
// Returning the desired value.
19+
return valueToReturn;
20+
};

0 commit comments

Comments
 (0)