2024-03-27 13:47:09 +01:00
|
|
|
import { createReducer } from '@reduxjs/toolkit';
|
|
|
|
|
2024-07-18 16:36:09 +02:00
|
|
|
import {
|
|
|
|
submitMarkersAction,
|
|
|
|
fetchMarkers,
|
|
|
|
} from 'flavours/glitch/actions/markers';
|
|
|
|
import { compareId } from 'flavours/glitch/compare_id';
|
2024-03-27 13:47:09 +01:00
|
|
|
|
|
|
|
const initialState = {
|
|
|
|
home: '0',
|
|
|
|
notifications: '0',
|
|
|
|
};
|
|
|
|
|
|
|
|
export const markersReducer = createReducer(initialState, (builder) => {
|
|
|
|
builder.addCase(
|
|
|
|
submitMarkersAction.fulfilled,
|
|
|
|
(state, { payload: { home, notifications } }) => {
|
|
|
|
if (home) state.home = home;
|
|
|
|
if (notifications) state.notifications = notifications;
|
|
|
|
},
|
|
|
|
);
|
2024-07-18 16:36:09 +02:00
|
|
|
builder.addCase(
|
|
|
|
fetchMarkers.fulfilled,
|
|
|
|
(
|
|
|
|
state,
|
|
|
|
{
|
|
|
|
payload: {
|
|
|
|
markers: { home, notifications },
|
|
|
|
},
|
|
|
|
},
|
|
|
|
) => {
|
|
|
|
if (home && compareId(home.last_read_id, state.home) > 0)
|
|
|
|
state.home = home.last_read_id;
|
|
|
|
if (
|
|
|
|
notifications &&
|
|
|
|
compareId(notifications.last_read_id, state.notifications) > 0
|
|
|
|
)
|
|
|
|
state.notifications = notifications.last_read_id;
|
|
|
|
},
|
|
|
|
);
|
2024-03-27 13:47:09 +01:00
|
|
|
});
|