Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
G
greg
Manage
Activity
Members
Labels
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
it-bott-integrasjoner
greg
Commits
8f155ee6
Commit
8f155ee6
authored
2 years ago
by
lass
Browse files
Options
Downloads
Patches
Plain Diff
Add error handling on submit
parent
909ce746
No related branches found
No related tags found
1 merge request
!398
Greg 342 roledate error
Pipeline
#187292
failed
2 years ago
Stage: venv update
Stage: tests and linting
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
frontend/src/routes/sponsor/guest/newGuestRole/index.tsx
+87
-41
87 additions, 41 deletions
frontend/src/routes/sponsor/guest/newGuestRole/index.tsx
with
87 additions
and
41 deletions
frontend/src/routes/sponsor/guest/newGuestRole/index.tsx
+
87
−
41
View file @
8f155ee6
import
{
useState
}
from
'
react
'
import
{
useEffect
,
useState
}
from
'
react
'
import
{
Controller
,
useForm
}
from
'
react-hook-form
'
import
{
Controller
,
useForm
}
from
'
react-hook-form
'
import
{
useTranslation
}
from
'
react-i18next
'
import
{
useTranslation
}
from
'
react-i18next
'
import
{
Link
,
useNavigate
,
useParams
}
from
'
react-router-dom
'
import
{
Link
,
useNavigate
,
useParams
}
from
'
react-router-dom
'
...
@@ -29,6 +29,11 @@ import { submitJsonOpts } from 'utils'
...
@@ -29,6 +29,11 @@ import { submitJsonOpts } from 'utils'
import
{
useFeatureContext
}
from
'
contexts/featureContext
'
import
{
useFeatureContext
}
from
'
contexts/featureContext
'
import
{
availableInSearchEnabled
}
from
'
appConfig
'
import
{
availableInSearchEnabled
}
from
'
appConfig
'
import
SubmitState
from
'
../../register/submitState
'
import
ServerErrorReport
,
{
ServerErrorReportData
,
}
from
'
../../../../components/errorReport
'
type
AddRoleFormData
=
{
type
AddRoleFormData
=
{
orgunit
:
number
orgunit
:
number
type
:
string
type
:
string
...
@@ -58,47 +63,11 @@ interface NewGuestRoleProps {
...
@@ -58,47 +63,11 @@ interface NewGuestRoleProps {
reloadGuestInfo
:
()
=>
void
reloadGuestInfo
:
()
=>
void
}
}
const
postRole
=
async
(
formData
:
AddRoleFormData
,
pid
:
string
)
=>
{
const
payload
:
AddRolePayload
=
{
orgunit
:
formData
.
orgunit
,
person
:
pid
,
type
:
formData
.
type
,
end_date
:
format
(
formData
.
end_date
as
Date
,
'
yyyy-MM-dd
'
),
}
if
(
formData
.
start_date
)
{
payload
.
start_date
=
format
(
formData
.
start_date
as
Date
,
'
yyyy-MM-dd
'
)
}
if
(
formData
.
contact_person_unit
)
{
payload
.
contact_person_unit
=
formData
.
contact_person_unit
}
if
(
formData
.
comments
)
{
payload
.
comments
=
formData
.
comments
}
if
(
availableInSearchEnabled
&&
formData
.
available_in_search
)
{
payload
.
available_in_search
=
formData
.
available_in_search
}
console
.
log
(
'
submitting
'
,
JSON
.
stringify
(
payload
))
fetch
(
'
/api/ui/v1/role
'
,
submitJsonOpts
(
'
POST
'
,
payload
))
.
then
((
res
)
=>
{
if
(
!
res
.
ok
)
{
console
.
log
(
'
result
'
,
res
)
return
null
}
console
.
log
(
'
result
'
,
res
)
return
res
.
text
()
})
.
then
((
result
)
=>
{
if
(
result
!==
null
)
{
console
.
log
(
'
result
'
,
result
)
}
})
.
catch
((
error
)
=>
{
console
.
log
(
'
error
'
,
error
)
})
}
function
NewGuestRole
({
guest
,
reloadGuestInfo
}:
NewGuestRoleProps
)
{
function
NewGuestRole
({
guest
,
reloadGuestInfo
}:
NewGuestRoleProps
)
{
const
[
submitState
,
setSubmitState
]
=
useState
(
SubmitState
.
NotSubmitted
)
const
{
const
{
register
,
register
,
control
,
control
,
...
@@ -118,8 +87,12 @@ function NewGuestRole({ guest, reloadGuestInfo }: NewGuestRoleProps) {
...
@@ -118,8 +87,12 @@ function NewGuestRole({ guest, reloadGuestInfo }: NewGuestRoleProps) {
const
navigate
=
useNavigate
()
const
navigate
=
useNavigate
()
const
onSubmit
=
handleSubmit
(
async
()
=>
{
const
onSubmit
=
handleSubmit
(
async
()
=>
{
await
postRole
(
getValues
(),
pid
)
await
postRole
(
getValues
(),
pid
)
reloadGuestInfo
()
reloadGuestInfo
()
navigate
(
`/sponsor/guest/
${
pid
}
`
)
if
(
submitState
==
SubmitState
.
SubmitSuccess
)
{
navigate
(
`/sponsor/guest/
${
pid
}
`
)
}
})
})
const
{
ous
}
=
useOus
()
const
{
ous
}
=
useOus
()
...
@@ -130,6 +103,8 @@ function NewGuestRole({ guest, reloadGuestInfo }: NewGuestRoleProps) {
...
@@ -130,6 +103,8 @@ function NewGuestRole({ guest, reloadGuestInfo }: NewGuestRoleProps) {
const
today
=
new
Date
()
const
today
=
new
Date
()
const
[
endDate
,
setEndDate
]
=
useState
<
Date
|
null
>
(
null
)
const
[
endDate
,
setEndDate
]
=
useState
<
Date
|
null
>
(
null
)
const
[
maxDate
,
setMaxDate
]
=
useState
(
today
)
const
[
maxDate
,
setMaxDate
]
=
useState
(
today
)
const
[
submitErrorReport
,
setSubmitErrorReport
]
=
useState
<
ServerErrorReportData
>
()
const
todayPlusMaxDays
=
(
roleTypeId
?:
number
)
=>
{
const
todayPlusMaxDays
=
(
roleTypeId
?:
number
)
=>
{
if
(
roleTypeId
)
{
if
(
roleTypeId
)
{
...
@@ -184,6 +159,68 @@ function NewGuestRole({ guest, reloadGuestInfo }: NewGuestRoleProps) {
...
@@ -184,6 +159,68 @@ function NewGuestRole({ guest, reloadGuestInfo }: NewGuestRoleProps) {
</
MenuItem
>
</
MenuItem
>
)
)
}
}
const
postRole
=
async
(
formData
:
AddRoleFormData
,
pid
:
string
)
=>
{
const
payload
:
AddRolePayload
=
{
orgunit
:
formData
.
orgunit
,
person
:
pid
,
type
:
formData
.
type
,
end_date
:
format
(
formData
.
end_date
as
Date
,
'
yyyy-MM-dd
'
),
}
if
(
formData
.
start_date
)
{
payload
.
start_date
=
format
(
formData
.
start_date
as
Date
,
'
yyyy-MM-dd
'
)
}
if
(
formData
.
contact_person_unit
)
{
payload
.
contact_person_unit
=
formData
.
contact_person_unit
}
if
(
formData
.
comments
)
{
payload
.
comments
=
formData
.
comments
}
if
(
availableInSearchEnabled
&&
formData
.
available_in_search
)
{
payload
.
available_in_search
=
formData
.
available_in_search
}
fetch
(
'
/api/ui/v1/role
'
,
submitJsonOpts
(
'
POST
'
,
payload
))
.
then
((
res
)
=>
{
if
(
!
res
.
ok
)
{
handleSubmitErrorResponse
(
res
)
}
else
{
setSubmitState
(
SubmitState
.
SubmitSuccess
)
}
})
}
function
handleSubmitErrorResponse
(
res
:
Response
)
{
// Try to extract data from body of error message
res
.
text
()
.
then
((
text
)
=>
{
setSubmitState
(
SubmitState
.
SubmitFailure
)
// Some error responses have a code that is used to look up a translated text.
// Attempt to parse the message text and extract the code
const
jsonResponse
=
JSON
.
parse
(
text
)
const
errorText
=
jsonResponse
.
code
===
undefined
?
text
:
t
(
`error.codes.
${
jsonResponse
.
code
}
`
)
setSubmitErrorReport
({
errorHeading
:
t
(
'
error.invitationCreationFailedHeader
'
),
statusCode
:
res
.
status
,
statusText
:
res
.
statusText
,
errorBodyText
:
errorText
,
})
})
.
catch
(()
=>
{
// Extracting data from body failed, just show an error message with no body text
setSubmitErrorReport
({
errorHeading
:
t
(
'
error.invitationCreationFailedHeader
'
),
statusCode
:
res
.
status
,
statusText
:
res
.
statusText
,
errorBodyText
:
undefined
,
})
setSubmitState
(
SubmitState
.
SubmitFailure
)
})
}
const
hasRoleTypeError
=
const
hasRoleTypeError
=
errors
&&
errors
.
type
&&
errors
.
type
.
type
===
'
required
'
errors
&&
errors
.
type
&&
errors
.
type
.
type
===
'
required
'
const
hasOuChoiceError
=
const
hasOuChoiceError
=
...
@@ -373,6 +410,15 @@ function NewGuestRole({ guest, reloadGuestInfo }: NewGuestRoleProps) {
...
@@ -373,6 +410,15 @@ function NewGuestRole({ guest, reloadGuestInfo }: NewGuestRoleProps) {
>
>
{
t
(
'
button.cancel
'
)
}
{
t
(
'
button.cancel
'
)
}
</
Button
>
</
Button
>
{
submitState
===
SubmitState
.
SubmitFailure
&&
submitErrorReport
!==
undefined
&&
(
<
ServerErrorReport
errorHeading
=
{
submitErrorReport
?.
errorHeading
}
statusCode
=
{
submitErrorReport
?.
statusCode
}
statusText
=
{
submitErrorReport
?.
statusText
}
errorBodyText
=
{
submitErrorReport
?.
errorBodyText
}
/>
)
}
</
Stack
>
</
Stack
>
</
form
>
</
form
>
</
Page
>
</
Page
>
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment