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

92.98% Statements 53/57
80% Branches 24/30
92.85% Functions 13/14
94.23% Lines 49/52

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                                          20x   68x 68x 68x   38x   38x 38x     38x 38x 38x 38x 38x 38x     38x 7x       38x 5x   5x 2x     38x   38x 2x 2x   2x   2x   2x 2x   2x     38x                                         21x 21x 21x   21x                           1x     1x 1x 1x                   2x 1x 1x 1x   2x                                     2x 2x                   3x 3x                             1x 1x 1x                                                                                                    
'use client'
import { useEffect, useState } from 'react'
import { Button } from '../../../../../components/ButtonsAndLinks/Button/Button'
import { DropdownButton } from '../../../../../components/ButtonsAndLinks/DropdownButton/DropdownButton'
import { FormStepsButtons } from '../../../../../components/Form/FormStepsButtons/FormStepsButtons'
import { Headline } from '../../../../../components/Headline/Headline'
import { IconButton } from '../../../../../components/IconButton/IconButton'
import { HeirsOrganisationModal } from '../../../../../components/Modal/HeirsModal/HeirsOrganisationModal/HeirsOrganisationModal'
import { HeirsPersonModal } from '../../../../../components/Modal/HeirsModal/HeirsPersonModal/HeirsPersonModal'
import { Modal } from '../../../../../components/Modal/ModalBase/Modal'
import { Tooltip } from '../../../../../components/Tooltip/Tooltip'
import { determineHeirRelationship, getPersonAddHeirsOptions } from '../../../../../services/heirs'
import { routes } from '../../../../../services/routes/routes'
import { useAppDispatch, useAppSelector } from '../../../../../store/hooks'
import { removeHeir, sendLastWillState, setProgressKeys } from '../../../../../store/lastwill/lastwill'
import { HeirsTypes, Organisation, Person, PersonType } from '../../../../../types/lastWill'
import { SidebarPages } from '../../../../../types/sidebar'
 
/**
 * Heirs Page
 */
