From da80af8b1773b7dfa968977e9953b691f732aa34 Mon Sep 17 00:00:00 2001 From: Scott Roach Date: Mon, 8 Mar 2021 02:09:07 -0800 Subject: [PATCH] Move delete logic from event list to event detail page and implement Dialog component for confirmation. Redirect to event list after delete --- web/src/routes/Event.jsx | 44 ++++++++++++++++++++++++++++++++++++--- web/src/routes/Events.jsx | 22 +------------------- 2 files changed, 42 insertions(+), 24 deletions(-) diff --git a/web/src/routes/Event.jsx b/web/src/routes/Event.jsx index cf45f9e62..6640f0dbd 100644 --- a/web/src/routes/Event.jsx +++ b/web/src/routes/Event.jsx @@ -1,5 +1,10 @@ import { h, Fragment } from 'preact'; +import { useCallback, useState } from 'preact/hooks'; +import { route } from 'preact-router'; import ActivityIndicator from '../components/ActivityIndicator'; +import Button from '../components/Button'; +import Delete from '../icons/Delete' +import Dialog from '../components/Dialog'; import Heading from '../components/Heading'; import Link from '../components/Link'; import { FetchStatus, useApiHost, useEvent } from '../api'; @@ -8,6 +13,23 @@ import { Table, Thead, Tbody, Th, Tr, Td } from '../components/Table'; export default function Event({ eventId }) { const apiHost = useApiHost(); const { data, status } = useEvent(eventId); + const [showDialog, setShowDialog] = useState(false); + + const handleClickDelete = () => { + setShowDialog(true); + } + + const handleDismissDeleteDialog = () => { + setShowDialog(false); + }; + + const handleClickDeleteDialog = useCallback( + async () => { + await fetch(`${apiHost}/api/events/${eventId}`, {method: 'DELETE'}); + setShowDialog(false); + route('/events', true); + }, [apiHost, eventId, setShowDialog] + ); if (status !== FetchStatus.LOADED) { return ; @@ -18,9 +40,25 @@ export default function Event({ eventId }) { return (
- - {data.camera} {data.label} {startime.toLocaleString()} - +
+ + {data.camera} {data.label} {startime.toLocaleString()} + + + {showDialog ? ( + + ) : null} +
diff --git a/web/src/routes/Events.jsx b/web/src/routes/Events.jsx index c126e9cad..76bce3d8e 100644 --- a/web/src/routes/Events.jsx +++ b/web/src/routes/Events.jsx @@ -7,8 +7,6 @@ import produce from 'immer'; import { route } from 'preact-router'; import { useIntersectionObserver } from '../hooks'; import { FetchStatus, useApiHost, useConfig, useEvents } from '../api'; -import Button from '../components/Button'; -import Delete from '../icons/Delete' import { Table, Thead, Tbody, Tfoot, Th, Tr, Td } from '../components/Table'; import { useCallback, useEffect, useMemo, useReducer, useState } from 'preact/hooks'; @@ -101,18 +99,6 @@ export default function Events({ path: pathname, limit = API_LIMIT } = {}) { [limit, pathname, setSearchString] ); - const handleDelete = useCallback( - async (eventId) => { - // eslint-disable-next-line no-alert - if(confirm('Are you sure you want to delete this event and any related clips and snapshots?')) { - await fetch(`${apiHost}/api/events/${eventId}`); - const { searchParams } = new URL(window.location); - handleFilter(searchParams) - } - }, - [apiHost, handleFilter] - ); - const searchParams = useMemo(() => new URLSearchParams(searchString), [searchString]); return ( @@ -133,7 +119,6 @@ export default function Events({ path: pathname, limit = API_LIMIT } = {}) { - @@ -194,11 +179,6 @@ export default function Events({ path: pathname, limit = API_LIMIT } = {}) { - ); } @@ -206,7 +186,7 @@ export default function Events({ path: pathname, limit = API_LIMIT } = {}) { -
Date Start End
{start.toLocaleDateString()} {start.toLocaleTimeString()} {end.toLocaleTimeString()} - -
+ {status === FetchStatus.LOADING ? : reachedEnd ? 'No more events' : null}