Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
S
scratch-www
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Analytics
Analytics
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
xpstem
scratch-www
Commits
7b2e7582
Commit
7b2e7582
authored
Nov 01, 2019
by
Ben Wheeler
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
revise function names and handling of registration errors
parent
e3d3d97c
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
46 additions
and
44 deletions
+46
-44
src/components/join-flow/join-flow.jsx
src/components/join-flow/join-flow.jsx
+46
-44
No files found.
src/components/join-flow/join-flow.jsx
View file @
7b2e7582
...
...
@@ -43,12 +43,12 @@ class JoinFlow extends React.Component {
this
.
state
=
this
.
initialState
;
}
canTryAgain
()
{
return
(
this
.
state
.
registrationError
.
can
TryAgain
&&
this
.
state
.
numAttempts
<=
1
);
return
(
this
.
state
.
registrationError
.
errorAllows
TryAgain
&&
this
.
state
.
numAttempts
<=
1
);
}
handleCaptchaError
()
{
this
.
setState
({
registrationError
:
{
can
TryAgain
:
false
,
errorAllows
TryAgain
:
false
,
errorMsg
:
this
.
props
.
intl
.
formatMessage
({
id
:
'
registration.errorCaptcha
'
})
...
...
@@ -64,36 +64,34 @@ class JoinFlow extends React.Component {
this
.
handleSubmitRegistration
(
this
.
state
.
formData
);
});
}
getBodyErrors
(
err
,
body
,
res
)
{
return
(
!
err
&&
res
.
statusCode
===
200
&&
body
&&
body
[
0
]
&&
body
[
0
].
errors
);
}
getSingleError
(
bodyErrors
)
{
if
(
Object
.
keys
(
bodyErrors
).
length
===
1
)
{
const
fieldName
=
Object
.
keys
(
bodyErrors
)[
0
];
if
(
bodyErrors
[
fieldName
].
length
===
1
)
{
return
{
fieldName
:
fieldName
,
errorStr
:
bodyErrors
[
fieldName
][
0
]};
getErrorsFromResponse
(
err
,
body
,
res
)
{
const
errorsFromResponse
=
[];
if
(
!
err
&&
res
.
statusCode
===
200
&&
body
&&
body
[
0
])
{
const
responseBodyErrors
=
body
[
0
].
errors
;
if
(
responseBodyErrors
)
{
Object
.
keys
(
responseBodyErrors
).
forEach
(
fieldName
=>
{
const
errorStrs
=
responseBodyErrors
[
fieldName
];
errorStrs
.
forEach
(
errorStr
=>
{
errorsFromResponse
.
push
({
fieldName
:
fieldName
,
errorStr
:
errorStr
});
});
});
}
}
return
null
;
return
errorsFromResponse
;
}
getCustomErrMsg
(
bodyErrors
)
{
getCustomErrMsg
(
errorsFromResponse
)
{
if
(
!
errorsFromResponse
||
errorsFromResponse
.
length
===
0
)
return
null
;
let
customErrMsg
=
''
;
// body can include zero or more error objects, each
// with its own key and description. Here we assemble
// body can include zero or more error objects. Here we assemble
// all of them into a single string, customErrMsg.
const
errorKeys
=
Object
.
keys
(
bodyErrors
);
errorKeys
.
forEach
(
key
=>
{
const
vals
=
bodyErrors
[
key
];
vals
.
forEach
(
val
=>
{
if
(
customErrMsg
.
length
)
customErrMsg
+=
'
;
'
;
customErrMsg
+=
`
${
key
}
:
${
val
}
`
;
});
errorsFromResponse
.
forEach
(
errorFromResponse
=>
{
if
(
customErrMsg
.
length
)
customErrMsg
+=
'
;
'
;
customErrMsg
+=
`
${
errorFromResponse
.
fieldName
}
:
${
errorFromResponse
.
errorStr
}
`
;
});
if
(
!
customErrMsg
)
return
null
;
// if no key-val pairs
const
problemsStr
=
this
.
props
.
intl
.
formatMessage
({
id
:
'
registration.problemsAre
'
});
return
`
${
problemsStr
}
"
${
customErrMsg
}
"`
;
return
`
${
problemsStr
}
:
"
${
customErrMsg
}
"`
;
}
getRegistrationSuccess
(
err
,
body
,
res
)
{
registrationIsSuccessful
(
err
,
body
,
res
)
{
return
!!
(
!
err
&&
res
.
statusCode
===
200
&&
body
&&
body
[
0
]
&&
body
[
0
].
success
);
}
// example of failing response:
...
...
@@ -126,41 +124,45 @@ class JoinFlow extends React.Component {
numAttempts
:
this
.
state
.
numAttempts
+
1
,
waiting
:
false
},
()
=>
{
const
success
=
this
.
getRegistrationSuccess
(
err
,
body
,
res
);
const
success
=
this
.
registrationIsSuccessful
(
err
,
body
,
res
);
if
(
success
)
{
this
.
props
.
refreshSession
();
this
.
setState
({
step
:
this
.
state
.
step
+
1
});
return
;
}
// now we know there was some error.
// if the error was client-side, prompt user to try again
if
(
err
||
(
res
.
statusCode
>=
400
&&
res
.
statusCode
<
500
))
{
this
.
setState
({
registrationError
:
{
canTryAgain
:
true
}});
// now we know something went wrong -- either an actual error (client-side
// or server-side), or just a problem with the registration content.
// if an actual error, prompt user to try again.
if
(
err
||
res
.
statusCode
!==
200
)
{
this
.
setState
({
registrationError
:
{
errorAllowsTryAgain
:
true
}});
return
;
}
// now we know error was server-side.
// if server provided us info on why registration failed,
// now we know there was a problem with the registration content.
// If the server provided us info on why registration failed,
// build a summary explanation string
let
errorMsg
=
null
;
const
bodyErrors
=
this
.
getBodyErrors
(
err
,
body
,
res
);
if
(
bodyErrors
)
{
const
singleError
=
this
.
getSingleError
(
bodyErrors
);
let
singleErrMsgId
=
null
;
if
(
singleError
)
{
singleErrMsgId
=
validate
.
responseErrorMsg
(
singleError
.
fieldName
,
singleError
.
errorStr
);
}
const
errorsFromResponse
=
this
.
getErrorsFromResponse
(
err
,
body
,
res
);
// if there was exactly one error, check if we have a pre-written message
// about that precise error
if
(
errorsFromResponse
.
length
===
1
)
{
const
singleErrMsgId
=
validate
.
responseErrorMsg
(
errorsFromResponse
[
0
].
fieldName
,
errorsFromResponse
[
0
].
errorStr
);
if
(
singleErrMsgId
)
{
// one error that we have a predefined explanation string for
errorMsg
=
this
.
props
.
intl
.
formatMessage
({
id
:
singleErrMsgId
});
}
else
{
// multiple errors, or unusual error; need custom message
errorMsg
=
this
.
getCustomErrMsg
(
bodyErrors
);
}
}
// if we have more than one error, build a custom message with all of the
// server-provided error messages
if
(
!
errorMsg
&&
errorsFromResponse
.
length
>
0
)
{
errorMsg
=
this
.
getCustomErrMsg
(
errorsFromResponse
);
}
this
.
setState
({
registrationError
:
{
can
TryAgain
:
false
,
errorAllows
TryAgain
:
false
,
errorMsg
:
errorMsg
}
});
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment