All files / app/(dynamic)/last-will/editor/marriage page.tsx

86.04% Statements 37/43
89.13% Branches 41/46
80% Functions 16/20
83.33% Lines 30/36

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307                                                                20x   9x     20x 9x 20x   20x 20x   20x 20x 20x   9x   9x 9x     9x         9x                                     9x                                 9x 2x   2x   2x 2x         2x             9x 5x     9x             2x     135x                             3x             2x                                                                                                                                                                                                                                                       1x                                                                  
'use client'
import { Form, Formik, FormikProps } from 'formik'
import { useRouter } from 'next/navigation'
import { useEffect } from 'react'
import { ObjectSchema, array, object, string } from 'yup'
import { partnerMoreInfosOptions } from '../../../../../../content/checkboxOptions'
import { genderOptions } from '../../../../../../content/dropdownOptions'
import { FormError } from '../../../../../components/Errors/FormError/FormError'
import { Checkbox } from '../../../../../components/Form/Checkbox/Checkbox'
import { CustomSelectionButton } from '../../../../../components/Form/CustomSelectionButton/CustomSelectionButton'
import { FormDatepicker } from '../../../../../components/Form/FormDatepicker/FormDatepicker'
import { FormDropdown } from '../../../../../components/Form/FormDropdown/FormDropdown'
import { FormStepsButtons } from '../../../../../components/Form/FormStepsButtons/FormStepsButtons'
import { Label } from '../../../../../components/Form/Label/Label'
import { TextInput } from '../../../../../components/Form/TextInput/TextInput'
import { Headline } from '../../../../../components/Headline/Headline'
import { routes } from '../../../../../services/routes/routes'
import { useAppDispatch, useAppSelector } from '../../../../../store/hooks'
import { sendLastWillState, setMarriage, setProgressKeys } from '../../../../../store/lastwill/lastwill'
import { Gender } from '../../../../../types/gender'
import {
	MarriageFormPayload,
	MatrimonialProperty,
	PartnerMoreInfos,
	Person,
	RelationshipStatus,
} from '../../../../../types/lastWill'
import { SidebarPages } from '../../../../../types/sidebar'
 
/**
 * Marriage Page
 */
