Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Let surveyors view past surveys in read-only mode, display embedded map on survey address card #506

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 26 additions & 5 deletions frontend/front/src/components/AddressUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,32 @@ export const AddressComponent = ({ home }) => {
return (
<Card sx={{ margin: "1em" }}>
<CardContent>
<address>
<h3>{"Address:"}</h3>
<div>{line1}</div>
<div>{line2}</div>
</address>
<div
style={{
display: "flex",
flexDirection: "row",
flexWrap: "wrap-reverse",
justifyContent: "center",
alignContent: "center",
}}
>
<div style={{ flex: 1, display: "inline-block" }}>
<address>
<h3>{"Address:"}</h3>
<div>{line1}</div>
<div>{line2}</div>
</address>
</div>

<iframe
title="Address map"
src={`https://maps.google.com/maps?q=${home.latitude},${home.longitude}&output=embed`}
style={{ border: 0, display: "inline-block" }}
allowfullscreen=""
loading="lazy"
referrerpolicy="no-referrer-when-downgrade"
></iframe>
</div>
</CardContent>
</Card>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export const HeatPumpAddressField = ({ control, label, disabled }) => {
message: "Invalid street number.",
},
}}
disabled={disabled}
render={({ field }) => (
<TextField
fullWidth
Expand All @@ -51,7 +52,6 @@ export const HeatPumpAddressField = ({ control, label, disabled }) => {
!!formState.errors.address?.street_number &&
formState.errors.address?.street_number.message
}
disabled={disabled}
autoComplete="address-line1"
/>
)}
Expand All @@ -68,6 +68,7 @@ export const HeatPumpAddressField = ({ control, label, disabled }) => {
message: "This field is required!",
},
}}
disabled={disabled}
render={({ field }) => (
<TextField
fullWidth
Expand All @@ -79,7 +80,6 @@ export const HeatPumpAddressField = ({ control, label, disabled }) => {
!!formState.errors.address?.street_name &&
formState.errors.address?.street_name.message
}
disabled={disabled}
autoComplete="address-line1"
/>
)}
Expand All @@ -96,6 +96,7 @@ export const HeatPumpAddressField = ({ control, label, disabled }) => {
message: "Invalid unit number.",
},
}}
disabled={disabled}
render={({ field }) => (
<TextField
fullWidth
Expand All @@ -107,7 +108,6 @@ export const HeatPumpAddressField = ({ control, label, disabled }) => {
!!formState.errors.address?.unit_number &&
formState.errors.address?.unit_number.message
}
disabled={disabled}
autoComplete="address-line2"
/>
)}
Expand All @@ -124,6 +124,7 @@ export const HeatPumpAddressField = ({ control, label, disabled }) => {
message: "This field is required!",
},
}}
disabled={disabled}
render={({ field }) => (
<TextField
fullWidth
Expand All @@ -135,7 +136,6 @@ export const HeatPumpAddressField = ({ control, label, disabled }) => {
!!formState.errors.address?.city &&
formState.errors.address?.city.message
}
disabled={disabled}
autoComplete="address-level2"
/>
)}
Expand All @@ -152,6 +152,7 @@ export const HeatPumpAddressField = ({ control, label, disabled }) => {
message: "This field is required!",
},
}}
disabled={disabled}
render={({ field }) => (
<TextField
fullWidth
Expand All @@ -163,7 +164,6 @@ export const HeatPumpAddressField = ({ control, label, disabled }) => {
!!formState.errors.address?.state &&
formState.errors.address?.state.message
}
disabled={disabled}
autoComplete="address-level1"
/>
)}
Expand All @@ -184,6 +184,7 @@ export const HeatPumpAddressField = ({ control, label, disabled }) => {
message: "Invalid ZIP code.",
},
}}
disabled={disabled}
render={({ field }) => (
<TextField
fullWidth
Expand All @@ -195,7 +196,6 @@ export const HeatPumpAddressField = ({ control, label, disabled }) => {
!!formState.errors.address?.zip_code &&
formState.errors.address?.zip_code.message
}
disabled={disabled}
autoComplete="postal-code"
/>
)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ export const HeatPumpDropdown = ({
name={`${name}-dropdown`}
aria-labelledby={`${name}-dropdown-label`}
variant="filled"
disabled={disabled}
inputProps={{
id: `${name}-dropdown`,
}}
Expand All @@ -65,7 +64,7 @@ export const HeatPumpDropdown = ({
</FormControl>
);
},
[name, options, label, mainFieldError, disabled]
[name, options, label, mainFieldError]
);

return (
Expand All @@ -79,11 +78,13 @@ export const HeatPumpDropdown = ({
}
}
render={({ field }) => renderDropdown(field)}
disabled={disabled}
/>
{showOtherInput && (
<Controller
name={otherFieldName}
control={control}
disabled={disabled}
rules={
required && {
required: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export const HeatPumpNameField = ({ control, label, disabled }) => {
message: "This field is required!",
},
}}
disabled={disabled}
render={({ field }) => (
<TextField
label="First Name"
Expand All @@ -35,7 +36,6 @@ export const HeatPumpNameField = ({ control, label, disabled }) => {
!!formState.errors.name?.first &&
formState.errors.name?.first.message
}
disabled={disabled}
/>
)}
/>
Expand All @@ -49,6 +49,7 @@ export const HeatPumpNameField = ({ control, label, disabled }) => {
message: "This field is required!",
},
}}
disabled={disabled}
render={({ field }) => (
<TextField
label="Last Name"
Expand All @@ -60,7 +61,6 @@ export const HeatPumpNameField = ({ control, label, disabled }) => {
!!formState.errors.name?.last &&
formState.errors.name?.last.message
}
disabled={disabled}
/>
)}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ export const HeatPumpPhoneField = ({ name, control, label, disabled }) => {
message: "Invalid phone number.",
},
}}
disabled={disabled}
render={({ field }) => (
<TextField
id={`${name}-phoneNumber`}
label={label}
variant="standard"
error={!!mainFieldError}
helperText={!!mainFieldError && mainFieldError.message}
disabled={disabled}
type="tel"
{...field}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export const HeatPumpTextField = ({
name={name}
control={control}
rules={rules}
disabled={disabled}
render={({ field }) => (
<FormControl fullWidth>
{disableFancyLabel && (
Expand All @@ -67,7 +68,6 @@ export const HeatPumpTextField = ({
helperText={
!!formState.errors[name] && formState.errors[name].message
}
disabled={disabled}
type={inputType}
inputProps={{ readOnly, id: `${name}-textField` }}
{...field}
Expand Down
25 changes: 14 additions & 11 deletions frontend/front/src/components/SurveyComponent/SurveyComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const SurveyComponent = ({
formDefault,
surveyStructure,
onDelete,
readonly,
}) => {
const navigate = useNavigate();

Expand All @@ -46,8 +47,8 @@ const SurveyComponent = ({
const [isDeleteModalOpen, setIsDeleteModalOpen] = useState(false);

const isDisabled = useMemo(
() => isEditable && !isEditing,
[isEditing, isEditable]
() => (isEditable && !isEditing) || readonly,
[isEditing, isEditable, readonly]
);

const cacheKey = useMemo(
Expand All @@ -69,12 +70,13 @@ const SurveyComponent = ({
// useEffect to set the default data for the form
// add in cached data here instead of in formDefault so that clicking "clear" doesn't treat the cache as default
useEffect(() => {
const cacheOrDefault = cachedData || formDefault;
const cacheOrDefault =
cachedData && !readonly && !isEditable ? cachedData : formDefault;

if (cacheOrDefault) {
reset(cacheOrDefault);
}
}, [cachedData, formDefault, reset]);
}, [cachedData, formDefault, isEditable, readonly, reset]);

useEffect(() => {
// function passed to watch is executed every time the form data changes
Expand Down Expand Up @@ -208,7 +210,7 @@ const SurveyComponent = ({
key={`q${q.id}`}
control={control}
name={`${q.id}`}
label={q.text}
label={q.question}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The field for the survey question "question" changed from text to question at some point...

options={q.response_options.map((o) => ({
value: o,
label: o,
Expand All @@ -223,7 +225,7 @@ const SurveyComponent = ({
key={`q${q.id}`}
control={control}
name={`${q.id}`}
label={q.text}
label={q.question}
disabled={isDisabled}
disableFancyLabel
/>
Expand All @@ -239,11 +241,12 @@ const SurveyComponent = ({
})}
<Stack direction="row" justifyContent="center" spacing={2}>
{isLoading && <Loader />}
{isEditable
? isEditing
? adminButtonsEditing()
: adminButtonsViewing()
: commonButtonSection()}
{!readonly &&
(isEditable
? isEditing
? adminButtonsEditing()
: adminButtonsViewing()
: commonButtonSection())}
</Stack>
</Stack>
</form>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,35 @@ describe("SurveyComponent", () => {
);
});

it("cache should not be used for readonly survey", () => {
localStorage.setItem(
DEFAULT_TEST_SURVEY_CACHE_KEY,
JSON.stringify({
...DEFAULT_TEST_DEFAULT_SURVEY_DATA,
3: "some text",
})
);

render(
<SurveyComponent
submitSurvey={jest.fn()}
isLoading={false}
activeHome={DEFAULT_TEST_HOME}
isEditable={false}
surveyId={DEFAULT_TEST_SURVEY.id}
formSpacing={5}
readonly
/>,
{}
);

// eslint-disable-next-line testing-library/no-node-access
expect(screen.getByTestId("3").querySelector("input")).not.toHaveAttribute(
"value",
"some text"
);
});

it("should cache data when form is edited", () => {
render(
<SurveyComponent
Expand Down