|
| 1 | +import React, { useRef } from 'react' |
| 2 | +import PropTypes from 'prop-types' |
| 3 | +import { connect } from 'react-redux' |
| 4 | +import { ComponentManager, ExportableGrid, GenericForm, GridManager, axios } from '../../../client' |
| 5 | +import { alertUserResponse, alertUserV2, ReactBootstrap } from '../../../elements'; |
| 6 | +import { Loading } from '../../ComponentsIndex'; |
| 7 | +import PerunMenuWrapper from './PerunMenuWrapper'; |
| 8 | +const { useReducer, useEffect } = React; |
| 9 | +const { Modal } = ReactBootstrap; |
| 10 | + |
| 11 | +const PerunMenu = (props, context) => { |
| 12 | + const initialState = { loading: false, tableName: 'PERUN_MENU', gridId: 'PERUN_MENU_GRID', show: false, objectId: 0 } |
| 13 | + const reducer = (currState, update) => ({ ...currState, ...update }) |
| 14 | + const [{ loading, tableName, gridId, show, objectId }, setState] = useReducer(reducer, initialState) |
| 15 | + |
| 16 | + const uploadInputRef = useRef(null) |
| 17 | + |
| 18 | + useEffect(() => { |
| 19 | + return () => { |
| 20 | + ComponentManager.cleanComponentReducerState(gridId); |
| 21 | + } |
| 22 | + }, []); |
| 23 | + |
| 24 | + const reloadGrid = () => { |
| 25 | + GridManager.reloadGridData(gridId) |
| 26 | + ComponentManager.setStateForComponent(gridId, null, { rowClicked: undefined }) |
| 27 | + } |
| 28 | + |
| 29 | + const handleRowClick = (_id, _rowIdx, row) => { |
| 30 | + setState({ objectId: row[`${tableName}.OBJECT_ID`] || 0, show: true }) |
| 31 | + }; |
| 32 | + |
| 33 | + const generatePerunMenuGrid = () => { |
| 34 | + const { svSession } = props; |
| 35 | + const buttonsArray = [ |
| 36 | + { |
| 37 | + id: 'import_perun_menu', |
| 38 | + name: context.intl.formatMessage({ id: 'perun.admin_console.import_menu', defaultMessage: 'perun.admin_console.import_menu' }), |
| 39 | + action: () => uploadInputRef?.current?.click() |
| 40 | + }, |
| 41 | + { |
| 42 | + id: 'add_perun_menu', |
| 43 | + name: context.intl.formatMessage({ id: 'perun.admin_console.add', defaultMessage: 'perun.admin_console.add' }), |
| 44 | + action: () => setState({ show: true, objectId: 0 }) |
| 45 | + }, |
| 46 | + ] |
| 47 | + |
| 48 | + return ( |
| 49 | + <ExportableGrid |
| 50 | + gridType={'READ_URL'} |
| 51 | + key={gridId} |
| 52 | + id={gridId} |
| 53 | + configTableName={`/ReactElements/getTableFieldList/${svSession}/${tableName}`} |
| 54 | + dataTableName={`/ReactElements/getTableData/${props.svSession}/${tableName}/0`} |
| 55 | + onRowClickFunct={handleRowClick} |
| 56 | + refreshData={true} |
| 57 | + buttonsArray={buttonsArray} |
| 58 | + heightRatio={0.75} |
| 59 | + editContextFunc={handleRowClick} |
| 60 | + /> |
| 61 | + ) |
| 62 | + }; |
| 63 | + |
| 64 | + const saveRecord = () => { |
| 65 | + const { svSession } = props |
| 66 | + const url = `${window.server}/Menu/add/${svSession}` |
| 67 | + const onConfirm = () => ComponentManager.setStateForComponent(`${tableName}_FORM`, null, { saveExecuted: false }) |
| 68 | + const formData = ComponentManager.getStateForComponent(`${tableName}_FORM`, 'formTableData'); |
| 69 | + const data = Object.assign({}, { |
| 70 | + ...formData, |
| 71 | + OBJECT_ID: objectId, |
| 72 | + MENU_CONF: formData.MENU_CONF ? JSON.parse(formData.MENU_CONF) : undefined |
| 73 | + }) |
| 74 | + const reqConfig = { method: 'post', data, url, headers: { 'Content-Type': 'application/json' } } |
| 75 | + axios(reqConfig).then((res) => { |
| 76 | + if (res?.data) { |
| 77 | + const resType = res.data?.type?.toLowerCase() || 'info' |
| 78 | + alertUserResponse({ type: resType, response: res, onConfirm }) |
| 79 | + if (resType === 'success') { |
| 80 | + reloadGrid() |
| 81 | + setState({ show: false }) |
| 82 | + } |
| 83 | + } |
| 84 | + }).catch(err => { |
| 85 | + console.error(err) |
| 86 | + alertUserResponse({ response: err, onConfirm }) |
| 87 | + }); |
| 88 | + }; |
| 89 | + |
| 90 | + const generatePerunMenuForm = (objectId) => { |
| 91 | + const { svSession } = props; |
| 92 | + return ( |
| 93 | + <GenericForm |
| 94 | + params={'READ_URL'} |
| 95 | + key={`${tableName}_FORM`} |
| 96 | + id={`${tableName}_FORM`} |
| 97 | + method={`/ReactElements/getTableJSONSchema/${svSession}/${tableName}`} |
| 98 | + uiSchemaConfigMethod={`/ReactElements/getTableUISchema/${svSession}/${tableName}`} |
| 99 | + tableFormDataMethod={`/ReactElements/getTableFormData/${svSession}/${objectId}/${tableName}`} |
| 100 | + addSaveFunction={saveRecord} |
| 101 | + hideBtns={objectId === 0 ? 'closeAndDelete' : 'close'} |
| 102 | + addDeleteFunction={deleteFunc} |
| 103 | + className={'admin-settings-forms'} |
| 104 | + inputWrapper={PerunMenuWrapper} |
| 105 | + objectId={objectId} |
| 106 | + /> |
| 107 | + ) |
| 108 | + }; |
| 109 | + |
| 110 | + const deleteFunc = (_id, _action, _session, formData) => { |
| 111 | + const { svSession } = props |
| 112 | + const onConfirm = () => ComponentManager.setStateForComponent(`${tableName}_FORM`, null, { deleteExecuted: false }) |
| 113 | + const data = JSON.parse(formData[4]['PARAM_VALUE']) |
| 114 | + const menuCode = data.MENU_CODE |
| 115 | + const url = `${window.server}/Menu/remove/${svSession}/${menuCode}` |
| 116 | + axios.get(url).then(res => { |
| 117 | + if (res?.data) { |
| 118 | + const resType = res.data?.type?.toLowerCase() || 'info' |
| 119 | + alertUserResponse({ response: res, onConfirm }) |
| 120 | + if (resType === 'success') { |
| 121 | + reloadGrid() |
| 122 | + setState({ show: false }) |
| 123 | + } |
| 124 | + } |
| 125 | + }).catch(err => { |
| 126 | + console.error(err) |
| 127 | + alertUserResponse({ response: err, onConfirm }) |
| 128 | + }) |
| 129 | + } |
| 130 | + |
| 131 | + const uploadFile = (file) => { |
| 132 | + setState({ loading: true }) |
| 133 | + const { svSession } = props |
| 134 | + const data = new FormData() |
| 135 | + data.append('file', file) |
| 136 | + const url = `${window.server}/Menu/upload/${svSession}` |
| 137 | + const reqConfig = { method: 'post', data, url, headers: { 'Content-Type': 'multipart/form-data' } } |
| 138 | + axios(reqConfig).then(res => { |
| 139 | + setState({ loading: false }) |
| 140 | + if (res?.data) { |
| 141 | + alertUserResponse({ response: res }) |
| 142 | + if (res.data?.type?.toLowerCase() === 'success') { |
| 143 | + reloadGrid() |
| 144 | + } |
| 145 | + } |
| 146 | + }).catch(err => { |
| 147 | + console.error(err) |
| 148 | + setState({ loading: false }) |
| 149 | + alertUserResponse({ response: err }) |
| 150 | + }) |
| 151 | + } |
| 152 | + |
| 153 | + const handleFileSelection = (e) => { |
| 154 | + const notJSONLabel = context.intl.formatMessage({ id: 'perun.admin_console.file_is_not_json', defaultMessage: 'perun.admin_console.file_is_not_json' }) |
| 155 | + const file = e.target.files[0] |
| 156 | + // Check if the selected file is valid JSON |
| 157 | + const fileReader = new FileReader() |
| 158 | + fileReader.onload = (e) => { |
| 159 | + try { |
| 160 | + JSON.parse(e.target.result) |
| 161 | + uploadFile(file) |
| 162 | + } catch (error) { |
| 163 | + alertUserV2({ type: 'info', title: notJSONLabel }) |
| 164 | + } |
| 165 | + } |
| 166 | + fileReader.onerror = () => alertUserV2({ type: 'info', title: notJSONLabel }) |
| 167 | + fileReader.readAsText(file) |
| 168 | + if (uploadInputRef?.current?.value) { |
| 169 | + uploadInputRef.current.value = '' |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + return ( |
| 174 | + <> |
| 175 | + {loading && <Loading />} |
| 176 | + <input type='file' id='upload-perun-menu-input' ref={uploadInputRef} onInput={handleFileSelection} style={{ display: 'none' }} /> |
| 177 | + <div className='admin-console-grid-container'> |
| 178 | + <div className='admin-console-component-header'> |
| 179 | + <p>{context.intl.formatMessage({ id: 'perun.admin_console.perun_menu', defaultMessage: 'perun.admin_console.perun_menu' })}</p> |
| 180 | + </div> |
| 181 | + {generatePerunMenuGrid()} |
| 182 | + </div> |
| 183 | + {show && ( |
| 184 | + <Modal className='admin-console-unit-modal' show={show} onHide={() => setState({ show: false })}> |
| 185 | + <Modal.Header className='admin-console-unit-modal-header' closeButton> |
| 186 | + <Modal.Title>{context.intl.formatMessage({ id: 'perun.admin_console.add', defaultMessage: 'perun.admin_console.add' })}</Modal.Title> |
| 187 | + </Modal.Header> |
| 188 | + <Modal.Body className='admin-console-unit-modal-body'> |
| 189 | + {generatePerunMenuForm(objectId)} |
| 190 | + </Modal.Body> |
| 191 | + <Modal.Footer className='admin-console-unit-modal-footer'></Modal.Footer> |
| 192 | + </Modal> |
| 193 | + )} |
| 194 | + </> |
| 195 | + ); |
| 196 | +}; |
| 197 | + |
| 198 | +const mapStateToProps = (state) => ({ |
| 199 | + svSession: state.security.svSession, |
| 200 | +}); |
| 201 | + |
| 202 | +PerunMenu.contextTypes = { |
| 203 | + intl: PropTypes.object.isRequired |
| 204 | +} |
| 205 | + |
| 206 | +export default connect(mapStateToProps)(PerunMenu); |
0 commit comments