All files / app/(dynamic)/last-will/(pages)/start page.tsx

92% Statements 23/25
81.25% Branches 13/16
100% Functions 7/7
92% Lines 23/25

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                                            15x         15x         15x                                                     15x 16x 16x 16x   16x 4x 4x   3x 1x 1x     2x 2x 2x 2x                 16x                                       45x                             5x                 1x                                           5x                 1x                                                                                          
'use client'
import { Form, Formik, FormikProps } from 'formik'
import Image from 'next/image'
import { useRouter } from 'next/navigation'
import { useState } from 'react'
import { ObjectSchema, boolean, object } from 'yup'
import image from '../../../../../assets/images/layout/family2.jpg'
import { Alert, AlertProps } from '../../../../../components/Alert/Alert'
import { Button } from '../../../../../components/ButtonsAndLinks/Button/Button'
import { Route } from '../../../../../components/ButtonsAndLinks/Route/Route'
import { FormError } from '../../../../../components/Errors/FormError/FormError'
import { CustomSelectionButton } from '../../../../../components/Form/CustomSelectionButton/CustomSelectionButton'
import { Label } from '../../../../../components/Form/Label/Label'
import { Headline } from '../../../../../components/Headline/Headline'
import { createLastWill } from '../../../../../services/api/lastwill/createLastWill'
import { routes } from '../../../../../services/routes/routes'
 
type StartLegal = {
	germanCitizenship?: boolean
	germanRightOfInheritance?: boolean
}
 
const initialFormValues: StartLegal = {
	germanCitizenship: undefined,
	germanRightOfInheritance: undefined,
}
 
const validationSchema: ObjectSchema<StartLegal> = object().shape({
	germanCitizenship: boolean().required('Dieses Feld ist erforderlich. Bitte wählen Sie eine Option aus.'),
	germanRightOfInheritance: boolean().required('Dieses Feld ist erforderlich. Bitte wählen Sie eine Option aus.'),
})
 
const alertContent: { [key: string]: AlertProps } = {
	PLANS_LIMIT_EXCEEDED: {
		headline: 'Limit erreicht',
		description: (
			<div>
				<p className="mb-4">
					Sie haben bereits die maximale Anzahl an Testamenten erstellt. Sie können ihr limit erhöhen oder bestehende
					Testamente löschen.
				</p>
				<div className="flex flex-col items-center gap-3 md:flex-row">
					<Route href={routes.lastWill.buy()}>Plan erhöhen</Route>
					<Route kind="tertiary" href={routes.profile.myLastWills}>
						Testamente verwalten
					</Route>
				</div>
			</div>
		),
	},
	ERROR: {
		headline: 'Fehler',
		description: 'Es ist ein Fehler aufgetreten. Bitte versuchen Sie es später erneut.',
	},
}
 
/**
 * Last Will Start Page for Legal.
 */
