From c1229e43aa2d31cf95625e7311ffeb54984d993a Mon Sep 17 00:00:00 2001 From: Brenda Nyokabi Bucha Date: Wed, 16 Mar 2022 15:07:57 +0300 Subject: [PATCH 01/23] Add prop --- src/components/ActNowPage/LoggedIn.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/components/ActNowPage/LoggedIn.js b/src/components/ActNowPage/LoggedIn.js index 93d5cc0c..59f5ca38 100644 --- a/src/components/ActNowPage/LoggedIn.js +++ b/src/components/ActNowPage/LoggedIn.js @@ -17,6 +17,7 @@ function ActNowLoggedInPage({ footer, title, navigation, + onClick, signedPetitions, ownedPetitions, ...props @@ -46,6 +47,7 @@ function ActNowLoggedInPage({ + + ); return ( @@ -109,8 +134,11 @@ ActNowLoggedInPage.propTypes = { navigation: PropTypes.shape({}), signedPetitions: PropTypes.arrayOf(PropTypes.shape({})), ownedPetitions: PropTypes.arrayOf(PropTypes.shape({})), + individualRegistrationDialogArgs: PropTypes.shape({}), + open: PropTypes.bool, title: PropTypes.string, onClick: PropTypes.func, + onClose: PropTypes.func, }; ActNowLoggedInPage.defaultProps = { @@ -120,6 +148,9 @@ ActNowLoggedInPage.defaultProps = { signedPetitions: undefined, title: undefined, onClick: undefined, + onClose: undefined, + open: undefined, + individualRegistrationDialogArgs: undefined, }; export default ActNowLoggedInPage; diff --git a/src/pages/act-now.js b/src/pages/act-now.js index 462b7a5d..f8bacaa9 100644 --- a/src/pages/act-now.js +++ b/src/pages/act-now.js @@ -8,6 +8,40 @@ import backendFn from "@/promisetracker/lib/backend"; import i18n from "@/promisetracker/lib/i18n"; import wp from "@/promisetracker/lib/wp"; +const individualRegistrationDialogArgs = { + title: "Register an Individual", + fields: { + agree: { + label: "Agree to our terms and conditions", + error: "You must agree to our terms and conditions to continue", + }, + bio: { + label: "About me / bio", + placeholder: "", + }, + email: { + label: "Email*", + placeholder: "", + error: "Required and must be a valid email address", + }, + firstName: { label: "First name*", placeholder: "", error: "Required" }, + lastName: { label: "Last name*", placeholder: "", error: "Required" }, + location: { + label: "Location (County, & Town, City, Country)*", + placeholder: "", + error: "Required", + }, + password: { + label: "Password*", + placeholder: "", + error: "Required", + }, + phoneNumber: { label: "Phone number", placeholder: "" }, + socialMedia: { label: "Social media link", placeholder: "" }, + submit: { label: "Submit" }, + }, +}; + function ActNow({ ...props }) { const { data: session, status } = useSession(); const [signedPetitions, setSignedPetitions] = React.useState([]); @@ -46,6 +80,7 @@ function ActNow({ ...props }) { ); From b0314d0741c08501278e831c0dfb20ee009a9b1f Mon Sep 17 00:00:00 2001 From: Brenda Nyokabi Bucha Date: Wed, 16 Mar 2022 16:08:10 +0300 Subject: [PATCH 03/23] Update props --- src/pages/act-now.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/pages/act-now.js b/src/pages/act-now.js index f8bacaa9..eba56c45 100644 --- a/src/pages/act-now.js +++ b/src/pages/act-now.js @@ -9,7 +9,7 @@ import i18n from "@/promisetracker/lib/i18n"; import wp from "@/promisetracker/lib/wp"; const individualRegistrationDialogArgs = { - title: "Register an Individual", + title: "Edit account details", fields: { agree: { label: "Agree to our terms and conditions", @@ -38,7 +38,7 @@ const individualRegistrationDialogArgs = { }, phoneNumber: { label: "Phone number", placeholder: "" }, socialMedia: { label: "Social media link", placeholder: "" }, - submit: { label: "Submit" }, + submit: { label: "Update" }, }, }; From 12f865c0012ecb67bc82cbb547571bd3f232f50d Mon Sep 17 00:00:00 2001 From: Brenda Nyokabi Bucha Date: Tue, 22 Mar 2022 16:28:46 +0300 Subject: [PATCH 04/23] Add update for form --- src/lib/actnow/index.js | 18 ++++++++++++++++++ src/pages/api/accounts/index.js | 2 ++ src/pages/api/accounts/update.js | 19 +++++++++++++++++++ 3 files changed, 39 insertions(+) create mode 100644 src/pages/api/accounts/update.js diff --git a/src/lib/actnow/index.js b/src/lib/actnow/index.js index 87d1c067..71c4acf0 100644 --- a/src/lib/actnow/index.js +++ b/src/lib/actnow/index.js @@ -26,6 +26,19 @@ function actnow(site) { return response.json(); } + async function updateLoggeduser(user) { + const headers = new Headers({ + "Content-Type": "application/json", + Authorization: `Token ${ACTNOW_API_KEY}`, + }); + const response = await fetch(`${ACTNOW_URL}/v1/accounts/`, { + method: "PATCH", + headers, + body: JSON.stringify(user), + }); /// isaiah should be able to give me this details + return response.json(); + } + async function fetchToken(url, params) { const Authorization = `Basic ${Buffer.from( `${ACTNOW_CLIENT_ID}:${ACTNOW_CLIENT_SECRET}` @@ -126,6 +139,11 @@ function actnow(site) { return refreshLoggedInUser(token); })(); }, + update: (user) => { + return (async () => { + return updateLoggeduser(user); + })(); + }, }; }, petitions: (queryParam) => { diff --git a/src/pages/api/accounts/index.js b/src/pages/api/accounts/index.js index ba0b8a27..de5b5191 100644 --- a/src/pages/api/accounts/index.js +++ b/src/pages/api/accounts/index.js @@ -16,4 +16,6 @@ async function handleAccounts(req, res) { return res.status(403).json(); } +// create update here + export default handleAccounts; diff --git a/src/pages/api/accounts/update.js b/src/pages/api/accounts/update.js new file mode 100644 index 00000000..75e13577 --- /dev/null +++ b/src/pages/api/accounts/update.js @@ -0,0 +1,19 @@ +import actnow from "@/promisetracker/lib/actnow"; + +async function updateUserDetails(req, res) { + try { + const account = await actnow().accounts().update(req.body); + return res.status(201).json(account); + } catch (error) { + return res.status(500).json(error); + } +} + +async function updateAccount(req, res) { + if (req.method === "PATCH") { + return updateUserDetails(req, res); + } + return res.status(403).json(); +} + +export default updateAccount; From c339f66d2679662d6ee2a2534ad38e1d0f004772 Mon Sep 17 00:00:00 2001 From: Brenda Nyokabi Bucha Date: Mon, 28 Mar 2022 16:09:56 +0300 Subject: [PATCH 05/23] Add update form --- src/components/ActNowPage/LoggedIn.js | 4 +- src/components/IndividualUpdateForm/Form.js | 243 ++++++++++++++++++ src/components/IndividualUpdateForm/index.js | 142 ++++++++++ .../IndividualUpdateForm/useStyles.js | 5 + .../IndividualUpdateFormDialog/index.js | 91 +++++++ .../IndividualUpdateFormDialog/useStyles.js | 42 +++ src/pages/act-now.js | 10 - 7 files changed, 525 insertions(+), 12 deletions(-) create mode 100644 src/components/IndividualUpdateForm/Form.js create mode 100644 src/components/IndividualUpdateForm/index.js create mode 100644 src/components/IndividualUpdateForm/useStyles.js create mode 100644 src/components/IndividualUpdateFormDialog/index.js create mode 100644 src/components/IndividualUpdateFormDialog/useStyles.js diff --git a/src/components/ActNowPage/LoggedIn.js b/src/components/ActNowPage/LoggedIn.js index 39bd68f1..d5f03678 100644 --- a/src/components/ActNowPage/LoggedIn.js +++ b/src/components/ActNowPage/LoggedIn.js @@ -10,7 +10,7 @@ import useStyles from "./useStyles"; import actNowLogo from "@/promisetracker/assets/actNowLogo2x.png"; import ContentPage from "@/promisetracker/components/ContentPage"; import Section from "@/promisetracker/components/ContentPage/Section"; -import IndividualRegistrationDialog from "@/promisetracker/components/IndividualRegistrationDialog"; +import IndividualUpdateFormDialog from "@/promisetracker/components/IndividualUpdateFormDialog"; import Petitions from "@/promisetracker/components/Petitions"; import Tabs from "@/promisetracker/components/Tabs"; @@ -71,7 +71,7 @@ function ActNowLoggedInPage({ Edit - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + } + label={ + {fields?.agree?.label} + } + className={classes.checkboxLabelAgree} + /> + + + + + + + + + + + ); +} + +Form.propTypes = { + disabled: PropTypes.bool, + fields: PropTypes.shape({ + agree: PropTypes.shape({ + label: PropTypes.string, + }), + bio: PropTypes.shape({ + label: PropTypes.string, + placeholder: PropTypes.string, + }), + firstName: PropTypes.shape({ + label: PropTypes.string, + placeholder: PropTypes.string, + }), + lastName: PropTypes.shape({ + label: PropTypes.string, + placeholder: PropTypes.string, + }), + location: PropTypes.shape({ + label: PropTypes.string, + placeholder: PropTypes.string, + }), + message: PropTypes.shape({ + label: PropTypes.string, + placeholder: PropTypes.string, + }), + phoneNumber: PropTypes.shape({ + label: PropTypes.string, + placeholder: PropTypes.string, + }), + socialMedia: PropTypes.shape({ + label: PropTypes.string, + placeholder: PropTypes.string, + }), + submit: PropTypes.shape({ + label: PropTypes.string, + }), + }), + errors: PropTypes.shape({ + agree: PropTypes.string, + bio: PropTypes.string, + firstName: PropTypes.string, + lastName: PropTypes.string, + location: PropTypes.string, + message: PropTypes.string, + phoneNumber: PropTypes.string, + socialMedia: PropTypes.string, + }), + name: PropTypes.string, + onSubmit: PropTypes.func, + status: PropTypes.shape({ + error: PropTypes.shape({}), + }), +}; + +Form.defaultProps = { + disabled: undefined, + errors: undefined, + fields: undefined, + name: undefined, + onSubmit: undefined, + status: undefined, +}; + +export default Form; diff --git a/src/components/IndividualUpdateForm/index.js b/src/components/IndividualUpdateForm/index.js new file mode 100644 index 00000000..d457f527 --- /dev/null +++ b/src/components/IndividualUpdateForm/index.js @@ -0,0 +1,142 @@ +import { Formik } from "formik"; +import PropTypes from "prop-types"; +import React, { useState } from "react"; + +import Form from "./Form"; + +function IndividualRegistrationForm({ + defaultErrorMessage, + fields, + onSubmit, + submitUrl, +}) { + const [status, setStatus] = useState({}); + + return ( + { + const errors = {}; + if (!values.firstName) { + errors.firstName = fields?.firstName?.error; + } + if (!values.lastName) { + errors.lastName = fields?.lastName?.error; + } + if (!values.email) { + errors.email = fields?.email?.error; + } else if ( + !/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i.test(values.email) + ) { + errors.email = fields?.email?.error; + } + if (!values.password) { + errors.password = fields?.password?.error; + } + if (!values.location) { + errors.location = fields?.location?.error; + } + if (!values.agree) { + errors.agree = fields?.agree?.error; + } + return errors; + }} + onSubmit={async ( + { + firstName, + lastName, + email, + password, + bio, + location, + phoneNumber, + socialMedia, + }, + { setErrors, setSubmitting } + ) => { + const body = { + first_name: firstName, + last_name: lastName, + email, + password, + bio, + location, + phone_number: phoneNumber, + social_media_link: socialMedia, + }; + if (submitUrl) { + try { + const headers = new Headers({ + "Content-Type": "application/json", + }); + const response = await fetch(`/api/accounts`, { + method: "POST", + headers, + body: JSON.stringify(body), + }); + const responseJson = await response.json(); + if (response.status === 201) { + setStatus({ children: undefined }); + if (onSubmit) { + onSubmit(); + } + } else { + setErrors(responseJson); + setStatus({ error: true, children: "Error" }); + } + } catch (e) { + setStatus({ error: true, children: "Error" }); + } finally { + setSubmitting(false); + } + } else { + setStatus({ error: true, children: defaultErrorMessage }); + } + }} + > + {({ submitForm, isSubmitting, errors }) => ( +
+ )} + + ); +} + +IndividualRegistrationForm.propTypes = { + defaultErrorMessage: PropTypes.string, + fields: PropTypes.shape({ + agree: PropTypes.shape({ + error: PropTypes.string, + }), + email: PropTypes.shape({ + error: PropTypes.string, + }), + firstName: PropTypes.shape({ error: PropTypes.string }), + lastName: PropTypes.shape({ error: PropTypes.string }), + location: PropTypes.shape({ error: PropTypes.string }), + password: PropTypes.shape({ error: PropTypes.string }), + }), + onSubmit: PropTypes.func, + submitUrl: PropTypes.string, +}; + +IndividualRegistrationForm.defaultProps = { + defaultErrorMessage: undefined, + fields: undefined, + onSubmit: false, + submitUrl: undefined, +}; + +export default IndividualRegistrationForm; diff --git a/src/components/IndividualUpdateForm/useStyles.js b/src/components/IndividualUpdateForm/useStyles.js new file mode 100644 index 00000000..72465f48 --- /dev/null +++ b/src/components/IndividualUpdateForm/useStyles.js @@ -0,0 +1,5 @@ +import { makeStyles } from "@material-ui/core/styles"; + +const useStyles = makeStyles(() => ({})); + +export default useStyles; diff --git a/src/components/IndividualUpdateFormDialog/index.js b/src/components/IndividualUpdateFormDialog/index.js new file mode 100644 index 00000000..ba7fcae6 --- /dev/null +++ b/src/components/IndividualUpdateFormDialog/index.js @@ -0,0 +1,91 @@ +import { + Dialog, + IconButton, + DialogTitle, + DialogContent, + Typography, +} from "@material-ui/core"; +import CloseIcon from "@material-ui/icons/Close"; +import PropTypes from "prop-types"; +import React from "react"; + +import useStyles from "./useStyles"; + +import Form from "@/promisetracker/components/IndividualUpdateForm"; + +function IndividualUpdateFormDialog({ + name: nameProp, + onClose, + onSubmit, + open, + title, + ...props +}) { + const classes = useStyles(props); + const name = nameProp || "individual-registration-dialog"; + + const handleClose = () => { + if (onClose) { + onClose(); + } + }; + + const handleSubmit = () => { + if (onSubmit) { + onSubmit(); + } else { + handleClose(); + } + }; + + return ( + + + {title} + + + + + + + + + ); +} + +IndividualUpdateFormDialog.propTypes = { + name: PropTypes.string, + onClose: PropTypes.func, + onSubmit: PropTypes.func, + open: PropTypes.bool, + title: PropTypes.string, +}; + +IndividualUpdateFormDialog.defaultProps = { + name: null, + onClose: null, + onSubmit: null, + open: false, + title: null, +}; + +export default IndividualUpdateFormDialog; diff --git a/src/components/IndividualUpdateFormDialog/useStyles.js b/src/components/IndividualUpdateFormDialog/useStyles.js new file mode 100644 index 00000000..2ad01325 --- /dev/null +++ b/src/components/IndividualUpdateFormDialog/useStyles.js @@ -0,0 +1,42 @@ +import { makeStyles } from "@material-ui/core/styles"; + +const useStyles = makeStyles(({ typography, breakpoints }) => ({ + section: {}, + paperScrollBody: { + margin: 0, + }, + paperWidthSm: { + maxWidth: typography.pxToRem(716), + }, + scrollBody: {}, + root: { + width: "100%", + backgroundColor: "#F7F7F7", + display: "flex", + alignItems: "center", + justifyContent: "center", + [breakpoints.up("lg")]: { + padding: `${typography.pxToRem(25)} 0`, + }, + }, + title: { + display: "flex", + alignItems: "center", + justifyContent: "space-between", + backgroundColor: "#F7F7F7", + borderRadius: `0 0 ${typography.pxToRem(10)} ${typography.pxToRem(10)}`, + border: "1px solid #ebebeb", + padding: ` + ${typography.pxToRem(19)} ${typography.pxToRem(51.5)} + ${typography.pxToRem(18)} ${typography.pxToRem(55)} + `, + }, + content: { + padding: `${typography.pxToRem(51)} ${typography.pxToRem(51)}`, + [breakpoints.down("sm")]: { + padding: `${typography.pxToRem(15)} ${typography.pxToRem(15)}`, + }, + }, +})); + +export default useStyles; diff --git a/src/pages/act-now.js b/src/pages/act-now.js index eba56c45..ef3794e0 100644 --- a/src/pages/act-now.js +++ b/src/pages/act-now.js @@ -19,11 +19,6 @@ const individualRegistrationDialogArgs = { label: "About me / bio", placeholder: "", }, - email: { - label: "Email*", - placeholder: "", - error: "Required and must be a valid email address", - }, firstName: { label: "First name*", placeholder: "", error: "Required" }, lastName: { label: "Last name*", placeholder: "", error: "Required" }, location: { @@ -31,11 +26,6 @@ const individualRegistrationDialogArgs = { placeholder: "", error: "Required", }, - password: { - label: "Password*", - placeholder: "", - error: "Required", - }, phoneNumber: { label: "Phone number", placeholder: "" }, socialMedia: { label: "Social media link", placeholder: "" }, submit: { label: "Update" }, From f71cd42cdab6433d5c4a65a583c036a86a25f077 Mon Sep 17 00:00:00 2001 From: Brenda Nyokabi Bucha Date: Mon, 28 Mar 2022 16:11:18 +0300 Subject: [PATCH 06/23] Minor update --- src/components/IndividualUpdateForm/Form.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/components/IndividualUpdateForm/Form.js b/src/components/IndividualUpdateForm/Form.js index d394c194..64ae3ff5 100644 --- a/src/components/IndividualUpdateForm/Form.js +++ b/src/components/IndividualUpdateForm/Form.js @@ -25,7 +25,7 @@ function Form({ ...props }) { const classes = useStyles(props); - const name = nameProp || "individual-registration-form"; + const name = nameProp || "individual-update-form"; return ( From d25a439492e85d0522f877234cbdc26956b53481 Mon Sep 17 00:00:00 2001 From: Brenda Nyokabi Bucha Date: Thu, 31 Mar 2022 09:49:37 +0300 Subject: [PATCH 07/23] Update index page --- src/components/IndividualUpdateForm/index.js | 29 +------------------- 1 file changed, 1 insertion(+), 28 deletions(-) diff --git a/src/components/IndividualUpdateForm/index.js b/src/components/IndividualUpdateForm/index.js index d457f527..b83d9c87 100644 --- a/src/components/IndividualUpdateForm/index.js +++ b/src/components/IndividualUpdateForm/index.js @@ -16,11 +16,9 @@ function IndividualRegistrationForm({ { const errors = {}; @@ -30,16 +28,6 @@ function IndividualRegistrationForm({ if (!values.lastName) { errors.lastName = fields?.lastName?.error; } - if (!values.email) { - errors.email = fields?.email?.error; - } else if ( - !/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i.test(values.email) - ) { - errors.email = fields?.email?.error; - } - if (!values.password) { - errors.password = fields?.password?.error; - } if (!values.location) { errors.location = fields?.location?.error; } @@ -49,23 +37,12 @@ function IndividualRegistrationForm({ return errors; }} onSubmit={async ( - { - firstName, - lastName, - email, - password, - bio, - location, - phoneNumber, - socialMedia, - }, + { firstName, lastName, bio, location, phoneNumber, socialMedia }, { setErrors, setSubmitting } ) => { const body = { first_name: firstName, last_name: lastName, - email, - password, bio, location, phone_number: phoneNumber, @@ -120,13 +97,9 @@ IndividualRegistrationForm.propTypes = { agree: PropTypes.shape({ error: PropTypes.string, }), - email: PropTypes.shape({ - error: PropTypes.string, - }), firstName: PropTypes.shape({ error: PropTypes.string }), lastName: PropTypes.shape({ error: PropTypes.string }), location: PropTypes.shape({ error: PropTypes.string }), - password: PropTypes.shape({ error: PropTypes.string }), }), onSubmit: PropTypes.func, submitUrl: PropTypes.string, From 6aa86cdc97aabbf75e5869b0afad2dacb8e53211 Mon Sep 17 00:00:00 2001 From: Brenda Nyokabi Bucha Date: Thu, 31 Mar 2022 09:58:11 +0300 Subject: [PATCH 08/23] Update props --- src/components/ActNowPage/LoggedIn.js | 8 ++++---- src/pages/act-now.js | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/components/ActNowPage/LoggedIn.js b/src/components/ActNowPage/LoggedIn.js index d5f03678..0c9a414c 100644 --- a/src/components/ActNowPage/LoggedIn.js +++ b/src/components/ActNowPage/LoggedIn.js @@ -22,7 +22,7 @@ function ActNowLoggedInPage({ onClick, signedPetitions, open: openProp, - individualRegistrationDialogArgs, + individualUpdateDialogArgs, ownedPetitions, ...props }) { @@ -72,7 +72,7 @@ function ActNowLoggedInPage({ ); From 08c4a48b6b3afa57f3b9111526fecde4745b9617 Mon Sep 17 00:00:00 2001 From: Brenda Nyokabi Bucha Date: Thu, 31 Mar 2022 14:23:28 +0300 Subject: [PATCH 09/23] Update formik --- src/components/ActNowPage/index.js | 1 + src/components/IndividualUpdateForm/index.js | 4 ++-- src/lib/actnow/index.js | 3 ++- src/pages/act-now.js | 2 +- src/pages/api/accounts/index.js | 2 -- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/components/ActNowPage/index.js b/src/components/ActNowPage/index.js index 1ac4cd34..ab9f24b9 100644 --- a/src/components/ActNowPage/index.js +++ b/src/components/ActNowPage/index.js @@ -66,6 +66,7 @@ function ActNow({ const submitUrl = actNow?.url; const summary = actNow?.summary; + console.log(submitUrl, "submitUrl"); const handleRegister = () => { setOpen(true); }; diff --git a/src/components/IndividualUpdateForm/index.js b/src/components/IndividualUpdateForm/index.js index b83d9c87..70fdb539 100644 --- a/src/components/IndividualUpdateForm/index.js +++ b/src/components/IndividualUpdateForm/index.js @@ -53,8 +53,8 @@ function IndividualRegistrationForm({ const headers = new Headers({ "Content-Type": "application/json", }); - const response = await fetch(`/api/accounts`, { - method: "POST", + const response = await fetch(`/api/accounts/update`, { + method: "PATCH", headers, body: JSON.stringify(body), }); diff --git a/src/lib/actnow/index.js b/src/lib/actnow/index.js index 71c4acf0..be23ac2a 100644 --- a/src/lib/actnow/index.js +++ b/src/lib/actnow/index.js @@ -14,6 +14,7 @@ function actnow(site) { const ACTNOW_URL = server.env("ACTNOW_URL"); async function createAccount(user) { + console.log(user); const headers = new Headers({ "Content-Type": "application/json", Authorization: `Token ${ACTNOW_API_KEY}`, @@ -31,7 +32,7 @@ function actnow(site) { "Content-Type": "application/json", Authorization: `Token ${ACTNOW_API_KEY}`, }); - const response = await fetch(`${ACTNOW_URL}/v1/accounts/`, { + const response = await fetch(`${ACTNOW_URL}/v1/profiles/users/`, { method: "PATCH", headers, body: JSON.stringify(user), diff --git a/src/pages/act-now.js b/src/pages/act-now.js index 7c947edc..b1242506 100644 --- a/src/pages/act-now.js +++ b/src/pages/act-now.js @@ -37,7 +37,7 @@ function ActNow({ ...props }) { const [signedPetitions, setSignedPetitions] = React.useState([]); const [ownedPetitions, setOwnedPetitions] = React.useState([]); - /** + /** ss * Note: Using session in frontend is a workaround for Next.js SSR file system bug.(Tracked here https://www.pivotaltracker.com/story/show/181432688) */ useEffect(() => { diff --git a/src/pages/api/accounts/index.js b/src/pages/api/accounts/index.js index de5b5191..ba0b8a27 100644 --- a/src/pages/api/accounts/index.js +++ b/src/pages/api/accounts/index.js @@ -16,6 +16,4 @@ async function handleAccounts(req, res) { return res.status(403).json(); } -// create update here - export default handleAccounts; From dc048e013799d542a4100542b82199efa36e6aeb Mon Sep 17 00:00:00 2001 From: Brenda Nyokabi Bucha Date: Thu, 31 Mar 2022 15:41:59 +0300 Subject: [PATCH 10/23] Minor update on function --- .../IndividualRegistrationForm/index.js | 1 + src/lib/actnow/index.js | 34 +++++++++++-------- src/pages/act-now.js | 1 - 3 files changed, 21 insertions(+), 15 deletions(-) diff --git a/src/components/IndividualRegistrationForm/index.js b/src/components/IndividualRegistrationForm/index.js index d457f527..14905694 100644 --- a/src/components/IndividualRegistrationForm/index.js +++ b/src/components/IndividualRegistrationForm/index.js @@ -81,6 +81,7 @@ function IndividualRegistrationForm({ headers, body: JSON.stringify(body), }); + const responseJson = await response.json(); if (response.status === 201) { setStatus({ children: undefined }); diff --git a/src/lib/actnow/index.js b/src/lib/actnow/index.js index be23ac2a..6911a2c2 100644 --- a/src/lib/actnow/index.js +++ b/src/lib/actnow/index.js @@ -14,7 +14,6 @@ function actnow(site) { const ACTNOW_URL = server.env("ACTNOW_URL"); async function createAccount(user) { - console.log(user); const headers = new Headers({ "Content-Type": "application/json", Authorization: `Token ${ACTNOW_API_KEY}`, @@ -27,19 +26,6 @@ function actnow(site) { return response.json(); } - async function updateLoggeduser(user) { - const headers = new Headers({ - "Content-Type": "application/json", - Authorization: `Token ${ACTNOW_API_KEY}`, - }); - const response = await fetch(`${ACTNOW_URL}/v1/profiles/users/`, { - method: "PATCH", - headers, - body: JSON.stringify(user), - }); /// isaiah should be able to give me this details - return response.json(); - } - async function fetchToken(url, params) { const Authorization = `Basic ${Buffer.from( `${ACTNOW_CLIENT_ID}:${ACTNOW_CLIENT_SECRET}` @@ -105,6 +91,26 @@ function actnow(site) { } } + async function updateLoggeduser(user) { + const loginUserTokens = loginUser(); + const { accessToken } = loginUserTokens; + + const headers = new Headers({ + "Content-Type": "application/json", + Authorization: `Bearer ${accessToken}`, + }); + + const response = await fetch( + `${ACTNOW_URL}/v1/profiles/users/${user?.profile?.id}`, + { + method: "PATCH", + headers, + body: JSON.stringify(user), + } + ); + return response.json(); + } + async function getPetitions(query) { const url = `${ACTNOW_URL}/v1/petitions/?${`${query}&` || ""}format=json`; const response = await fetch(url); diff --git a/src/pages/act-now.js b/src/pages/act-now.js index b1242506..70ae66c8 100644 --- a/src/pages/act-now.js +++ b/src/pages/act-now.js @@ -36,7 +36,6 @@ function ActNow({ ...props }) { const { data: session, status } = useSession(); const [signedPetitions, setSignedPetitions] = React.useState([]); const [ownedPetitions, setOwnedPetitions] = React.useState([]); - /** ss * Note: Using session in frontend is a workaround for Next.js SSR file system bug.(Tracked here https://www.pivotaltracker.com/story/show/181432688) */ From 9ba936e6186a9aba1e895b26099bc9dbd720ffa6 Mon Sep 17 00:00:00 2001 From: Brenda Nyokabi Bucha Date: Thu, 31 Mar 2022 16:08:28 +0300 Subject: [PATCH 11/23] Update patch function --- src/lib/actnow/index.js | 5 +---- src/pages/act-now.js | 7 ++++++- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/lib/actnow/index.js b/src/lib/actnow/index.js index 6911a2c2..e0c8b0f4 100644 --- a/src/lib/actnow/index.js +++ b/src/lib/actnow/index.js @@ -92,12 +92,9 @@ function actnow(site) { } async function updateLoggeduser(user) { - const loginUserTokens = loginUser(); - const { accessToken } = loginUserTokens; - const headers = new Headers({ "Content-Type": "application/json", - Authorization: `Bearer ${accessToken}`, + Authorization: `Token ${ACTNOW_API_KEY}`, }); const response = await fetch( diff --git a/src/pages/act-now.js b/src/pages/act-now.js index 70ae66c8..ba1ad549 100644 --- a/src/pages/act-now.js +++ b/src/pages/act-now.js @@ -36,6 +36,7 @@ function ActNow({ ...props }) { const { data: session, status } = useSession(); const [signedPetitions, setSignedPetitions] = React.useState([]); const [ownedPetitions, setOwnedPetitions] = React.useState([]); + const submitUrl = `https://actnow.dev.codeforafrica.org/v1/profiles/users/${session?.user?.profile?.id}/`; /** ss * Note: Using session in frontend is a workaround for Next.js SSR file system bug.(Tracked here https://www.pivotaltracker.com/story/show/181432688) */ @@ -69,8 +70,11 @@ function ActNow({ ...props }) { ); } @@ -99,6 +103,7 @@ export async function getStaticProps({ locale }) { const languageAlternates = _.languageAlternates("/act-now"); actNow.url = process.env.ACTNOW_URL ?? null; + console.log(actNow.url); const headers = new Headers({ "Content-Type": "application/json", From ab675f720ab6e4fb57f19648d9d9c0ba3e84c42c Mon Sep 17 00:00:00 2001 From: Brenda Nyokabi Bucha Date: Thu, 31 Mar 2022 16:10:13 +0300 Subject: [PATCH 12/23] Remove terms and services --- src/components/IndividualUpdateForm/Form.js | 37 ++------------------- 1 file changed, 2 insertions(+), 35 deletions(-) diff --git a/src/components/IndividualUpdateForm/Form.js b/src/components/IndividualUpdateForm/Form.js index 64ae3ff5..68455d85 100644 --- a/src/components/IndividualUpdateForm/Form.js +++ b/src/components/IndividualUpdateForm/Form.js @@ -1,12 +1,5 @@ -import { - Button, - FormControlLabel, - FormHelperText, - Grid, - Typography, -} from "@material-ui/core"; -import { ErrorMessage, Form as FForm, Field } from "formik"; -import { Checkbox } from "formik-material-ui"; +import { Button, FormHelperText, Grid } from "@material-ui/core"; +import { ErrorMessage, Form as FForm } from "formik"; import PropTypes from "prop-types"; import React from "react"; @@ -129,32 +122,6 @@ function Form({ - - - } - label={ - {fields?.agree?.label} - } - className={classes.checkboxLabelAgree} - /> - - - - Date: Wed, 6 Apr 2022 10:07:36 +0300 Subject: [PATCH 13/23] Update changes --- src/lib/actnow/index.js | 2 +- src/pages/act-now.js | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/lib/actnow/index.js b/src/lib/actnow/index.js index e0c8b0f4..7c315e11 100644 --- a/src/lib/actnow/index.js +++ b/src/lib/actnow/index.js @@ -94,7 +94,7 @@ function actnow(site) { async function updateLoggeduser(user) { const headers = new Headers({ "Content-Type": "application/json", - Authorization: `Token ${ACTNOW_API_KEY}`, + Authorization: `Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJzY29wZSI6Im9wZW5pZCBwcm9maWxlIGVtYWlsIiwiZXhwIjoxNjQ4NzYxMjIzLCJhdWQiOiJjc01MbTZmTjRXVDJZckhrMjRyN0RlaDBVRmlpSmJLUzVrNG83TFBwIiwiaXNzIjoiYWN0Tk9XIiwic3ViIjoxOH0.vP5qxBe7trLKvbpCXqlrQoff6qveAGT-vxZkhjcjhbMPzpBE_qbymDWaNwyfqC71Si86JD-hloEXAf1JEZw2P8u8b4nUr5oj63as9PNW3IENXp6YFdWec8m-IY8rmc_vzw7W7DH0Jf2qcMPjZQHMGtkCRsX1tt6JJ8RFJhAdDpLIyaR-KSrfFI5mtlmP1gcKEQtOfVz3Auyqs4lEOebnFOx4rQ6rL2hwgGAn11Gk26s_qz1kcF5Eb3qH3mqoTch5hDs-l1zEYk6h8Gm3EvxrLMhwap_jRV75j5w339KrUexmtbGTLxEugSTM6Lt1mg98o8ocIW2D3sL9Pz0szMMm8d7admL67xRQYeIXw4Dcs9n8nNPcyo-M6hNDFzD5TM10GM83uF_WEpc-5UijLtfZx7ztSpWyr2-unZ9L0Obda3cc48N73lR-lQM1FPmRkA876kLUSLzsckhJiKlXRhITzWRZ7cwEBCm4tgd3_AKAXxCr64xVX0BQqLgxbdNDRVMooZmS5NgUbfWKHLNzPk1GU6MPwuSyonVnd2mS9P3SxSHgM2Qn6xHNcCx-lSkAbOeAjxenwb4JH38msr1PAHedexmdSb2zmnQX2F51wSBOzG0EcCFsl4h58qoLVKf-WzLc5ZWTGV2NQySEfp9u5PpjxEZ-IwaAxa5XizxWF9qMQ4s`, }); const response = await fetch( diff --git a/src/pages/act-now.js b/src/pages/act-now.js index ba1ad549..7115953b 100644 --- a/src/pages/act-now.js +++ b/src/pages/act-now.js @@ -36,6 +36,7 @@ function ActNow({ ...props }) { const { data: session, status } = useSession(); const [signedPetitions, setSignedPetitions] = React.useState([]); const [ownedPetitions, setOwnedPetitions] = React.useState([]); + console.log(session); const submitUrl = `https://actnow.dev.codeforafrica.org/v1/profiles/users/${session?.user?.profile?.id}/`; /** ss * Note: Using session in frontend is a workaround for Next.js SSR file system bug.(Tracked here https://www.pivotaltracker.com/story/show/181432688) From 8977a045e14471b07d071cf268ee86242fc13688 Mon Sep 17 00:00:00 2001 From: Brenda Nyokabi Bucha Date: Wed, 6 Apr 2022 14:21:53 +0300 Subject: [PATCH 14/23] Remove token, remove console,log --- src/lib/actnow/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/actnow/index.js b/src/lib/actnow/index.js index 7c315e11..e0c8b0f4 100644 --- a/src/lib/actnow/index.js +++ b/src/lib/actnow/index.js @@ -94,7 +94,7 @@ function actnow(site) { async function updateLoggeduser(user) { const headers = new Headers({ "Content-Type": "application/json", - Authorization: `Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJzY29wZSI6Im9wZW5pZCBwcm9maWxlIGVtYWlsIiwiZXhwIjoxNjQ4NzYxMjIzLCJhdWQiOiJjc01MbTZmTjRXVDJZckhrMjRyN0RlaDBVRmlpSmJLUzVrNG83TFBwIiwiaXNzIjoiYWN0Tk9XIiwic3ViIjoxOH0.vP5qxBe7trLKvbpCXqlrQoff6qveAGT-vxZkhjcjhbMPzpBE_qbymDWaNwyfqC71Si86JD-hloEXAf1JEZw2P8u8b4nUr5oj63as9PNW3IENXp6YFdWec8m-IY8rmc_vzw7W7DH0Jf2qcMPjZQHMGtkCRsX1tt6JJ8RFJhAdDpLIyaR-KSrfFI5mtlmP1gcKEQtOfVz3Auyqs4lEOebnFOx4rQ6rL2hwgGAn11Gk26s_qz1kcF5Eb3qH3mqoTch5hDs-l1zEYk6h8Gm3EvxrLMhwap_jRV75j5w339KrUexmtbGTLxEugSTM6Lt1mg98o8ocIW2D3sL9Pz0szMMm8d7admL67xRQYeIXw4Dcs9n8nNPcyo-M6hNDFzD5TM10GM83uF_WEpc-5UijLtfZx7ztSpWyr2-unZ9L0Obda3cc48N73lR-lQM1FPmRkA876kLUSLzsckhJiKlXRhITzWRZ7cwEBCm4tgd3_AKAXxCr64xVX0BQqLgxbdNDRVMooZmS5NgUbfWKHLNzPk1GU6MPwuSyonVnd2mS9P3SxSHgM2Qn6xHNcCx-lSkAbOeAjxenwb4JH38msr1PAHedexmdSb2zmnQX2F51wSBOzG0EcCFsl4h58qoLVKf-WzLc5ZWTGV2NQySEfp9u5PpjxEZ-IwaAxa5XizxWF9qMQ4s`, + Authorization: `Token ${ACTNOW_API_KEY}`, }); const response = await fetch( From 16f2842e864f6ad5ca8316c4539886ac679b2fd6 Mon Sep 17 00:00:00 2001 From: Brenda Nyokabi Bucha Date: Wed, 6 Apr 2022 16:06:49 +0300 Subject: [PATCH 15/23] Update put method --- src/components/ActNowPage/index.js | 1 - src/components/IndividualUpdateForm/index.js | 38 +++++++++++++++----- src/lib/actnow/index.js | 10 +++--- src/pages/act-now.js | 5 ++- src/pages/api/accounts/update.js | 2 +- 5 files changed, 38 insertions(+), 18 deletions(-) diff --git a/src/components/ActNowPage/index.js b/src/components/ActNowPage/index.js index ab9f24b9..1ac4cd34 100644 --- a/src/components/ActNowPage/index.js +++ b/src/components/ActNowPage/index.js @@ -66,7 +66,6 @@ function ActNow({ const submitUrl = actNow?.url; const summary = actNow?.summary; - console.log(submitUrl, "submitUrl"); const handleRegister = () => { setOpen(true); }; diff --git a/src/components/IndividualUpdateForm/index.js b/src/components/IndividualUpdateForm/index.js index 70fdb539..18141b1f 100644 --- a/src/components/IndividualUpdateForm/index.js +++ b/src/components/IndividualUpdateForm/index.js @@ -15,10 +15,12 @@ function IndividualRegistrationForm({ return ( { const errors = {}; @@ -31,18 +33,35 @@ function IndividualRegistrationForm({ if (!values.location) { errors.location = fields?.location?.error; } - if (!values.agree) { - errors.agree = fields?.agree?.error; + if (!values.bio) { + errors.bio = fields?.bio?.error; + } + if (!values.phoneNumber) { + errors.phoneNumber = fields?.phoneNumber?.error; + } + if (!values.socialMedia) { + errors.socialMedia = fields?.socialMedia?.error; } return errors; }} onSubmit={async ( - { firstName, lastName, bio, location, phoneNumber, socialMedia }, + { + firstName, + lastName, + email, + password, + bio, + location, + phoneNumber, + socialMedia, + }, { setErrors, setSubmitting } ) => { const body = { first_name: firstName, last_name: lastName, + email, + password, bio, location, phone_number: phoneNumber, @@ -53,11 +72,12 @@ function IndividualRegistrationForm({ const headers = new Headers({ "Content-Type": "application/json", }); - const response = await fetch(`/api/accounts/update`, { - method: "PATCH", + const response = await fetch(`/api/accounts`, { + method: "POST", headers, body: JSON.stringify(body), }); + const responseJson = await response.json(); if (response.status === 201) { setStatus({ children: undefined }); @@ -94,12 +114,12 @@ function IndividualRegistrationForm({ IndividualRegistrationForm.propTypes = { defaultErrorMessage: PropTypes.string, fields: PropTypes.shape({ - agree: PropTypes.shape({ - error: PropTypes.string, - }), firstName: PropTypes.shape({ error: PropTypes.string }), lastName: PropTypes.shape({ error: PropTypes.string }), location: PropTypes.shape({ error: PropTypes.string }), + bio: PropTypes.shape({ error: PropTypes.string }), + phoneNumber: PropTypes.shape({ error: PropTypes.string }), + socialMedia: PropTypes.shape({ error: PropTypes.string }), }), onSubmit: PropTypes.func, submitUrl: PropTypes.string, diff --git a/src/lib/actnow/index.js b/src/lib/actnow/index.js index e0c8b0f4..326072ab 100644 --- a/src/lib/actnow/index.js +++ b/src/lib/actnow/index.js @@ -1,4 +1,5 @@ import jwtDecode from "jwt-decode"; +import { useSession } from "next-auth/react"; import serverFn from "@/promisetracker/lib/server"; @@ -91,16 +92,17 @@ function actnow(site) { } } - async function updateLoggeduser(user) { + async function UpdateLoggedUser(user) { + const { data: session } = useSession(); const headers = new Headers({ "Content-Type": "application/json", - Authorization: `Token ${ACTNOW_API_KEY}`, + Authorization: `Bearer ${session?.accessToken}`, }); const response = await fetch( `${ACTNOW_URL}/v1/profiles/users/${user?.profile?.id}`, { - method: "PATCH", + method: "PUT", headers, body: JSON.stringify(user), } @@ -145,7 +147,7 @@ function actnow(site) { }, update: (user) => { return (async () => { - return updateLoggeduser(user); + return UpdateLoggedUser(user); })(); }, }; diff --git a/src/pages/act-now.js b/src/pages/act-now.js index 7115953b..fae5e939 100644 --- a/src/pages/act-now.js +++ b/src/pages/act-now.js @@ -36,9 +36,8 @@ function ActNow({ ...props }) { const { data: session, status } = useSession(); const [signedPetitions, setSignedPetitions] = React.useState([]); const [ownedPetitions, setOwnedPetitions] = React.useState([]); - console.log(session); const submitUrl = `https://actnow.dev.codeforafrica.org/v1/profiles/users/${session?.user?.profile?.id}/`; - /** ss + /** * Note: Using session in frontend is a workaround for Next.js SSR file system bug.(Tracked here https://www.pivotaltracker.com/story/show/181432688) */ useEffect(() => { @@ -104,12 +103,12 @@ export async function getStaticProps({ locale }) { const languageAlternates = _.languageAlternates("/act-now"); actNow.url = process.env.ACTNOW_URL ?? null; - console.log(actNow.url); const headers = new Headers({ "Content-Type": "application/json", Authorization: `Token ${process.env.ACTNOW_API_KEY}`, }); + const actnowSummary = await fetch(`${actNow.url}/v1/`, { method: "GET", headers, diff --git a/src/pages/api/accounts/update.js b/src/pages/api/accounts/update.js index 75e13577..9460727d 100644 --- a/src/pages/api/accounts/update.js +++ b/src/pages/api/accounts/update.js @@ -10,7 +10,7 @@ async function updateUserDetails(req, res) { } async function updateAccount(req, res) { - if (req.method === "PATCH") { + if (req.method === "PUT") { return updateUserDetails(req, res); } return res.status(403).json(); From 6f0c19c1d5e96442391934315e0f61c470cc75d1 Mon Sep 17 00:00:00 2001 From: Brenda Nyokabi Bucha Date: Mon, 11 Apr 2022 16:32:22 +0300 Subject: [PATCH 16/23] Update user session. patch method --- src/components/IndividualUpdateForm/index.js | 28 ++++++++-------- src/lib/actnow/index.js | 34 +++++++++++--------- src/pages/act-now.js | 2 ++ src/pages/api/accounts/update.js | 19 ----------- src/pages/api/accounts/update/[id].js | 25 ++++++++++++++ 5 files changed, 60 insertions(+), 48 deletions(-) delete mode 100644 src/pages/api/accounts/update.js create mode 100644 src/pages/api/accounts/update/[id].js diff --git a/src/components/IndividualUpdateForm/index.js b/src/components/IndividualUpdateForm/index.js index 18141b1f..58b6d95e 100644 --- a/src/components/IndividualUpdateForm/index.js +++ b/src/components/IndividualUpdateForm/index.js @@ -1,4 +1,5 @@ import { Formik } from "formik"; +import { useSession } from "next-auth/react"; import PropTypes from "prop-types"; import React, { useState } from "react"; @@ -10,6 +11,7 @@ function IndividualRegistrationForm({ onSubmit, submitUrl, }) { + const { data: session } = useSession(); const [status, setStatus] = useState({}); return ( @@ -45,35 +47,33 @@ function IndividualRegistrationForm({ return errors; }} onSubmit={async ( - { - firstName, - lastName, - email, - password, - bio, - location, - phoneNumber, - socialMedia, - }, + { firstName, lastName, bio, location, phoneNumber, socialMedia }, { setErrors, setSubmitting } ) => { const body = { first_name: firstName, last_name: lastName, - email, - password, bio, location, phone_number: phoneNumber, social_media_link: socialMedia, }; + console.log(body); if (submitUrl) { + const { + accessToken, + user: { + profile: { id }, + }, + } = session; try { const headers = new Headers({ "Content-Type": "application/json", + Accept: "application/json", + Authorization: `Bearer ${accessToken}`, }); - const response = await fetch(`/api/accounts`, { - method: "POST", + const response = await fetch(`/api/accounts/update/${id}`, { + method: "PATCH", headers, body: JSON.stringify(body), }); diff --git a/src/lib/actnow/index.js b/src/lib/actnow/index.js index 326072ab..7701df5e 100644 --- a/src/lib/actnow/index.js +++ b/src/lib/actnow/index.js @@ -1,5 +1,4 @@ import jwtDecode from "jwt-decode"; -import { useSession } from "next-auth/react"; import serverFn from "@/promisetracker/lib/server"; @@ -92,22 +91,27 @@ function actnow(site) { } } - async function UpdateLoggedUser(user) { - const { data: session } = useSession(); + async function updateUser(profile, session) { + const { + accessToken, + user: { + profile: { id }, + }, + } = session; + const headers = new Headers({ "Content-Type": "application/json", - Authorization: `Bearer ${session?.accessToken}`, + Authorization: `Bearer ${accessToken}`, }); - const response = await fetch( - `${ACTNOW_URL}/v1/profiles/users/${user?.profile?.id}`, - { - method: "PUT", - headers, - body: JSON.stringify(user), - } - ); - return response.json(); + console.log(profile, session); + + const responseData = await fetch(`${ACTNOW_URL}/v1/profiles/users/${id}/`, { + method: "PATCH", + headers, + body: JSON.stringify(profile), + }).then(async (response) => response.json()); + return responseData; } async function getPetitions(query) { @@ -145,9 +149,9 @@ function actnow(site) { return refreshLoggedInUser(token); })(); }, - update: (user) => { + updateUserDetails: (profile, session) => { return (async () => { - return UpdateLoggedUser(user); + return updateUser(profile, session); })(); }, }; diff --git a/src/pages/act-now.js b/src/pages/act-now.js index fae5e939..40df2721 100644 --- a/src/pages/act-now.js +++ b/src/pages/act-now.js @@ -36,6 +36,8 @@ function ActNow({ ...props }) { const { data: session, status } = useSession(); const [signedPetitions, setSignedPetitions] = React.useState([]); const [ownedPetitions, setOwnedPetitions] = React.useState([]); + + /* console.log(session); */ const submitUrl = `https://actnow.dev.codeforafrica.org/v1/profiles/users/${session?.user?.profile?.id}/`; /** * Note: Using session in frontend is a workaround for Next.js SSR file system bug.(Tracked here https://www.pivotaltracker.com/story/show/181432688) diff --git a/src/pages/api/accounts/update.js b/src/pages/api/accounts/update.js deleted file mode 100644 index 9460727d..00000000 --- a/src/pages/api/accounts/update.js +++ /dev/null @@ -1,19 +0,0 @@ -import actnow from "@/promisetracker/lib/actnow"; - -async function updateUserDetails(req, res) { - try { - const account = await actnow().accounts().update(req.body); - return res.status(201).json(account); - } catch (error) { - return res.status(500).json(error); - } -} - -async function updateAccount(req, res) { - if (req.method === "PUT") { - return updateUserDetails(req, res); - } - return res.status(403).json(); -} - -export default updateAccount; diff --git a/src/pages/api/accounts/update/[id].js b/src/pages/api/accounts/update/[id].js new file mode 100644 index 00000000..58ecaae3 --- /dev/null +++ b/src/pages/api/accounts/update/[id].js @@ -0,0 +1,25 @@ +import { getSession } from "next-auth/react"; + +import actnow from "@/promisetracker/lib/actnow"; + +async function updateUserInformation(req, res) { + const session = await getSession({ req }); + try { + const results = await actnow() + .accounts() + .updateUserDetails(req?.body, session); + return res.status(201).json(results); + } catch (error) { + console.log(error); + return res.status(500).json(error); + } +} + +async function handleAccounts(req, res) { + if (req.method === "PATCH") { + return updateUserInformation(req, res); + } + return res.status(403).json(); +} + +export default handleAccounts; From 3158ba93d753f9b48edf5290ee27602fe6621d38 Mon Sep 17 00:00:00 2001 From: Brenda Nyokabi Bucha Date: Mon, 11 Apr 2022 17:08:07 +0300 Subject: [PATCH 17/23] Add success message --- src/components/ActNowPage/LoggedIn.js | 59 ++++++++++++++++----------- src/pages/act-now.js | 1 - 2 files changed, 36 insertions(+), 24 deletions(-) diff --git a/src/components/ActNowPage/LoggedIn.js b/src/components/ActNowPage/LoggedIn.js index 0c9a414c..65d2828b 100644 --- a/src/components/ActNowPage/LoggedIn.js +++ b/src/components/ActNowPage/LoggedIn.js @@ -1,4 +1,5 @@ import { Box, Button } from "@material-ui/core"; +import Alert from "@material-ui/lab/Alert"; import clsx from "clsx"; import { signOut } from "next-auth/react"; import Image from "next/image"; @@ -39,6 +40,7 @@ function ActNowLoggedInPage({ } }; + console.log(individualUpdateDialogArgs); const handleClickIndividual = () => setOpenDialog("individual"); const formatedItems = [ @@ -53,31 +55,42 @@ function ActNowLoggedInPage({ }); const aside = ( - - +
+ + - + - - + + +
+ {open === false ? ( + + Your Profile details have been updated! + + ) : ( + "" + )} +
+
); return ( Date: Tue, 12 Apr 2022 09:57:36 +0300 Subject: [PATCH 18/23] Add get request --- src/components/ActNowPage/LoggedIn.js | 1 - src/components/IndividualUpdateForm/index.js | 1 - src/lib/actnow/index.js | 26 ++++++++++++++++++-- src/pages/api/accounts/index.js | 3 +++ src/pages/api/accounts/update/[id].js | 17 +++++++++++++ 5 files changed, 44 insertions(+), 4 deletions(-) diff --git a/src/components/ActNowPage/LoggedIn.js b/src/components/ActNowPage/LoggedIn.js index 65d2828b..7a9771ff 100644 --- a/src/components/ActNowPage/LoggedIn.js +++ b/src/components/ActNowPage/LoggedIn.js @@ -40,7 +40,6 @@ function ActNowLoggedInPage({ } }; - console.log(individualUpdateDialogArgs); const handleClickIndividual = () => setOpenDialog("individual"); const formatedItems = [ diff --git a/src/components/IndividualUpdateForm/index.js b/src/components/IndividualUpdateForm/index.js index 58b6d95e..f82bee1d 100644 --- a/src/components/IndividualUpdateForm/index.js +++ b/src/components/IndividualUpdateForm/index.js @@ -77,7 +77,6 @@ function IndividualRegistrationForm({ headers, body: JSON.stringify(body), }); - const responseJson = await response.json(); if (response.status === 201) { setStatus({ children: undefined }); diff --git a/src/lib/actnow/index.js b/src/lib/actnow/index.js index 7701df5e..9ee9163c 100644 --- a/src/lib/actnow/index.js +++ b/src/lib/actnow/index.js @@ -91,7 +91,7 @@ function actnow(site) { } } - async function updateUser(profile, session) { + async function getUserProfile(profile, session) { const { accessToken, user: { @@ -103,9 +103,26 @@ function actnow(site) { "Content-Type": "application/json", Authorization: `Bearer ${accessToken}`, }); + const responseData = await fetch(`${ACTNOW_URL}/v1/profiles/users/${id}/`, { + method: "GET", + headers, + body: JSON.stringify(profile), + }).then(async (response) => response.json()); + return responseData; + } - console.log(profile, session); + async function updateUser(profile, session) { + const { + accessToken, + user: { + profile: { id }, + }, + } = session; + const headers = new Headers({ + "Content-Type": "application/json", + Authorization: `Bearer ${accessToken}`, + }); const responseData = await fetch(`${ACTNOW_URL}/v1/profiles/users/${id}/`, { method: "PATCH", headers, @@ -149,6 +166,11 @@ function actnow(site) { return refreshLoggedInUser(token); })(); }, + getUserDetails: (profile, session) => { + return (async () => { + return getUserProfile(profile, session); + })(); + }, updateUserDetails: (profile, session) => { return (async () => { return updateUser(profile, session); diff --git a/src/pages/api/accounts/index.js b/src/pages/api/accounts/index.js index ba0b8a27..d86877d4 100644 --- a/src/pages/api/accounts/index.js +++ b/src/pages/api/accounts/index.js @@ -13,6 +13,9 @@ async function handleAccounts(req, res) { if (req.method === "POST") { return registerNewUser(req, res); } + if (req.method === "GET") { + return registerNewUser(req, res); + } return res.status(403).json(); } diff --git a/src/pages/api/accounts/update/[id].js b/src/pages/api/accounts/update/[id].js index 58ecaae3..1f35debd 100644 --- a/src/pages/api/accounts/update/[id].js +++ b/src/pages/api/accounts/update/[id].js @@ -2,6 +2,20 @@ import { getSession } from "next-auth/react"; import actnow from "@/promisetracker/lib/actnow"; +async function getUserInformation(req, res) { + const session = await getSession({ req }); + try { + const results = await actnow() + .accounts() + .getUserDetails(req?.body, session); + console.log(results); + return res.status(201).json(results); + } catch (error) { + console.log(error); + return res.status(500).json(error); + } +} + async function updateUserInformation(req, res) { const session = await getSession({ req }); try { @@ -16,6 +30,9 @@ async function updateUserInformation(req, res) { } async function handleAccounts(req, res) { + if (req.method === "GET") { + return getUserInformation(req, res); + } if (req.method === "PATCH") { return updateUserInformation(req, res); } From 5b76e0c3a46a461dacee83d95db4e2d1653874c3 Mon Sep 17 00:00:00 2001 From: Brenda Nyokabi Bucha Date: Wed, 13 Apr 2022 12:11:11 +0300 Subject: [PATCH 19/23] Add get request --- src/components/IndividualUpdateForm/index.js | 1 - src/lib/actnow/index.js | 7 +++---- src/pages/api/accounts/getProfile/[id].js | 22 ++++++++++++++++++++ src/pages/api/accounts/update/[id].js | 18 ---------------- 4 files changed, 25 insertions(+), 23 deletions(-) create mode 100644 src/pages/api/accounts/getProfile/[id].js diff --git a/src/components/IndividualUpdateForm/index.js b/src/components/IndividualUpdateForm/index.js index f82bee1d..aff2ab50 100644 --- a/src/components/IndividualUpdateForm/index.js +++ b/src/components/IndividualUpdateForm/index.js @@ -58,7 +58,6 @@ function IndividualRegistrationForm({ phone_number: phoneNumber, social_media_link: socialMedia, }; - console.log(body); if (submitUrl) { const { accessToken, diff --git a/src/lib/actnow/index.js b/src/lib/actnow/index.js index 9ee9163c..12780970 100644 --- a/src/lib/actnow/index.js +++ b/src/lib/actnow/index.js @@ -91,7 +91,7 @@ function actnow(site) { } } - async function getUserProfile(profile, session) { + async function getUserProfile(session) { const { accessToken, user: { @@ -106,7 +106,6 @@ function actnow(site) { const responseData = await fetch(`${ACTNOW_URL}/v1/profiles/users/${id}/`, { method: "GET", headers, - body: JSON.stringify(profile), }).then(async (response) => response.json()); return responseData; } @@ -166,9 +165,9 @@ function actnow(site) { return refreshLoggedInUser(token); })(); }, - getUserDetails: (profile, session) => { + getUserDetails: (session) => { return (async () => { - return getUserProfile(profile, session); + return getUserProfile(session); })(); }, updateUserDetails: (profile, session) => { diff --git a/src/pages/api/accounts/getProfile/[id].js b/src/pages/api/accounts/getProfile/[id].js new file mode 100644 index 00000000..ecb4da8f --- /dev/null +++ b/src/pages/api/accounts/getProfile/[id].js @@ -0,0 +1,22 @@ +import { getSession } from "next-auth/react"; + +import actnow from "@/promisetracker/lib/actnow"; + +async function getUserInformation(req, res) { + const session = await getSession({ req }); + try { + const results = await actnow().accounts().getUserDetails(session); + return res.status(201).json(results); + } catch (error) { + return res.status(500).json(error); + } +} + +async function handleAccounts(req, res) { + if (req.method === "GET") { + return getUserInformation(req, res); + } + return res.status(403).json(); +} + +export default handleAccounts; diff --git a/src/pages/api/accounts/update/[id].js b/src/pages/api/accounts/update/[id].js index 1f35debd..c4461fc4 100644 --- a/src/pages/api/accounts/update/[id].js +++ b/src/pages/api/accounts/update/[id].js @@ -2,20 +2,6 @@ import { getSession } from "next-auth/react"; import actnow from "@/promisetracker/lib/actnow"; -async function getUserInformation(req, res) { - const session = await getSession({ req }); - try { - const results = await actnow() - .accounts() - .getUserDetails(req?.body, session); - console.log(results); - return res.status(201).json(results); - } catch (error) { - console.log(error); - return res.status(500).json(error); - } -} - async function updateUserInformation(req, res) { const session = await getSession({ req }); try { @@ -24,15 +10,11 @@ async function updateUserInformation(req, res) { .updateUserDetails(req?.body, session); return res.status(201).json(results); } catch (error) { - console.log(error); return res.status(500).json(error); } } async function handleAccounts(req, res) { - if (req.method === "GET") { - return getUserInformation(req, res); - } if (req.method === "PATCH") { return updateUserInformation(req, res); } From d0391cac54797ab2c27d5813569b82d0faf88346 Mon Sep 17 00:00:00 2001 From: Brenda Nyokabi Bucha Date: Thu, 14 Apr 2022 09:57:29 +0300 Subject: [PATCH 20/23] Add inital values --- src/components/IndividualUpdateForm/index.js | 39 ++++++++++++++++---- src/pages/act-now.js | 1 - 2 files changed, 31 insertions(+), 9 deletions(-) diff --git a/src/components/IndividualUpdateForm/index.js b/src/components/IndividualUpdateForm/index.js index aff2ab50..e304a2da 100644 --- a/src/components/IndividualUpdateForm/index.js +++ b/src/components/IndividualUpdateForm/index.js @@ -14,16 +14,39 @@ function IndividualRegistrationForm({ const { data: session } = useSession(); const [status, setStatus] = useState({}); + const getAccountDetails = async () => { + const { + accessToken, + user: { + profile: { id }, + }, + } = session; + + const headers = new Headers({ + "Content-Type": "application/json", + Accept: "application/json", + Authorization: `Bearer ${accessToken}`, + }); + const response = await fetch(`/api/accounts/getProfile/${id}`, { + method: "GET", + headers, + }); + const responseJson = await response.json(); + return responseJson; + }; + return ( { + return { + firstName: `${profile.first_name}`, + lastName: `${profile.last_name}`, + location: `${profile.location}`, + bio: `${profile.bio}`, + phoneNumber: `${profile.phone_number}`, + socialMedia: `${profile.social_media_link}`, + }; + })} validate={(values) => { const errors = {}; if (!values.firstName) { diff --git a/src/pages/act-now.js b/src/pages/act-now.js index 449cffa2..c3f6bf41 100644 --- a/src/pages/act-now.js +++ b/src/pages/act-now.js @@ -115,7 +115,6 @@ export async function getStaticProps({ locale }) { headers, }).then(async (response) => response.json()); actNow.summary = actnowSummary?.summary; - return { props: { ...page, From 7fe0591f21ee3660387fd0bf1ee25171d5b0d226 Mon Sep 17 00:00:00 2001 From: Brenda Nyokabi Bucha Date: Thu, 14 Apr 2022 14:29:09 +0300 Subject: [PATCH 21/23] Update api, minor cleanup --- src/components/IndividualUpdateForm/index.js | 4 ++-- src/pages/api/accounts/getProfile/[id].js | 22 ------------------- src/pages/api/accounts/index.js | 3 --- .../api/accounts/{update => profile}/[id].js | 13 +++++++++++ 4 files changed, 15 insertions(+), 27 deletions(-) delete mode 100644 src/pages/api/accounts/getProfile/[id].js rename src/pages/api/accounts/{update => profile}/[id].js (62%) diff --git a/src/components/IndividualUpdateForm/index.js b/src/components/IndividualUpdateForm/index.js index e304a2da..c4d90f2f 100644 --- a/src/components/IndividualUpdateForm/index.js +++ b/src/components/IndividualUpdateForm/index.js @@ -27,7 +27,7 @@ function IndividualRegistrationForm({ Accept: "application/json", Authorization: `Bearer ${accessToken}`, }); - const response = await fetch(`/api/accounts/getProfile/${id}`, { + const response = await fetch(`/api/accounts/profile/${id}`, { method: "GET", headers, }); @@ -94,7 +94,7 @@ function IndividualRegistrationForm({ Accept: "application/json", Authorization: `Bearer ${accessToken}`, }); - const response = await fetch(`/api/accounts/update/${id}`, { + const response = await fetch(`/api/accounts/profile/${id}`, { method: "PATCH", headers, body: JSON.stringify(body), diff --git a/src/pages/api/accounts/getProfile/[id].js b/src/pages/api/accounts/getProfile/[id].js deleted file mode 100644 index ecb4da8f..00000000 --- a/src/pages/api/accounts/getProfile/[id].js +++ /dev/null @@ -1,22 +0,0 @@ -import { getSession } from "next-auth/react"; - -import actnow from "@/promisetracker/lib/actnow"; - -async function getUserInformation(req, res) { - const session = await getSession({ req }); - try { - const results = await actnow().accounts().getUserDetails(session); - return res.status(201).json(results); - } catch (error) { - return res.status(500).json(error); - } -} - -async function handleAccounts(req, res) { - if (req.method === "GET") { - return getUserInformation(req, res); - } - return res.status(403).json(); -} - -export default handleAccounts; diff --git a/src/pages/api/accounts/index.js b/src/pages/api/accounts/index.js index d86877d4..ba0b8a27 100644 --- a/src/pages/api/accounts/index.js +++ b/src/pages/api/accounts/index.js @@ -13,9 +13,6 @@ async function handleAccounts(req, res) { if (req.method === "POST") { return registerNewUser(req, res); } - if (req.method === "GET") { - return registerNewUser(req, res); - } return res.status(403).json(); } diff --git a/src/pages/api/accounts/update/[id].js b/src/pages/api/accounts/profile/[id].js similarity index 62% rename from src/pages/api/accounts/update/[id].js rename to src/pages/api/accounts/profile/[id].js index c4461fc4..e46acde8 100644 --- a/src/pages/api/accounts/update/[id].js +++ b/src/pages/api/accounts/profile/[id].js @@ -2,6 +2,16 @@ import { getSession } from "next-auth/react"; import actnow from "@/promisetracker/lib/actnow"; +async function getUserInformation(req, res) { + const session = await getSession({ req }); + try { + const results = await actnow().accounts().getUserDetails(session); + return res.status(201).json(results); + } catch (error) { + return res.status(500).json(error); + } +} + async function updateUserInformation(req, res) { const session = await getSession({ req }); try { @@ -15,6 +25,9 @@ async function updateUserInformation(req, res) { } async function handleAccounts(req, res) { + if (req.method === "GET") { + return getUserInformation(req, res); + } if (req.method === "PATCH") { return updateUserInformation(req, res); } From 6b671002ac7ecd25f3dd7dbc1ccc44362d7ea9cf Mon Sep 17 00:00:00 2001 From: Brenda Nyokabi Bucha Date: Wed, 20 Apr 2022 09:42:06 +0300 Subject: [PATCH 22/23] Rmove session --- src/components/ActNowPage/LoggedIn.js | 2 +- src/pages/api/accounts/profile/[id].js | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/components/ActNowPage/LoggedIn.js b/src/components/ActNowPage/LoggedIn.js index 7a9771ff..39817ef8 100644 --- a/src/components/ActNowPage/LoggedIn.js +++ b/src/components/ActNowPage/LoggedIn.js @@ -36,7 +36,7 @@ function ActNowLoggedInPage({ if (onClose) { onClose(); } else { - setOpen(false); + setOpen(true); } }; diff --git a/src/pages/api/accounts/profile/[id].js b/src/pages/api/accounts/profile/[id].js index e46acde8..bcdb0f3f 100644 --- a/src/pages/api/accounts/profile/[id].js +++ b/src/pages/api/accounts/profile/[id].js @@ -3,9 +3,8 @@ import { getSession } from "next-auth/react"; import actnow from "@/promisetracker/lib/actnow"; async function getUserInformation(req, res) { - const session = await getSession({ req }); try { - const results = await actnow().accounts().getUserDetails(session); + const results = await actnow().accounts().getUserDetails(); return res.status(201).json(results); } catch (error) { return res.status(500).json(error); From b7a7e6b8063cd62a331aed2efe535ffa4abc926e Mon Sep 17 00:00:00 2001 From: Brenda Nyokabi Bucha Date: Wed, 20 Apr 2022 09:49:25 +0300 Subject: [PATCH 23/23] Minor update --- src/components/IndividualUpdateForm/index.js | 10 +++++----- src/pages/api/accounts/profile/[id].js | 4 +++- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/components/IndividualUpdateForm/index.js b/src/components/IndividualUpdateForm/index.js index c4d90f2f..c1a92845 100644 --- a/src/components/IndividualUpdateForm/index.js +++ b/src/components/IndividualUpdateForm/index.js @@ -39,12 +39,12 @@ function IndividualRegistrationForm({ { return { - firstName: `${profile.first_name}`, - lastName: `${profile.last_name}`, + firstName: `${profile?.first_name}`, + lastName: `${profile?.last_name}`, location: `${profile.location}`, - bio: `${profile.bio}`, - phoneNumber: `${profile.phone_number}`, - socialMedia: `${profile.social_media_link}`, + bio: `${profile?.bio}`, + phoneNumber: `${profile?.phone_number}`, + socialMedia: `${profile?.social_media_link}`, }; })} validate={(values) => { diff --git a/src/pages/api/accounts/profile/[id].js b/src/pages/api/accounts/profile/[id].js index bcdb0f3f..eac938ef 100644 --- a/src/pages/api/accounts/profile/[id].js +++ b/src/pages/api/accounts/profile/[id].js @@ -3,8 +3,10 @@ import { getSession } from "next-auth/react"; import actnow from "@/promisetracker/lib/actnow"; async function getUserInformation(req, res) { + const session = await getSession({ req }); try { - const results = await actnow().accounts().getUserDetails(); + const results = await actnow().accounts().getUserDetails(session); + console.log(results); return res.status(201).json(results); } catch (error) { return res.status(500).json(error);