forked from openedx/frontend-app-authoring
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathItemHierarchyPublisher.tsx
More file actions
112 lines (102 loc) · 3.09 KB
/
ItemHierarchyPublisher.tsx
File metadata and controls
112 lines (102 loc) · 3.09 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
import { type ReactNode } from 'react';
import type { MessageDescriptor } from 'react-intl';
import { useIntl } from '@edx/frontend-platform/i18n';
import {
ActionRow,
Button,
Container,
} from '@openedx/paragon';
import LoadingButton from '@src/generic/loading-button';
import { ContainerType, getBlockType } from '@src/generic/key-utils';
import Loading from '@src/generic/Loading';
import messages from './messages';
import { ItemHierarchy } from './ItemHierarchy';
import { useLibraryItemHierarchy } from '../data/apiHooks';
type ItemHierarchyPublisherProps = {
itemId: string;
handleClose: () => void;
handlePublish: () => void;
};
export const ItemHierarchyPublisher = ({
itemId,
handleClose,
handlePublish,
}: ItemHierarchyPublisherProps) => {
const intl = useIntl();
const itemType = getBlockType(itemId);
const {
data: hierarchy,
isPending,
isError,
} = useLibraryItemHierarchy(itemId);
if (isPending) {
return <Loading />;
}
// istanbul ignore if: this should never happen
if (isError) {
return null;
}
const highlight = (...chunks: ReactNode[]) => <strong>{chunks}</strong>;
const childWarningMessage = () => {
let childCount: number;
let childMessage: MessageDescriptor;
let noChildMessage: MessageDescriptor;
switch (itemType) {
case ContainerType.Section:
childCount = hierarchy.subsections.length;
childMessage = messages.publishSectionWithChildrenWarning;
noChildMessage = messages.publishSectionWarning;
break;
case ContainerType.Subsection:
childCount = hierarchy.units.length;
childMessage = messages.publishSubsectionWithChildrenWarning;
noChildMessage = messages.publishSubsectionWarning;
break;
case ContainerType.Unit:
childCount = hierarchy.components.length;
childMessage = messages.publishUnitWithChildrenWarning;
noChildMessage = messages.publishUnitWarning;
break;
default: // The item is a component
childCount = 0;
childMessage = messages.empty; // Never used
noChildMessage = messages.publishComponentWarning;
break;
}
return intl.formatMessage(
childCount ? childMessage : noChildMessage,
{
childCount,
highlight,
},
);
};
return (
<Container className="p-3 status-box draft-status">
<h4>{intl.formatMessage(messages.publishConfirmHeading)}</h4>
<p>{childWarningMessage()}</p>
<ItemHierarchy showPublishStatus />
<ActionRow>
<Button
variant="outline-primary rounded-0"
onClick={(e) => {
e.preventDefault();
e.stopPropagation();
handleClose();
}}
>
{intl.formatMessage(messages.publishCancel)}
</Button>
<LoadingButton
onClick={(e) => {
e.preventDefault();
e.stopPropagation();
handlePublish();
}}
variant="primary rounded-0"
label={intl.formatMessage(messages.publishConfirm)}
/>
</ActionRow>
</Container>
);
};