- big progress commit

Signed-off-by: Dave Richer <dave@imexsystems.ca>
This commit is contained in:
Dave Richer
2024-02-16 12:25:24 -05:00
parent 9cc0d6175e
commit 3ec4dbb5b8
4 changed files with 120 additions and 69 deletions

View File

@@ -1,24 +1,12 @@
import {Button, Card, Checkbox, Col, Form, Input, InputNumber, Row, Select} from "antd"; import {Button, Card, Checkbox, Col, Form, Row, Select} from "antd";
import React, {useEffect, useState} from "react"; import React, {useEffect, useState} from "react";
import {fetchFilterData} from "../../utils/RenderTemplate"; import {fetchFilterData} from "../../utils/RenderTemplate";
import {DeleteFilled} from "@ant-design/icons"; import {DeleteFilled} from "@ant-design/icons";
import {useTranslation} from "react-i18next"; import {useTranslation} from "react-i18next";
import {getOperatorsByType} from "../../utils/graphQLmodifier"; import {getOrderOperatorsByType, getWhereOperatorsByType} from "../../utils/graphQLmodifier";
import LoadingSkeleton from "../loading-skeleton/loading-skeleton.component"; import LoadingSkeleton from "../loading-skeleton/loading-skeleton.component";
import {setModalContext} from "../../redux/modals/modals.actions";
import {connect} from "react-redux";
import {createStructuredSelector} from "reselect";
import {selectBodyshop, selectCurrentUser} from "../../redux/user/user.selectors";
const mapDispatchToProps = (dispatch) => ({}); export default function ReportCenterModalFiltersSortersComponent({form}) {
const mapStateToProps = createStructuredSelector({
bodyshop: selectBodyshop,
currentUser: selectCurrentUser
});
export function ReportCenterModalFiltersSortersComponent({form, bodyshop, currentUser}) {
console.dir(bodyshop, {depth: null})
console.dir(currentUser, {depth: null})
return ( return (
<Form.Item style={{margin: 0, padding: 0}} dependencies={["key"]}> <Form.Item style={{margin: 0, padding: 0}} dependencies={["key"]}>
{() => { {() => {
@@ -29,9 +17,6 @@ export function ReportCenterModalFiltersSortersComponent({form, bodyshop, curre
); );
} }
export default connect(mapStateToProps, mapDispatchToProps)(ReportCenterModalFiltersSortersComponent);
function RenderFilters({templateId, form}) { function RenderFilters({templateId, form}) {
const [state, setState] = useState(null); const [state, setState] = useState(null);
const [visible, setVisible] = useState(false); const [visible, setVisible] = useState(false);
@@ -125,7 +110,8 @@ function RenderFilters({templateId, form}) {
}, },
]} ]}
> >
<Select options={getOperatorsByType(type)}/> <Select
options={getWhereOperatorsByType(type)}/>
</Form.Item> </Form.Item>
} }
} }
@@ -138,6 +124,7 @@ function RenderFilters({templateId, form}) {
() => { () => {
const name = form.getFieldValue(['filters', field.name, "field"]); const name = form.getFieldValue(['filters', field.name, "field"]);
const type = state.filters.find(f => f.name === name)?.type; const type = state.filters.find(f => f.name === name)?.type;
const reflector = state.filters.find(f => f.name === name)?.reflector;
return <Form.Item return <Form.Item
key={`${index}value`} key={`${index}value`}
@@ -150,19 +137,7 @@ function RenderFilters({templateId, form}) {
}, },
]} ]}
> >
{type === 'number' ? <ReportCenterModalFiltersSortersComponent form={form} field={field} type={type} reflector={reflector}/>
<InputNumber
onChange={(value) => {
form.setFieldsValue({[field.name]: {value: parseInt(value)}});
}}
/>
:
<Input
onChange={(value) => {
form.setFieldsValue({[field.name]: {value: value.toString()}});
}}
/>
}
</Form.Item> </Form.Item>
} }
} }
@@ -245,7 +220,7 @@ function RenderFilters({templateId, form}) {
]} ]}
> >
<Select <Select
options={getOperatorsByType()} options={getOrderOperatorsByType()}
/> />
</Form.Item> </Form.Item>
</Col> </Col>

