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

91.89% Statements 34/37
50% Branches 3/6
94.44% Functions 17/18
90.9% Lines 30/33

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                                            23x 10x     20x 20x 20x 20x   10x     10x 10x     10x         10x 1x   1x   1x   1x           1x           10x                                             10x 8x     10x             1x     174x                       174x   174x                                                                         1x                       9x                                                                         174x   153x                                             1x                       9x                                                                  
'use client'
import { nanoid } from '@reduxjs/toolkit'
import { ArrayHelpers, FieldArray, Form, Formik, FormikProps } from 'formik'
import { useRouter } from 'next/navigation'
import { Fragment, useEffect } from 'react'
import { ObjectSchema, array, number, object, string } from 'yup'
import { CURRENCY_REQUIRED_ERROR, NAME_REQUIRED_ERROR, VALUE_GREATER_THAN_ZERO_ERROR, VALUE_REQUIRED_ERROR } from '../../../../../../content/validation'
import { Button } from '../../../../../components/ButtonsAndLinks/Button/Button'
import { Route } from '../../../../../components/ButtonsAndLinks/Route/Route'
import { FormStepsButtons } from '../../../../../components/Form/FormStepsButtons/FormStepsButtons'
import { TextInput } from '../../../../../components/Form/TextInput/TextInput'
import { Headline } from '../../../../../components/Headline/Headline'
import { IconButton } from '../../../../../components/IconButton/IconButton'
import { routes } from '../../../../../services/routes/routes'
import { useAppDispatch, useAppSelector } from '../../../../../store/hooks'
import { sendLastWillState, setInheritance, setProgressKeys } from '../../../../../store/lastwill/lastwill'
import { FinancialAsset, InheritanceFormPayload, Item } from '../../../../../types/lastWill'
import { SidebarPages } from '../../../../../types/sidebar'
 
/**
 * Inheritance Page
 */
