Skip to content
Snippets Groups Projects
Commit 1877e538 authored by Tore.Brede's avatar Tore.Brede
Browse files

GREG-156: Updating max date for role when user presses back button

parent e2f1f255
No related branches found
No related tags found
1 merge request!219GREG-156: Updating max date for role when user presses back button
Pipeline #105402 passed
import { useState, useEffect } from 'react' import { useState, useEffect } from 'react'
import { fetchJsonOpts } from '../../utils'
type RoleTypeData = { type RoleTypeData = {
id: number id: number
...@@ -11,16 +12,16 @@ type RoleTypeData = { ...@@ -11,16 +12,16 @@ type RoleTypeData = {
function useRoleTypes(): RoleTypeData[] { function useRoleTypes(): RoleTypeData[] {
const [roleTypes, setRoleTypes] = useState<RoleTypeData[]>([]) const [roleTypes, setRoleTypes] = useState<RoleTypeData[]>([])
async function fetchRoleTypes() { const fetchRoleTypes = async () => {
fetch(`/api/ui/v1/roletypes/`) const response = await fetch(`/api/ui/v1/roletypes/`, fetchJsonOpts())
.then((data) => data.text())
.then((result) => { if (response.ok) {
// The response is a JSON-array // The response is a JSON-array
setRoleTypes(JSON.parse(result)) const roleTypesJson = await response.json()
}) setRoleTypes(roleTypesJson)
.catch((error) => { } else {
console.debug(error) console.error(response)
}) }
} }
useEffect(() => { useEffect(() => {
......
export type RegisterFormData = { export type RegisterFormData = {
first_name?: string first_name?: string
last_name?: string last_name?: string
role_type?: string role_type?: number
role_start: Date role_start: Date
role_end: Date role_end: Date
contact_person_unit?: string contact_person_unit?: string
......
...@@ -58,21 +58,18 @@ const StepPersonForm = forwardRef( ...@@ -58,21 +58,18 @@ const StepPersonForm = forwardRef(
nextHandler(data) nextHandler(data)
} }
// Set up the form with the data given as input to the component
const { const {
register, register,
control, control,
handleSubmit, handleSubmit,
formState: { errors }, formState: { errors },
reset,
setValue, setValue,
getValues, getValues,
} = useForm<RegisterFormData>() trigger,
} = useForm<RegisterFormData>({ defaultValues: formData })
const onSubmit = handleSubmit(submit) const onSubmit = handleSubmit(submit)
useEffect(() => {
reset(formData)
}, [])
const handleOuChange = (event: any) => { const handleOuChange = (event: any) => {
if (event.target.value) { if (event.target.value) {
setOuChoice(event.target.value) setOuChoice(event.target.value)
...@@ -86,16 +83,40 @@ const StepPersonForm = forwardRef( ...@@ -86,16 +83,40 @@ const StepPersonForm = forwardRef(
required: t<string>('validation.roleTypeRequired'), required: t<string>('validation.roleTypeRequired'),
}) })
const getMaxDateForRoleType = (roleType: RoleTypeData | undefined) => {
if (roleType === undefined) {
return today
}
return addDays(roleType.max_days)(today)
}
function updateMaxDaysForRoleType(roleTypeId: any) {
const selectedRoleTypeInfo = roleTypes.find((rt) => rt.id === roleTypeId)
const maxDate = getMaxDateForRoleType(selectedRoleTypeInfo)
setTodayPlusMaxDays(maxDate)
// Trigger revalidation of role end since the max end date is
// different for different role types
trigger('role_end')
}
// When the component is first rendered and there is some role_type set in
// the input formData, the roleTypes-array is most
// probably empty since the data has not come back from the server yet.
// Need to have this effect here to make sure the max end date gets
// updated when the role types are available
useEffect(() => {
if (formData.role_type) {
updateMaxDaysForRoleType(formData.role_type)
}
}, [roleTypes])
const handleRoleTypeChange = (event: any) => { const handleRoleTypeChange = (event: any) => {
setValue('role_type', event.target.value) if (event.target.value) {
setSelectedRoleType(event.target.value) setValue('role_type', event.target.value)
const selectedRoleTypeInfo = roleTypes.find( setSelectedRoleType(event.target.value)
(rt) => rt.id === event.target.value updateMaxDaysForRoleType(event.target.value)
)
if (selectedRoleTypeInfo === undefined) {
setTodayPlusMaxDays(today)
} else { } else {
setTodayPlusMaxDays(addDays(selectedRoleTypeInfo.max_days)(today)) setValue('role_type', undefined)
} }
} }
......
...@@ -37,8 +37,7 @@ function StepSummary(props: StepSummaryProperties) { ...@@ -37,8 +37,7 @@ function StepSummary(props: StepSummaryProperties) {
// TODO Fix this confusing mix between use of id and identifier // TODO Fix this confusing mix between use of id and identifier
const selectedRoleType = roleTypes.find( const selectedRoleType = roleTypes.find(
(value) => (value) =>
value.id === value.id === (formData.role_type === undefined ? -1 : formData.role_type)
(formData.role_type === undefined ? -1 : parseInt(formData.role_type, 10))
) )
const roleTypeName = const roleTypeName =
selectedRoleType === undefined selectedRoleType === undefined
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment