Skip to content

Commit 04ed1ae

Browse files
committed
refactor: learning materials
1 parent 45d77c5 commit 04ed1ae

File tree

8 files changed

+64
-57
lines changed

8 files changed

+64
-57
lines changed

src/components/sections/communities/overview/LearningMaterials.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@ export default function LearningMaterialsOverview() {
3131
<div className="text-sm font-light lg:w-full lg:pr-7 pt-2 mb-6 md:mb-0">{t("communities.overview.learning-materials.description")} </div>
3232
</div>
3333
<div className="grid gap-6">
34-
{courses?.map((course, index) => {
35-
return <CourseCard key={`learning-material-course-${index}`} course={course} community={community} />
34+
{courses?.map((course) => {
35+
return <CourseCard key={`learning-material-course-${course.id}`} course={course} community={community} />
3636
})}
3737
</div>
38-
<div className="grid md:grid-cols-2 gap-3 w-full ">
39-
{learningModules?.map((module, index) => {
38+
<div className="grid md:grid-cols-2 gap-3 w-full">
39+
{learningModules?.map((module) => {
4040
return <RelatedLearningCard
41-
key={`learning-material-material-${index}`}
41+
key={`learning-material-material-${module.id}`}
4242
title={module.title}
4343
description={module.description}
4444
path={`/communities/${community.slug}`} />

src/components/sections/communities/overview/Sidebar.tsx

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import Link from "next/link";
33
import { useRouter } from "next/router";
44
import { useTranslation } from "next-i18next";
55
import classNames from "classnames";
6+
import { useMemo } from "react";
67

78
/**
89
* @interface SidebarProps
@@ -29,22 +30,33 @@ export default function Sidebar(): JSX.Element {
2930
return router.asPath === link;
3031
};
3132

32-
const scoreboardLink = hasCurrentCommunity ? `/communities/${currentCommunity.slug}/scoreboard` : "";
33-
const learningMaterialsLink = hasCurrentCommunity ? `/communities/${currentCommunity.slug}/learning-materials` : "";
34-
const mainLink = hasCurrentCommunity ? `/communities/${currentCommunity.slug}` : "";
33+
const links = useMemo(() => {
34+
if (!hasCurrentCommunity)
35+
return {
36+
scoreboardLink: "",
37+
learningMaterialsLink: "",
38+
mainLink: "",
39+
};
40+
const { slug } = currentCommunity;
41+
return {
42+
scoreboardLink: `/communities/${slug}/scoreboard`,
43+
learningMaterialsLink: `/communities/${slug}/learning-materials`,
44+
mainLink: `/communities/${slug}`,
45+
};
46+
}, [hasCurrentCommunity, currentCommunity]);
3547

3648
return (
3749
<div className="flex flex-col md:divide-y divide-solid divide-gray-100 w-full text-gray-700 space-y-6">
38-
<Link href={mainLink}>
39-
<div className={isActive(mainLink) ? "" : "text-tertiary"}>
50+
<Link href={links.mainLink}>
51+
<div className={isActive(links.mainLink) ? "" : "text-tertiary"}>
4052
<div className="font-medium text-.5xl leading-snug">{t("communities.overview.challenges.title")}</div>
4153
<div className="text-sm font-light lg:w-full lg:pr-7 pt-2 mb-6 md:mb-0">{t("communities.overview.challenges.description")} </div>
4254
</div>
4355
</Link>
44-
<Link href={learningMaterialsLink}>
56+
<Link href={links.learningMaterialsLink}>
4557
<div className={classNames("pt-6",
4658
{
47-
"text-tertiary": !isActive(learningMaterialsLink),
59+
"text-tertiary": !isActive(links.learningMaterialsLink),
4860
}
4961
)}
5062
>
@@ -53,11 +65,11 @@ export default function Sidebar(): JSX.Element {
5365
</div>
5466
</Link>
5567
{hasCurrentCommunity && (
56-
<Link href={scoreboardLink}>
68+
<Link href={links.scoreboardLink}>
5769
<div className={classNames("pt-6",
5870
{
59-
"md:block hidden scroll-smooth": isActive(scoreboardLink),
60-
"text-tertiary": !isActive(scoreboardLink)
71+
"md:block hidden scroll-smooth": isActive(links.scoreboardLink),
72+
"text-tertiary": !isActive(links.scoreboardLink)
6173
},
6274
)}>
6375
<div className="font-medium text-.5xl leading-snug">{t("communities.overview.scoreboard.title")}</div>

src/pages/communities/[slug]/index.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { useTranslation } from "next-i18next";
1515
import Head from "next/head";
1616
import { getMetadataDescription, getMetadataTitle } from "@/utilities/Metadata";
1717
import LearningMaterialsOverview from "@/components/sections/communities/overview/LearningMaterials";
18+
import { v4 as uuidv4 } from "uuid";
1819

1920
export default function Slug(props: {
2021
pageProps: {
@@ -28,8 +29,8 @@ export default function Slug(props: {
2829
<>
2930
<Head>
3031
<title>{getMetadataTitle(t("communities.navigation.courses"), community?.name as string)}</title>
31-
{getMetadataDescription(community?.description as string).map((attributes, i) => (
32-
<meta key={`scoreboard-meta-${i}`} {...attributes} />
32+
{getMetadataDescription(community?.description as string).map((attributes) => (
33+
<meta key={`scoreboard-meta-${uuidv4()}`} {...attributes} />
3334
))}
3435
</Head>
3536
<CommunityWrapper>
@@ -64,13 +65,15 @@ export const getServerSideProps = wrapper.getServerSideProps((store) => async ({
6465
store.dispatch(fetchLearningMaterials({ slug, locale })),
6566
serverSideTranslations(locale as string),
6667
]);
68+
6769
if (!community || !challenges) throw new NotFoundError();
70+
6871
return {
6972
props: {
7073
community,
7174
challenges,
7275
scoreboard,
73-
learningMaterials,
76+
learningMaterials: learningMaterials || { courses: [], learningModule: [] },
7477
...translations,
7578
},
7679
};

src/pages/communities/[slug]/learning-materials.tsx

Lines changed: 29 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -12,46 +12,41 @@ import CommunityWrapper from "@/components/sections/communities/overview/Wrapper
1212
import { Course, LearningModule } from "@/types/course";
1313
import LearningMaterialsOverview from "@/components/sections/communities/overview/LearningMaterials";
1414

15-
16-
1715
export default function LearningMaterials(props: {
18-
pageProps: {
19-
community: Community;
20-
learningMaterials: { courses: Course[], learningModules: LearningModule[] };
21-
};
16+
pageProps: {
17+
community: Community;
18+
learningMaterials: { courses: Course[]; learningModules: LearningModule[] };
19+
};
2220
}) {
23-
const { t } = useTranslation()
24-
const { community } = props.pageProps
25-
return <div>
26-
<Head>
27-
<title>{getMetadataTitle(t("communities.navigation.learning-materials"), community?.name as string)}</title>
28-
{getMetadataDescription(community?.description as string).map((attributes, i) => (
29-
<meta key={`scoreboard-meta-${i}`} {...attributes} />
30-
))}
31-
</Head>
32-
<CommunityWrapper >
33-
<LearningMaterialsOverview />
34-
</CommunityWrapper>
21+
const { t } = useTranslation();
22+
const { community } = props.pageProps;
23+
return (
24+
<div>
25+
<Head>
26+
<title>{getMetadataTitle(t("communities.navigation.learning-materials"), community?.name as string)}</title>
27+
{getMetadataDescription(community?.description as string).map((attributes, i) => (
28+
<meta key={`scoreboard-meta-${i}`} {...attributes} />
29+
))}
30+
</Head>
31+
<CommunityWrapper>
32+
<LearningMaterialsOverview />
33+
</CommunityWrapper>
3534
</div>
36-
35+
);
3736
}
3837

3938
LearningMaterials.getLayout = function (page: ReactElement) {
40-
return <CommunityLayout>{page}</CommunityLayout>;
39+
return <CommunityLayout>{page}</CommunityLayout>;
4140
};
4241

4342
export const getServerSideProps: GetServerSideProps = wrapper.getServerSideProps((store) => async ({ locale, params }) => {
44-
const slug = params?.slug as string;
45-
try {
46-
const [{ data: community }] = await Promise.all([
47-
store.dispatch(fetchCurrentCommunity({ slug, locale })),
48-
store.dispatch(fetchLearningMaterials({ slug, locale })),
49-
])
50-
return { props: { community, ...(await i18Translate(locale as string)) } };
51-
}
52-
catch (err) {
53-
return {
54-
notFound: true,
55-
};
56-
}
57-
})
43+
const slug = params?.slug as string;
44+
try {
45+
const [{ data: community }] = await Promise.all([store.dispatch(fetchCurrentCommunity({ slug, locale })), store.dispatch(fetchLearningMaterials({ slug, locale }))]);
46+
return { props: { community, ...(await i18Translate(locale as string)) } };
47+
} catch (err) {
48+
return {
49+
notFound: true,
50+
};
51+
}
52+
});

src/pages/communities/[slug]/scoreboard.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export const getServerSideProps: GetServerSideProps = wrapper.getServerSideProps
5353
const slug = params?.slug as string;
5454

5555
try {
56-
const [{ data: community }, { data: scoreboards },] = await Promise.all([
56+
const [{ data: community }, { data: scoreboards }] = await Promise.all([
5757
store.dispatch(fetchCurrentCommunity({ slug, locale })),
5858
store.dispatch(fetchAllScoreboards({ slug, locale: locale || "en" })),
5959
]);

src/store/feature/learningMaterials.slice.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ const learningMaterialsSlice = createSlice({
3030
[HYDRATE]: (state, action) => {
3131
return {
3232
...state,
33-
...action.payload["learningMaterials"],
33+
...action.payload.learningMaterials,
3434
};
3535
},
3636
}

src/store/services/community.service.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,9 @@ export const communityService = createApi({
6565
const { data } = await queryFulfilled
6666
dispatch(setCourseList(data.courses))
6767
dispatch(setLearningModulesList(data.learningModules))
68-
console.log("these are the data", data.courses?.length)
6968
return data
7069
}
7170
catch (error) {
72-
console.log("Encoutered an error while fetching the learning materials", error)
7371
return null
7472
}
7573
finally {

src/store/services/learningModules.service.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ export const learningModulesService = createApi({
3737
onQueryStarted: async (_, { dispatch, queryFulfilled }) => {
3838

3939
const { data } = await queryFulfilled;
40-
console.log("the learning module data -------------------------", data)
4140
dispatch(setLearningModuleList(data));
4241
},
4342
}),

0 commit comments

Comments
 (0)