const Inheritance = () => {
	const router = useRouter()
 
	// Global State
	const _id = useAppSelector((state) => state.lastWill.data._id)
	const financialAssets = useAppSelector((state) => state.lastWill.data.financialAssets)
	const items = useAppSelector((state) => state.lastWill.data.items)
	const isLoading = useAppSelector((state) => state.lastWill.isLoading)
 
	const dispatch = useAppDispatch()
 
	// Prepare links
	const PREVIOUS_LINK = routes.lastWill.heirs(_id)
	const NEXT_LINK = routes.lastWill.succession(_id)
 
	// Formik
	const initialFormValues: InheritanceFormPayload = {
		financialAssets,
		items,
	}
 
	const onSubmit = async (values: InheritanceFormPayload, href: string) => {
		try {
			// Update inheritance global state only if values have changed
			dispatch(setInheritance(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)
		}
	}
 
	const validationSchema: ObjectSchema<InheritanceFormPayload> = object().shape({
		financialAssets: array()
			.of(
				object().shape({
					id: string().required(),
					where: string().required(NAME_REQUIRED_ERROR),
					amount: number().min(1, VALUE_GREATER_THAN_ZERO_ERROR).required(VALUE_REQUIRED_ERROR),
					currency: string().required(CURRENCY_REQUIRED_ERROR),
				})
			)
			.required(),
		items: array()
			.of(
				object().shape({
					id: string().required(),
					name: string().required(NAME_REQUIRED_ERROR),
					description: string(),
				})
			)
			.required(),
	})
 
	// Use to handle sidebar display state and progress
	useEffect(() => {
		dispatch(setProgressKeys(SidebarPages.INHERITANCE))
	}, [dispatch])
 
	return (
		<div className="container mt-5">
			<Headline className="hidden lg:block">Erbschaft</Headline>
 
			<Formik
				initialValues={initialFormValues}
				validationSchema={validationSchema}
				onSubmit={(values) => onSubmit(values, NEXT_LINK)}
			>
				{({ values, dirty }: FormikProps<InheritanceFormPayload>) => (
					<Form>
						{/* Financial Assets */}
						<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">
								Geld Vermögen
							</Headline>
							<p className="mb-2 text-gray-500 md:mb-4">
								Wie viel Vermögen haben Sie in allen Banken, Aktien, Krypto, Bar...?
							</p>
 
							<FieldArray name="financialAssets">
								{(arrayHelpers: ArrayHelpers<FinancialAsset[]>) => (
									<div className="2xl:w-2/3">
										{values.financialAssets.map((financialAsset, index) => (
											<Fragment key={financialAsset.id}>
												{/* Financial Asset Field */}
												<div className="grid grid-cols-[1fr,auto,auto] lg:grid-cols-[2fr,3fr,auto] lg:grid-rows-1 lg:gap-x-3">
													<TextInput
														datacy={`textinput-financialAssets-${index}-where`}
														name={`financialAssets.${index}.where`}
														inputRequired
														labelText="Bank/Ort"
														placeholder="BW Bank Stuttgart, Bar..."
													/>
													<div className="col-start-1 col-end-4 row-start-2 flex gap-x-3 lg:col-start-2 lg:col-end-auto lg:row-start-1">
														<div className="w-2/3">
															<TextInput
																datacy={`textinput-financialAssets-${index}-amount`}
																name={`financialAssets.${index}.amount`}
																type="number"
																min={1}
																inputRequired
																labelText="Betrag"
																placeholder="10.000"
															/>
														</div>
														<div className="w-1/3">
															<TextInput
																datacy={`textinput-financialAssets-${index}-currency`}
																name={`financialAssets.${index}.currency`}
																inputRequired
																labelText="Währung"
																placeholder="€, Bitcoin,.."
															/>
														</div>
													</div>
													<IconButton
														datacy={`button-delete-financial-asset-${index}`}
														icon="delete"
														className="col-start-2 row-start-1 ml-2 mt-[30px] lg:col-start-3 lg:ml-0"
														disabled={values.financialAssets.length <= 1}
														onClick={() => (values.financialAssets.length <= 1 ? '' : arrayHelpers.remove(index))}
													/>
												</div>
 
												<hr className="mb-6 mt-3 border-gray-200" />
											</Fragment>
										))}
 
										{/* Add Financial Asset Button */}
										<Button
											datacy="button-add-financial-asset"
											onClick={() =>
												arrayHelpers.push({
													id: nanoid(),
													where: '',
													amount: '',
													currency: '€',
												})
											}
											type="button"
											className="ml-auto mt-4 md:mt-0"
											kind="tertiary"
										>
											Geldvermögen hinzufügen
										</Button>
									</div>
								)}
							</FieldArray>
						</div>
 
						{/* Items */}
						<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">
								Gegenstände
							</Headline>
							<p className="mb-2 text-gray-500 md:mb-4">
								Hier können Sie die Vermächtnisse erstellen, die dann in der{' '}
								<Route
									kind="tertiary"
									className="inline-flex text-red hover:text-red-600"
									href={routes.lastWill.succession('1')}
								>
									Erbfolge
								</Route>{' '}
								zugewiesen werden können.
							</p>
 
							<FieldArray name="items">
								{(arrayHelpers: ArrayHelpers) => (
									<div className="2xl:w-2/3">
										{values.items.map((item, index) => (
											<Fragment key={item.id}>
												{/* Item Field */}
												<div className="grid grid-cols-[1fr,auto] lg:grid-cols-[1fr,1fr,auto] lg:gap-x-3">
													<TextInput
														datacy={`textinput-items-${index}-name`}
														name={`items.${index}.name`}
														inputRequired
														labelText="Name des Gegenstandes"
														placeholder="Ferienhaus in Italien"
													/>
													<div className="col-start-1 col-end-3 lg:col-start-2 lg:row-start-1">
														<TextInput
															datacy={`textinput-items-${index}-description`}
															name={`items.${index}.description`}
															labelText="Beschreibung (Zweck)"
															placeholder="Die begünstigte Person soll mein Grab pflegen."
														/>
													</div>
													<IconButton
														datacy={`button-delete-item-${index}`}
														icon="delete"
														className="col-start-2 row-start-1 ml-2 mt-[30px] lg:col-start-4 lg:ml-0"
														disabled={values.items.length <= 1}
														onClick={() => (values.items.length <= 1 ? '' : arrayHelpers.remove(index))}
													/>
												</div>
 
												<hr className="mb-6 mt-3 border-gray-200" />
											</Fragment>
										))}
 
										{/* Add Item Button */}
										<Button
											datacy="button-add-item"
											onClick={() =>
												arrayHelpers.push({
													id: nanoid(),
													name: '',
													description: '',
												} as Item)
											}
											type="button"
											className="ml-auto mt-4 md:mt-0"
											kind="tertiary"
										>
											Gegenstand hinzufügen
										</Button>
									</div>
								)}
							</FieldArray>
						</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 Inheritance