@inseefr/lunatic 0.3.0-experimental → 0.3.1-experimental

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inseefr/lunatic",
3
- "version": "0.3.0-experimental",
3
+ "version": "0.3.1-experimental",
4
4
  "workersVersion": "0.2.1-experimental",
5
5
  "description": "Library of questionnaire components",
6
6
  "repository": {
@@ -1,4 +1,4 @@
1
- import React from 'react';
1
+ import React, { useState } from 'react';
2
2
  import { storiesOf } from '@storybook/react';
3
3
  import Orchestrator from '../utils/orchestrator';
4
4
  import { titleDecorator } from 'utils/lib';
@@ -10,6 +10,8 @@ import logementSequence from './logement-sequence';
10
10
  import dataLogement from './data-logement';
11
11
  import simpsons from './simpsons';
12
12
  import arithmetic from './arithmetic';
13
+ import updateExternalQuestionnaire from './update-external/questionnaire';
14
+ import updateExternalData from './update-external/data';
13
15
  import { positioningOptions, featuresOptions } from '../utils/options';
14
16
  import { boolean, select } from '@storybook/addon-knobs/react';
15
17
 
@@ -170,3 +172,34 @@ paginated.addWithJSX('Simpsons', () => (
170
172
  pagination
171
173
  />
172
174
  ));
175
+
176
+ const other = storiesOf('Questionnaire/Other', module).addDecorator(
177
+ (Component) => {
178
+ const WrappedComponent = titleDecorator(Component);
179
+ return <WrappedComponent title="<Questionnaire />" />;
180
+ }
181
+ );
182
+
183
+ other.addWithJSX('Update external', () => {
184
+ const [addExternal, setAddExternal] = useState(null);
185
+ return (
186
+ <>
187
+ <button
188
+ onClick={() => setAddExternal({ PROMO: true })}
189
+ >{`Fire PROMO --> True`}</button>
190
+ <button
191
+ onClick={() => setAddExternal({ PROMO: false })}
192
+ >{`Fire PROMO --> False`}</button>
193
+ <Orchestrator
194
+ id="props"
195
+ source={updateExternalQuestionnaire}
196
+ data={updateExternalData}
197
+ features={select('Features', featuresOptions, ['VTL', 'MD'])}
198
+ positioning={select('Items positioning', positioningOptions, 'DEFAULT')}
199
+ disabled={boolean('Disabled', false)}
200
+ focused={boolean('Focused', false)}
201
+ addExternal={addExternal}
202
+ />
203
+ </>
204
+ );
205
+ });
@@ -0,0 +1 @@
1
+ { "EXTERNAL": { "PROMO": null } }
@@ -0,0 +1,75 @@
1
+ {
2
+ "components": [
3
+ {
4
+ "id": "seq1",
5
+ "componentType": "Sequence",
6
+ "label": "\"I - Collect number\"",
7
+ "conditionFilter": { "value": "true" }
8
+ },
9
+ {
10
+ "id": "input1",
11
+ "componentType": "InputNumber",
12
+ "label": "Number",
13
+ "conditionFilter": { "value": "true" },
14
+ "response": { "name": "NUM" },
15
+ "bindingDependencies": ["NUM"]
16
+ },
17
+ {
18
+ "id": "seq2",
19
+ "componentType": "Sequence",
20
+ "label": "\"II - Promo rate\"",
21
+ "conditionFilter": {
22
+ "value": "PROMO = true",
23
+ "bindingDependencies": ["PROMO"]
24
+ },
25
+ "bindingDependencies": ["PROMO"]
26
+ },
27
+ {
28
+ "id": "input2",
29
+ "componentType": "InputNumber",
30
+ "label": "Rate",
31
+ "conditionFilter": {
32
+ "value": "PROMO = true",
33
+ "bindingDependencies": ["PROMO"]
34
+ },
35
+ "response": {
36
+ "name": "PROMO_RATE",
37
+ "bindingDependencies": ["PROMO_RATE"]
38
+ },
39
+ "bindingDependencies": ["PROMO", "PROMO_RATE"]
40
+ }
41
+ ],
42
+ "variables": [
43
+ {
44
+ "variableType": "COLLECTED",
45
+ "name": "NUM",
46
+ "componentRef": "input1",
47
+ "values": {
48
+ "PREVIOUS": null,
49
+ "COLLECTED": null,
50
+ "FORCED": null,
51
+ "EDITED": null,
52
+ "INPUTED": null
53
+ }
54
+ },
55
+ {
56
+ "variableType": "COLLECTED",
57
+ "name": "PROMO_RATE",
58
+ "componentRef": "input2",
59
+ "values": {
60
+ "PREVIOUS": null,
61
+ "COLLECTED": null,
62
+ "FORCED": null,
63
+ "EDITED": null,
64
+ "INPUTED": null
65
+ }
66
+ },
67
+ { "variableType": "EXTERNAL", "name": "PROMO", "value": null },
68
+ {
69
+ "variableType": "CALCULATED",
70
+ "name": "DOUBLE_PROMO_RATE",
71
+ "expression": "cast(PROMO_RATE, integer) * 2",
72
+ "bindingDependencies": ["PROMO_RATE"]
73
+ }
74
+ ]
75
+ }
@@ -1,4 +1,4 @@
1
- import React from 'react';
1
+ import React, { useEffect } from 'react';
2
2
  import * as lunatic from 'components';
3
3
  import './custom-lunatic.scss';
4
4
 
@@ -22,6 +22,7 @@ const OrchestratorForStories = ({
22
22
  activeGoNextForMissing = false,
23
23
  suggesterFetcher,
24
24
  autoSuggesterLoading,
25
+ addExternal,
25
26
  ...rest
26
27
  }) => {
27
28
  const preferences = management
@@ -30,6 +31,7 @@ const OrchestratorForStories = ({
30
31
  const savingType = management ? 'EDITED' : 'COLLECTED';
31
32
  const {
32
33
  handleChange,
34
+ handleExternals,
33
35
  components,
34
36
  bindings,
35
37
  pagination: {
@@ -54,6 +56,11 @@ const OrchestratorForStories = ({
54
56
  suggesterFetcher,
55
57
  autoSuggesterLoading,
56
58
  });
59
+
60
+ useEffect(() => {
61
+ handleExternals(addExternal);
62
+ }, [addExternal, handleExternals]);
63
+
57
64
  const Button = lunatic.Button;
58
65
 
59
66
  const missingStrategy = (b) => goNext(null, b);
@@ -59,6 +59,26 @@ export const updateQuestionnaire =
59
59
  };
60
60
  };
61
61
 
62
+ export const updateExternals =
63
+ (questionnaire) => (logFunction) => (updatedValues) => {
64
+ const { variables, ...other } = questionnaire;
65
+ const { EXTERNAL } = variables;
66
+ const newVariables = {
67
+ ...variables,
68
+ EXTERNAL: { ...EXTERNAL, ...updatedValues },
69
+ };
70
+
71
+ const newVariablesWithCalculated = addCalculatedVars(
72
+ newVariables,
73
+ updatedValues
74
+ )(logFunction);
75
+
76
+ return {
77
+ ...other,
78
+ variables: newVariablesWithCalculated,
79
+ };
80
+ };
81
+
62
82
  export const buildNewValue =
63
83
  (preferences) => (valueType) => (oldValues) => (value) => {
64
84
  if (preferences.length === 1) return value;
@@ -1,7 +1,7 @@
1
1
  import { useState, useEffect, useCallback } from 'react';
2
2
  import { mergeQuestionnaireAndData } from '../init-questionnaire';
3
3
  import { getBindings } from '../state';
4
- import { updateQuestionnaire } from '../handler';
4
+ import { updateQuestionnaire, updateExternals } from '../handler';
5
5
  import { getPage, FLOW_NEXT, FLOW_PREVIOUS, getControls } from '../../lib';
6
6
  import { COLLECTED } from '../../../constants';
7
7
  import { useFilterComponents } from './filter-components';
@@ -33,6 +33,7 @@ const useLunatic = (
33
33
  const [page, setPage] = useState(initialPage);
34
34
 
35
35
  const [todo, setTodo] = useState({});
36
+ const [todoExternals, setTodoExternals] = useState({});
36
37
 
37
38
  const [modalContent, setModalContent] = useState(null);
38
39
 
@@ -43,7 +44,7 @@ const useLunatic = (
43
44
  features: featuresWithoutMD,
44
45
  page,
45
46
  pagination,
46
- todo,
47
+ todo: { ...todo, ...todoExternals },
47
48
  });
48
49
 
49
50
  const { suggesters: suggestersToLoad } = source;
@@ -168,6 +169,10 @@ const useLunatic = (
168
169
  setTodo((t) => ({ ...t, ...updatedValue }));
169
170
  }, []);
170
171
 
172
+ const handleExternals = useCallback((externals) => {
173
+ setTodoExternals((t) => ({ ...t, ...externals }));
174
+ }, []);
175
+
171
176
  // Assume we only want to handle source update
172
177
  useEffect(() => {
173
178
  setQuestionnaire(mergeQuestionnaireAndData(source)(data));
@@ -193,6 +198,14 @@ const useLunatic = (
193
198
  management,
194
199
  ]);
195
200
 
201
+ useEffect(() => {
202
+ if (Object.keys(todoExternals).length !== 0) {
203
+ const newQ = updateExternals(questionnaire)(logFunction)(todoExternals);
204
+ setQuestionnaire(newQ);
205
+ setTodoExternals({});
206
+ }
207
+ }, [todoExternals, logFunction, questionnaire]);
208
+
196
209
  const cancelModal = () => {
197
210
  setModalContent(null);
198
211
  };
@@ -218,6 +231,7 @@ const useLunatic = (
218
231
  return {
219
232
  questionnaire,
220
233
  handleChange,
234
+ handleExternals,
221
235
  components: componentsToDiplay,
222
236
  bindings,
223
237
  pagination: {