(
{ initialContact, formData: formDataObject, error, notice, addressBookId }: ViewContactProps,
)
| 78 | } |
| 79 | |
| 80 | export default function ViewContact( |
| 81 | { initialContact, formData: formDataObject, error, notice, addressBookId }: ViewContactProps, |
| 82 | ) { |
| 83 | const isDeleting = useSignal<boolean>(false); |
| 84 | const contact = useSignal<Contact>(initialContact); |
| 85 | |
| 86 | const formData = convertObjectToFormData(formDataObject); |
| 87 | |
| 88 | async function onClickDeleteContact() { |
| 89 | if (confirm('Are you sure you want to delete this contact?')) { |
| 90 | if (isDeleting.value) { |
| 91 | return; |
| 92 | } |
| 93 | |
| 94 | isDeleting.value = true; |
| 95 | |
| 96 | try { |
| 97 | const requestBody: DeleteRequestBody = { contactId: contact.value.uid!, addressBookId }; |
| 98 | const response = await fetch(`/api/contacts/delete`, { |
| 99 | method: 'POST', |
| 100 | body: JSON.stringify(requestBody), |
| 101 | }); |
| 102 | |
| 103 | if (!response.ok) { |
| 104 | throw new Error(`Failed to delete contact. ${response.statusText} ${await response.text()}`); |
| 105 | } |
| 106 | |
| 107 | const result = await response.json() as DeleteResponseBody; |
| 108 | |
| 109 | if (!result.success) { |
| 110 | throw new Error('Failed to delete contact!'); |
| 111 | } |
| 112 | |
| 113 | window.location.href = '/contacts'; |
| 114 | } catch (error) { |
| 115 | console.error(error); |
| 116 | } |
| 117 | |
| 118 | isDeleting.value = false; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | return ( |
| 123 | <> |
| 124 | <section class='flex flex-row items-center justify-between mb-4'> |
| 125 | <a href='/contacts' class='mr-2'>View contacts</a> |
| 126 | <section class='flex items-center'> |
| 127 | <button |
| 128 | class='inline-block justify-center gap-x-1.5 rounded-md bg-red-800 px-3 py-2 text-sm font-semibold text-white shadow-sm hover:bg-red-600 ml-2' |
| 129 | type='button' |
| 130 | title='Delete contact' |
| 131 | onClick={() => onClickDeleteContact()} |
| 132 | > |
| 133 | <img |
| 134 | src='/public/images/delete.svg' |
| 135 | alt='Delete contact' |
| 136 | class={`white ${isDeleting.value ? 'animate-spin' : ''}`} |
| 137 | width={20} |
nothing calls this directly
no test coverage detected