Skip to content

Commit d0f6abe

Browse files
authored
Merge pull request #1384 from rdmorganiser/fix_apply_to_all
Fix copyValue and only "apply" to empty sets
2 parents 5b6d955 + e433f62 commit d0f6abe

3 files changed

Lines changed: 30 additions & 18 deletions

File tree

rdmo/projects/assets/js/interview/actions/interviewActions.js

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ export function updateValue(value, attrs, store = true) {
375375
}
376376
}
377377

378-
export function copyValue(...originalValues) {
378+
export function copyValue(question, ...originalValues) {
379379
const firstValue = first(originalValues)
380380
const pendingId = `copyValue/${firstValue.attribute}/${firstValue.set_prefix}/${firstValue.set_index}`
381381

@@ -390,22 +390,33 @@ export function copyValue(...originalValues) {
390390
...copies,
391391
...sets.filter((set) => (
392392
(set.set_prefix == value.set_prefix) &&
393-
(set.set_index != value.set_index)
393+
(set.set_index != value.set_index) &&
394+
(set.element == question.parent)
394395
)).map((set) => {
395-
const siblingIndex = values.findIndex((v) => (
396+
// check if every sibling is empty
397+
if (values.filter((v) => (
396398
(v.attribute == value.attribute) &&
397399
(v.set_prefix == set.set_prefix) &&
398-
(v.set_index == set.set_index) &&
399-
(v.collection_index == value.collection_index)
400-
))
401-
402-
const sibling = siblingIndex > 0 ? values[siblingIndex] : null
403-
404-
if (isNil(sibling)) {
405-
return [ValueFactory.create({ ...value, set_index: set.set_index }), siblingIndex]
406-
} else if (isEmptyValue(sibling)) {
407-
// the spread operator { ...sibling } does prevent an update in place
408-
return [ValueFactory.update({ ...sibling }, value), siblingIndex]
400+
(v.set_index == set.set_index)
401+
)).every(v => isEmptyValue(v))) {
402+
// find the corresponding sibling to this original value
403+
const siblingIndex = values.findIndex((v) => (
404+
(v.attribute == value.attribute) &&
405+
(v.set_prefix == set.set_prefix) &&
406+
(v.set_index == set.set_index) &&
407+
(v.collection_index == value.collection_index)
408+
))
409+
410+
const sibling = siblingIndex > 0 ? values[siblingIndex] : null
411+
412+
if (isNil(sibling)) {
413+
return [ValueFactory.create({ ...value, set_index: set.set_index }), siblingIndex]
414+
} else if (isEmptyValue(sibling)) {
415+
// the spread operator { ...sibling } does prevent an update in place
416+
return [ValueFactory.update({ ...sibling }, value), siblingIndex]
417+
} else {
418+
return null
419+
}
409420
} else {
410421
return null
411422
}

rdmo/projects/assets/js/interview/components/main/question/QuestionCopyValue.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const QuestionCopyValue = ({ question, value, siblings, copyValue }) => {
99
!question.is_collection &&
1010
!isEmptyValue(value) &&
1111
siblings.some((value) => isEmptyValue(value)) && (
12-
<button type="button" className="btn btn-link btn-apply-to-all" onClick={() => copyValue(value)}
12+
<button type="button" className="btn btn-link btn-apply-to-all" onClick={() => copyValue(question, value)}
1313
title={gettext('Apply this answer to all tabs where this question is empty')}
1414
aria-label={gettext('Apply this answer to all tabs where this question is empty')}>
1515
<i className="fa fa-arrow-circle-right fa-btn" aria-hidden="true"></i>

rdmo/projects/assets/js/interview/components/main/question/QuestionCopyValues.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ import { isEmptyValue } from '../../../utils/value'
66

77
const QuestionCopyValues = ({ question, sets, values, siblings, currentSet, copyValue }) => {
88
const button = question.widget_type == 'checkbox' ? (
9-
<button className="btn btn-link btn-apply-to-all" onClick={() => copyValue(...values)}
9+
<button className="btn btn-link btn-apply-to-all" onClick={() => copyValue(question, ...values)}
1010
title={gettext('Apply this answer to all tabs where this question is empty')}
1111
aria-label={gettext('Apply this answer to all tabs where this question is empty')}>
1212
<i className="fa fa-arrow-circle-right fa-btn" aria-hidden="true"></i>
1313
</button>
1414
) : (
15-
<button type="button" className="btn btn-primary btn-xs copy-value-button ml-10" onClick={() => copyValue(...values)}>
15+
<button type="button" className="btn btn-primary btn-xs copy-value-button ml-10" onClick={() => copyValue(question, ...values)}>
1616
<i className="fa fa-arrow-circle-right fa-btn" aria-hidden="true"></i> {gettext('Apply to all')}
1717
</button>
1818
)
@@ -21,7 +21,8 @@ const QuestionCopyValues = ({ question, sets, values, siblings, currentSet, copy
2121

2222
const hasEmptySiblings = sets.filter((set) => (
2323
(set.set_prefix == currentSet.set_prefix) &&
24-
(set.set_index != currentSet.set_index)
24+
(set.set_index != currentSet.set_index) &&
25+
(set.element == question.parent)
2526
)).some((set) => {
2627
// loop over all other sets and filter siblings accordingly
2728
const setSiblings = siblings.filter((value) => (

0 commit comments

Comments
 (0)