const Marriage = () => {
	// Router
	const router = useRouter()
 
	// Gloabl State
	const _id = useAppSelector((state) => state.lastWill.data._id)
	const partner = useAppSelector((state) =>
		state.lastWill.data.heirs.find((heir): heir is Person => 'type' in heir && heir.type === 'partner')
	)
	const isLoading = useAppSelector((state) => state.lastWill.isLoading)
	const isBerlinWill = useAppSelector((state) => state.lastWill.data.common?.isBerlinWill) ?? false
	const isPartnerGermanCitizenship =
		useAppSelector((state) => state.lastWill.data.common?.isPartnerGermanCitizenship) ?? false
	const matrimonialProperty = useAppSelector((state) => state.lastWill.data.common?.matrimonialProperty)
	const relationshipStatus = useAppSelector((state) => state.lastWill.data.testator.relationshipStatus)
 
	const dispatch = useAppDispatch()
 
	const PREVIOUS_LINK = routes.lastWill.testator(_id)
	const NEXT_LINK = routes.lastWill.heirs(_id)
 
	// Formik
	const { isHandicapped, isInsolvent, address, name, gender, birthDate, birthPlace } = partner ?? {
		isHandicapped: false,
		isInsolvent: false,
	}
 
	const initialFormValues: MarriageFormPayload = {
		name: name ?? '',
		gender: gender ?? undefined,
		birthDate: birthDate ?? '',
		birthPlace: birthPlace ?? '',
		street: address ? address.street ?? '' : '',
		houseNumber: address ? address.houseNumber ?? '' : '',
		zipCode: address ? address.zipCode ?? '' : '',
		city: address ? address.city ?? '' : '',
		moreInfos: [
			...(isHandicapped ? ['isHandicapped'] : []),
			...(isInsolvent ? ['isInsolvent'] : []),
			...(isBerlinWill ? ['isBerlinWill'] : []),
		],
		isPartnerGermanCitizenship: isPartnerGermanCitizenship ? ['isPartnerGermanCitizenship'] : [],
		relationshipStatus: relationshipStatus ?? undefined,
		matrimonialProperty,
	}
 
	const validationSchema: ObjectSchema<MarriageFormPayload> = object().shape({
		name: string(),
		gender: string<Gender>(),
		birthDate: string(),
		birthPlace: string(),
 
		street: string(),
		houseNumber: string(),
		zipCode: string(),
		city: string(),
 
		relationshipStatus: string<RelationshipStatus>(),
		isPartnerGermanCitizenship: array<string[]>(),
		moreInfos: array<PartnerMoreInfos[]>(),
		matrimonialProperty: string<MatrimonialProperty>(),
	})
 
	const onSubmit = async (values: MarriageFormPayload, href: string) => {
		try {
			// Update marriage global state only if values have changed
			dispatch(setMarriage(values))
 
			const response = await dispatch(sendLastWillState())
			Iif (response.meta.requestStatus === 'rejected') {
				return
				// TODO: Add error handling here
			}
			// Redirect to previous or next page
			router.push(href)
		} catch (error) {
			console.error('An error occurred while submitting the form: ', error)
		}
	}
 
	// Use to handle sidebar display state and progress
	useEffect(() => {
		dispatch(setProgressKeys(SidebarPages.MARRIAGE))
	}, [dispatch])
 
	return (
		<div className="container mt-5">
			<Headline className="hidden lg:block">Familienstand</Headline>
 
			<Formik
				initialValues={initialFormValues}
				validationSchema={validationSchema}
				onSubmit={(values) => onSubmit(values, NEXT_LINK)}
			>
				{({ values, setFieldValue, dirty }: FormikProps<MarriageFormPayload>) => (
					<Form>
						{/* Marriage Field */}
						<div className="mb-4">
							<Label
								name="relationshipStatus"
								className="mb-2 block font-semibold"
								labelText="Beziehungsstatus"
								isLegend
								inputRequired
							/>
							<div className="mb-2 grid grid-cols-2 gap-3 md:grid-cols-4 xl:w-2/3">
								<CustomSelectionButton
									datacy="field-relationshipStatus-married"
									active={values.relationshipStatus === 'married'}
									icon="favorite"
									onClick={() => setFieldValue('relationshipStatus', 'married')}
									headline="Verheiratet"
								/>
								<CustomSelectionButton
									datacy="field-relationshipStatus-divorced"
									active={values.relationshipStatus === 'divorced'}
									icon="heart_broken"
									onClick={() => setFieldValue('relationshipStatus', 'divorced')}
									headline="Geschieden"
								/>
								<CustomSelectionButton
									datacy="field-relationshipStatus-widowed"
									active={values.relationshipStatus === 'widowed'}
									icon="deceased"
									onClick={() => setFieldValue('relationshipStatus', 'widowed')}
									headline="Verwitwet"
								/>
								<CustomSelectionButton
									datacy="field-relationshipStatus-unmarried"
									active={values.relationshipStatus === 'unmarried'}
									icon="man"
									onClick={() => setFieldValue('relationshipStatus', 'unmarried')}
									headline="Ledig"
								/>
							</div>
							<FormError fieldName="relationshipStatus" />
						</div>
						{/* Marriage Field end */}
 
						{/* Married Partner Fields */}
						{values.relationshipStatus === 'married' && (
							<div datacy="partner-fields">
								{/* Checkbox German Citizenship */}
								<Checkbox
									name="isPartnerGermanCitizenship"
									options={[
										{
											value: 'isPartnerGermanCitizenship',
											label: 'Besitzt ihr Partner die deutsche Staatsbürgerschaft?',
										},
									]}
								/>
 
								{/* Partner Personal Data */}
								<div className="mt-5 rounded-xl border-2 border-gray-100 px-4 py-3 md:mt-8 md:px-8 md:py-6">
									<Headline level={3} size="md:text-lg" className="mb-4">
										Persönliche Daten des Ehepartners
									</Headline>
 
									<div className="2xl:w-2/3">
										{/* Name */}
										<div className="mb-2 grid gap-x-3 md:mb-4 md:grid-cols-2">
											<TextInput
												name="name"
												inputRequired
												labelText="Vor- und Nachname"
												placeholder="Vor- und Nachname"
												autoComplete="name"
											/>
										</div>
 
										{/* Gender and Birth */}
										<div className="mb-4 grid gap-x-3 md:mb-0 md:grid-cols-3">
											<FormDropdown
												name="gender"
												labelText="Geschlecht"
												placeholder="Geschlecht"
												hasMargin
												options={genderOptions}
											/>
											<FormDatepicker name="birthDate" labelText="Geburtstag" autoComplete="bday" />
											<TextInput name="birthPlace" labelText="Geburtsort" placeholder="Geburtsort" />
										</div>
 
										{/* Adress */}
										<div className="flex gap-x-3">
											<div className="mb-2 w-2/3 md:mb-4 md:w-3/4">
												<TextInput
													name="street"
													inputRequired
													labelText="Straße"
													placeholder="Straße"
													autoComplete="street-address"
												/>
											</div>
											<div className="mb-2 w-1/3 md:mb-4 md:w-1/4">
												<TextInput name="houseNumber" inputRequired labelText="Hausnummer" placeholder="Hausnummer" />
											</div>
										</div>
 
										<div className="flex gap-x-3">
											<div className="mb-2 w-1/3 md:mb-4 md:w-1/4">
												<TextInput
													name="zipCode"
													inputRequired
													labelText="Postleitzahl"
													placeholder="Postleitzahl"
													autoComplete="postal-code"
												/>
											</div>
											<div className="w-2/3 md:w-3/4">
												<TextInput name="city" inputRequired labelText="Stadt" placeholder="Stadt" />
											</div>
										</div>
									</div>
								</div>
								{/* Partner Personal Data end */}
 
								{/* More Infos */}
								<div className="mt-5 rounded-xl border-2 border-gray-100 px-4 py-3 md:mt-8 md:px-8 md:py-6">
									<Checkbox
										name="moreInfos"
										labelText="Weitere relevante Infos"
										inputRequired
										options={partnerMoreInfosOptions}
									/>
								</div>
 
								{/* Property Status */}
								<div className="mt-5 rounded-xl border-2 border-gray-100 px-4 py-3 md:mt-8 md:px-8 md:py-6">
									<Label
										name="matrimonialProperty"
										className="mb-2 block font-semibold"
										labelText="Güterstand"
										isLegend
										inputRequired
									/>
									<div className="mb-2 grid gap-3 md:grid-cols-2 xl:w-2/3 2xl:w-1/2">
										<CustomSelectionButton
											datacy="field-matrimonialProperty-communityOfGain"
											active={values.matrimonialProperty === 'communityOfGain'}
											onClick={() => setFieldValue('matrimonialProperty', 'communityOfGain')}
											headline="Zugewinngemeinschaft"
											description="Der erwirtschaftete Vermögenszuwachs wird hälftig aufgeteilt."
										/>
										<CustomSelectionButton
											datacy="field-matrimonialProperty-separationOfProperty"
											active={values.matrimonialProperty === 'separationOfProperty'}
											onClick={() => setFieldValue('matrimonialProperty', 'separationOfProperty')}
											headline="Gütertrennung"
											description="Jeder Ehepartner behält sein eigenes Vermögen."
										/>
									</div>
									<FormError fieldName="matrimonialProperty" />
								</div>
							</div>
						)}
 
						{/* Form Steps Buttons */}
						<FormStepsButtons
							loading={isLoading}
							dirty={dirty}
							previousOnClick={() => onSubmit(values, PREVIOUS_LINK)}
							previousHref={PREVIOUS_LINK}
							nextHref={NEXT_LINK}
						/>
					</Form>
				)}
			</Formik>
		</div>
	)
}
 
export default Marriage