bazarr/frontend/src/@redux/reducers/site.ts

86 lines
2.1 KiB
TypeScript
Raw Normal View History

2021-05-09 03:22:24 +00:00
import { remove, uniqBy } from "lodash";
2021-03-25 14:22:43 +00:00
import { Action, handleActions } from "redux-actions";
import apis from "../../apis";
2021-03-25 14:22:43 +00:00
import {
SITE_BADGE_UPDATE,
SITE_INITIALIZED,
SITE_INITIALIZE_FAILED,
SITE_NEED_AUTH,
SITE_NOTIFICATIONS_ADD,
SITE_NOTIFICATIONS_REMOVE,
SITE_OFFLINE_UPDATE,
SITE_SIDEBAR_UPDATE,
} from "../constants";
import { AsyncAction } from "../types";
const reducer = handleActions<ReduxStore.Site, any>(
{
[SITE_NEED_AUTH]: (state) => {
if (process.env.NODE_ENV !== "development") {
apis.danger_resetApi("NEED_AUTH");
}
return {
...state,
auth: false,
};
},
2021-03-25 14:22:43 +00:00
[SITE_INITIALIZED]: (state) => ({
...state,
initialized: true,
}),
[SITE_INITIALIZE_FAILED]: (state) => ({
...state,
initialized: "An Error Occurred When Initializing Bazarr UI",
}),
[SITE_NOTIFICATIONS_ADD]: (
state,
action: Action<ReduxStore.Notification[]>
2021-03-25 14:22:43 +00:00
) => {
2021-05-09 03:22:24 +00:00
const notifications = uniqBy(
[...action.payload, ...state.notifications],
(n) => n.id
2021-03-25 14:22:43 +00:00
);
2021-05-09 03:22:24 +00:00
return { ...state, notifications };
},
[SITE_NOTIFICATIONS_REMOVE]: (state, action: Action<string>) => {
const notifications = [...state.notifications];
remove(notifications, (n) => n.id === action.payload);
return { ...state, notifications };
2021-03-25 14:22:43 +00:00
},
[SITE_SIDEBAR_UPDATE]: (state, action: Action<string>) => {
return {
...state,
sidebar: action.payload,
};
},
[SITE_BADGE_UPDATE]: {
next: (state, action: AsyncAction<Badge>) => {
const badges = action.payload.item;
if (badges && action.error !== true) {
return { ...state, badges: badges as Badge };
}
return state;
},
throw: (state) => state,
},
[SITE_OFFLINE_UPDATE]: (state, action: Action<boolean>) => {
return { ...state, offline: action.payload };
},
},
{
initialized: false,
auth: true,
notifications: [],
sidebar: "",
badges: {
movies: 0,
episodes: 0,
providers: 0,
status: 0,
2021-03-25 14:22:43 +00:00
},
offline: false,
}
);
export default reducer;