View File

@@ -0,0 +1,48 @@
import {generateReflections} from "../../utils/graphQLmodifier";
import {Input, InputNumber, Select} from "antd";
import React from "react";
import {createStructuredSelector} from "reselect";
import {selectBodyshop, selectCurrentUser} from "../../redux/user/user.selectors";
import {connect} from "react-redux";
const mapDispatchToProps = (dispatch) => ({});
const mapStateToProps = createStructuredSelector({
bodyshop: selectBodyshop,
currentUser: selectCurrentUser
});
export function ReportCenterModalValueSelectorComponent ({type, reflector, form, field, bodyshop, currentUser}) {
console.log(`Entering ReportCenterModalValueSelectorComponent`);
console.log('Type')
console.log(type)
console.log('Reflector')
console.dir(reflector, {depth: null})
console.log('Bodyshop')
console.dir(bodyshop, {depth: null})
console.log('CurrentUser')
console.dir(currentUser, {depth: null})
if (reflector) {
const reflections = generateReflections(reflector);
if (reflections.length > 0) {
return <Select options={reflections}/>
}
}
// Number Input
if (type === "number") {
return <InputNumber
onChange={(value) => {form.setFieldsValue({[field.name]: {value: parseInt(value)}});
}}
/>
}
// Default to String Input
return <Input
onChange={(value) => {
form.setFieldsValue({[field.name]: {value: value.toString()}});
}}
/>
}
export default connect(mapStateToProps, mapDispatchToProps)(ReportCenterModalValueSelectorComponent);

View File

