From 0d979cdbd767c3dd559a6a925a2c5bbaf1a4af4f Mon Sep 17 00:00:00 2001 From: Andrew Date: Tue, 24 Sep 2024 20:04:58 -0400 Subject: [PATCH] 613 fix survey format (#621) * Add new response_types to survey_question and survey component. * Update jsonserver data * Update snapshot * Update webrick --- backend/Gemfile.lock | 2 +- backend/app/models/survey_question.rb | 2 +- backend/lib/seeds/test_survey_questions.csv | 12 +-- .../SurveyComponent/HeatPumpPhoneField.js | 79 +++++++++++++------ .../SurveyComponent/SurveyComponent.js | 18 +++++ .../SurveyComponent.test.js.snap | 10 +-- frontend/jsonserver/data/survey_index.json | 8 +- frontend/jsonserver/data/survey_show.json | 8 +- 8 files changed, 95 insertions(+), 44 deletions(-) diff --git a/backend/Gemfile.lock b/backend/Gemfile.lock index b00d055d..77b32326 100644 --- a/backend/Gemfile.lock +++ b/backend/Gemfile.lock @@ -311,7 +311,7 @@ GEM activemodel (>= 6.0.0) bindex (>= 0.4.0) railties (>= 6.0.0) - webrick (1.8.1) + webrick (1.8.2) websocket-driver (0.7.6) websocket-extensions (>= 0.1.0) websocket-extensions (0.1.5) diff --git a/backend/app/models/survey_question.rb b/backend/app/models/survey_question.rb index 15867f58..593a88db 100644 --- a/backend/app/models/survey_question.rb +++ b/backend/app/models/survey_question.rb @@ -2,7 +2,7 @@ class SurveyQuestion < ApplicationRecord belongs_to :survey - enum response_type: { radio: 0, text: 1 } + enum response_type: { radio: 0, text: 1, email: 2, tel: 3 } has_many :localized_survey_questions, dependent: nil accepts_nested_attributes_for :localized_survey_questions diff --git a/backend/lib/seeds/test_survey_questions.csv b/backend/lib/seeds/test_survey_questions.csv index 30145f56..eab58412 100644 --- a/backend/lib/seeds/test_survey_questions.csv +++ b/backend/lib/seeds/test_survey_questions.csv @@ -2,17 +2,17 @@ display_order,text,response_type,response_options 1,"Have you heard of heat pumps?",0,"Yes/No" 2,"If yes, have you considered them for home heating/cooling?",0,"Yes/No" 3,"If yes, how far have you taken your interest in heat pumps?",1,"" -4,"Have you heard of Mass Save… the energy efficieny program?",0,"Yes/No" +4,"Have you heard of Mass Save… the energy efficiency program?",0,"Yes/No" 5,"If yes, have you had contact with them?",0,"Yes/No" 6,"If you could save money on your utility bills with a state-discounted or free heating/cooling system upgrade, would you like to learn more?",0,"Yes/No" 7,"If yes, please confirm your name",1,"" -8,"If yes, please confirm your email address",1,"" -9,"If yes, please confirm your phone number",1,"" -10,"So we can report back on your potential savings, what type of heating system do you have? (Across the whole home, not just in individual rooms)",0,"Forced hot air – vents/Radiators – water/Radiators – steam/Basebaord – water/Baseboard – electric/Other" +8,"If yes, please confirm your email address",2,"" +9,"If yes, please confirm your phone number",3,"" +10,"So we can report back on your potential savings, what type of heating system do you have? (Across the whole home, not just in individual rooms)",0,"Forced hot air – vents/Radiators – water/Radiators – steam/Baseboard – water/Baseboard – electric/Other" 11,"If other, please specify",1,"" 12,"What type of heating FUEL do you use for that heating system?",0,"Oil/Gas/Electric (for baseboard)/Other" 13,"If other, please specify",1,"" 14,"We expect to have “heat pump coaches” available to walk through the process with you (volunteers, who are not affiliated with any installer or brand). Would you like to know more about how to get free, personal help from a heat pump coach?",0,"Yes/No" 15,"If yes, please confirm your name",1,"" -16,"If yes, please confirm your email address",1,"" -17,"If yes, please confirm your phone number",1,"" +16,"If yes, please confirm your email address",2,"" +17,"If yes, please confirm your phone number",3,"" diff --git a/frontend/front/src/components/SurveyComponent/HeatPumpPhoneField.js b/frontend/front/src/components/SurveyComponent/HeatPumpPhoneField.js index 9a446369..9072fce4 100644 --- a/frontend/front/src/components/SurveyComponent/HeatPumpPhoneField.js +++ b/frontend/front/src/components/SurveyComponent/HeatPumpPhoneField.js @@ -1,35 +1,70 @@ import { Controller, useController } from "react-hook-form"; +import { useMemo } from "react"; +import { FormControl, FormLabel, TextField } from "@mui/material"; -import React from "react"; -import { TextField } from "@mui/material"; - -export const HeatPumpPhoneField = ({ name, control, label, disabled }) => { +export const HeatPumpPhoneField = ({ + name, + control, + label, + disabled, + required, + readOnly, + disableFancyLabel, + styles = {}, +}) => { const { formState } = useController({ name, control }); const mainFieldError = formState.errors[name]; + const rules = useMemo(() => { + const ruleList = {}; + if (required) { + ruleList.required = { value: true, message: "This field is required!" }; + } + ruleList.pattern = { + value: /^\(?([0-9]{3})\)?[-.\s]?([0-9]{3})[-.\s]?([0-9]{4})$/, + message: "Invalid phone number.", + }; + return ruleList; + }, [required]); return ( ( - + + {disableFancyLabel && ( + + {label} + + )} + + )} /> ); diff --git a/frontend/front/src/components/SurveyComponent/SurveyComponent.js b/frontend/front/src/components/SurveyComponent/SurveyComponent.js index dea45bcf..3213146e 100644 --- a/frontend/front/src/components/SurveyComponent/SurveyComponent.js +++ b/frontend/front/src/components/SurveyComponent/SurveyComponent.js @@ -18,6 +18,7 @@ import { import { AddressComponent } from "../AddressUtils"; import Loader from "../Loader"; import ConditionalQuestion from "./ConditionalQuestion"; +import { HeatPumpPhoneField } from "./HeatPumpPhoneField"; import { HeatPumpRadio } from "./HeatPumpRadio"; import { HeatPumpTextField } from "./HeatPumpTextField"; import { SurveyError } from "./SurveyStructureError"; @@ -205,9 +206,11 @@ const SurveyComponent = ({ }))} disabled={readOnly} styles={styles} + readOnly={readOnly} /> ); case "text": + case "email": return ( + ); + case "tel": + return ( + ); default: diff --git a/frontend/front/src/components/SurveyComponent/__tests__/__snapshots__/SurveyComponent.test.js.snap b/frontend/front/src/components/SurveyComponent/__tests__/__snapshots__/SurveyComponent.test.js.snap index cb8048f2..17e73efd 100644 --- a/frontend/front/src/components/SurveyComponent/__tests__/__snapshots__/SurveyComponent.test.js.snap +++ b/frontend/front/src/components/SurveyComponent/__tests__/__snapshots__/SurveyComponent.test.js.snap @@ -653,7 +653,7 @@ exports[`SurveyComponent no errors when rendering 1`] = ` class="MuiInputBase-input MuiInput-input css-1x51dt5-MuiInputBase-input-MuiInput-input" id="8-textField" name="8" - type="text" + type="email" value="" /> @@ -669,7 +669,6 @@ exports[`SurveyComponent no errors when rendering 1`] = ` />
@@ -1404,7 +1403,7 @@ exports[`SurveyComponent no errors when rendering 1`] = ` class="MuiInputBase-input MuiInput-input css-1x51dt5-MuiInputBase-input-MuiInput-input" id="16-textField" name="16" - type="text" + type="email" value="" />
@@ -1420,7 +1419,6 @@ exports[`SurveyComponent no errors when rendering 1`] = ` />
diff --git a/frontend/jsonserver/data/survey_index.json b/frontend/jsonserver/data/survey_index.json index 05ab85d8..3e09e4c8 100644 --- a/frontend/jsonserver/data/survey_index.json +++ b/frontend/jsonserver/data/survey_index.json @@ -56,14 +56,14 @@ "id": 8, "display_order": 8, "text": "If yes, please confirm your email address", - "response_type": "text", + "response_type": "email", "response_options": [] }, { "id": 9, "display_order": 9, "text": "If yes, please confirm your phone number", - "response_type": "text", + "response_type": "tel", "response_options": [] }, { @@ -119,14 +119,14 @@ "id": 16, "display_order": 16, "text": "If yes, please confirm your email address", - "response_type": "text", + "response_type": "email", "response_options": [] }, { "id": 17, "display_order": 17, "text": "If yes, please confirm your phone number", - "response_type": "text", + "response_type": "tel", "response_options": [] } ], diff --git a/frontend/jsonserver/data/survey_show.json b/frontend/jsonserver/data/survey_show.json index 0b80a0ba..5384c37e 100644 --- a/frontend/jsonserver/data/survey_show.json +++ b/frontend/jsonserver/data/survey_show.json @@ -55,14 +55,14 @@ "id": 8, "display_order": 8, "text": "If yes, please confirm your email address", - "response_type": "text", + "response_type": "email", "response_options": [] }, { "id": 9, "display_order": 9, "text": "If yes, please confirm your phone number", - "response_type": "text", + "response_type": "tel", "response_options": [] }, { @@ -118,14 +118,14 @@ "id": 16, "display_order": 16, "text": "If yes, please confirm your email address", - "response_type": "text", + "response_type": "email", "response_options": [] }, { "id": 17, "display_order": 17, "text": "If yes, please confirm your phone number", - "response_type": "text", + "response_type": "tel", "response_options": [] } ],