-
Notifications
You must be signed in to change notification settings - Fork 4
/
SitewideAnnouncements.tsx
126 lines (109 loc) · 3.81 KB
/
SitewideAnnouncements.tsx
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
import * as React from "react";
import { connect } from "react-redux";
import { Alert } from "react-bootstrap";
import { Form } from "library-simplified-reusable-components";
import LoadingIndicator from "@thepalaceproject/web-opds-client/lib/components/LoadingIndicator";
import ActionCreator from "../actions";
import { SitewideAnnouncementsData, AnnouncementData } from "../interfaces";
import EditableConfigList, {
EditableConfigListStateProps,
EditableConfigListDispatchProps,
EditableConfigListOwnProps,
EditableConfigListProps,
} from "./EditableConfigList";
import ErrorMessage from "./ErrorMessage";
import AnnouncementsSection from "./AnnouncementsSection";
/** Right panel for sitewide announcements on the system configuration page. */
export class SitewideAnnouncements extends EditableConfigList<
SitewideAnnouncementsData,
AnnouncementData
> {
// There is no individual item edit form for an announcement.
EditForm = null;
listDataKey = "announcements";
itemTypeName = "sitewide announcement";
urlBase = "/admin/web/config/sitewideAnnouncements/";
identifierKey = "id";
labelKey = "content";
private announcementsRef = React.createRef<AnnouncementsSection>();
constructor(props: EditableConfigListProps<SitewideAnnouncementsData>) {
super(props);
this.submit = this.submit.bind(this);
}
render() {
const {
data,
editOrCreate,
fetchError,
formError,
isFetching,
responseBody,
} = this.props;
const headers = this.getHeaders();
const canListAllData =
!isFetching && !editOrCreate && data?.[this.listDataKey];
const canEdit = this.canEdit(data?.settings?.[0] || {});
return (
<div className={this.getClassName()}>
<h2>{headers["h2"]}</h2>
{canListAllData && this.links?.["info"] && (
<Alert bsStyle="info">{this.links["info"]}</Alert>
)}
{responseBody && (
<Alert bsStyle="success">{this.successMessage()}</Alert>
)}
{fetchError && <ErrorMessage error={fetchError} />}
{formError && <ErrorMessage error={formError} />}
{isFetching && <LoadingIndicator />}
{canListAllData && (
<Form
onSubmit={this.submit}
className="no-border edit-form"
disableButton={!canEdit || isFetching}
content={[
<AnnouncementsSection
key="announcements-section"
setting={data.settings[0]}
value={data[this.listDataKey]}
ref={this.announcementsRef}
/>,
]}
/>
)}
</div>
);
}
async submit(data: FormData) {
const announcements = this.announcementsRef.current.getValue();
data?.set("announcements", JSON.stringify(announcements));
await this.editItem(data);
}
}
function mapStateToProps(state) {
return {
data: state.editor.sitewideAnnouncements.data,
responseBody: state.editor.sitewideAnnouncements.successMessage,
fetchError: state.editor.sitewideAnnouncements.fetchError,
formError: state.editor.sitewideAnnouncements.formError,
isFetching:
state.editor.sitewideAnnouncements.isFetching ||
state.editor.sitewideAnnouncements.isEditing,
};
}
function mapDispatchToProps(dispatch, ownProps) {
const actions = new ActionCreator(null, ownProps.csrfToken);
return {
fetchData: () => dispatch(actions.fetchSitewideAnnouncements()),
editItem: (data: FormData) =>
dispatch(actions.editSitewideAnnouncements(data)),
};
}
const ConnectedSitewideAnnouncements = connect<
EditableConfigListStateProps<SitewideAnnouncementsData>,
EditableConfigListDispatchProps<SitewideAnnouncementsData>,
EditableConfigListOwnProps
>(
mapStateToProps,
mapDispatchToProps
)(SitewideAnnouncements);
export default ConnectedSitewideAnnouncements;