From d36219368a12e58bdd4475f66e4578752c99c231 Mon Sep 17 00:00:00 2001 From: Patrick Fic Date: Fri, 22 May 2020 15:50:36 -0700 Subject: [PATCH] Added saving of CSI survey + error handling BOD-98 --- bodyshop_translations.babel | 94 ++++++++++ client/src/graphql/csi.queries.js | 7 + client/src/pages/csi/csi.container.page.jsx | 176 ++++++++++-------- client/src/translations/en_us/common.json | 8 + client/src/translations/es/common.json | 8 + client/src/translations/fr/common.json | 8 + .../down.yaml | 5 + .../up.yaml | 5 + .../down.yaml | 11 ++ .../up.yaml | 10 + .../down.yaml | 34 ++++ .../up.yaml | 36 ++++ .../down.yaml | 22 +++ .../up.yaml | 24 +++ hasura/migrations/metadata.yaml | 4 + 15 files changed, 372 insertions(+), 80 deletions(-) create mode 100644 hasura/migrations/1590184694200_alter_table_public_csi_add_column_completed/down.yaml create mode 100644 hasura/migrations/1590184694200_alter_table_public_csi_add_column_completed/up.yaml create mode 100644 hasura/migrations/1590184702149_alter_table_public_csi_alter_column_completed/down.yaml create mode 100644 hasura/migrations/1590184702149_alter_table_public_csi_alter_column_completed/up.yaml create mode 100644 hasura/migrations/1590184719915_update_permission_user_public_table_csi/down.yaml create mode 100644 hasura/migrations/1590184719915_update_permission_user_public_table_csi/up.yaml create mode 100644 hasura/migrations/1590184732654_update_permission_anonymous_public_table_csi/down.yaml create mode 100644 hasura/migrations/1590184732654_update_permission_anonymous_public_table_csi/up.yaml diff --git a/bodyshop_translations.babel b/bodyshop_translations.babel index b8632faa4..3970446ba 100644 --- a/bodyshop_translations.babel +++ b/bodyshop_translations.babel @@ -3807,6 +3807,53 @@ csi + + errors + + + notfoundsubtitle + false + + + + + + en-US + false + + + es-MX + false + + + fr-CA + false + + + + + notfoundtitle + false + + + + + + en-US + false + + + es-MX + false + + + fr-CA + false + + + + + labels @@ -3833,6 +3880,53 @@ + + successes + + + submitted + false + + + + + + en-US + false + + + es-MX + false + + + fr-CA + false + + + + + submittedsub + false + + + + + + en-US + false + + + es-MX + false + + + fr-CA + false + + + + + diff --git a/client/src/graphql/csi.queries.js b/client/src/graphql/csi.queries.js index 8fe0fc742..3d296c915 100644 --- a/client/src/graphql/csi.queries.js +++ b/client/src/graphql/csi.queries.js @@ -14,3 +14,10 @@ export const QUERY_SURVEY = gql` } } `; +export const COMPLETE_SURVEY = gql` + mutation COMPLETE_SURVEY($surveyId: uuid!, $survey: csi_set_input) { + update_csi(where: { id: { _eq: $surveyId } }, _set: $survey) { + affected_rows + } + } +`; diff --git a/client/src/pages/csi/csi.container.page.jsx b/client/src/pages/csi/csi.container.page.jsx index 62c3e901d..97eabe566 100644 --- a/client/src/pages/csi/csi.container.page.jsx +++ b/client/src/pages/csi/csi.container.page.jsx @@ -1,29 +1,73 @@ -import { useQuery } from "@apollo/react-hooks"; -import { Form, Layout, Typography, Button } from "antd"; -import React from "react"; +import { useQuery, useMutation } from "@apollo/react-hooks"; +import { Form, Layout, Typography, Button, Result } from "antd"; +import React, { useState } from "react"; import { useTranslation } from "react-i18next"; import { useParams } from "react-router-dom"; import AlertComponent from "../../components/alert/alert.component"; import ConfigFormComponents from "../../components/config-form-components/config-form-components.component"; import LoadingSpinner from "../../components/loading-spinner/loading-spinner.component"; -import { QUERY_SURVEY } from "../../graphql/csi.queries"; +import { QUERY_SURVEY, COMPLETE_SURVEY } from "../../graphql/csi.queries"; export default function CsiContainerPage() { const { surveyId } = useParams(); const [form] = Form.useForm(); + const [submitting, setSubmitting] = useState({ + loading: false, + submitted: false, + }); + const { loading, error, data } = useQuery(QUERY_SURVEY, { variables: { surveyId }, }); - const { t } = useTranslation(); - if (loading) return ; - if (error) return ; - const handleFinish = (values) => { - console.log("values", values); + const { t } = useTranslation(); + const [completeSurvey] = useMutation(COMPLETE_SURVEY); + if (loading) return ; + + if (error || !!!data.csi_by_pk) + return ( +
+ + {error ? ( +
ERROR: {error.graphQLErrors.map((e) => e.message)}
+ ) : null} +
+
+ ); + + const handleFinish = async (values) => { + setSubmitting({ ...submitting, loading: true }); + + const result = await completeSurvey({ + variables: { + surveyId, + survey: { + response: values, + valid: false, + completedon: new Date(), + }, + }, + }); + + if (!!!result.errors) { + setSubmitting({ ...submitting, loading: false, submitted: true }); + } else { + setSubmitting({ + ...submitting, + loading: false, + error: JSON.stringify(result.errors), + }); + } }; - //const { relateddata } = data.csi_by_pk; - const { bodyshop, job, csiquestion } = relateddata; + const { + relateddata: { bodyshop, job }, + csiquestion: { config: csiquestions }, + } = data.csi_by_pk; + return ( @@ -33,7 +77,7 @@ export default function CsiContainerPage() { flexDirection: "column", alignItems: "center", }}> -
+
{bodyshop.logo_img_path ? ( Logo ) : null} @@ -49,80 +93,52 @@ export default function CsiContainerPage() { {t("csi.labels.title")} {`Hi ${job.ownr_fn || ""}!`} - At {bodyshop.shopname || ""}, we value our customer's feedback. We - would love to hear what you have to say. Please fill out the form - below. + At {bodyshop.shopname || ""}, we value your feedback. We would love to + hear what you have to say. Please fill out the form below.
- -
- - - -
+ {submitting.error ? ( + + ) : null} + + {submitting.submitted ? ( + + + + ) : ( + +
+ + + +
+ )} + Copyright ImEX.Online. Survey ID: {surveyId} ); } - -const relateddata = { - bodyshop: { - logo_img_path: "https://via.placeholder.com/150", - shopname: "Kavia Test Autobody", - address1: "123 Fake St", - address2: "Unit #100", - city: "Vancouver", - state: "BC", - zip_post: "V6B 1M9", - country: "Canada", - email: "snaptsoft@gmail.com", - }, - job: { - id: "1234", - ro_number: "RO102384", - ownr_fn: "Patrick", - v_make_desc: "Toyota", - v_model_desc: "Camry", - v_model_yr: "2019", - }, - csiquestion: [ - { - name: "item1", - type: "checkbox", - label: "Checklist Item 1", - required: true, - }, - { - name: "item2", - type: "slider", - label: "Checklist Item 2", - min: 0, - max: 5, - required: true, - }, - { - name: "item3", - type: "textarea", - label: "Checklist Item 3", - required: true, - }, - { - name: "item4", - type: "text", - label: "Checklist Item 4", - required: true, - }, - { - name: "item5", - type: "rate", - label: "Checklist Item 4", - required: true, - }, - ], -}; diff --git a/client/src/translations/en_us/common.json b/client/src/translations/en_us/common.json index 57167a2d2..2301a3bb6 100644 --- a/client/src/translations/en_us/common.json +++ b/client/src/translations/en_us/common.json @@ -250,8 +250,16 @@ } }, "csi": { + "errors": { + "notfoundsubtitle": "We were unable to find a survey using the link you provided. Please ensure the URL is correct or reach out to your shop for more help.", + "notfoundtitle": "No survey found." + }, "labels": { "title": "Customer Satisfaction Survey" + }, + "successes": { + "submitted": "Your responses have been submitted successfully.", + "submittedsub": "Your input is highly appreciated." } }, "documents": { diff --git a/client/src/translations/es/common.json b/client/src/translations/es/common.json index 2de7cdceb..7311933a7 100644 --- a/client/src/translations/es/common.json +++ b/client/src/translations/es/common.json @@ -250,8 +250,16 @@ } }, "csi": { + "errors": { + "notfoundsubtitle": "", + "notfoundtitle": "" + }, "labels": { "title": "" + }, + "successes": { + "submitted": "", + "submittedsub": "" } }, "documents": { diff --git a/client/src/translations/fr/common.json b/client/src/translations/fr/common.json index 80ee8df55..85da028cf 100644 --- a/client/src/translations/fr/common.json +++ b/client/src/translations/fr/common.json @@ -250,8 +250,16 @@ } }, "csi": { + "errors": { + "notfoundsubtitle": "", + "notfoundtitle": "" + }, "labels": { "title": "" + }, + "successes": { + "submitted": "", + "submittedsub": "" } }, "documents": { diff --git a/hasura/migrations/1590184694200_alter_table_public_csi_add_column_completed/down.yaml b/hasura/migrations/1590184694200_alter_table_public_csi_add_column_completed/down.yaml new file mode 100644 index 000000000..ba51b40ae --- /dev/null +++ b/hasura/migrations/1590184694200_alter_table_public_csi_add_column_completed/down.yaml @@ -0,0 +1,5 @@ +- args: + cascade: false + read_only: false + sql: ALTER TABLE "public"."csi" DROP COLUMN "completed"; + type: run_sql diff --git a/hasura/migrations/1590184694200_alter_table_public_csi_add_column_completed/up.yaml b/hasura/migrations/1590184694200_alter_table_public_csi_add_column_completed/up.yaml new file mode 100644 index 000000000..98289ecd3 --- /dev/null +++ b/hasura/migrations/1590184694200_alter_table_public_csi_add_column_completed/up.yaml @@ -0,0 +1,5 @@ +- args: + cascade: false + read_only: false + sql: ALTER TABLE "public"."csi" ADD COLUMN "completed" timestamptz NULL; + type: run_sql diff --git a/hasura/migrations/1590184702149_alter_table_public_csi_alter_column_completed/down.yaml b/hasura/migrations/1590184702149_alter_table_public_csi_alter_column_completed/down.yaml new file mode 100644 index 000000000..6b99c635c --- /dev/null +++ b/hasura/migrations/1590184702149_alter_table_public_csi_alter_column_completed/down.yaml @@ -0,0 +1,11 @@ +- args: + cascade: false + read_only: false + sql: ALTER TABLE "public"."csi" ALTER COLUMN "completed" TYPE timestamp with time + zone; + type: run_sql +- args: + cascade: false + read_only: false + sql: alter table "public"."csi" rename column "completedon" to "completed"; + type: run_sql diff --git a/hasura/migrations/1590184702149_alter_table_public_csi_alter_column_completed/up.yaml b/hasura/migrations/1590184702149_alter_table_public_csi_alter_column_completed/up.yaml new file mode 100644 index 000000000..b9953727f --- /dev/null +++ b/hasura/migrations/1590184702149_alter_table_public_csi_alter_column_completed/up.yaml @@ -0,0 +1,10 @@ +- args: + cascade: false + read_only: false + sql: ALTER TABLE "public"."csi" ALTER COLUMN "completed" TYPE timestamptz; + type: run_sql +- args: + cascade: false + read_only: false + sql: alter table "public"."csi" rename column "completed" to "completedon"; + type: run_sql diff --git a/hasura/migrations/1590184719915_update_permission_user_public_table_csi/down.yaml b/hasura/migrations/1590184719915_update_permission_user_public_table_csi/down.yaml new file mode 100644 index 000000000..ee974fbb7 --- /dev/null +++ b/hasura/migrations/1590184719915_update_permission_user_public_table_csi/down.yaml @@ -0,0 +1,34 @@ +- args: + role: user + table: + name: csi + schema: public + type: drop_select_permission +- args: + permission: + allow_aggregations: false + columns: + - bodyshopid + - created_at + - id + - jobid + - relateddata + - response + - updated_at + - valid + - validuntil + computed_fields: [] + filter: + bodyshop: + associations: + _and: + - user: + authid: + _eq: X-Hasura-User-Id + - active: + _eq: true + role: user + table: + name: csi + schema: public + type: create_select_permission diff --git a/hasura/migrations/1590184719915_update_permission_user_public_table_csi/up.yaml b/hasura/migrations/1590184719915_update_permission_user_public_table_csi/up.yaml new file mode 100644 index 000000000..a45ce22ba --- /dev/null +++ b/hasura/migrations/1590184719915_update_permission_user_public_table_csi/up.yaml @@ -0,0 +1,36 @@ +- args: + role: user + table: + name: csi + schema: public + type: drop_select_permission +- args: + permission: + allow_aggregations: false + columns: + - bodyshopid + - completedon + - created_at + - id + - jobid + - questionset + - relateddata + - response + - updated_at + - valid + - validuntil + computed_fields: [] + filter: + bodyshop: + associations: + _and: + - user: + authid: + _eq: X-Hasura-User-Id + - active: + _eq: true + role: user + table: + name: csi + schema: public + type: create_select_permission diff --git a/hasura/migrations/1590184732654_update_permission_anonymous_public_table_csi/down.yaml b/hasura/migrations/1590184732654_update_permission_anonymous_public_table_csi/down.yaml new file mode 100644 index 000000000..bae48de62 --- /dev/null +++ b/hasura/migrations/1590184732654_update_permission_anonymous_public_table_csi/down.yaml @@ -0,0 +1,22 @@ +- args: + role: anonymous + table: + name: csi + schema: public + type: drop_update_permission +- args: + permission: + columns: + - response + filter: + valid: + _eq: true + localPresets: + - key: "" + value: "" + set: {} + role: anonymous + table: + name: csi + schema: public + type: create_update_permission diff --git a/hasura/migrations/1590184732654_update_permission_anonymous_public_table_csi/up.yaml b/hasura/migrations/1590184732654_update_permission_anonymous_public_table_csi/up.yaml new file mode 100644 index 000000000..f12bda76e --- /dev/null +++ b/hasura/migrations/1590184732654_update_permission_anonymous_public_table_csi/up.yaml @@ -0,0 +1,24 @@ +- args: + role: anonymous + table: + name: csi + schema: public + type: drop_update_permission +- args: + permission: + columns: + - completedon + - response + - valid + filter: + valid: + _eq: true + localPresets: + - key: "" + value: "" + set: {} + role: anonymous + table: + name: csi + schema: public + type: create_update_permission diff --git a/hasura/migrations/metadata.yaml b/hasura/migrations/metadata.yaml index c0d3e00c3..b5928c964 100644 --- a/hasura/migrations/metadata.yaml +++ b/hasura/migrations/metadata.yaml @@ -933,9 +933,11 @@ tables: permission: columns: - bodyshopid + - completedon - created_at - id - jobid + - questionset - relateddata - response - updated_at @@ -954,7 +956,9 @@ tables: - role: anonymous permission: columns: + - completedon - response + - valid filter: valid: _eq: true