forked from openedx/frontend-app-authoring
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLegacyLibContentBlockAlert.tsx
More file actions
91 lines (83 loc) · 3.14 KB
/
LegacyLibContentBlockAlert.tsx
File metadata and controls
91 lines (83 loc) · 3.14 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
import { useIntl } from '@edx/frontend-platform/i18n';
import { Button, Hyperlink } from '@openedx/paragon';
import {
useContext, useEffect, useMemo, useState,
} from 'react';
import { UserTaskStatus } from '@src/data/constants';
import AlertMessage from '@src/generic/alert-message';
import LoadingButton from '@src/generic/loading-button';
import { ToastContext } from '@src/generic/toast-context';
import {
useCheckMigrateCourseLegacyLibReadyToMigrateBlocksOptions,
useCourseLegacyLibReadyToMigrateBlocks,
useMigrateCourseLegacyLibReadyToMigrateBlocks,
} from './data/apiHooks';
import messages from './messages';
interface Props {
courseId: string,
}
const LegacyLibContentBlockAlert = ({ courseId }: Props) => {
const intl = useIntl();
const { showToast } = useContext(ToastContext);
const [taskId, setTaskId] = useState<string | undefined>(undefined);
const { data, isPending, refetch } = useCourseLegacyLibReadyToMigrateBlocks(courseId);
const { mutateAsync } = useMigrateCourseLegacyLibReadyToMigrateBlocks(courseId);
const taskStatus = useCheckMigrateCourseLegacyLibReadyToMigrateBlocksOptions(courseId, taskId);
const learnMoreUrl = 'https://docs.openedx.org/en/latest/educators/how-tos/course_development/migrate_legacy_libraries.html#id8';
useEffect(() => {
if (taskStatus.data?.state === UserTaskStatus.Succeeded) {
showToast(intl.formatMessage(messages.legacyLibReadyToMigrateTaskCompleted));
setTaskId(undefined);
// eslint-disable-next-line @typescript-eslint/no-floating-promises
refetch();
} else if (taskStatus.data?.state === UserTaskStatus.Failed
|| taskStatus.data?.state === UserTaskStatus.Cancelled) {
showToast(intl.formatMessage(messages.legacyLibReadyToMigrateTaskFailed));
setTaskId(undefined);
// eslint-disable-next-line @typescript-eslint/no-floating-promises
refetch();
} else if (taskId) {
showToast(intl.formatMessage(messages.legacyLibReadyToMigrateTaskInProgress));
}
}, [taskStatus, taskId, refetch]);
const migrateFn = async () => {
await mutateAsync(undefined, {
onSuccess: (result) => {
setTaskId(result.uuid);
},
onError: () => {
setTaskId(undefined);
},
});
};
const alertCount = useMemo(() => data?.length || 0, [data]);
if (isPending || taskId) {
return null;
}
return (
<AlertMessage
title={intl.formatMessage(messages.legacyLibReadyToMigrateAlertTitle, { count: alertCount })}
description={intl.formatMessage(messages.legacyLibReadyToMigrateAlertDescription)}
show={alertCount > 0}
variant="info"
actions={[
<Button
target="_blank"
as={Hyperlink}
variant="tertiary"
key="learn-more"
showLaunchIcon={false}
destination={learnMoreUrl}
>
{intl.formatMessage(messages.legacyLibReadyToMigrateAlertLearnMoreBtn)}
</Button>,
<LoadingButton
onClick={migrateFn}
key="migrate-button"
label={intl.formatMessage(messages.legacyLibReadyToMigrateAlertActionBtn)}
/>,
]}
/>
);
};
export default LegacyLibContentBlockAlert;