Skip to content

Commit

Permalink
client: Fix deletion of source form
Browse files Browse the repository at this point in the history
This was missed in 0432fed

The menu element is removed from DOM by the time the deletion handler
run so jQuery’s `parents()` function will not be able to locate the form.

Let’s store an explicit reference to the form and pass it to the event handler.
  • Loading branch information
jtojnar committed Jul 12, 2022
1 parent 2d007fe commit f1e8fa4
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions assets/js/templates/Source.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React from 'react';
import { useRef } from 'react';
import { Button as MenuButton, Wrapper as MenuWrapper, Menu, MenuItem } from 'react-aria-menubutton';
import { useHistory, useLocation } from 'react-router-dom';
import { makeEntriesLinkLocation } from '../helpers/uri';
Expand All @@ -14,12 +15,16 @@ import { LoadingState } from '../requests/LoadingState';
import { LocalizationContext } from '../helpers/i18n';

// cancel source editing
function handleCancel({ event, source, setSources, setEditedSource }) {
function handleCancel({
source,
sourceElem,
setSources,
setEditedSource
}) {
const id = source.id;

if (id.toString().startsWith('new-')) {
const parent = $(event.target).parents('.source');
parent.fadeOut('fast', () => {
$(sourceElem.current).fadeOut('fast', () => {
// Remove the source from this page’s model.
setSources((sources) =>
sources.filter((source) => source.id !== id)
Expand Down Expand Up @@ -124,6 +129,7 @@ function handleSave({
function handleDelete({
event,
source,
sourceElem,
setSources,
setSourceBeingDeleted,
setDirty,
Expand All @@ -147,8 +153,7 @@ function handleDelete({
sourceRequests
.remove(id)
.then(() => {
const parent = $(event.target).parents('.source');
parent.fadeOut('fast', () => {
$(sourceElem.current).fadeOut('fast', () => {
// Remove the source from this page’s model.
setSources((sources) =>
sources.filter((source) => source.id !== id)
Expand Down Expand Up @@ -247,6 +252,7 @@ function daysAgo(date) {

function SourceEditForm({
source,
sourceElem,
sourceError,
setSources,
spouts,
Expand Down Expand Up @@ -335,11 +341,12 @@ function SourceEditForm({
handleCancel({
event,
source,
sourceElem,
setSources,
setEditedSource
});
},
[source, setSources, setEditedSource, dirty, setDirty]
[source, sourceElem, setSources, setEditedSource, dirty, setDirty]
);

const _ = React.useContext(LocalizationContext);
Expand Down Expand Up @@ -518,6 +525,7 @@ function SourceEditForm({

SourceEditForm.propTypes = {
source: PropTypes.object.isRequired,
sourceElem: PropTypes.object.isRequired,
sourceError: PropTypes.string,
setSources: PropTypes.func.isRequired,
spouts: PropTypes.object.isRequired,
Expand Down Expand Up @@ -582,12 +590,15 @@ export default function Source({ source, setSources, spouts, setSpouts, dirty, s
const history = useHistory();
const location = useLocation();

const sourceElem = useRef(null);

const extraMenuOnSelection = React.useCallback(
(value, event) => {
if (value === 'delete') {
handleDelete({
event,
source,
sourceElem,
setSources,
setSourceBeingDeleted,
setDirty,
Expand All @@ -596,7 +607,7 @@ export default function Source({ source, setSources, spouts, setSpouts, dirty, s
history.push(makeEntriesLinkLocation(location, { category: `source-${source.id}` }));
}
},
[source, setSources, setDirty, location, history]
[source, sourceElem, setSources, setDirty, location, history]
);

const _ = React.useContext(LocalizationContext);
Expand All @@ -606,6 +617,7 @@ export default function Source({ source, setSources, spouts, setSpouts, dirty, s
className={classNames(classes)}
data-id={source.id}
id={`source-${source.id}`}
ref={sourceElem}
>
<div className="source-icon">
{source.icon && source.icon != '0' ? (
Expand Down Expand Up @@ -705,6 +717,7 @@ export default function Source({ source, setSources, spouts, setSpouts, dirty, s
setSourceErrors,
dirty,
setDirty,
sourceElem,
}}
sourceError={source.error}
source={editedSource}
Expand Down

0 comments on commit f1e8fa4

Please sign in to comment.