Skip to content

Commit 882acc7

Browse files
fix: add backend call and support static and dynamic tabs
1 parent a5cbad5 commit 882acc7

File tree

4 files changed

+21
-47
lines changed

4 files changed

+21
-47
lines changed

src/data/apiHook.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import { useQuery } from '@tanstack/react-query';
22
import { getCourseInfo } from './api';
33

4-
const COURSE_INFO_QUERY_KEY = ['courseInfo'];
5-
64
export const useCourseInfo = (courseId: string) => (
75
useQuery({
8-
queryKey: COURSE_INFO_QUERY_KEY,
6+
queryKey: ['courseInfo', courseId],
97
queryFn: () => getCourseInfo(courseId),
8+
enabled: !!courseId,
109
})
1110
);

src/instructorTabs/InstructorTabs.tsx

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { useContext } from 'react';
22
import { useNavigate, useParams } from 'react-router-dom';
33
import { Tab, Tabs } from '@openedx/paragon';
44
import { SlotContext, useWidgetsForId } from '@openedx/frontend-base';
5+
import { useCourseInfo } from '../data/apiHook';
56

67
export interface TabProps {
78
tab_id: string,
@@ -28,23 +29,37 @@ const InstructorTabs = () => {
2829
const navigate = useNavigate();
2930
const { courseId, tabId } = useParams<{ courseId: string, tabId?: string }>();
3031
const { id: slotId } = useContext(SlotContext);
32+
const { data: courseInfo, isLoading } = useCourseInfo(courseId ?? '');
3133
const widgetPropsArray = useWidgetProps(slotId);
3234

35+
const apiTabs: TabProps[] = courseInfo?.tabs ?? [];
36+
const allTabs = [...apiTabs];
37+
38+
widgetPropsArray.forEach(slotTab => {
39+
if (!apiTabs.find(apiTab => apiTab.tab_id === slotTab.tab_id)) {
40+
allTabs.push(slotTab);
41+
}
42+
});
43+
3344
const activeKey = tabId ?? 'course_info';
3445
const handleSelect = (eventKey: string | null) => {
3546
if (eventKey && courseId) {
36-
const selectedTab = widgetPropsArray.find(({ tab_id }) => tab_id === eventKey);
47+
const selectedTab = allTabs.find(({ tab_id }) => tab_id === eventKey);
3748
if (selectedTab) {
3849
navigate(`/${courseId}/${eventKey}`);
3950
}
4051
}
4152
};
4253

43-
if (widgetPropsArray.length === 0) return null;
54+
if (isLoading) {
55+
return <div>Loading tabs...</div>;
56+
}
57+
58+
if (allTabs.length === 0) return null;
4459

4560
return (
4661
<Tabs id="instructor-tabs" activeKey={activeKey} onSelect={handleSelect}>
47-
{widgetPropsArray.map(({ tab_id, title }) => (
62+
{allTabs.map(({ tab_id, title }) => (
4863
<Tab key={tab_id} eventKey={tab_id} title={title} />
4964
))}
5065
</Tabs>

src/instructorTabs/app.tsx

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

src/slots.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
import { SlotOperation } from '@openedx/frontend-base';
2-
import { tabSlots } from './instructorTabs/app';
32

4-
const slots: SlotOperation[] = [
5-
...tabSlots ?? [],
6-
];
3+
const slots: SlotOperation[] = [];
74

85
export default slots;

0 commit comments

Comments
 (0)