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

90% Statements 27/30
75% Branches 15/20
100% Functions 11/11
90% Lines 27/30

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                                                            47x 29x   29x 29x 29x 29x   29x   29x 15x 11x     29x 14x     29x                                                     28x   1x                                 1x 1x               3x 3x                             168x                                                                   1x                             1x                     1x 1x 1x             1x 1x                                      
'use client'
import { MaterialSymbol } from 'material-symbols'
import { useRouter } from 'next/navigation'
import { useCallback, useEffect, useState } from 'react'
import { Alert, AlertProps } from '../../../../../components/Alert/Alert'
import { Button } from '../../../../../components/ButtonsAndLinks/Button/Button'
import { Route } from '../../../../../components/ButtonsAndLinks/Route/Route'
import { Headline } from '../../../../../components/Headline/Headline'
import { Icon } from '../../../../../components/Icon/Icon'
import { IconButton } from '../../../../../components/IconButton/IconButton'
import { Modal } from '../../../../../components/Modal/ModalBase/Modal'
import { deleteLastWillById } from '../../../../../services/api/lastwill/deleteLastWillById'
import { getLastWills } from '../../../../../services/api/lastwill/getLastWills'
import { prepareLastWills } from '../../../../../services/profile/prepareLastWills'
import { routes } from '../../../../../services/routes/routes'
 
export type LastWillProfile = {
	id: string
	title: string
	createdAt: Date
	updatedAt: Date
	steps: {
		label: string
		icon: MaterialSymbol
	}[]
}
 
/**
 * Profile My LastWills Page
 */
const MyLastWills = () => {
	const router = useRouter()
 
	const [lastWills, setLastWills] = useState<LastWillProfile[]>([])
	const [isLoadingDelete, setIsLoadingDelete] = useState(false)
	const [selectedLastWill, setSelectedLastWill] = useState<LastWillProfile | null>(null)
	const [deleteError, setDeleteError] = useState<AlertProps | null>(null)
 
	const isDeleteModalOpen = Boolean(selectedLastWill)
 
	const fetchLastWills = useCallback(async () => {
		const lastWills = await getLastWills()
		setLastWills(prepareLastWills(lastWills))
	}, [])
 
	useEffect(() => {
		fetchLastWills()
	}, [fetchLastWills])
 
	return (
		<div>
			{lastWills.length === 0 ? (
				<div
					datacy="last-will-empty-state"
					className="mb-10 mt-10 flex flex-col items-center justify-center md:mb-0 md:mt-20"
				>
					<Headline level={3}>Erstellen Sie ein neues Testament</Headline>
					<p className="mb-2 text-gray-500 md:mb-4">Später können Sie hier ihr erstelltes Testament bearbeiten.</p>
					<Route datacy="create-new-last-will-button" href={routes.lastWill.start}>
						Neues Testament erstellen
					</Route>
				</div>
			) : (
				<>
					<div className="mb-2 flex items-center justify-between md:mb-4">
						<Headline level={3} hasMargin={false}>
							Meine Testamente <span className="ml-2 text-base text-gray-600">({lastWills.length})</span>
						</Headline>
 
						<Route datacy="create-new-last-will-button" href={routes.lastWill.start}>
							Neues Testament erstellen
						</Route>
					</div>
 
					{/* Last Will List */}
					{lastWills.map((lastWill) => (
						<div
							datacy="last-will-element"
							onClick={() => router.push(routes.lastWill.testator(lastWill.id))}
							key={lastWill.id}
							className="mb-2 cursor-pointer rounded-2xl border-2 border-gray-200 px-6 py-5 md:mb-4"
						>
							<div className="flex items-start justify-between md:items-center">
								{/* Header */}
								<div>
									<Headline hasMargin={false} level={4} size="text-lg">
										{lastWill.title}
									</Headline>
								</div>
 
								{/* Actions */}
								<div className="flex gap-2">
									<IconButton
										datacy="last-will-copy"
										onClick={(event) => {
											event.stopPropagation()
											router.push(routes.lastWill.final(lastWill.id))
										}}
										icon="draw"
									/>
									<IconButton icon="edit" />
									<IconButton
										datacy="last-will-delete"
										onClick={(event) => {
											event.stopPropagation()
											setSelectedLastWill(lastWill)
										}}
										icon="delete"
									/>
								</div>
							</div>
 
							<div className="mb-4">
								<p className="text-gray-500">erstellt am {lastWill.createdAt.toLocaleDateString()}</p>
								<p className="text-gray-500">zuletzt geändert am {lastWill.updatedAt.toLocaleDateString()}</p>
							</div>
 
							{/* Steps */}
							<div className="flex flex-col items-center space-y-2 2xl:flex-row 2xl:space-x-4 2xl:space-y-0">
								{lastWill.steps.map((step, index) => (
									<div
										key={step.label}
										className="flex flex-col items-center space-y-2 2xl:flex-row 2xl:items-center 2xl:space-x-2 2xl:space-y-0"
									>
										{/* Label with icon */}
										<div className="flex items-center space-x-1">
											<Icon
												className={`${step.icon === 'check_circle' ? 'text-green-500' : ''} ${
													step.icon === 'circle' ? 'text-orange-500' : ''
												} ${step.icon === 'cancel' ? 'text-gray-300' : ''}`}
												icon={step.icon}
											/>
											<span>{step.label}</span>
										</div>
 
										{/* Line */}
										{index < lastWill.steps.length - 1 && (
											<div className="h-4 w-[2px] self-center bg-gray-200 2xl:h-[2px] 2xl:w-8"></div>
										)}
									</div>
								))}
							</div>
						</div>
					))}
				</>
			)}
 
			{/* Delete Modal */}
			{isDeleteModalOpen && (
				<Modal
					datacy="last-will-delete-modal"
					open={isDeleteModalOpen}
					headline={`Testament löschen?`}
					onClose={() => {
						setSelectedLastWill(null)
					}}
				>
					<p className="mb-2 md:mb-4">
						Wenn Sie jetzt löschen klicken wird das Testament von {selectedLastWill?.title} für immer gelöscht.
					</p>
 
					{deleteError && <Alert {...deleteError} />}
 
					{/* Buttons */}
					<div className="mt-5 flex flex-col items-center justify-between md:flex-row">
						{/* Cancel Button */}
						<Button
							datacy="button-cancel"
							type="button"
							onClick={() => setSelectedLastWill(null)}
							className="order-1 md:order-none"
							kind="tertiary"
						>
							Abbrechen
						</Button>
 
						{/* Submit Button */}
						<Button
							datacy="button-delete"
							onClick={async () => {
								setIsLoadingDelete(true)
								const response = await deleteLastWillById(selectedLastWill?.id || '')
								Iif (response === 'ERROR') {
									setDeleteError({
										color: 'red',
										icon: 'error',
										headline: 'Fehler beim Löschen',
										description: 'Es ist ein Fehler aufgetreten. Bitte versuchen Sie es erneut.',
									})
								} else Eif (response === 'OK') {
									await fetchLastWills()
									setSelectedLastWill(null)
								}
								setIsLoadingDelete(false)
							}}
							loading={isLoadingDelete}
							className="mb-4 md:mb-0"
							icon="delete"
						>
							Löschen
						</Button>
					</div>
				</Modal>
			)}
		</div>
	)
}
 
export default MyLastWills