BOD-34 Moved template rendering to utility class. Added basic config for some estimate reports.

This commit is contained in:
Patrick Fic
2020-04-28 16:41:36 -07:00
parent 113bf3f0fb
commit c6a29ba417
14 changed files with 396 additions and 133 deletions

View File

@@ -0,0 +1,37 @@
import { gql } from "apollo-boost";
import { QUERY_TEMPLATES_BY_NAME } from "../graphql/templates.queries";
import axios from "axios";
export default async function RenderTemplate(templateObject, client, bodyshop) {
const { data: templateRecords } = await client.query({
query: QUERY_TEMPLATES_BY_NAME,
variables: { name: templateObject.name },
fetchPolicy: "network-only",
});
let templateToUse;
if (templateRecords.templates.length === 1) {
templateToUse = templateRecords.templates[0];
} else if (templateRecords.templates.length === 2) {
templateToUse = templateRecords.templates.filter((t) => !!t.bodyshopid);
} else {
//No template found.Uh oh.
alert("Template key does not exist.");
throw new Error("Template key does not exist.");
}
const { data: contextData } = await client.query({
query: gql(templateToUse.query),
variables: { ...templateObject.variables },
fetchPolicy: "network-only",
});
const { data } = await axios.post("/render", {
view: templateToUse.html,
context: { ...contextData, bodyshop: bodyshop },
});
return new Promise((resolve, reject) => {
resolve(data);
});
}