Skip to content

Commit c580f9c

Browse files
committed
refactor(app/learn): use new request way
1 parent be915d0 commit c580f9c

File tree

16 files changed

+143
-249
lines changed

16 files changed

+143
-249
lines changed

src/app/creator/learn/[type]/[id]/StepThree.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import { Button } from '@/components/Button';
2323
import { PlusIcon } from '@/components/icon/outlined';
2424
import { EditIcon, DeleteIcon, AddIcon } from '@/components/Icons';
2525
import { OEditor } from '@/components/MarkDown';
26-
// import { useCourseDetails } from '#/services/learn/hooks'
2726
import { Modal } from '@/components/Modal';
2827
import { Select } from '@/components/Select';
2928
import { BASE_INPUT_STYLE } from '@/constants/config';

src/app/learn/NFTCertificates.js

Lines changed: 0 additions & 83 deletions
This file was deleted.

src/app/learn/[type]/Container.tsx

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,13 @@
1515
*/
1616

1717
import { PAGE_SIZE } from '@/constants/config';
18-
import { get } from '@/utils/request';
18+
import { pick } from '@/utils';
19+
20+
import { fetchList as fetchChallengeList } from '#/domain/challenge/repository';
21+
import { fetchList as fetchCourseList } from '#/domain/course/repository';
22+
import { fetchList as fetchRoadmapList } from '#/domain/roadmap/repository';
23+
24+
import type { ListValue, ResponseResult } from '@/types';
1925

2026
import LearnListAdapter from './LearnListAdapter';
2127

