|
| 1 | +import * as React from "react"; |
| 2 | +import { connect } from "react-redux"; |
| 3 | +import { Alert } from "react-bootstrap"; |
| 4 | +import { Form } from "library-simplified-reusable-components"; |
| 5 | +import LoadingIndicator from "@thepalaceproject/web-opds-client/lib/components/LoadingIndicator"; |
| 6 | +import ActionCreator from "../actions"; |
| 7 | +import { GlobalSettingsData } from "../interfaces"; |
| 8 | +import EditableConfigList, { |
| 9 | + EditableConfigListStateProps, |
| 10 | + EditableConfigListDispatchProps, |
| 11 | + EditableConfigListOwnProps, |
| 12 | + EditableConfigListProps, |
| 13 | +} from "./EditableConfigList"; |
| 14 | +import ErrorMessage from "./ErrorMessage"; |
| 15 | +import ProtocolFormField from "./ProtocolFormField"; |
| 16 | + |
| 17 | +/** Settings panel for sitewide global configuration (country, state, etc.). */ |
| 18 | +export class GlobalSettings extends EditableConfigList< |
| 19 | + GlobalSettingsData, |
| 20 | + never |
| 21 | +> { |
| 22 | + EditForm = null; |
| 23 | + listDataKey = "schema"; |
| 24 | + itemTypeName = "global setting"; |
| 25 | + urlBase = "/admin/web/config/globalSettings/"; |
| 26 | + identifierKey = "key"; |
| 27 | + labelKey = "label"; |
| 28 | + |
| 29 | + render() { |
| 30 | + const { |
| 31 | + data, |
| 32 | + fetchError, |
| 33 | + formError, |
| 34 | + isFetching, |
| 35 | + responseBody, |
| 36 | + } = this.props; |
| 37 | + |
| 38 | + const canEdit = this.context.admin.isSystemAdmin(); |
| 39 | + |
| 40 | + return ( |
| 41 | + <div className="global-settings"> |
| 42 | + <h2>Global Settings</h2> |
| 43 | + {responseBody && ( |
| 44 | + <Alert bsStyle="success">Global settings saved successfully.</Alert> |
| 45 | + )} |
| 46 | + {fetchError && <ErrorMessage error={fetchError} />} |
| 47 | + {formError && <ErrorMessage error={formError} />} |
| 48 | + {isFetching && <LoadingIndicator />} |
| 49 | + {!isFetching && data?.schema && ( |
| 50 | + <Form |
| 51 | + onSubmit={this.editItem} |
| 52 | + className="no-border edit-form" |
| 53 | + disableButton={!canEdit} |
| 54 | + content={data.schema.map((setting) => ( |
| 55 | + <ProtocolFormField |
| 56 | + key={setting.key} |
| 57 | + setting={setting} |
| 58 | + disabled={!canEdit} |
| 59 | + value={data.settings?.[setting.key]} |
| 60 | + readOnly={!canEdit} |
| 61 | + /> |
| 62 | + ))} |
| 63 | + /> |
| 64 | + )} |
| 65 | + </div> |
| 66 | + ); |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +function mapStateToProps(state) { |
| 71 | + return { |
| 72 | + data: state.editor.globalSettings.data, |
| 73 | + responseBody: state.editor.globalSettings.successMessage, |
| 74 | + fetchError: state.editor.globalSettings.fetchError, |
| 75 | + formError: state.editor.globalSettings.formError, |
| 76 | + isFetching: |
| 77 | + state.editor.globalSettings.isFetching || |
| 78 | + state.editor.globalSettings.isEditing, |
| 79 | + }; |
| 80 | +} |
| 81 | + |
| 82 | +function mapDispatchToProps(dispatch, ownProps) { |
| 83 | + const actions = new ActionCreator(null, ownProps.csrfToken); |
| 84 | + return { |
| 85 | + fetchData: () => dispatch(actions.fetchGlobalSettings()), |
| 86 | + editItem: (data: FormData) => dispatch(actions.editGlobalSettings(data)), |
| 87 | + }; |
| 88 | +} |
| 89 | + |
| 90 | +const ConnectedGlobalSettings = connect< |
| 91 | + EditableConfigListStateProps<GlobalSettingsData>, |
| 92 | + EditableConfigListDispatchProps<GlobalSettingsData>, |
| 93 | + EditableConfigListOwnProps |
| 94 | +>( |
| 95 | + mapStateToProps, |
| 96 | + mapDispatchToProps |
| 97 | +)(GlobalSettings); |
| 98 | + |
| 99 | +export default ConnectedGlobalSettings; |
0 commit comments