Fix duplicate key bug

This commit is contained in:
soupette 2018-08-03 16:07:23 +02:00
parent 10f323640d
commit 6165ba01ad
6 changed files with 77 additions and 8 deletions

View File

@ -52,8 +52,9 @@ class Manager {
default: default:
ret = ['__col-md-3__']; ret = ['__col-md-3__'];
} }
const random = Math.floor(Math.random() * 1000);
const random1 = Math.floor(Math.random() * 1000); const random = Math.random().toString(36).substring(7);
const random1 = Math.random().toString(36).substring(8);
return ret.map((v, i) => { return ret.map((v, i) => {

View File

@ -104,6 +104,10 @@ class DraggableAttr extends React.Component {
if (isDraggingSibling !== prevProps.isDraggingSibling && isDraggingSibling) { if (isDraggingSibling !== prevProps.isDraggingSibling && isDraggingSibling) {
this.handleMouseLeave(); this.handleMouseLeave();
} }
if (prevProps.isDragging !== this.props.isDragging && this.props.isDragging) {
this.props.onClickEdit(this.props.index);
}
} }
handleClickEdit = (e) => { handleClickEdit = (e) => {

View File

@ -156,6 +156,10 @@ class VariableDraggableAttr extends React.Component {
if (prevProps.isDragging !== this.props.isDragging) { if (prevProps.isDragging !== this.props.isDragging) {
this.handleDragEffect(); this.handleDragEffect();
} }
if (prevProps.isDragging !== this.props.isDragging && this.props.isDragging) {
this.handleClickEdit();
}
} }
handleClickEdit = () => { handleClickEdit = () => {
@ -179,20 +183,43 @@ class VariableDraggableAttr extends React.Component {
renderContent = () => { renderContent = () => {
let { classNames, isOver, style, dragStart } = this.state; let { classNames, isOver, style, dragStart } = this.state;
const { data, hoverIndex, index, isEditing, name } = this.props; const { data, draggedItemName, grid, hoverIndex, index, initDragLine, isEditing, name } = this.props;
const isFullSize = classNames.bootstrap === 'col-md-12'; const isFullSize = classNames.bootstrap === 'col-md-12';
const showCarret = hoverIndex === index; let itemLine = -1;
let itemLineEls = [];
grid.forEach((line, index) => {
if (line.indexOf(name) !== -1) {
itemLine = index;
itemLineEls = line;
}
});
const itemPosition = get(grid, itemLine, []).indexOf(name);
const draggedItemLineIndex = get(grid, itemLine, []).indexOf(draggedItemName);
let showLeftCarret = hoverIndex === index && initDragLine !== itemLine;
let showRightCarret = hoverIndex === index && initDragLine === itemLine;
if (hoverIndex === index && initDragLine === itemLine && (itemPosition === 0 || itemPosition === 1 && itemLineEls.length > 2)) {
if (itemLineEls.length < 3 || itemPosition === 0 || draggedItemLineIndex > itemPosition) {
showLeftCarret = true;
showRightCarret = false;
}
}
const carretStyle = (() => { const carretStyle = (() => {
let style = { height: '30px' }; let style = { height: '30px', marginRight: '3px' };
if (classNames.withLongerHeight) { if (classNames.withLongerHeight) {
style = { height: '84px' }; style = { height: '84px', marginRight: '3px' };
} }
if (isFullSize) { if (isFullSize) {
style = { width: '100%', height: '10px', marginBottom: '6px' }; style = { width: '100%', height: '10px', marginBottom: '6px' };
} }
if (showRightCarret) {
style = { height: '30px', marginLeft: '3px' };
}
return style; return style;
})(); })();
@ -202,7 +229,7 @@ class VariableDraggableAttr extends React.Component {
return ( return (
<div style={{ display: 'flex' }}> <div style={{ display: 'flex' }}>
{ showCarret && <Carret style={carretStyle} />} { showLeftCarret && <Carret style={carretStyle} />}
<div className={cn(classNames.wrapper, isEditing && styles.editingVariableAttr)} style={style}> <div className={cn(classNames.wrapper, isEditing && styles.editingVariableAttr)} style={style}>
<i className="fa fa-th" /> <i className="fa fa-th" />
<span className={styles.truncated}> <span className={styles.truncated}>
@ -220,6 +247,7 @@ class VariableDraggableAttr extends React.Component {
<DraggedRemovedIcon withLongerHeight={classNames.withLongerHeight} onRemove={this.handleRemove} /> <DraggedRemovedIcon withLongerHeight={classNames.withLongerHeight} onRemove={this.handleRemove} />
)} )}
</div> </div>
{ showRightCarret && <Carret style={carretStyle} />}
</div> </div>
); );
} }
@ -252,8 +280,11 @@ VariableDraggableAttr.defaultProps = {
data: { data: {
type: 'text', type: 'text',
}, },
draggedItemName: null,
grid: [],
hoverIndex: -1, hoverIndex: -1,
index: 0, index: 0,
initDragLine: -1,
isDragging: false, isDragging: false,
isEditing: false, isEditing: false,
keys: '', keys: '',
@ -268,8 +299,11 @@ VariableDraggableAttr.propTypes = {
connectDragSource: PropTypes.func.isRequired, connectDragSource: PropTypes.func.isRequired,
connectDropTarget: PropTypes.func.isRequired, connectDropTarget: PropTypes.func.isRequired,
data: PropTypes.object, data: PropTypes.object,
draggedItemName: PropTypes.string,
grid: PropTypes.array,
hoverIndex: PropTypes.number, hoverIndex: PropTypes.number,
index: PropTypes.number, index: PropTypes.number,
initDragLine: PropTypes.number,
isDragging: PropTypes.bool, isDragging: PropTypes.bool,
isEditing: PropTypes.bool, isEditing: PropTypes.bool,
keys: PropTypes.string, keys: PropTypes.string,

View File

@ -57,8 +57,20 @@ const reorderList = (manager, list) => {
return acc.concat(line); return acc.concat(line);
}, []) }, [])
.filter(a => a !== undefined); .filter(a => a !== undefined);
const uniqueIdList = reordered.reduce((acc, current, index) => {
if (reordered.indexOf(current) === index) {
acc.push(current);
} else {
const bootstrapCol = parseInt(current.split('__')[1].split('-')[2], 10);
const random = Math.random().toString(36).substring(7);
acc.push(`__col-md-${bootstrapCol}__${random}`);
}
return List(flattenDeep(reordered)); return acc;
}, []);
return List(flattenDeep(uniqueIdList));
}; };
const getLines = (manager, list) => { const getLines = (manager, list) => {
const array = createArrayOfLastEls(manager, list); const array = createArrayOfLastEls(manager, list);

View File

@ -29,6 +29,10 @@ const makeSelectAddedField = () =>
createSelector(selectGlobalDomain(), globalState => createSelector(selectGlobalDomain(), globalState =>
globalState.get('addedField') globalState.get('addedField')
); );
const makeSelectDraggedItemName = () =>
createSelector(selectGlobalDomain(), globalState =>
globalState.get('draggedItemName')
);
const makeSelectHoverIndex = () => const makeSelectHoverIndex = () =>
createSelector(selectGlobalDomain(), globalState => createSelector(selectGlobalDomain(), globalState =>
globalState.get('hoverIndex') globalState.get('hoverIndex')
@ -39,6 +43,8 @@ const makeSelectModelEntries = () =>
); );
const makeSelectGrid = () => const makeSelectGrid = () =>
createSelector(selectGlobalDomain(), substate => substate.get('grid').toJS()); createSelector(selectGlobalDomain(), substate => substate.get('grid').toJS());
const makeSelectInitDragLine = () =>
createSelector(selectGlobalDomain(), substate => substate.get('initDragLine'));
const makeSelectLoading = () => const makeSelectLoading = () =>
createSelector(selectGlobalDomain(), substate => substate.get('loading')); createSelector(selectGlobalDomain(), substate => substate.get('loading'));
const makeSelectSchema = () => const makeSelectSchema = () =>
@ -54,8 +60,10 @@ export {
selectGlobalDomain, selectGlobalDomain,
selectLocationState, selectLocationState,
makeSelectAddedField, makeSelectAddedField,
makeSelectDraggedItemName,
makeSelectHoverIndex, makeSelectHoverIndex,
makeSelectGrid, makeSelectGrid,
makeSelectInitDragLine,
makeSelectLoading, makeSelectLoading,
makeSelectModelEntries, makeSelectModelEntries,
makeSelectModifiedSchema, makeSelectModifiedSchema,

View File

@ -32,8 +32,10 @@ import {
} from 'containers/App/actions'; } from 'containers/App/actions';
import { import {
makeSelectAddedField, makeSelectAddedField,
makeSelectDraggedItemName,
makeSelectGrid, makeSelectGrid,
makeSelectHoverIndex, makeSelectHoverIndex,
makeSelectInitDragLine,
makeSelectModifiedSchema, makeSelectModifiedSchema,
makeSelectShouldResetGrid, makeSelectShouldResetGrid,
makeSelectSubmitSuccess, makeSelectSubmitSuccess,
@ -443,10 +445,13 @@ class SettingPage extends React.PureComponent {
<VariableDraggableAttr <VariableDraggableAttr
beginMove={this.props.beginMove} beginMove={this.props.beginMove}
data={this.getAttrData(attr)} data={this.getAttrData(attr)}
draggedItemName={this.props.draggedItemName}
endMove={this.props.endMove} endMove={this.props.endMove}
grid={this.props.grid}
hoverIndex={this.props.hoverIndex} hoverIndex={this.props.hoverIndex}
id={attr} id={attr}
index={index} index={index}
initDragLine={this.props.initDragLine}
isEditing={index === this.findIndexFieldToEdit()} isEditing={index === this.findIndexFieldToEdit()}
key={attr} key={attr}
keys={`${this.getPath()}.editDisplay`} keys={`${this.getPath()}.editDisplay`}
@ -805,16 +810,19 @@ class SettingPage extends React.PureComponent {
} }
SettingPage.defaultProps = { SettingPage.defaultProps = {
draggedItemName: null,
grid: [], grid: [],
}; };
SettingPage.propTypes = { SettingPage.propTypes = {
addedField: PropTypes.bool.isRequired, addedField: PropTypes.bool.isRequired,
beginMove: PropTypes.func.isRequired, beginMove: PropTypes.func.isRequired,
draggedItemName: PropTypes.string,
endMove: PropTypes.func.isRequired, endMove: PropTypes.func.isRequired,
grid: PropTypes.array, grid: PropTypes.array,
history: PropTypes.object.isRequired, history: PropTypes.object.isRequired,
hoverIndex: PropTypes.number.isRequired, hoverIndex: PropTypes.number.isRequired,
initDragLine: PropTypes.number.isRequired,
match: PropTypes.object.isRequired, match: PropTypes.object.isRequired,
moveAttr: PropTypes.func.isRequired, moveAttr: PropTypes.func.isRequired,
moveAttrEditView: PropTypes.func.isRequired, moveAttrEditView: PropTypes.func.isRequired,
@ -863,8 +871,10 @@ const mapDispatchToProps = (dispatch) => (
); );
const mapStateToProps = createStructuredSelector({ const mapStateToProps = createStructuredSelector({
addedField: makeSelectAddedField(), addedField: makeSelectAddedField(),
draggedItemName: makeSelectDraggedItemName(),
grid: makeSelectGrid(), grid: makeSelectGrid(),
hoverIndex: makeSelectHoverIndex(), hoverIndex: makeSelectHoverIndex(),
initDragLine: makeSelectInitDragLine(),
schema: makeSelectModifiedSchema(), schema: makeSelectModifiedSchema(),
settingPage: makeSelectSettingPage(), settingPage: makeSelectSettingPage(),
shouldResetGrid: makeSelectShouldResetGrid(), shouldResetGrid: makeSelectShouldResetGrid(),