@@ -32,23 +38,41 @@ export async function Container({ type, searchParams }: { type: string; searchPa
3238
const body_type = searchParams?.body_type || '';
3339
const lang = searchParams?.lang || '';
3440

35-
let URL;
41+
const params = {
42+
skip: (page - 1) * PAGE_SIZE,
43+
take: PAGE_SIZE,
44+
sort: order,
45+
labels,
46+
search: query,
47+
};
48+
49+
let req: Promise<ResponseResult> | undefined;
3650

3751
if (type === 'courses' && lang) {
38-
URL = `v1/learn/course/opencourse?&skip=${(page - 1) * PAGE_SIZE}&take=${PAGE_SIZE}&labels=${labels}&order=${order}&search=${query}&recommend_type=${featured}&body_type=${body_type}&lang=${lang}`;
52+
req = fetchCourseList({
53+
...params,
54+
recommend_type: featured,
55+
body_type,
56+
lang,
57+
});
3958
} else if (type === 'challenges') {
40-
URL = `v1/learn/course/challenges?&skip=${(page - 1) * PAGE_SIZE}&take=${PAGE_SIZE}&labels=${labels}&order=${order}&search=${query}&status=${status}&feeds=${feeds}&c_type=${c_type}`;
59+
req = fetchChallengeList({
60+
...params,
61+
status,
62+
feeds,
63+
c_type,
64+
});
4165
} else if (type === 'career_path') {
42-
URL = `/ts/v1/learn/general/course/grow_path?order=${order}`;
66+
req = fetchRoadmapList(pick(params, ['sort']));
4367
}
4468

45-
let data;
69+
let data: { list: ListValue; count: number };
4670

47-
if (URL) {
48-
const res = await get(URL, { isServer: true });
71+
if (req) {
72+
const res = await req;
4973
data = res.data;
5074
} else {
51-
data = await Promise.resolve({ count: 0 });
75+
data = await Promise.resolve({ list: [], count: 0 });
5276
}
5377

5478
return (

src/app/learn/[type]/[id]/actions.js

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@
1818

1919
import { revalidatePath } from 'next/cache';
2020

21-
import { post } from '@/utils/request';
22-
2321
import { enrollOne as enrollCourse } from '#/domain/course/repository';
22+
import { enrollOne as enrollRoadmap } from '#/domain/roadmap/repository';
2423

2524
export async function enrollAction(id) {
2625
try {
@@ -37,13 +36,8 @@ export async function revalidatePathAction() {
3736

3837
export async function growPathEnrollAction(id) {
3938
try {
40-
const res = await post(`ts/v1/learn/general/course/grow_path/${id}/permission/enrool`, {}, { isServer: true });
41-
console.log(res, 'res');
42-
if (res.code === 200) {
43-
return revalidatePath('/');
44-
} else {
45-
return res;
46-
}
39+
const res = await enrollRoadmap(id);
40+
return res.success ? revalidatePath('/') : res;
4741
} catch (e) {
4842
return { message: 'Failed to request' };
4943
}

src/app/learn/[type]/[id]/page.js

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,18 @@
1515
*/
1616

1717
import { PreviewAlert } from '@/components/PreviewAlert';
18-
import { get } from '@/utils/request';
1918

20-
import { fetchOneWithPermission as fetchChallengeWithPermission } from '#/domain/challenge/repository';
21-
import { fetchOneWithPermission as fetchCourseWithPermission } from '#/domain/course/repository';
19+
import { fetchOne as fetchChallenge, fetchOneWithPermission as fetchChallengeWithPermission, fetchRelatedCourse } from '#/domain/challenge/repository';
20+
import { fetchOne as fetchCourse, fetchOneWithPermission as fetchCourseWithPermission } from '#/domain/course/repository';
21+
import { fetchOneWithPermission as fetchRoadmapWithPermission } from '#/domain/roadmap/repository';
2222

2323
import { enrollAction, revalidatePathAction } from './actions';
2424
import CourseDetailPageAdapter from './CourseDetailPageAdapter';
2525
import GrowPath from './GrowPath';
2626

2727
export async function generateMetadata({ params }) {
28-
// fetch data
29-
const { data } = await get(`v1/learn/course/${params.type === 'courses' ? 'opencourse' : 'challenges'}/${params.id}`, {isServer: true});
28+
const fetchOne = params.type === 'courses'? fetchCourse : fetchChallenge;
29+
const { data } = await fetchOne(params.id);
3030
const previousImages = data?.base?.course_series_img ? `https://file-cdn.openbuild.xyz${data.base.course_series_img}` : '';
3131
return {
3232
title: data?.base?.course_series_title,
@@ -52,12 +52,9 @@ export default async function LearnDetailsPage({ params, searchParams }) {
5252
let data, permission;
5353

5454
if (learnType === 'career_path') {
55-
const datas = await Promise.all([
56-
get(`ts/v1/learn/general/course/grow_path/${learnId}`, {isServer: true}),
57-
get(`ts/v1/learn/general/course/grow_path/${learnId}/permission`, {isServer: true}),
58-
]);
59-
data = datas[0].data;
60-
permission = datas[1].data;
55+
const roadmapRes = await fetchRoadmapWithPermission(learnId);
56+
data = roadmapRes.data;
57+
permission = (roadmapRes.data || {}).permission;
6158
} else {
6259
const fetchOneWithPermission = learnType === 'courses' ? fetchCourseWithPermission : fetchChallengeWithPermission;
6360
const res = await fetchOneWithPermission(learnId);
@@ -68,7 +65,7 @@ export default async function LearnDetailsPage({ params, searchParams }) {
6865
let related = null;
6966

7067
if (learnType === 'challenges' && data?.challenges_extra?.course_challenges_extra_time_order === 0) {
71-
const res = await get(`ts/v1/learn/general/course/challenges/${learnId}/link`, { isServer: true });
68+
const res = await fetchRelatedCourse(learnId);
7269

7370
if (res.data.link.toString() !== learnId) {
7471
related = res.data;

src/domain/challenge/repository.js

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,30 @@
1414
* limitations under the License.
1515
*/
1616

17+
import { PAGE_SIZE } from '@/constants/config';
1718
import { merge } from '@/utils';
1819
import httpClient, { legacyClient, mergeMultipleResponses } from '@/utils/http';
1920

2021
import { fetchPermission, fetchLessonDetail } from '../course/repository';
2122

23+
async function fetchList(params = {}) {
24+
const { sort, ...others } = params;
25+
26+
return legacyClient.get('/learn/course/challenges', {
27+
params: merge({ take: PAGE_SIZE }, others, {
28+
order: sort || 'default',
29+
}),
30+
});
31+
}
32+
2233
async function fetchOne(id) {
2334
return legacyClient.get(`/learn/course/challenges/${id}`);
2435
}
2536

37+
async function enrollOne(id, data) {
38+
return httpClient.post(`/learn/general/course/challenges/${id}/permission/enrool`, data);
39+
}
40+
2641
async function fetchOneWithPermission(id) {
2742
return mergeMultipleResponses([fetchOne(id), fetchPermission(id)], ([{ data, ...others }, permission]) => ({
2843
...others,
@@ -37,29 +52,30 @@ async function fetchLessonWithEntity({ id, entityId }) {
3752
}));
3853
}
3954

40-
async function fetchPublishedChallengeList(params = {}) {
41-
const { userId, sort, ...others } = params;
55+
async function fetchRelatedCourse(id) {
56+
return httpClient.get(`/learn/general/course/challenges/${id}/link`);
57+
}
4258

43-
return legacyClient.get('/learn/course/challenges', {
44-
params: merge({ take: 20 }, others, {
45-
team_uid: userId,
46-
order: sort || 'default',
47-
}),
48-
});
59+
async function fetchPublishedChallengeList(params = {}) {
60+
return fetchList({ ...params, team_uid: params.userId });
4961
}
5062

5163
async function fetchEnrolledChallengeList(params = {}) {
5264
const { userId, sort, ...others } = params;
5365

5466
return legacyClient.get('/learn/dashboard/public/enrool/series', {
55-
params: merge({ take: 20 }, others, {
67+
params: merge({ take: PAGE_SIZE }, others, {
5668
id: userId,
5769
series_type: 'challenges',
5870
order: sort || 'default',
5971
}),
6072
});
6173
}
6274

75+
async function updateTransaction(id, data) {
76+
return httpClient.post(`/learn/general/course/challenges/${id}/permission/pay`, data);
77+
}
78+
6379
async function updateMultipleApplicantStatus(id, { userIds, status }) {
6480
return httpClient.post(`/learn/creator/series/${id}/batch/enrool/status`, { uids: userIds, status });
6581
}
@@ -79,8 +95,9 @@ async function updateEmailTemplate(id, { title, body }) {
7995
}
8096

8197
export {
82-
fetchOne, fetchOneWithPermission, fetchLessonWithEntity,
98+
fetchList, fetchOne, enrollOne,
99+
fetchOneWithPermission, fetchLessonWithEntity, fetchRelatedCourse,
83100
fetchPublishedChallengeList, fetchEnrolledChallengeList,
84-
updateMultipleApplicantStatus,
101+
updateTransaction, updateMultipleApplicantStatus,
85102
fetchEmailTemplate, updateEmailTemplate,
86103
};

src/domain/course/repository.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ async function fetchLessonWithEntity({ id, entityId }) {
5858
}));
5959
}
6060

61+
async function updateWatchProgress(lessonId) {
62+
return httpClient.post(`/learn/general/course/single/${lessonId}/time`);
63+
}
64+
6165
async function fetchPublishedCourseList(params = {}) {
6266
return fetchList({ ...params, team_uid: params.userId });
6367
}
@@ -77,6 +81,6 @@ async function fetchEnrolledCourseList(params = {}) {
7781
export {
7882
fetchList, fetchOne, enrollOne,
7983
fetchPermission, fetchOneWithPermission,
80-
fetchLessonDetail, fetchLessonWithEntity,
84+
fetchLessonDetail, fetchLessonWithEntity, updateWatchProgress,
8185
fetchPublishedCourseList, fetchEnrolledCourseList,
8286
};

src/domain/roadmap/repository.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* Copyright 2024 OpenBuild
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import httpClient, { mergeMultipleResponses } from '@/utils/http';
18+
19+
import type { EntityId } from '@/types';
20+
21+
async function fetchList(params: { sort?: string; } = {}) {
22+
const { sort: order, ...others } = params;
23+
24+
return httpClient.get('/learn/general/course/grow_path', { params: { ...others, order } });
25+
}
26+
27+
async function fetchOne(id: EntityId) {
28+
return httpClient.get(`/learn/general/course/grow_path/${id}`);
29+
}
30+
31+
async function enrollOne(id: EntityId) {
32+
return httpClient.post(`/learn/general/course/grow_path/${id}/permission/enrool`);
33+
}
34+
35+
async function fetchPermission(id: EntityId) {
36+
return httpClient.get(`/learn/general/course/grow_path/${id}/permission`);
37+
}
38+
39+
async function fetchOneWithPermission(id: EntityId) {
40+
return mergeMultipleResponses([fetchOne(id), fetchPermission(id)], ([{ data, ...others }, permission]) => ({
41+
...others,
42+
data: { ...data, permission: permission.data },
43+
}));
44+
}
45+
46+
export { fetchList, fetchOne, enrollOne, fetchOneWithPermission };

src/entry/components/list-wrapper/typing.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@
1414
* limitations under the License.
1515
*/
1616

17-
import type { DataValue } from '@/types';
17+
import type { ListValue } from '@/types';
1818
import type { ClassValue } from 'clsx';
1919
import type { JSXElementConstructor, PropsWithChildren, ReactNode } from 'react';
2020

2121
type ListProps = {
2222
className?: ClassValue;
23-
data: Record<string, DataValue>[];
23+
data: ListValue;
2424
total: number;
2525
};
2626

0 commit comments

Comments
 (0)