const Heirs = () => {
	// Global State
	const heirs = useAppSelector((state) => state.lastWill.data.heirs)
	const _id = useAppSelector((state) => state.lastWill.data._id)
	const isLoading = useAppSelector((state) => state.lastWill.isLoading)
 
	const dispatch = useAppDispatch()
 
	const PREVIOUS_LINK = routes.lastWill.marriage(_id)
	const NEXT_LINK = routes.lastWill.inheritance(_id)
 
	// Local State
	const [isPersonModalOpen, setIsPersonModalOpen] = useState(false)
	const [isOrganisationModalOpen, setIsOrganisationModalOpen] = useState(false)
	const [isDeleteModalOpen, setIsDeleteModalOpen] = useState(false)
	const [selectedPerson, setSelectedPerson] = useState<Person | null>(null)
	const [selectedOrganisation, setSelectedOrganisation] = useState<Organisation | null>(null)
	const [heirsType, setHeirsType] = useState<HeirsTypes>('other')
 
	// Use to handle sidebar display state and progress
	useEffect(() => {
		dispatch(setProgressKeys(SidebarPages.HEIRS))
	}, [dispatch])
 
	// Functions
	const setDropdownOption = (type: HeirsTypes) => {
		setHeirsType(type)
 
		if (type === 'organisation') setIsOrganisationModalOpen(true)
		else setIsPersonModalOpen(true)
	}
 
	const personAddHeirsOptions = getPersonAddHeirsOptions(setDropdownOption)
 
	const deleteHeirs = async () => {
		const isPerson = selectedPerson !== null
		const isOrganisation = selectedOrganisation !== null
 
		const id = isPerson ? selectedPerson.id : isOrganisation ? selectedOrganisation.id : null
 
		Iif (id === null) return
 
		dispatch(removeHeir(id))
		await dispatch(sendLastWillState())
 
		setIsDeleteModalOpen(false)
	}
 
	return (
		<div className="container mt-5">
			<Headline className="hidden lg:block">Erben</Headline>
 
			{/* Overview all heirs */}
			{heirs.length === 0 ? (
				<p className="mb-2 text-gray-600 md:mb-4">
					Fügen Sie neue Erben wie die Mutter, Vater, Kinder, Geschwister, andere Personen oder Organisationen hinzu.
				</p>
			) : (
				<table className="mb-4 mt-2 w-full table-fixed border-collapse md:mt-8">
					<thead>
						{/* Table Header */}
						<tr>
							<th className="w-10/12"></th>
							<th className="w-2/12"></th>
						</tr>
					</thead>
					<tbody>
						{/* Persons and Organisations */}
						{heirs.map((heir) => {
							const isOrganisation = heir.type === 'organisation'
							const isPerson = heir.type !== 'organisation'
							const colType = isOrganisation ? 'organisations' : 'persons'
 
							return (
								<tr datacy={`${colType}-row-${heir.name}`} key={heir.id} className="border-b border-gray-300">
									<td className="pr-4" title={heir.name}>
										<p title={heir.name} className="truncate font-bold">
											{heir.name}
										</p>
										<p>{determineHeirRelationship(heir)}</p>
									</td>
									<td className="p-4">
										<div className="flex justify-end gap-2">
											<Tooltip content="Bearbeiten">
												<IconButton
													datacy={`${colType}-editbutton-${heir.name}`}
													onClick={() => {
														Iif (isPerson) {
															setSelectedPerson(heir)
															setIsPersonModalOpen(true)
														} else Eif (isOrganisation) {
															setSelectedOrganisation(heir)
															setIsOrganisationModalOpen(true)
														}
													}}
													icon="edit"
												/>
											</Tooltip>
											<Tooltip content="Löschen">
												<IconButton
													datacy={`${colType}-deletebutton-${heir.name}`}
													onClick={() => {
														if (isPerson) {
															setSelectedPerson(heir)
														} else Eif (isOrganisation) {
															setSelectedOrganisation(heir)
														}
														setIsDeleteModalOpen(true)
													}}
													icon="delete"
												/>
											</Tooltip>
										</div>
									</td>
								</tr>
							)
						})}
					</tbody>
				</table>
			)}
 
			{/* Modals */}
			{isPersonModalOpen && (
				<HeirsPersonModal
					isOpenModal={isPersonModalOpen}
					onClose={() => {
						setSelectedPerson(null)
						setIsPersonModalOpen(false)
					}}
					editPerson={selectedPerson}
					type={heirsType as PersonType}
				/>
			)}
			{isOrganisationModalOpen && (
				<HeirsOrganisationModal
					isOpenModal={isOrganisationModalOpen}
					onClose={() => {
						setSelectedOrganisation(null)
						setIsOrganisationModalOpen(false)
					}}
					editOrganisation={selectedOrganisation}
				/>
			)}
			<Modal
				open={isDeleteModalOpen}
				headline={`${
					selectedPerson !== null
						? selectedPerson.name ?? ''
						: selectedOrganisation !== null
						? selectedOrganisation.name
						: ''
				} löschen?`}
				onClose={() => {
					setSelectedOrganisation(null)
					setSelectedPerson(null)
					setIsDeleteModalOpen(false)
				}}
			>
				<p className="mb-2 md:mb-4">Wenn Sie jetzt löschen klicken wird der Erbe für immer entfernt.</p>
 
				{/* Buttons */}
				<div className="flex flex-col items-center justify-between md:flex-row">
					{/* Cancel Button */}
					<Button
						datacy="button-cancel"
						type="button"
						onClick={() => setIsDeleteModalOpen(false)}
						className="order-1 md:order-none"
						kind="tertiary"
					>
						Abbrechen
					</Button>
 
					{/* Submit Button */}
					<Button
						datacy="button-delete"
						onClick={deleteHeirs}
						loading={isLoading}
						className="mb-4 md:mb-0"
						icon="delete"
					>
						Löschen
					</Button>
				</div>
			</Modal>
 
			{/* Add heirs button */}
			<DropdownButton
				datacy="heirs-dropdownbutton"
				buttonProps={{
					kind: 'secondary',
					icon: 'add',
				}}
				options={personAddHeirsOptions}
			>
				Erbe hinzufügen
			</DropdownButton>
 
			{/* Form Steps Buttons */}
			<FormStepsButtons dirty={false} previousHref={PREVIOUS_LINK} nextHref={NEXT_LINK} />
		</div>
	)
}
 
export default Heirs