@@ -9,7 +9,7 @@ import {store} from "../redux/store";
import client from "../utils/GraphQLClient"; import client from "../utils/GraphQLClient";
import cleanAxios from "./CleanAxios"; import cleanAxios from "./CleanAxios";
import {TemplateList} from "./TemplateConstants"; import {TemplateList} from "./TemplateConstants";
import {applyFilters, applySorters, parseQuery, printQuery, wrapFiltersInAnd} from "./graphQLmodifier"; import {generateTemplate} from "./graphQLmodifier";
const server = process.env.REACT_APP_REPORTS_SERVER_URL; const server = process.env.REACT_APP_REPORTS_SERVER_URL;
@@ -278,7 +278,9 @@ export const GenerateDocument = async (
sendType, sendType,
jobid jobid
) => { ) => {
const bodyshop = store.getState().user.bodyshop; const bodyshop = store.getState().user.bodyshop;
if (sendType === "e") { if (sendType === "e") {
store.dispatch( store.dispatch(
setEmailOptions({ setEmailOptions({
@@ -404,7 +406,7 @@ const fetchContextData = async (templateObject, jsrAuth) => {
// We have no template filters or sorters, so we can just execute the query and return the data // We have no template filters or sorters, so we can just execute the query and return the data
if ((!templateObject?.filters && !templateObject?.filters?.length && !templateObject?.sorters && !templateObject?.sorters?.length)) { if (!(templateObject?.filters?.length || templateObject?.sorters?.length)) {
let contextData = {}; let contextData = {};
if (templateQueryToExecute) { if (templateQueryToExecute) {
const {data} = await client.query({ const {data} = await client.query({
@@ -417,36 +419,11 @@ const fetchContextData = async (templateObject, jsrAuth) => {
return {contextData, useShopSpecificTemplate}; return {contextData, useShopSpecificTemplate};
} }
// Parse the query and apply the filters and sorters return await generateTemplate(
const ast = parseQuery(templateQueryToExecute); templateQueryToExecute,
templateObject,
let filterFields = []; useShopSpecificTemplate
);
if (templateObject?.filters && templateObject?.filters?.length) {
applyFilters(ast, templateObject.filters, filterFields);
wrapFiltersInAnd(ast, filterFields);
}
if (templateObject?.sorters && templateObject?.sorters?.length) {
applySorters(ast, templateObject.sorters);
}
const finalQuery = printQuery(ast);
// commented out for future revision debugging
// console.log('Modified Query');
// console.log(finalQuery);
let contextData = {};
if (templateQueryToExecute) {
const {data} = await client.query({
query: gql(finalQuery),
variables: {...templateObject.variables},
});
contextData = data;
}
return {contextData, useShopSpecificTemplate};
}; };
//export const displayTemplateInWindow = (html) => { //export const displayTemplateInWindow = (html) => {

View File

@@ -1,4 +1,6 @@
import {Kind, parse, print, visit} from "graphql"; import {Kind, parse, print, visit} from "graphql";
import client from "./GraphQLClient";
import {gql} from "@apollo/client";
const STRING_OPERATORS = [ const STRING_OPERATORS = [
{value: "_eq", label: "equals"}, {value: "_eq", label: "equals"},
@@ -25,16 +27,23 @@ const ORDER_BY_OPERATORS = [
* Get the available operators for filtering * Get the available operators for filtering
* @returns {[{label: string, value: string},{label: string, value: string}]} * @returns {[{label: string, value: string},{label: string, value: string}]}
*/ */
export function getOrderByOperators() { export function getOrderOperatorsByType() {
return ORDER_BY_OPERATORS; return ORDER_BY_OPERATORS;
} }
export function generateReflections(reflector) {
// const type = reflector?.type;
// const name = reflector?.name;
return [];
}
/** /**
* Get the available operators for filtering * Get the available operators for filtering
* @param type * @param type
* @returns {[{label: string, value: string},{label: string, value: string},{label: string, value: string},{label: string, value: string},{label: string, value: string},null]} * @returns {[{label: string, value: string},{label: string, value: string},{label: string, value: string},{label: string, value: string},{label: string, value: string},null]}
*/ */
export function getOperatorsByType(type = 'string') { export function getWhereOperatorsByType(type = 'string') {
const operators = { const operators = {
string: STRING_OPERATORS, string: STRING_OPERATORS,
number: NUMBER_OPERATORS number: NUMBER_OPERATORS
@@ -61,6 +70,50 @@ export function parseQuery(query) {
export function printQuery(query) { export function printQuery(query) {
return print(query); return print(query);
} }
/**
* Generate a template based on the query and object
* @param templateQueryToExecute
* @param templateObject
* @param useShopSpecificTemplate
* @returns {Promise<{contextData: {}, useShopSpecificTemplate}>}
*/
export async function generateTemplate(templateQueryToExecute, templateObject, useShopSpecificTemplate) {
// Advanced Filtering and Sorting modifications start here
// Parse the query and apply the filters and sorters
const ast = parseQuery(templateQueryToExecute);
let filterFields = [];
if (templateObject?.filters && templateObject?.filters?.length) {
applyFilters(ast, templateObject.filters, filterFields);
wrapFiltersInAnd(ast, filterFields);
}
if (templateObject?.sorters && templateObject?.sorters?.length) {
applySorters(ast, templateObject.sorters);
}
const finalQuery = printQuery(ast);
// commented out for future revision debugging
// console.log('Modified Query');
// console.log(finalQuery);
let contextData = {};
if (templateQueryToExecute) {
const {data} = await client.query({
query: gql(finalQuery),
variables: {...templateObject.variables},
});
contextData = data;
}
return {contextData, useShopSpecificTemplate};
}
/** /**
* Apply sorters to the AST * Apply sorters to the AST
* @param ast * @param ast
@@ -278,8 +331,6 @@ export function applyFilters(ast, filters) {
}); });
} }
/** /**
* Get the GraphQL kind for a value * Get the GraphQL kind for a value
* @param value * @param value