moved delete reducer to event page

This commit is contained in:
Bernt Christian Egeland 2021-08-22 11:21:49 +02:00
parent 3128632e59
commit 287a96a630
2 changed files with 10 additions and 16 deletions

View File

@ -39,18 +39,6 @@ function reducer(state, { type, payload, meta }) {
return produce(state, (draftState) => { return produce(state, (draftState) => {
Object.keys(draftState.queries).map((url, index) => { Object.keys(draftState.queries).map((url, index) => {
// If data has no array length then just return state.
if (!('data' in draftState.queries[url]) || !draftState.queries[url].data.length) return state;
//Find the index to remove
const removeIndex = draftState.queries[url].data.map((event) => event.id).indexOf(eventId);
if (removeIndex === -1) return state;
// We need to keep track of deleted items, This will be used to re-calculate "ReachEnd" for auto load new events. Events.jsx
const totDeleted = state.queries[url].deleted || 0;
// Splice the deleted index.
draftState.queries[url].data.splice(removeIndex, 1);
draftState.queries[url].deletedId = eventId; draftState.queries[url].deletedId = eventId;
}); });
}); });

View File

@ -36,6 +36,7 @@ const reducer = (state = initialState, action) => {
return produce(state, (draftState) => { return produce(state, (draftState) => {
draftState.searchStrings[searchString] = true; draftState.searchStrings[searchString] = true;
draftState.events.push(...payload); draftState.events.push(...payload);
draftState.deleted = 0;
}); });
} }
@ -71,6 +72,10 @@ export default function Events({ path: pathname, limit = API_LIMIT } = {}) {
const [viewEvent, setViewEvent] = useState(null); const [viewEvent, setViewEvent] = useState(null);
const [searchString, setSearchString] = useState(`${defaultSearchString(limit)}&${initialSearchParams.toString()}`); const [searchString, setSearchString] = useState(`${defaultSearchString(limit)}&${initialSearchParams.toString()}`);
const { data, status, deletedId } = useEvents(searchString); const { data, status, deletedId } = useEvents(searchString);
console.log('deleted', deleted);
console.log('deletedId', deletedId);
console.log('limit', limit);
console.log('data.length', data && data.length);
let scrollToRef = {}; let scrollToRef = {};
useEffect(() => { useEffect(() => {
@ -79,13 +84,16 @@ export default function Events({ path: pathname, limit = API_LIMIT } = {}) {
} }
if (data && Array.isArray(data) && data.length + deleted < limit) { if (data && Array.isArray(data) && data.length + deleted < limit) {
console.log('reached end');
dispatch({ type: 'REACHED_END', meta: { searchString } }); dispatch({ type: 'REACHED_END', meta: { searchString } });
} }
}, [data, limit, searchString, searchStrings, deleted]);
useEffect(() => {
if (deletedId) { if (deletedId) {
dispatch({ type: 'DELETE_EVENT', deletedId }); dispatch({ type: 'DELETE_EVENT', deletedId });
} }
}, [data, limit, searchString, searchStrings, deleted, deletedId]); }, [deletedId]);
const [entry, setIntersectNode] = useIntersectionObserver(); const [entry, setIntersectNode] = useIntersectionObserver();
@ -214,9 +222,7 @@ export default function Events({ path: pathname, limit = API_LIMIT } = {}) {
{viewEvent === id ? ( {viewEvent === id ? (
<Tr className="border-b-1"> <Tr className="border-b-1">
<Td colspan="8"> <Td colspan="8">
<div> <Event eventId={viewEvent} close={() => setViewEvent(null)} />
<Event eventId={viewEvent} close={() => setViewEvent(null)} />
</div>
</Td> </Td>
</Tr> </Tr>
) : null} ) : null}