Skip to content

Commit

Permalink
3360: on account registration redirect new users to the page they sta…
Browse files Browse the repository at this point in the history
…rted on (#2127)

* This is working.

* Cleanup, handle register without next

* format

* Remove history
  • Loading branch information
collinpreston authored Mar 18, 2024
1 parent d7a46bd commit 1227278
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ import { connect } from "react-redux"
import { mutateAsync, requestAsync } from "redux-query"
import { connectRequest } from "redux-query-react"
import { createStructuredSelector } from "reselect"
import { routes } from "../../../lib/urls"

import { qsNextSelector } from "../../../lib/selectors"

import users, { currentUserSelector } from "../../../lib/queries/users"
import { routes } from "../../../lib/urls"
import queries from "../../../lib/queries"

import {
Expand All @@ -32,10 +34,11 @@ type DispatchProps = {|
getCurrentUser: () => Promise<HttpResponse<User>>
|}

type Props = {|
type Props = {
params: { next: string },
...StateProps,
...DispatchProps
|}
}

const getInitialValues = (user: User) => ({
name: user.name,
Expand All @@ -46,7 +49,10 @@ const getInitialValues = (user: User) => ({

export class RegisterAdditionalDetailsPage extends React.Component<Props> {
async onSubmit(detailsData: any, { setSubmitting, setErrors }: any) {
const { editProfile } = this.props
const {
editProfile,
params: { next }
} = this.props

// On this page, if the user selects stuff for learner type and education
// level, we also set the field flag so we don't ping the learner later to
Expand All @@ -63,14 +69,15 @@ export class RegisterAdditionalDetailsPage extends React.Component<Props> {
}

try {
const {
body: { errors }
}: { body: Object } = await editProfile(detailsData)

if (errors && errors.length > 0) {
setErrors(errors)
const { body }: { body: Object } = await editProfile(detailsData)
if (body.errors && body.errors.length > 0) {
setErrors(body.errors)
} else {
window.location = routes.dashboard
if (next) {
window.location = next
} else {
window.location = routes.dashboard
}
}
} finally {
setSubmitting(false)
Expand Down Expand Up @@ -135,7 +142,10 @@ export class RegisterAdditionalDetailsPage extends React.Component<Props> {
}

const mapStateToProps = createStructuredSelector({
currentUser: currentUserSelector
currentUser: currentUserSelector,
params: createStructuredSelector({
next: qsNextSelector
})
})

const mapPropsToConfig = () => [queries.users.countriesQuery()]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ type DispatchProps = {|
username: string,
legalAddress: LegalAddress,
userProfile: UserProfile,
partialToken: string,
next: ?string
partialToken: string
) => Promise<HttpResponse<AuthResponse>>,
getCurrentUser: () => Promise<HttpResponse<User>>,
addUserNotification: Function
Expand Down Expand Up @@ -101,7 +100,11 @@ export class RegisterDetailsPage extends React.Component<Props> {
}

if (body.state === STATE_SUCCESS) {
const nextParam = body.redirect_url
body.redirect_url = routes.register.additionalDetails
if (nextParam) {
body.redirect_url += `?next=${encodeURIComponent(nextParam)}`
}
}

/* eslint-disable camelcase */
Expand Down
6 changes: 4 additions & 2 deletions frontend/public/src/lib/queries/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ export default {
username: string,
legalAddress: LegalAddress,
userProfile: ?UserProfile,
partialToken: string
partialToken: string,
next: ?string
) => ({
...DEFAULT_OPTIONS,
url: "/api/register/extra/",
Expand All @@ -98,7 +99,8 @@ export default {
legal_address: legalAddress,
user_profile: userProfile,
flow: FLOW_REGISTER,
partial_token: partialToken
partial_token: partialToken,
next
}
}),

Expand Down

0 comments on commit 1227278

Please sign in to comment.