implement mapping update

This commit is contained in:
Bilal Catic
2019-08-28 12:44:47 +02:00
parent 4999b1323f
commit 9878b5e6b4
6 changed files with 166 additions and 23 deletions

View File

@@ -1,12 +1,12 @@
import React, { Component } from 'react'; import React, { Component } from 'react';
import { connect } from 'react-redux'; import { connect } from 'react-redux';
import { Table, Label, Icon } from 'semantic-ui-react'; import { Table, Label, Icon, Dropdown } from 'semantic-ui-react';
import PromptMessage from '../../../components/PromptMessage'; import PromptMessage from '../../../components/PromptMessage';
import { deleteMapping } from '../../../store/actions'; import { deleteMapping, updateMapping } from '../../../store/actions';
class SingleMapping extends Component { class SingleMapping extends Component {
state = {showDeletePrompt: false}; state = {showDeletePrompt: false, newOfficeId: null, newResourceId: null};
deleteMapping = () => { deleteMapping = () => {
this.setState({showDeletePrompt: true}); this.setState({showDeletePrompt: true});
@@ -24,10 +24,70 @@ class SingleMapping extends Component {
this.onPromptClose(); this.onPromptClose();
}; };
onOfficeChange = (event, data) => {
const newOfficeId = data.value;
const { allResourceOptions, officeId: oldOfficeId } = this.props;
if (oldOfficeId !== newOfficeId){
let newResourceId = null;
const roomOptionsForOffice = allResourceOptions[newOfficeId] && allResourceOptions[newOfficeId].roomOptions ?
allResourceOptions[newOfficeId].roomOptions : [];
if (roomOptionsForOffice.length > 0){
newResourceId = roomOptionsForOffice[0].value;
}
this.setState({newOfficeId, newResourceId});
}else{
this.setState({newOfficeId: null, newResourceId: null});
}
};
onResourceChange = (event, data) => {
const newResourceId = data.value;
const oldResourceId = this.props.resourceId;
if (oldResourceId !== newResourceId){
this.setState({newResourceId});
}else{
this.setState({newResourceId: null});
}
};
onMappingUpdate = () => {
const { singleMapping: { id, officeSlug, resourceSlug }, updateMapping, officeId, resourceId } = this.props;
const { newOfficeId, newResourceId } = this.state;
const selectedOfficeId = newOfficeId ? newOfficeId : officeId;
const selectedResourceId = newResourceId ? newResourceId : resourceId;
if (parseInt(id)){
const modifiedMapping = {
officeSlug,
resourceSlug,
officeId: selectedOfficeId,
resourceId: selectedResourceId,
};
updateMapping(id, modifiedMapping);
this.setState({newOfficeId: null, newResourceId: null});
}
};
render() { render() {
const { singleMapping: { officeSlug, resourceSlug } } = this.props; const { singleMapping: { officeSlug, resourceSlug } } = this.props;
const { officeName, resourceName } = this.props; const { officeId, resourceId, officeOptions, allResourceOptions, pendingMappings } = this.props;
const { showDeletePrompt } = this.state; const { showDeletePrompt, newOfficeId, newResourceId } = this.state;
const selectedOfficeId = newOfficeId ? newOfficeId : officeId;
const selectedResourceId = newResourceId ? newResourceId : resourceId;
const resourceOptions = allResourceOptions && officeId ? allResourceOptions[selectedOfficeId].roomOptions : [];
const enableActions = !pendingMappings;
const enableSave = enableActions && ((newOfficeId !== null) || (newResourceId !== null));
const saveIconColor = enableSave ? 'green' : null;
return ( return (
<Table.Row> <Table.Row>
@@ -42,20 +102,36 @@ class SingleMapping extends Component {
<Table.Cell> <Table.Cell>
<Label size={'big'}>{`[${officeSlug}-${resourceSlug}]`}</Label> <Label size={'big'}>{`[${officeSlug}-${resourceSlug}]`}</Label>
</Table.Cell> </Table.Cell>
<Table.Cell>{officeName}</Table.Cell> <Table.Cell>
<Table.Cell>{resourceName}</Table.Cell> <Dropdown
options={officeOptions}
defaultValue={officeId}
onChange={this.onOfficeChange}
/>
</Table.Cell>
<Table.Cell>
<Dropdown
options={resourceOptions}
value={selectedResourceId}
onChange={this.onResourceChange}
/>
</Table.Cell>
<Table.Cell> <Table.Cell>
<Icon <Icon
circular circular
name={'check'} name={'check'}
size={'large'} size={'large'}
disabled disabled={!enableSave}
link={enableSave || null}
color={saveIconColor}
onClick={this.onMappingUpdate}
/> />
<Icon <Icon
circular circular
name={'trash'} name={'trash'}
size={'large'} size={'large'}
link disabled={!enableActions}
link={enableActions || null}
onClick={this.deleteMapping} onClick={this.deleteMapping}
/> />
</Table.Cell> </Table.Cell>
@@ -64,8 +140,13 @@ class SingleMapping extends Component {
} }
} }
const mapDispatchToProps = (dispatch) => ({ const mapStateToProps = (state) => ({
deleteMapping: (id) => deleteMapping(dispatch, id), pendingMappings: state.mappingsData.pending,
}); });
export default connect(null, mapDispatchToProps)(SingleMapping); const mapDispatchToProps = (dispatch) => ({
deleteMapping: (id) => deleteMapping(dispatch, id),
updateMapping: (id, mapping) => updateMapping(dispatch, id, mapping),
});
export default connect(mapStateToProps, mapDispatchToProps)(SingleMapping);

View File

@@ -20,17 +20,30 @@ class RoomOfficeNameMapping extends Component {
const offices = mappings && mappings.offices ? mappings.offices : []; const offices = mappings && mappings.offices ? mappings.offices : [];
const resources = mappings && mappings.resources ? mappings.resources : []; const resources = mappings && mappings.resources ? mappings.resources : [];
const officesMap = {}; const officeDropdownOptions = offices.map((office) => {
const resourcesMap = {};
offices.forEach((office) => {
const { officeId, officeName } = office; const { officeId, officeName } = office;
officesMap[officeId] = officeName; return {
key: officeId,
value: officeId,
text: officeName,
}
}); });
const allResourceOptions = {};
resources.forEach((resource) => { resources.forEach((resource) => {
const { resourceId, resourceName } = resource; const { resourceId, resourceName, officeId } = resource;
resourcesMap[resourceId] = resourceName;
if (!allResourceOptions[officeId]){
allResourceOptions[officeId] = {
roomOptions: []
}
}
allResourceOptions[officeId].roomOptions.push({
key: resourceId,
value: resourceId,
text: resourceName,
});
}); });
return ( return (
@@ -56,8 +69,10 @@ class RoomOfficeNameMapping extends Component {
return <SingleMapping return <SingleMapping
key={singleMapping.id} key={singleMapping.id}
singleMapping={singleMapping} singleMapping={singleMapping}
officeName={officesMap[officeId]} officeId={officeId}
resourceName={resourcesMap[resourceId]} resourceId={resourceId}
officeOptions={officeDropdownOptions}
allResourceOptions={allResourceOptions}
/> />
}) })
} }

View File

@@ -49,6 +49,19 @@ export const deleteMapping = (dispatch, mappingId) => {
}); });
}; };
export const updateMapping = (dispatch, id, mapping) => {
dispatch({type: FETCH_MAPPINGS_PENDING});
API.put(`integration/mappings/${id}`, {
mapping
})
.then(response => {
dispatch({type: FETCH_MAPPINGS_SUCCESS, payload: response.data});
})
.catch(error => {
dispatch({type: FETCH_MAPPINGS_FAILED, payload: error.response});
});
};
export const addNewMapping = (dispatch, mapping) => { export const addNewMapping = (dispatch, mapping) => {
dispatch({type: ADD_NEW_MAPPING_PENDING}); dispatch({type: ADD_NEW_MAPPING_PENDING});
API.post('integration/mappings', { API.post('integration/mappings', {

View File

@@ -2,7 +2,7 @@
const moment = require('moment-timezone'); const moment = require('moment-timezone');
const { getMappingsFromDatabase, fetchOffices, fetchResources, saveNewMappingToDatabase, deleteMappingById } = require('../services/officeRnD/resources'); const { getMappingsFromDatabase, fetchOffices, fetchResources, saveNewMappingToDatabase, deleteMappingById, updateMappingById } = require('../services/officeRnD/resources');
const { getAllIncidents, getMemberPracticeSummaryReport } = require('../services/integration/reports'); const { getAllIncidents, getMemberPracticeSummaryReport } = require('../services/integration/reports');
const { getMembersFeesForDateRange } = require('../services/integration/invoiceIntegration'); const { getMembersFeesForDateRange } = require('../services/integration/invoiceIntegration');
const { deleteFeesFromORD, addFeesToORD } = require('../services/officeRnD/fees'); const { deleteFeesFromORD, addFeesToORD } = require('../services/officeRnD/fees');
@@ -24,6 +24,7 @@ const getKnownOfficeResourceMappings = (req, res) => {
}); });
}) })
.catch(error => { .catch(error => {
console.log('Error with fetching mappings : ', error);
res.status(500).send(); res.status(500).send();
}); });
}; };
@@ -54,6 +55,27 @@ const deleteMapping = (req, res) => {
console.log(error); console.log(error);
res.status(500).send(); res.status(500).send();
}); });
}else{
getKnownOfficeResourceMappings(req, res);
}
};
const updateMapping = (req, res) => {
const mappingId = req.params.mappingId;
const mapping = req.body && req.body.mapping ? req.body.mapping : null;
if (mappingId && parseInt(mappingId)){
updateMappingById(mappingId, mapping)
.then(() => {
getKnownOfficeResourceMappings(req, res);
})
.catch((error) => {
console.log('Error updating mapping with id = ', mappingId);
console.log(error);
res.status(500).send();
});
}else{
getKnownOfficeResourceMappings(req, res);
} }
}; };
@@ -187,6 +209,7 @@ module.exports = {
getKnownOfficeResourceMappings, getKnownOfficeResourceMappings,
addNewMapping, addNewMapping,
deleteMapping, deleteMapping,
updateMapping,
getAllIncidentsController, getAllIncidentsController,
getMemberIncidents, getMemberIncidents,
addFees, addFees,

View File

@@ -7,6 +7,7 @@ const {
getKnownOfficeResourceMappings, getKnownOfficeResourceMappings,
addNewMapping, addNewMapping,
deleteMapping, deleteMapping,
updateMapping,
getAllIncidentsController, getAllIncidentsController,
getMemberIncidents, getMemberIncidents,
addFees, addFees,
@@ -25,6 +26,7 @@ router.post('/doorLock/upload', uploadDoorLockData);
router.get('/integration/mappings', getKnownOfficeResourceMappings); router.get('/integration/mappings', getKnownOfficeResourceMappings);
router.post('/integration/mappings', addNewMapping); router.post('/integration/mappings', addNewMapping);
router.delete('/integration/mappings/:mappingId', deleteMapping); router.delete('/integration/mappings/:mappingId', deleteMapping);
router.put('/integration/mappings/:mappingId', updateMapping);
router.get('/integration/report/member/:memberId/:startDate/:endDate', getMemberIncidents); router.get('/integration/report/member/:memberId/:startDate/:endDate', getMemberIncidents);
router.get('/integration/report/allIncidents/:startDate/:endDate', getAllIncidentsController); router.get('/integration/report/allIncidents/:startDate/:endDate', getAllIncidentsController);

View File

@@ -70,13 +70,21 @@ const getResourceMappings = () => {
}; };
const getMappingsFromDatabase = () => { const getMappingsFromDatabase = () => {
return db.officeResourceMapping.findAll(); return db.officeResourceMapping.findAll({order: [['id']]});
}; };
const deleteMappingById = (id) => { const deleteMappingById = (id) => {
return db.officeResourceMapping.destroy({where: {id}}); return db.officeResourceMapping.destroy({where: {id}});
}; };
const updateMappingById = (id, mapping) => {
if (mapping.id) {
delete mapping.id;
}
return db.officeResourceMapping.update(mapping, {where: {id}});
};
const saveNewMappingToDatabase = (mapping) => { const saveNewMappingToDatabase = (mapping) => {
return db.officeResourceMapping.findOrCreate({where: {...mapping}, defaults: {...mapping}}); return db.officeResourceMapping.findOrCreate({where: {...mapping}, defaults: {...mapping}});
}; };
@@ -88,4 +96,5 @@ module.exports = {
getResourceMappings, getResourceMappings,
saveNewMappingToDatabase, saveNewMappingToDatabase,
deleteMappingById, deleteMappingById,
updateMappingById,
}; };