Skip to content

Commit

Permalink
Merge pull request #48 from akvo/feature/46-support-autofield-questio…
Browse files Browse the repository at this point in the history
…n-type

Feature/46 support autofield question type
  • Loading branch information
dedenbangkit committed Sep 4, 2023
2 parents 6787f6a + 7961612 commit 99187e3
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 1 deletion.
20 changes: 20 additions & 0 deletions example/src/example-initial-value.json
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,26 @@
}
]
},
{
"id": 29,
"name": "Average spent for meal",
"order": 3,
"type": "autofield",
"required": true,
"fn": {
"multiline": false,
"fnString": "function () { return #8 / #9 || 0 }",
"fnColor": {
"50000": "#ef6548"
}
},
"translations": [
{
"name": "Rata-rata biaya makan",
"language": "id"
}
]
},
{
"id": 11,
"name": "Favorite Food",
Expand Down
2 changes: 2 additions & 0 deletions src/components/QuestionSetting.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
SettingDate,
SettingTable,
SettingImage,
SettingAutofield,
} from './question-type';
import QuestionHint from './QuestionHint';
import { map, groupBy, orderBy, isEmpty } from 'lodash';
Expand Down Expand Up @@ -309,6 +310,7 @@ const QuestionSetting = ({ question, dependant }) => {
{qType === questionType.date && <SettingDate {...question} />}
{qType === questionType.table && <SettingTable {...question} />}
{qType === questionType.image && <SettingImage {...question} />}
{qType === questionType.autofield && <SettingAutofield {...question} />}
</div>
);
};
Expand Down
109 changes: 109 additions & 0 deletions src/components/question-type/SettingAutofield.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import React from 'react';
import { Form, Checkbox, Space, Input } from 'antd';
import styles from '../../styles.module.css';
import { UIStore, questionGroupFn } from '../../lib/store';
import isEmpty from 'lodash/isEmpty';

const fnStringExample =
"function () { return #question_id / #question_id } OR () => { return #1.includes('Test') ? #question_id / #question_id : 0 }";

const fnColorExample = "{ 'answer_value': '#CCFFC4' }";

const SettingAutofield = ({
id,
questionGroupId,
fn = { multiline: false, fnString: null, fnColor: {} },
}) => {
const namePreffix = `question-${id}`;
const UIText = UIStore.useState((s) => s.UIText);

const updateState = (name, value) => {
questionGroupFn.store.update((s) => {
s.questionGroups = s.questionGroups.map((qg) => {
if (qg.id === questionGroupId) {
const questions = qg.questions.map((q) => {
if (q.id === id) {
return {
...q,
[name]: value,
};
}
return q;
});
return {
...qg,
questions: questions,
};
}
return qg;
});
});
};

const handleChangeMultiline = (e) => {
const value = e?.target?.checked;
updateState('fn', { ...fn, multiline: value });
};

const handleChangeFnString = (e) => {
const value = e?.target?.value;
updateState('fn', { ...fn, fnString: value });
};

const handleChangeFnColor = (e) => {
let value = e?.target?.value;
try {
value = JSON.parse(value);
updateState('fn', { ...fn, fnColor: value });
return true;
} catch (error) {
return false;
}
};

return (
<div>
<p className={styles['more-question-setting-text']}>
{UIText.questionMoreAutofieldTypeSettingText}
</p>
<Space className={styles['space-align-left']}>
<Form.Item name={`${namePreffix}-autofield_multiline`}>
<Checkbox
onChange={handleChangeMultiline}
checked={fn?.multiline || false}
>
{' '}
{UIText.inputQuestionAutofieldMultiline}
</Checkbox>
</Form.Item>
</Space>
<Form.Item
label={UIText.inputQuestionAutofieldFnString}
name={`${namePreffix}-autofield_fnString`}
initialValue={fn?.fnString || null}
required
>
<Input.TextArea
rows={5}
allowClear
onChange={handleChangeFnString}
placeholder={fnStringExample}
/>
</Form.Item>
<Form.Item
label={UIText.inputQuestionAutofieldFnColor}
name={`${namePreffix}-autofield_fnColor`}
initialValue={isEmpty(fn?.fnColor) ? null : JSON.stringify(fn?.fnColor)}
>
<Input.TextArea
rows={5}
allowClear
onChange={handleChangeFnColor}
placeholder={fnColorExample}
/>
</Form.Item>
</div>
);
};

export default SettingAutofield;
1 change: 1 addition & 0 deletions src/components/question-type/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ export { default as SettingCascade } from './SettingCascade';
export { default as SettingDate } from './SettingDate';
export { default as SettingTable } from './SettingTable';
export { default as SettingImage } from './SettingImage';
export { default as SettingAutofield } from './SettingAutofield';
4 changes: 4 additions & 0 deletions src/lib/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ const UIStaticText = {
inputQuestionHintButtonTextLabel: 'Hint Button Text',
questionMoreImageTypeSettingText: 'More Image Question Setting',
inputQuestionImageLimitValidationText: 'Limit / Max file size',
questionMoreAutofieldTypeSettingText: 'More Autofield Question Setting',
inputQuestionAutofieldMultiline: 'Support Multiline Function',
inputQuestionAutofieldFnString: 'Add Function (String) Here',
inputQuestionAutofieldFnColor: 'Add Function Color Here',
},
};

Expand Down
2 changes: 1 addition & 1 deletion src/lib/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ const questionType = {
multiple_option: 'multiple_option',
tree: 'tree',
table: 'table',
// autofield: 'autofield',
image: 'image',
autofield: 'autofield',
};

const defaultForm = () => {
Expand Down

0 comments on commit 99187e3

Please sign in to comment.