BOD-34 Moved template rendering to utility class. Added basic config for some estimate reports.
This commit is contained in:
37
client/src/utils/RenderTemplate.js
Normal file
37
client/src/utils/RenderTemplate.js
Normal 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);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user