Skip to content

Commit

Permalink
602 remove email edit (#608)
Browse files Browse the repository at this point in the history
* Change id in token from user id to surveyor id

* Create util to transform backend keys and add to apiSlice

* Switch EditAccount page to component and connect to backend

* Remove email inputs and change spacing
  • Loading branch information
Andrewy-gh authored Aug 22, 2024
1 parent 9164524 commit 45a198f
Show file tree
Hide file tree
Showing 9 changed files with 343 additions and 283 deletions.
3 changes: 3 additions & 0 deletions frontend/front/src/api/apiSlice.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { createApi, fetchBaseQuery } from "@reduxjs/toolkit/query/react";

import { AUTHORIZATION_HEADER } from "../features/login/loginUtils";
import { transformSurveyorKeys } from "../features/surveyor/surveyorUtils";

const baseUrl = process.env.REACT_APP_API_URL || "http://localhost:3000";

Expand Down Expand Up @@ -98,6 +99,7 @@ export const apiSlice = createApi({
// }),
getSurveyor: builder.query({
query: (id) => `/surveyors/${id}`,
transformResponse: (result) => result && transformSurveyorKeys(result),
providesTags: (result, error, arg) => [{ type: "Surveyor", id: arg }],
}),
createSurveyor: builder.mutation({
Expand All @@ -114,6 +116,7 @@ export const apiSlice = createApi({
method: "PUT",
body: surveyor,
}),
transformResponse: (result) => result && transformSurveyorKeys(result),
invalidatesTags: (result, error, arg) => [
{ type: "Surveyor", id: arg.id },
],
Expand Down
4 changes: 2 additions & 2 deletions frontend/front/src/features/login/loginUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ export const useLocallyStoredJWT = () => {

export const decodeJwt = (token) => {
// "sub" = "subject", aka the user id
const { sub, email, role } = jwt_decode(token);
return { id: sub, email, role };
const { surveyorId, sub, email, role } = jwt_decode(token);
return { id: surveyorId, userId: sub, email, role };
};

export const generatePassword = () =>
Expand Down
15 changes: 15 additions & 0 deletions frontend/front/src/features/surveyor/surveyorUtils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* Transform backend surveyor keys to match surveyor form name fields
*/
export const transformSurveyorKeys = (data) => {
if (!data) return null;

const { firstname, lastname, street_address, zipcode, ...rest } = data;
return {
...rest,
firstName: firstname,
lastName: lastname,
streetAddress: street_address,
zipCode: zipcode,
};
};
60 changes: 23 additions & 37 deletions frontend/front/src/pages/Admin/user/UserProfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ const UserProfile = () => {
defaultValues: {
firstName: "",
lastName: "",
email: "",
phone: "",
streetAddress: "",
city: "",
Expand All @@ -57,10 +56,10 @@ const UserProfile = () => {
...surveyorData,
firstname: data.firstName,
lastname: data.lastName,
email: data.email,
phone: data.phone,
street_address: data.street_address,
street_address: data.streetAddress,
city: data.city,
zipcode: data.zipCode,
};
updateSurveyor(newSurveyorData)
.unwrap()
Expand Down Expand Up @@ -127,13 +126,12 @@ const UserProfile = () => {
useEffect(() => {
if (surveyorData) {
reset({
firstName: surveyorData.firstname,
lastName: surveyorData.lastname,
email: surveyorData.email,
firstName: surveyorData.firstName,
lastName: surveyorData.lastName,
phone: surveyorData.phone,
streetAddress: surveyorData.street_address,
streetAddress: surveyorData.streetAddress,
city: surveyorData.city,
zipCode: surveyorData.zipcode,
zipCode: surveyorData.zipCode,
state: surveyorData.state,
});
}
Expand Down Expand Up @@ -226,9 +224,11 @@ const UserProfile = () => {
title="Confirm Deactivation"
message="Please confirm to deactivate this user."
/>
<Box maxWidth={500} mt={5}>
<Box sx={{ bgcolor: "primary.main", color: "white" }} p={1}>
<Typography variant="h5">User Profile: {uid}</Typography>
<Box maxWidth={500} my={5} px={2}>
<Box sx={{ bgcolor: "primary.main", color: "white" }} p={2}>
<Typography variant="h5" mb={1}>
User Profile: {surveyorData.email}
</Typography>
<Typography variant="h6">
Status: {surveyorData.status}
</Typography>
Expand All @@ -247,7 +247,7 @@ const UserProfile = () => {
value={value}
label="First Name"
variant="standard"
sx={{ width: "95%", mx: 2, mt: 3 }}
sx={{ width: "95%", mx: 1, mt: 3 }}
/>
)}
/>
Expand All @@ -261,21 +261,7 @@ const UserProfile = () => {
value={value}
label="Last Name"
variant="standard"
sx={{ width: "95%", mx: 2, mt: 3 }}
/>
)}
/>
<Controller
name={"email"}
control={control}
render={({ field: { onChange, value } }) => (
<TextField
disabled={!editMode}
onChange={onChange}
value={value}
label="Email"
variant="standard"
sx={{ width: "95%", mx: 2, mt: 3 }}
sx={{ width: "95%", mx: 1, mt: 3 }}
/>
)}
/>
Expand All @@ -286,10 +272,10 @@ const UserProfile = () => {
<TextField
disabled={!editMode}
onChange={onChange}
value={surveyorData?.phone || value}
value={value}
label="Phone"
variant="standard"
sx={{ width: "95%", mx: 2, mt: 3 }}
sx={{ width: "95%", mx: 1, mt: 3 }}
/>
)}
/>
Expand All @@ -303,7 +289,7 @@ const UserProfile = () => {
value={value}
label="Street Address"
variant="standard"
sx={{ width: "95%", mx: 2, mt: 3 }}
sx={{ width: "95%", mx: 1, mt: 3 }}
/>
)}
/>
Expand All @@ -317,35 +303,35 @@ const UserProfile = () => {
value={value}
label="City"
variant="standard"
sx={{ width: "95%", mx: 2, mt: 3 }}
sx={{ width: "95%", mx: 1, mt: 3 }}
/>
)}
/>
<Controller
name={"zipCode"}
name={"state"}
control={control}
render={({ field: { onChange, value } }) => (
<TextField
disabled={!editMode}
onChange={onChange}
value={value}
label="Zip Code"
label="State"
variant="standard"
sx={{ width: "95%", mx: 2, mt: 3 }}
sx={{ width: "95%", mx: 1, mt: 3 }}
/>
)}
/>
<Controller
name={"state"}
name={"zipCode"}
control={control}
render={({ field: { onChange, value } }) => (
<TextField
disabled={!editMode}
onChange={onChange}
value={value}
label="State"
label="Zip Code"
variant="standard"
sx={{ width: "95%", mx: 2, mt: 3 }}
sx={{ width: "95%", mx: 1, mt: 3 }}
/>
)}
/>
Expand Down
Loading

0 comments on commit 45a198f

Please sign in to comment.