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

88.23% Statements 15/17
87.5% Branches 7/8
85.71% Functions 6/7
88.23% Lines 15/17

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                                          11x         11x               11x 7x 7x   7x 1x 1x   1x       1x     7x                                       18x                             2x                                                             2x                 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 } from '../../../../../components/Alert/Alert'
import { Button } from '../../../../../components/ButtonsAndLinks/Button/Button'
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.'),
})
 
/**
 * Last Will Start Page for Legal.
 */
const Start = () => {
	const router = useRouter()
	const [isLoading, setIsLoading] = useState(false)
 
	const onSubmit = async () => {
		setIsLoading(true)
		const response = await createLastWill()
 
		Iif (response !== null) {
			// Redirect to Testator Page
			router.push(routes.lastWill.testator(response._id))
		}
		setIsLoading(false)
	}
 
	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 */}
						{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."
							/>
						)}
 
						{/* 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