diff --git a/src/components/form/components/CheckboxGroup.tsx b/src/components/form/components/CheckboxGroup.tsx index bd853567..3455caa2 100644 --- a/src/components/form/components/CheckboxGroup.tsx +++ b/src/components/form/components/CheckboxGroup.tsx @@ -13,6 +13,8 @@ import { RootState } from '../../../store/store'; import QuestionRenderingEngine from '../QuestionRenderingEngine'; import ErrorMessage from './ErrorMessage'; import RNCheckboxGroup from '../../../../RN-UI-LIB/src/components/chechbox/CheckboxGroup'; +import { ICheckboxOption } from '../../../../RN-UI-LIB/src/components/chechbox/types'; +import { AnswerType } from '../interface'; interface ICheckBoxGroup { questionType: string; @@ -48,26 +50,6 @@ const CheckBoxGroup: React.FC = props => { // eslint-disable-next-line react-hooks/exhaustive-deps }, [dataFromRedux]); - // const dispatch = useDispatch(); - // const answer = useSelector( - // (state: RootState) => - // state.counter.caseForm?.[caseId]?.[journeyId]?.[widgetId]?.[ - // sectionId - // ]?.[questionId] || 'not_found', - // ); - - // const registerValue = (id: string) => { - // dispatch( - // updateInteraction({ - // caseId, - // journeyId, - // widgetId, - // questionId, - // answer: id, - // sectionId, - // }), - // ); - // }; const computeNextQuestion = (optionId: keyof typeof options) => { if (options[optionId]?.associateQuestions.length < 1) { @@ -79,6 +61,14 @@ const CheckBoxGroup: React.FC = props => { const optiosList = question.options.map(option =>({label: options[option].text, value: option})); + const handleChange = (change : Array | null, onChange : (...event: any[]) => void) => { + const values = change?.map(item=>item.value) + onChange({ + answer: values, + type: AnswerType.array + }) + } + return ( {question.text ? @@ -95,7 +85,7 @@ const CheckBoxGroup: React.FC = props => { render={({field: {onChange, value}}) => { console.log(value) return( - {const values = cange?.map(item=>item.value); onChange(values)})} defaultValue={value} options={optiosList} /> + handleChange(change, onChange))} defaultValue={value?.answer} options={optiosList} /> ) }} name={`${sectionId}.${questionId}`} diff --git a/src/components/form/components/Dropdown.tsx b/src/components/form/components/Dropdown.tsx index bef7843d..786ca778 100644 --- a/src/components/form/components/Dropdown.tsx +++ b/src/components/form/components/Dropdown.tsx @@ -4,17 +4,15 @@ import {View} from 'react-native'; import {useState} from 'react'; import {Control, Controller} from 'react-hook-form'; import {useSelector} from 'react-redux'; -import RNRadioButton from '../../../../RN-UI-LIB/src/components/radio_button/RadioButton'; -import RadioChip from '../../../../RN-UI-LIB/src/components/radio_button/RadioChip'; -import RadioGroup from '../../../../RN-UI-LIB/src/components/radio_button/RadioGroup'; +import RNDropDown from '../../../../RN-UI-LIB/src/components/dropdown/Dropdown'; +import RNOptions from '../../../../RN-UI-LIB/src/components/dropdown/Options'; import Text from '../../../../RN-UI-LIB/src/components/Text'; import {GenericStyles} from '../../../../RN-UI-LIB/src/styles'; import template from '../../../data/RealTemplateData.json'; import {RootState} from '../../../store/store'; import QuestionRenderingEngine from '../QuestionRenderingEngine'; import ErrorMessage from './ErrorMessage'; -import RNDropDown from '../../../../RN-UI-LIB/src/components/dropdown/Dropdown'; -import RNOptions from '../../../../RN-UI-LIB/src/components/dropdown/Options'; +import {AnswerType} from '../interface'; interface IDropDown { questionType: string; @@ -79,6 +77,16 @@ const DropDown: React.FC = props => { setAssociatedQuestions(options[optionId]?.associateQuestions); }; + const handleChange = ( + change: string | null, + onChange: (...event: any[]) => void, + ) => { + onChange({ + answer: change, + type: AnswerType.option, + }); + }; + return ( @@ -93,19 +101,21 @@ const DropDown: React.FC = props => { required: question.type === 'mandatory', }} render={({field: {onChange, value}}) => ( - - {question.options.map(option => { - return( - - )})} - + handleChange(change, onChange)} + value={value?.answer}> + {question.options.map(option => { + return ( + + ); + })} )} name={`${sectionId}.${questionId}`} diff --git a/src/components/form/components/ImageUpload.tsx b/src/components/form/components/ImageUpload.tsx index 0c4ef648..e94c733d 100644 --- a/src/components/form/components/ImageUpload.tsx +++ b/src/components/form/components/ImageUpload.tsx @@ -18,6 +18,7 @@ import ErrorMessage from './ErrorMessage'; import DeleteIcon from '../../../../RN-UI-LIB/src/Icons/DeleteIcon'; import {COLORS} from '../../../../RN-UI-LIB/src/styles/colors'; import Text from '../../../../RN-UI-LIB/src/components/Text'; +import { AnswerType } from '../interface'; interface IImageUpload { questionType: string; @@ -51,6 +52,19 @@ const ImageUpload: React.FC = props => { if (!question) { return null; } + + const handleChange = ( + clickedImage: string | null, + onChange: (...event: any[]) => void, + ) => { + const data = `data:image/jpeg;base64,${clickedImage}`; + setImage(data); + onChange({ + answer: data, + type: AnswerType.text, + }); + }; + return ( @@ -67,11 +81,7 @@ const ImageUpload: React.FC = props => { }} render={({field: {onChange}}) => ( { - const data = `data:image/jpeg;base64,${clickedImage}`; - setImage(data); - onChange(data); - }} + onPictureClickSuccess={clickedImage => handleChange(clickedImage, onChange)} /> )} name={`${props.sectionId}.${questionId}`} diff --git a/src/components/form/components/RadioButton.tsx b/src/components/form/components/RadioButton.tsx index 3fe49827..bc7b7c45 100644 --- a/src/components/form/components/RadioButton.tsx +++ b/src/components/form/components/RadioButton.tsx @@ -13,6 +13,7 @@ import template from '../../../data/RealTemplateData.json'; import {RootState} from '../../../store/store'; import QuestionRenderingEngine from '../QuestionRenderingEngine'; import ErrorMessage from './ErrorMessage'; +import { AnswerType } from '../interface'; interface IRadioButton { questionType: string; @@ -77,6 +78,17 @@ const RadioButton: React.FC = props => { setAssociatedQuestions(options[optionId]?.associateQuestions); }; + + const handleChange = (change : string | null, onChange : (...event: any[]) => void) => { + computeNextQuestion(change as keyof typeof options); + onChange({ + answer: change, + type: AnswerType.option + }) + } + + + return ( @@ -92,12 +104,8 @@ const RadioButton: React.FC = props => { }} render={({field: {onChange, value}}) => ( { - onChange(change); - computeNextQuestion(change as keyof typeof options); - // registerValue(change); - }} + value={value?.answer} + onValueChange={change => handleChange(change, onChange)} orientation={ question.metadata.orientation || 'verticle' }> @@ -128,7 +136,7 @@ const RadioButton: React.FC = props => { name={`${sectionId}.${questionId}`} /> - {associatedQuestions.map((nextQuestion: string, index) => { + {associatedQuestions?.map((nextQuestion: string, index) => { if ( template.questions[ nextQuestion as keyof typeof template.questions diff --git a/src/components/form/components/Rating.tsx b/src/components/form/components/Rating.tsx index 117c030f..59a05209 100644 --- a/src/components/form/components/Rating.tsx +++ b/src/components/form/components/Rating.tsx @@ -6,6 +6,7 @@ import StarRating from '../../../../RN-UI-LIB/src/components/star_rating/StarRat import template from '../../../data/RealTemplateData.json'; import ErrorMessage from './ErrorMessage'; import Text from '../../../../RN-UI-LIB/src/components/Text'; +import { AnswerType } from '../interface'; interface IRating { questionType: string; questionId: string; @@ -25,6 +26,15 @@ const Rating: React.FC = props => { if (!question) { return null; } + + const handleChange = (text : number, onChange : (...event: any[]) => void) => { + onChange({ + answer: text, + type: AnswerType.number + }) + } + + return ( @@ -40,8 +50,8 @@ const Rating: React.FC = props => { }} render={({field: {onChange, value}}) => ( handleChange(value,onChange)} + defaultValue={value?.answer} maxRating={5} /> )} diff --git a/src/components/form/components/TextArea.tsx b/src/components/form/components/TextArea.tsx index 3aec91fc..b094fd84 100644 --- a/src/components/form/components/TextArea.tsx +++ b/src/components/form/components/TextArea.tsx @@ -7,6 +7,7 @@ import RNTextArea from '../../../../RN-UI-LIB/src/components/TextArea'; import Text from '../../../../RN-UI-LIB/src/components/Text'; import {GenericStyles} from '../../../../RN-UI-LIB/src/styles'; import ErrorMessage from './ErrorMessage'; +import { AnswerType } from '../interface'; interface ITextArea { questionType: string; @@ -27,6 +28,14 @@ const TextArea: React.FC = props => { if (!question) { return null; } + + const handleChange = (text : string, onChange : (...event: any[]) => void) => { + onChange({ + answer: text, + type: AnswerType.text + }) + } + return ( @@ -42,8 +51,8 @@ const TextArea: React.FC = props => { }} render={({field: {onChange, value}}) => ( handleChange(text, onChange)} + value={value?.answer} containerStyle={[GenericStyles.mt12]} numberOfLines={3} title="" diff --git a/src/components/form/components/TextInput.tsx b/src/components/form/components/TextInput.tsx index 33bcfa99..63ffb5d1 100644 --- a/src/components/form/components/TextInput.tsx +++ b/src/components/form/components/TextInput.tsx @@ -7,6 +7,7 @@ import {Control, Controller} from 'react-hook-form'; import template from '../../../data/RealTemplateData.json'; import Text from '../../../../RN-UI-LIB/src/components/Text'; import ErrorMessage from './ErrorMessage'; +import { AnswerType } from '../interface'; interface ITextInput { questionType: string; @@ -27,6 +28,14 @@ const TextInput: React.FC = props => { if (!question) { return null; } + + const handleChange = (text : string, onChange : (...event: any[]) => void) => { + onChange({ + answer: text, + type: AnswerType.text + }) + } + return ( @@ -42,8 +51,8 @@ const TextInput: React.FC = props => { }} render={({field: {onChange, value}}) => ( handleChange(text, onChange)} + value={value?.answer} containerStyle={[GenericStyles.mt12]} /> )} diff --git a/src/components/form/index.tsx b/src/components/form/index.tsx index 6fcb4e0d..2ab23adc 100644 --- a/src/components/form/index.tsx +++ b/src/components/form/index.tsx @@ -1,24 +1,21 @@ -import React, {useState} from 'react'; -import {useForm} from 'react-hook-form'; -import {Pressable, ScrollView, StyleSheet, View} from 'react-native'; -import {SafeAreaView} from 'react-native-safe-area-context'; -import {useDispatch, useSelector} from 'react-redux'; +import React, { useState } from 'react'; +import { useForm } from 'react-hook-form'; +import { Pressable, ScrollView, StyleSheet, View } from 'react-native'; +import { SafeAreaView } from 'react-native-safe-area-context'; +import { useDispatch, useSelector } from 'react-redux'; import Button from '../../../RN-UI-LIB/src/components/Button'; import Heading from '../../../RN-UI-LIB/src/components/Heading'; import Text from '../../../RN-UI-LIB/src/components/Text'; import CloseIcon from '../../../RN-UI-LIB/src/Icons/CloseIcon'; -import {GenericStyles, getShadowStyle} from '../../../RN-UI-LIB/src/styles'; -import {COLORS} from '../../../RN-UI-LIB/src/styles/colors'; +import { GenericStyles, getShadowStyle } from '../../../RN-UI-LIB/src/styles'; +import { COLORS } from '../../../RN-UI-LIB/src/styles/colors'; import Realjson from '../../data/RealTemplateData.json'; -import {updateInteraction} from '../../reducer/caseReducre'; -import {RootState} from '../../store/store'; -import {goBack, navigateToScreen} from '../utlis/navigationUtlis'; +import { updateInteraction } from '../../reducer/caseReducre'; +import { RootState } from '../../store/store'; +import { goBack, navigateToScreen } from '../utlis/navigationUtlis'; import { ConditionType, - IDecision, - IEvaluateLeaf, - IEvaluateLeafJourney, - ILeaf, + IDecision, ILeaf } from './interface'; import RenderQuestion from './RenderQuestion'; @@ -35,7 +32,7 @@ interface IWidget { const Widget: React.FC = props => { const {name, params} = props.route; const {caseId, journey} = params; - const {sections, transitionRules, isLeaf, isNoBrainer} = + const {sections, transitionRules, isLeaf} = Realjson.widget[name as keyof typeof Realjson.widget]; const sectionMap = Realjson.sections; const {actions, conditions} = transitionRules; @@ -58,7 +55,7 @@ const Widget: React.FC = props => { const evaluateLeaf = (leaf: ILeaf, data: any): boolean => { switch (leaf.operator) { case 'MATCHES': - return leaf.right === data[leaf.section][leaf.left]; + return leaf.right === data[leaf.section][leaf.left]?.answer; default: return false; } @@ -99,7 +96,7 @@ const Widget: React.FC = props => { const handleValidation = (rules: any, data: any) => { let nextScreenName = ''; - if (!isNoBrainer) { + if (rules !== null) { if (rules.conditionType === 'LEAF_NODE') { const answer = String(evaluateLeaf(rules, data)); nextScreenName = actions[answer as keyof typeof actions]; @@ -117,6 +114,7 @@ const Widget: React.FC = props => { }; const onSubmit = (data: any) => { + console.log(data) dispatch( updateInteraction({ caseId, diff --git a/src/components/form/interface.ts b/src/components/form/interface.ts index 519ae635..192bbbbc 100644 --- a/src/components/form/interface.ts +++ b/src/components/form/interface.ts @@ -50,3 +50,11 @@ export interface ILeaf { section: string; widgetId: string; } + + +export enum AnswerType { + text = 'text', + option = 'option', + number = 'number', + array = 'array' +} diff --git a/src/data/RealTemplateData.json b/src/data/RealTemplateData.json index a36b8669..90dbdf4e 100644 --- a/src/data/RealTemplateData.json +++ b/src/data/RealTemplateData.json @@ -105,9 +105,8 @@ "isLeaf": true }, "w4": { - "isNoBrainer": true, "transitionRules": { - "conditions":{}, + "conditions":null, "actions": { "true": "w6", "false": "w6" @@ -122,9 +121,8 @@ "isLeaf": false }, "w5": { - "isNoBrainer": true, "transitionRules": { - "conditions":{}, + "conditions":null, "actions": { "true": "w4", "false": "w4" @@ -136,9 +134,8 @@ "isLeaf": false }, "w6":{ - "isNoBrainer": true, "transitionRules": { - "conditions":{}, + "conditions":null, "actions": { "true": "w6", "false": "w6"