Skip to content

Commit

Permalink
Don't revert shape edits on save failure
Browse files Browse the repository at this point in the history
These changes are intentionally not fixed up into ff73200 because
it can serve as a reference for how to revere manaual cache updates.
  • Loading branch information
Matt Stone committed Oct 20, 2022
1 parent 551e61d commit 5c56803
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 40 deletions.
19 changes: 3 additions & 16 deletions src/app/src/components/DrawTools/useEditingPolygon.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@ import { useDispatch, useSelector } from 'react-redux';
import { customizePrototypeIcon } from '../../utils';
import { PANES } from '../../constants';
import { useUpdateBoundaryShapeMutation } from '../../api/boundaries';
import {
useBoundaryId,
useReverseQueue,
useTrailingDebounceCallback,
} from '../../hooks';
import { useBoundaryId, useTrailingDebounceCallback } from '../../hooks';
import api from '../../api/api';

const POLYGON_LAYER_OPTIONS = {
Expand Down Expand Up @@ -58,20 +54,13 @@ export default function useEditingPolygon() {
const { polygon, editMode, basemapType } = useSelector(state => state.map);

const [updateShape] = useUpdateBoundaryShapeMutation();
const reverseQueue = useReverseQueue({
endpointName: 'getBoundaryDetails',
queryArgs: id,
});

const updatePolygonFromDrawEvent = useTrailingDebounceCallback({
callback: event => {
updateShape({ id, shape: getShapeFromDrawEvent(event) })
.unwrap()
.then(reverseQueue.clear)
.catch(reverseQueue.apply);
updateShape({ id, shape: getShapeFromDrawEvent(event) });
},
immediateCallback: event => {
const { inversePatches } = dispatch(
dispatch(
api.util.updateQueryData(
'getBoundaryDetails',
id,
Expand All @@ -81,8 +70,6 @@ export default function useEditingPolygon() {
}
)
);

reverseQueue.push(inversePatches);
},
interval: 3000,
});
Expand Down
24 changes: 0 additions & 24 deletions src/app/src/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
updateReferenceImage,
} from './store/mapSlice';
import { useParams } from 'react-router';
import api from './api/api';

export function useDialogController() {
const [isOpen, setIsOpen] = useState(false);
Expand Down Expand Up @@ -153,29 +152,6 @@ export function useTrailingDebounceCallback({
);
}

export function useReverseQueue({ queryArgs, endpointName }) {
const dispatch = useDispatch();

const queue = useRef([]);

const clear = useCallback(() => {
queue.current = [];
}, []);

const apply = useCallback(() => {
dispatch(
api.util.patchQueryData(endpointName, queryArgs, queue.current)
);
clear();
}, [dispatch, queryArgs, endpointName, clear]);

const push = useCallback(patches => {
queue.current = [...patches, ...queue.current];
}, []);

return { queue, clear, apply, push };
}

export function useEndpointToastError(error, message = 'An error occured') {
const toast = useToast();

Expand Down

0 comments on commit 5c56803

Please sign in to comment.