From f47858469fa7adf8d794b00f48ed3a01ef7ec05e Mon Sep 17 00:00:00 2001 From: James Date: Wed, 27 May 2020 10:55:19 -0400 Subject: [PATCH] allows for WhereBuilder condition values to be refilled --- .../elements/WhereBuilder/Condition/index.js | 45 ++++++++++++------- .../elements/WhereBuilder/field-types.js | 24 +++++----- .../components/elements/WhereBuilder/index.js | 2 +- .../elements/WhereBuilder/reducer.js | 35 ++++++++++++--- 4 files changed, 71 insertions(+), 35 deletions(-) diff --git a/src/client/components/elements/WhereBuilder/Condition/index.js b/src/client/components/elements/WhereBuilder/Condition/index.js index 9156112859..a71176d563 100644 --- a/src/client/components/elements/WhereBuilder/Condition/index.js +++ b/src/client/components/elements/WhereBuilder/Condition/index.js @@ -20,8 +20,12 @@ const Condition = (props) => { const [activeOperators, setActiveOperators] = useState([]); useEffect(() => { + const activeField = fields.find(field => value.field === field.value); - }, [value]); + if (activeField) { + setActiveOperators(activeField.operators); + } + }, [value, fields]); return (
@@ -29,20 +33,31 @@ const Condition = (props) => {
value.field === field.value)} options={fields} - onChange={newField => console.log('changing field', newField)} + onChange={field => dispatch({ + type: 'update', + orIndex, + andIndex, + field, + })} />
value.operator === operator.value)} options={activeOperators} - onChange={() => console.log('changing')} + onChange={operator => dispatch({ + type: 'update', + orIndex, + andIndex, + operator, + })} />
{ buttonStyle="icon-label" onClick={() => dispatch({ type: 'remove', - payload: { - orIndex, - andIndex, - }, + orIndex, + andIndex, })} />
@@ -89,8 +100,10 @@ Condition.propTypes = { fields: PropTypes.arrayOf( PropTypes.shape({ label: PropTypes.string, - name: PropTypes.string, - type: PropTypes.string, + value: PropTypes.string, + operators: PropTypes.arrayOf( + PropTypes.shape({}), + ), }), ).isRequired, value: PropTypes.shape({ diff --git a/src/client/components/elements/WhereBuilder/field-types.js b/src/client/components/elements/WhereBuilder/field-types.js index afcf9665c0..3669a8ad84 100644 --- a/src/client/components/elements/WhereBuilder/field-types.js +++ b/src/client/components/elements/WhereBuilder/field-types.js @@ -1,12 +1,10 @@ -import { buildASTSchema } from 'graphql'; - const boolean = [ { - label: 'Equals', + label: 'equals', value: 'equals', }, { - label: 'Not Equals', + label: 'is not equal to', value: 'not-equals', }, ]; @@ -14,11 +12,11 @@ const boolean = [ const base = [ ...boolean, { - label: 'In', + label: 'is in', value: 'in', }, { - label: 'Not In', + label: 'is not in', value: 'not-in', }, ]; @@ -26,26 +24,26 @@ const base = [ const numeric = [ ...base, { - label: 'Greater Than', + label: 'is greater Than', value: 'greater_than', }, { - label: 'Less Than', + label: 'is less than', value: 'less_than', }, { - label: 'Less Than Equals', + label: 'is less than or equal to', value: 'less_than_equals', }, { - label: 'Greater Than Equals', + label: 'is greater than or equal to', value: 'greater_than_equals', }, ]; const like = { - label: 'like', - value: 'Like', + label: 'is like', + value: 'like', }; const fieldTypeConditions = { @@ -95,4 +93,4 @@ const fieldTypeConditions = { }, }; -module.exports = fieldTypeConditions; +export default fieldTypeConditions; diff --git a/src/client/components/elements/WhereBuilder/index.js b/src/client/components/elements/WhereBuilder/index.js index 27a81f3af3..b4634cad1a 100644 --- a/src/client/components/elements/WhereBuilder/index.js +++ b/src/client/components/elements/WhereBuilder/index.js @@ -31,7 +31,7 @@ const WhereBuilder = (props) => { { label: field.label, value: field.name, - operators: fieldTypes[field.type], + ...fieldTypes[field.type], }, ]; } diff --git a/src/client/components/elements/WhereBuilder/reducer.js b/src/client/components/elements/WhereBuilder/reducer.js index fe5636a3c7..c295aedb4c 100644 --- a/src/client/components/elements/WhereBuilder/reducer.js +++ b/src/client/components/elements/WhereBuilder/reducer.js @@ -1,9 +1,14 @@ -const reducer = (state, { - type, payload: { - relation, orIndex, andIndex, - } = {}, -}) => { +const reducer = (state, action = {}) => { const newState = [...state]; + const { + type, + relation, + orIndex, + andIndex, + field, + operator, + value, + } = action; switch (type) { case 'add': { @@ -28,6 +33,26 @@ const reducer = (state, { return newState; } + case 'update': { + newState[orIndex][andIndex] = { + ...newState[orIndex][andIndex], + }; + + if (operator) { + newState[orIndex][andIndex].operator = operator; + } + + if (field) { + newState[orIndex][andIndex].field = field; + } + + if (value) { + newState[orIndex][andIndex].value = value; + } + + return newState; + } + default: { return newState; }