begins transformation of where internal state to query-compatible structure

This commit is contained in:
James
2020-05-27 19:28:56 -04:00
parent 2db103233c
commit a8277c1947
7 changed files with 38 additions and 14 deletions

View File

@@ -127,7 +127,7 @@ const Button = (props) => {
Button.defaultProps = {
className: null,
type: 'submit',
type: 'button',
buttonStyle: 'primary',
el: null,
to: null,

View File

@@ -18,7 +18,7 @@ const NumberField = ({ onChange, value }) => {
};
NumberField.defaultProps = {
value: null,
value: '',
};
NumberField.propTypes = {

View File

@@ -44,7 +44,29 @@ const WhereBuilder = (props) => {
}, [fields]);
useEffect(() => {
if (typeof handleChange === 'function') handleChange(where);
const whereQuery = {
or: [],
};
if (where) {
whereQuery.or = where.map((or) => {
return or.reduce((and, condition) => {
const { field, operator, value } = condition;
if (field && operator && value) {
return {
...and,
[field]: {
[operator]: value,
},
};
}
return and;
}, {});
});
}
if (typeof handleChange === 'function') handleChange(whereQuery);
}, [where, handleChange]);
return (
@@ -94,6 +116,7 @@ const WhereBuilder = (props) => {
})}
</ul>
<Button
className={`${baseClass}__add-or`}
icon="plus"
buttonStyle="icon-label"
iconPosition="left"
@@ -107,6 +130,7 @@ const WhereBuilder = (props) => {
<div className={`${baseClass}__no-filters`}>
<div className={`${baseClass}__label`}>No filters set</div>
<Button
className={`${baseClass}__add-first-filter`}
icon="plus"
buttonStyle="icon-label"
iconPosition="left"

View File

@@ -15,9 +15,12 @@
padding: 0;
}
&__no-filters {
.btn {
margin: 0;
}
&__add-or,
&__add-first-filter {
margin-bottom: 0;
}
&__add-first-filter {
margin-top: 0;
}
}

View File

@@ -1,7 +1,3 @@
const initialCondition = {
operators: [],
};
const reducer = (state, action = {}) => {
const newState = [...state];
@@ -18,13 +14,13 @@ const reducer = (state, action = {}) => {
switch (type) {
case 'add': {
if (relation === 'and') {
newState[orIndex].splice(andIndex, 0, initialCondition);
newState[orIndex].splice(andIndex, 0, {});
return newState;
}
return [
...newState,
[initialCondition],
[{}],
];
}

View File

@@ -12,6 +12,7 @@ const FormSubmit = ({ children }) => {
return (
<div className={baseClass}>
<Button
type="submit"
disabled={formContext.processing ? true : undefined}
>
{children}

View File

@@ -8,7 +8,7 @@
/////////////////////////////
$breakpoint-xs-width: 400px;
$breakpoint-s-width: 500px;
$breakpoint-s-width: 768px;
$breakpoint-m-width: 1024px;
$breakpoint-l-width: 1440px;