Skip to content

Commit

Permalink
disable default value editing if key is autoincremental
Browse files Browse the repository at this point in the history
  • Loading branch information
SteRiccio committed Sep 11, 2024
1 parent 7fc3b39 commit 8c9b520
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 19 deletions.
19 changes: 13 additions & 6 deletions core/survey/nodeDef.js
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,8 @@ export const mergePropsAdvanced = (propsAdvanced) => (nodeDef) =>
)(nodeDef)
export const assocDefaultValues = (defaultValues) =>
mergePropsAdvanced({ [keysPropsAdvanced.defaultValues]: defaultValues })
export const assocDefaultValueEvaluatedOnlyOneTime = (evaluatedOnlyOneTime) =>
mergePropsAdvanced({ [keysPropsAdvanced.defaultValueEvaluatedOneTime]: evaluatedOnlyOneTime })
export const assocValidations = (validations) => mergePropsAdvanced({ [keysPropsAdvanced.validations]: validations })
export const dissocTemporary = R.dissoc(keys.temporary)
export const assocProp = ({ key, value }) =>
Expand Down Expand Up @@ -660,6 +662,16 @@ export const clearNotApplicableProps = (cycle) => (nodeDef) => {
export const canHaveMobileProps = (cycle) => (nodeDef) =>
canBeHiddenInMobile(nodeDef) || canIncludeInMultipleEntitySummary(cycle)(nodeDef)

const isDefaultValueAutoIncrementExpression = (defaultValue) => {
const expression = NodeDefExpression.getExpression(defaultValue)
return (
(expression === autoIncrementalKeyExpression ||
StringUtils.removeSuffix('\n')(expression).replaceAll(' ', '') ===
autoIncrementalKeyExpression.replaceAll(' ', '')) &&
Objects.isEmpty(NodeDefExpression.getApplyIf(defaultValue))
)
}

export const canHaveAutoIncrementalKey = (nodeDef) => {
if ([nodeDefType.decimal, nodeDefType.integer].includes(nodeDef)) return false

Expand All @@ -668,10 +680,5 @@ export const canHaveAutoIncrementalKey = (nodeDef) => {
if (defaultValues.length > 1) return false

const defaultValue = defaultValues[0]

// apply if specified => it cannot be auto incremental expression
if (Objects.isNotEmpty(NodeDefExpression.getApplyIf(defaultValue))) return false

const expression = NodeDefExpression.getExpression(defaultValue)
return Objects.isEmpty(expression) || expression === autoIncrementalKeyExpression
return NodeDefExpression.isEmpty(defaultValue) || isDefaultValueAutoIncrementExpression(defaultValue)
}
18 changes: 11 additions & 7 deletions webapp/components/expression/expressionEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const ExpressionEditor = (props) => {
placeholder = false,
qualifier,
query = '',
readOnly = false,
types = [ExpressionEditorType.basic, ExpressionEditorType.advanced],
} = props

Expand Down Expand Up @@ -84,13 +85,15 @@ const ExpressionEditor = (props) => {
{query}
</div>
)}
<Button
className="btn-s btn-edit"
iconClassName="icon-pencil2 icon-14px"
id={`${idPrefix}-edit-btn`}
onClick={() => setEdit(true)}
testId={TestId.expressionEditor.editBtn(qualifier)}
/>
{!readOnly && (
<Button
className="btn-s btn-edit"
iconClassName="icon-pencil2 icon-14px"
id={`${idPrefix}-edit-btn`}
onClick={() => setEdit(true)}
testId={TestId.expressionEditor.editBtn(qualifier)}
/>
)}
</div>
)}
</div>
Expand All @@ -111,6 +114,7 @@ ExpressionEditor.propTypes = {
canBeConstant: PropTypes.bool,
isBoolean: PropTypes.bool,
onChange: PropTypes.func,
readOnly: PropTypes.bool,
}

export default ExpressionEditor
4 changes: 2 additions & 2 deletions webapp/components/survey/NodeDefDetails/AdvancedProps.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ const AdvancedProps = (props) => {
state={state}
Actions={Actions}
label={i18n.t('nodeDefEdit.advancedProps.defaultValues')}
readOnly={readOnly}
readOnly={readOnly || NodeDef.isAutoIncrementalKey(nodeDef)}
propName={NodeDef.keysPropsAdvanced.defaultValues}
nodeDefUuidContext={nodeDefUuidContext}
canBeConstant
Expand All @@ -90,7 +90,7 @@ const AdvancedProps = (props) => {
<div className="form_row without-label">
<Checkbox
checked={NodeDef.isDefaultValueEvaluatedOneTime(nodeDef)}
disabled={readOnly}
disabled={readOnly || NodeDef.isAutoIncrementalKey(nodeDef)}
label="nodeDefEdit.advancedProps.defaultValueEvaluatedOneTime"
validation={Validation.getFieldValidation(NodeDef.keysPropsAdvanced.defaultValueEvaluatedOneTime)(
validation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,8 @@ const ExpressionProp = (props) => {
return (
<ValidationTooltip validation={validation} showKeys={false}>
<div className={`node-def-edit__expression${isPlaceholder ? ' placeholder' : ''}`}>
{!isPlaceholder && (
{!isPlaceholder && !readOnly && (
<ButtonIconDelete
disabled={readOnly}
id={`expression-editor-${index}-${qualifier}-expression-btn-delete`}
onClick={() => onDelete(expression)}
testId={TestId.nodeDefDetails.expressionDeleteBtn(qualifier)}
Expand All @@ -80,6 +79,7 @@ const ExpressionProp = (props) => {
isBoolean={isBoolean}
types={expressionEditorTypes}
mode={mode}
readOnly={readOnly}
/>
</div>

Expand All @@ -98,6 +98,7 @@ const ExpressionProp = (props) => {
onChange={({ query, callback }) => onUpdate(NodeDefExpression.assocApplyIf(query)(expression), callback)}
isContextParent={isContextParent}
canBeConstant={false}
readOnly={readOnly}
types={expressionEditorTypes}
/>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ const ExpressionsProp = (props) => {
validation: Validation.getFieldValidation(index)(validation),
})
)}
{(multiple || R.isEmpty(values)) &&
{!readOnly &&
(multiple || R.isEmpty(values)) &&
createExpressionProp({
index: values.length,
expression: NodeDefExpression.createExpressionPlaceholder(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useCallback } from 'react'
import { useDispatch, useSelector } from 'react-redux'

import * as A from '@core/arena'
import * as StringUtils from '@core/stringUtils'
import * as Survey from '@core/survey/survey'
import * as NodeDef from '@core/survey/nodeDef'
Expand Down Expand Up @@ -92,7 +93,10 @@ const _onUpdateAutoIncrementalKey = ({ nodeDef, value }) => {
})
defaultValues.push(defaultValueExpression)
}
return NodeDef.assocDefaultValues(defaultValues)(nodeDef)
return A.pipe(
NodeDef.assocDefaultValues(defaultValues),
NodeDef.assocDefaultValueEvaluatedOnlyOneTime(value)
)(nodeDef)
}

const updateFunctionByProp = {
Expand Down

0 comments on commit 8c9b520

Please sign in to comment.