|
| 1 | +import { useContext } from 'react'; |
| 2 | +import { Helmet } from 'react-helmet'; |
| 3 | +import { useNavigate, useParams } from 'react-router'; |
| 4 | +import { FormattedMessage, useIntl } from '@edx/frontend-platform/i18n'; |
| 5 | +import { |
| 6 | + Stack, Container, Alert, Layout, Button, |
| 7 | +} from '@openedx/paragon'; |
| 8 | + |
| 9 | +import Header from '@src/header'; |
| 10 | +import { useCourseDetails } from '@src/course-outline/data/apiHooks'; |
| 11 | +import SubHeader from '@src/generic/sub-header/SubHeader'; |
| 12 | +import { ArrowForward, CheckCircle, Info } from '@openedx/paragon/icons'; |
| 13 | +import Loading from '@src/generic/Loading'; |
| 14 | +import { ToastContext } from '@src/generic/toast-context'; |
| 15 | + |
| 16 | +import { useBulkModulestoreMigrate, useModulestoreMigrationStatus } from '@src/data/apiHooks'; |
| 17 | +import messages from './messages'; |
| 18 | +import { SummaryCard } from './stepper/SummaryCard'; |
| 19 | +import { HelpSidebar } from './HelpSidebar'; |
| 20 | +import { useLibraryContext } from '../common/context/LibraryContext'; |
| 21 | + |
| 22 | +export const ImportDetailsPage = () => { |
| 23 | + const intl = useIntl(); |
| 24 | + const navigate = useNavigate(); |
| 25 | + const { libraryId, libraryData, readOnly } = useLibraryContext(); |
| 26 | + const { courseId, migrationTaskId } = useParams(); |
| 27 | + const { showToast } = useContext(ToastContext); |
| 28 | + // Using bulk migrate as it allows us to create collection automatically |
| 29 | + // TODO: Modify single migration API to allow create collection |
| 30 | + const migrate = useBulkModulestoreMigrate(); |
| 31 | + |
| 32 | + if (libraryId === undefined) { |
| 33 | + // istanbul ignore next - This shouldn't be possible; it's just here to satisfy the type checker. |
| 34 | + throw new Error('Error: route is missing libraryId.'); |
| 35 | + } |
| 36 | + if (migrationTaskId === undefined) { |
| 37 | + // istanbul ignore next - This shouldn't be possible; it's just here to satisfy the type checker. |
| 38 | + throw new Error('Error: route is missing migrationId.'); |
| 39 | + } |
| 40 | + |
| 41 | + const { |
| 42 | + data: courseDetails, |
| 43 | + isPending: isPendingCourseDetails, |
| 44 | + } = useCourseDetails(courseId); |
| 45 | + const { |
| 46 | + data: migrationStatusData, |
| 47 | + isPaused: isPendingMigrationStatusData, |
| 48 | + } = useModulestoreMigrationStatus(migrationTaskId); |
| 49 | + // Get the first migration, because the courses are imported one by one |
| 50 | + const courseImportDetails = migrationStatusData?.parameters?.[0]; |
| 51 | + |
| 52 | + const isPending = isPendingCourseDetails || isPendingMigrationStatusData; |
| 53 | + |
| 54 | + const collectionLink = () => { |
| 55 | + let libUrl = `/library/${libraryId}`; |
| 56 | + if (courseImportDetails?.targetCollection?.key) { |
| 57 | + libUrl += `/collection/${courseImportDetails.targetCollection.key}`; |
| 58 | + } |
| 59 | + return libUrl; |
| 60 | + }; |
| 61 | + |
| 62 | + const handleImportCourse = async () => { |
| 63 | + if (!courseId || !courseImportDetails || !courseDetails || !migrationStatusData) { |
| 64 | + return; |
| 65 | + } |
| 66 | + |
| 67 | + try { |
| 68 | + await migrate.mutateAsync({ |
| 69 | + sources: [courseId!], |
| 70 | + target: libraryId, |
| 71 | + createCollections: true, |
| 72 | + repeatHandlingStrategy: 'fork', |
| 73 | + compositionLevel: 'section', |
| 74 | + }); |
| 75 | + showToast(intl.formatMessage(messages.importCourseCompleteToastMessage, { |
| 76 | + courseName: courseDetails.title, |
| 77 | + })); |
| 78 | + navigate(`${courseImportDetails.source}/${migrationStatusData.uuid}`); |
| 79 | + } catch (error) { |
| 80 | + showToast(intl.formatMessage(messages.importCourseCompleteFailedToastMessage, { |
| 81 | + courseName: courseDetails.title, |
| 82 | + })); |
| 83 | + } |
| 84 | + }; |
| 85 | + |
| 86 | + const renderBody = () => { |
| 87 | + if (isPending) { |
| 88 | + return <Loading />; |
| 89 | + } |
| 90 | + |
| 91 | + if (migrationStatusData?.state === 'Succeeded') { |
| 92 | + return ( |
| 93 | + <Stack gap={3}> |
| 94 | + <Alert variant="success" icon={CheckCircle}> |
| 95 | + <Alert.Heading> |
| 96 | + <FormattedMessage {...messages.importSuccessfulAlertTitle} /> |
| 97 | + </Alert.Heading> |
| 98 | + <p> |
| 99 | + <FormattedMessage |
| 100 | + {...messages.importSuccessfulAlertBody} |
| 101 | + values={{ |
| 102 | + courseName: courseDetails?.title, |
| 103 | + collectionName: courseImportDetails?.targetCollection?.title, |
| 104 | + }} |
| 105 | + /> |
| 106 | + </p> |
| 107 | + </Alert> |
| 108 | + <h4><FormattedMessage {...messages.importSummaryTitle} /></h4> |
| 109 | + <SummaryCard isPending /> |
| 110 | + <p> |
| 111 | + <FormattedMessage |
| 112 | + {...messages.importSuccessfulBody} |
| 113 | + values={{ |
| 114 | + courseName: courseDetails?.title, |
| 115 | + }} |
| 116 | + /> |
| 117 | + </p> |
| 118 | + <div className="w-100 d-flex justify-content-end"> |
| 119 | + <Button |
| 120 | + variant="outline-primary" |
| 121 | + iconAfter={ArrowForward} |
| 122 | + onClick={() => navigate(collectionLink())} |
| 123 | + > |
| 124 | + <FormattedMessage {...messages.viewImportedContentButton} /> |
| 125 | + </Button> |
| 126 | + </div> |
| 127 | + </Stack> |
| 128 | + ); |
| 129 | + } if (migrationStatusData?.state === 'Failed') { |
| 130 | + return ( |
| 131 | + <Stack gap={3}> |
| 132 | + <Alert variant="danger" icon={Info}> |
| 133 | + <Alert.Heading> |
| 134 | + <FormattedMessage {...messages.importFailedAlertTitle} /> |
| 135 | + </Alert.Heading> |
| 136 | + <p> |
| 137 | + <FormattedMessage |
| 138 | + {...messages.importFailedAlertBody} |
| 139 | + values={{ |
| 140 | + courseName: courseDetails?.title, |
| 141 | + }} |
| 142 | + /> |
| 143 | + </p> |
| 144 | + </Alert> |
| 145 | + <h4><FormattedMessage {...messages.importFailedDetailsSectionTitle} /></h4> |
| 146 | + <p> |
| 147 | + <FormattedMessage {...messages.importFailedDetailsSectionBody} /> |
| 148 | + </p> |
| 149 | + <div className="w-100 d-flex justify-content-end"> |
| 150 | + <Button |
| 151 | + variant="outline-primary" |
| 152 | + iconAfter={ArrowForward} |
| 153 | + onClick={handleImportCourse} |
| 154 | + > |
| 155 | + <FormattedMessage {...messages.importFailedRetryImportButton} /> |
| 156 | + </Button> |
| 157 | + </div> |
| 158 | + </Stack> |
| 159 | + ); |
| 160 | + } |
| 161 | + return ( |
| 162 | + // In Progress |
| 163 | + <Stack gap={2}> |
| 164 | + In progress |
| 165 | + </Stack> |
| 166 | + ); |
| 167 | + }; |
| 168 | + |
| 169 | + return ( |
| 170 | + <div className="d-flex"> |
| 171 | + <div className="flex-grow-1"> |
| 172 | + <Helmet> |
| 173 | + <title>{courseDetails?.title ?? ''} | {process.env.SITE_NAME}</title> |
| 174 | + </Helmet> |
| 175 | + <Header |
| 176 | + number={libraryData?.slug} |
| 177 | + title={libraryData?.title} |
| 178 | + org={libraryData?.org} |
| 179 | + contextId={libraryId} |
| 180 | + isLibrary |
| 181 | + readOnly={readOnly} |
| 182 | + containerProps={{ |
| 183 | + size: undefined, |
| 184 | + }} |
| 185 | + /> |
| 186 | + <Container className="mt-4 mb-5"> |
| 187 | + <div className="px-4 bg-light-200 border-bottom"> |
| 188 | + <SubHeader |
| 189 | + title={intl.formatMessage(messages.importDetailsTitle)} |
| 190 | + hideBorder |
| 191 | + /> |
| 192 | + </div> |
| 193 | + <Layout xs={[{ span: 9 }, { span: 3 }]}> |
| 194 | + <Layout.Element> |
| 195 | + <div className="mt-4 px-4"> |
| 196 | + {renderBody()} |
| 197 | + </div> |
| 198 | + </Layout.Element> |
| 199 | + <Layout.Element> |
| 200 | + <HelpSidebar /> |
| 201 | + </Layout.Element> |
| 202 | + </Layout> |
| 203 | + </Container> |
| 204 | + </div> |
| 205 | + </div> |
| 206 | + ); |
| 207 | +}; |
0 commit comments