const Start = () => {
	const router = useRouter()
	const [isLoading, setIsLoading] = useState(false)
	const [error, setError] = useState<string | null>(null)
 
	const onSubmit = async () => {
		setIsLoading(true)
		const response = await createLastWill()
 
		if (response === 'UNAUTHORIZED') {
			router.push(routes.account.login({ callbackUrl: routes.lastWill.start }))
			return
		}
 
		Eif (response === 'PLANS_LIMIT_EXCEEDED' || response === 'ERROR') {
			setError(response)
			setIsLoading(false)
			return
		}
 
		if (response) {
			// Redirect to Testator Page
			router.push(routes.lastWill.testator(response._id))
		}
	}
 
	return (
		<div className="container mb-5 mt-8 lg:mt-[30px] lg:flex lg:min-h-[calc(100vh-130px-60px)]">
			{/* Left Image with Text */}
			<header className="relative mb-5 rounded-2xl bg-black bg-opacity-50 bg-cover bg-no-repeat bg-blend-darken lg:mb-0 lg:w-1/2 xl:w-1/3">
				{/* Image absolute */}
				<Image className="absolute -z-10 h-full rounded-2xl object-cover object-center" src={image} alt="Couple" />
 
				{/* Content */}
				<div className="flex h-full flex-col justify-center p-5 md:p-10">
					<Headline className="text-yellow">Jetzt loslegen</Headline>
					<p className="text-white md:text-lg">
						Erstellen Sie Ihr Testament in nur wenigen Schritten und hinterlassen Sie ein Vermächtnis, das zählt.
					</p>
				</div>
			</header>
			{/* Left Image with Text end */}
 
			{/* Form Fields */}
			<Formik initialValues={initialFormValues} validationSchema={validationSchema} onSubmit={onSubmit}>
				{({ values, setFieldValue, dirty }: FormikProps<StartLegal>) => (
					<Form className="flex h-full flex-col lg:pl-10 xl:w-1/2">
						{/* German Citizenship Field */}
						<div className="mb-5 lg:my-10">
							<Label
								name="germanCitizenship"
								className="mb-2 block font-semibold"
								labelText="Besitzt der Erblasser die deutsche Staatsbürgerschaft?"
								isLegend
								inputRequired
							/>
							<div className="mb-2 grid grid-cols-2 gap-3">
								<CustomSelectionButton
									datacy="field-germanCitizenship-true"
									active={values.germanCitizenship === true}
									activeColor="green"
									onClick={() => setFieldValue('germanCitizenship', true)}
									headline="Ja"
									description="Der Erblasser besitzt die deutsche Staatsbürgerschaft."
								/>
								<CustomSelectionButton
									datacy="field-germanCitizenship-false"
									active={values.germanCitizenship === false}
									activeColor="red"
									activeIcon="cancel"
									onClick={() => setFieldValue('germanCitizenship', false)}
									headline="Nein"
									description="Er besitzt eine ausländische Staatsbürgerschaft."
								/>
							</div>
							<FormError fieldName="germanCitizenship" />
						</div>
 
						{/* German Right of Inheritance Field */}
						<div className="mb-5 md:mb-10">
							<Label
								name="germanRightOfInheritance"
								className="mb-2 block font-semibold"
								labelText="Soll das Testament nach deutschem Erbrecht verfasst werden?"
								isLegend
								inputRequired
							/>
							<div className="mb-2 grid grid-cols-2 gap-3">
								<CustomSelectionButton
									datacy="field-germanRightOfInheritance-true"
									active={values.germanRightOfInheritance === true}
									activeColor="green"
									onClick={() => setFieldValue('germanRightOfInheritance', true)}
									headline="Ja"
									description="Das Testament soll nach Deutschem Erbrecht verfasst werden."
								/>
								<CustomSelectionButton
									datacy="field-germanRightOfInheritance-false"
									active={values.germanRightOfInheritance === false}
									activeColor="red"
									activeIcon="cancel"
									onClick={() => setFieldValue('germanRightOfInheritance', false)}
									headline="Nein"
									description="Das Testament soll nach einem anderen Erbrecht verfasst werden."
								/>
							</div>
							<FormError fieldName="germanRightOfInheritance" />
						</div>
 
						{/* Alert editor cannot be used */}
						{dirty && (values.germanCitizenship === false || values.germanRightOfInheritance === false) && (
							<Alert
								datacy="alert"
								className="mb-5"
								headline="Nutzung nicht möglich"
								description="Der Editor kann nur genutzt werden, wenn der Erblasser die deutsche Staatsbürgerschaft besitzt und das Testament nach deutschem Erbrecht verfasst werden soll."
							/>
						)}
 
						{/* Error alert */}
						{error && (
							<div className="mb-4">
								<Alert {...alertContent[error]} />
							</div>
						)}
 
						{/* Submit Button */}
						<Button
							datacy="button-submit"
							type="submit"
							icon="arrow_forward"
							disabled={values.germanCitizenship === false || values.germanRightOfInheritance === false}
							className="ml-auto mt-auto"
							loading={isLoading}
						>
							Nächster Schritt
						</Button>
					</Form>
				)}
			</Formik>
			{/* Form Fields end */}
		</div>
	)
}
 
export default Start