forked from openedx/frontend-app-authoring
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImportStepperPage.tsx
More file actions
202 lines (189 loc) · 7.55 KB
/
ImportStepperPage.tsx
File metadata and controls
202 lines (189 loc) · 7.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import { useContext, useState } from 'react';
import { Helmet } from 'react-helmet';
import { useNavigate } from 'react-router-dom';
import { FormattedMessage, useIntl } from '@edx/frontend-platform/i18n';
import {
ActionRow, Button, Chip, Container, Layout, OverlayTrigger, Stepper,
Tooltip,
} from '@openedx/paragon';
import { CoursesList, MigrationStatusProps } from '@src/studio-home/tabs-section/courses-tab';
import { useLibraryContext } from '@src/library-authoring/common/context/LibraryContext';
import Loading from '@src/generic/Loading';
import Header from '@src/header';
import SubHeader from '@src/generic/sub-header/SubHeader';
import { useBulkModulestoreMigrate, usePreviewMigration } from '@src/data/apiHooks';
import { ToastContext } from '@src/generic/toast-context';
import LoadingButton from '@src/generic/loading-button';
import { useCourseDetails } from '@src/course-outline/data/apiHooks';
import {
CourseImportFilterProvider,
useCourseImportFilter,
} from '@src/studio-home/tabs-section/courses-tab/courses-filters/courses-imported-filter-modal/context';
import { ReviewImportDetails } from './ReviewImportDetails';
import messages from '../messages';
import { HelpSidebar } from '../HelpSidebar';
type MigrationStep = 'select-course' | 'review-details';
export const MigrationStatus = ({
courseId,
}: MigrationStatusProps) => {
const { libraryId } = useLibraryContext();
const { processedMigrationInfo } = useCourseImportFilter() || {};
const isPreviouslyMigrated = processedMigrationInfo?.[courseId]?.includes(libraryId);
if (!isPreviouslyMigrated) {
return null;
}
return (
<div
key={`${courseId}-${processedMigrationInfo?.[courseId].join('-')}`}
className="previously-migrated-chip"
>
<Chip>
<FormattedMessage {...messages.previouslyImported} />
</Chip>
</div>
);
};
export const ImportStepperPage = () => {
const intl = useIntl();
const navigate = useNavigate();
const [currentStep, setCurrentStep] = useState<MigrationStep>('select-course');
const [selectedCourseId, setSelectedCourseId] = useState<string>('');
const { data: courseData } = useCourseDetails(selectedCourseId);
const { libraryId, libraryData, readOnly } = useLibraryContext();
const { showToast } = useContext(ToastContext);
// Using bulk migrate as it allows us to create collection automatically
// TODO: Modify single migration API to allow create collection
const migrate = useBulkModulestoreMigrate();
const {
data: previewMigrationData,
isPending: isPreviewMigrationPending,
} = usePreviewMigration(libraryId, selectedCourseId);
const analysisCompleted = !isPreviewMigrationPending;
const importIsBlocked = previewMigrationData?.state === 'block_limit_reached';
const handleImportCourse = async () => {
// istanbul ignore if: this can never happen, just for satisfying type checker.
if (!selectedCourseId) {
return;
}
try {
const migrationTask = await migrate.mutateAsync({
sources: [selectedCourseId],
target: libraryId,
createCollections: true,
repeatHandlingStrategy: 'fork',
compositionLevel: 'section',
});
navigate(`../import/${selectedCourseId}/${migrationTask.uuid}`);
} catch {
showToast(intl.formatMessage(messages.importCourseCompleteFailedToastMessage, {
courseName: courseData?.title,
}));
}
};
if (!libraryData) {
return <Loading />;
}
return (
<div className="import-course-stepper d-flex">
<div className="flex-grow-1">
<Helmet>
<title>{libraryData.title} | {process.env.SITE_NAME}</title>
</Helmet>
<Header
number={libraryData.slug}
title={libraryData.title}
org={libraryData.org}
contextId={libraryId}
isLibrary
readOnly={readOnly}
containerProps={{
size: undefined,
}}
/>
<Container className="mt-4">
<div className="px-4 bg-light-200 border-bottom">
<SubHeader
title={intl.formatMessage(messages.importCourseStepperTitle)}
hideBorder
/>
</div>
<Layout xs={[{ span: 9 }, { span: 3 }]}>
<Layout.Element>
<div className="import-container px-4">
<Stepper activeKey={currentStep}>
<Stepper.Header />
<Stepper.Step
eventKey="select-course"
title={intl.formatMessage(messages.importCourseSelectCourseStep)}
>
<CourseImportFilterProvider
selectedCourseId={selectedCourseId}
handleSelect={setSelectedCourseId}
>
<CoursesList
selectedCourseId={selectedCourseId}
handleSelect={setSelectedCourseId}
cardMigrationStatusWidget={MigrationStatus}
/>
</CourseImportFilterProvider>
</Stepper.Step>
<Stepper.Step
eventKey="review-details"
title={intl.formatMessage(messages.importCourseReviewDetailsStep)}
>
<ReviewImportDetails courseId={selectedCourseId} />
</Stepper.Step>
</Stepper>
</div>
<div className="content-buttons mt-5 px-5 py-2 bg-white box-shadow-up-1">
{currentStep === 'select-course' ? (
<ActionRow className="d-flex justify-content-between">
<Button variant="outline-primary" onClick={() => navigate('../import')}>
<FormattedMessage {...messages.importCourseCalcel} />
</Button>
<Button
onClick={() => setCurrentStep('review-details')}
disabled={!selectedCourseId}
>
<FormattedMessage {...messages.importCourseNext} />
</Button>
</ActionRow>
) : (
<ActionRow className="d-flex justify-content-between">
<Button variant="outline-primary" onClick={() => setCurrentStep('select-course')}>
<FormattedMessage {...messages.importCourseBack} />
</Button>
{importIsBlocked ? (
<OverlayTrigger
placement="top"
overlay={(
<Tooltip id="tooltip-import-course-button">
<FormattedMessage {...messages.importNotPossibleTooltip} />
</Tooltip>
)}
>
<Button variant="primary" disabled>
<FormattedMessage {...messages.importCourseButton} />
</Button>
</OverlayTrigger>
) : (
<LoadingButton
onClick={handleImportCourse}
label={intl.formatMessage(messages.importCourseButton)}
variant="primary"
disabled={!analysisCompleted}
/>
)}
</ActionRow>
)}
</div>
</Layout.Element>
<Layout.Element>
<HelpSidebar />
</Layout.Element>
</Layout>
</Container>
</div>
</div>
);
};