summaryrefslogtreecommitdiffhomepage
path: root/frontend/src/@redux/reducers/site.ts
blob: 54aedc3d03703fdd74de077e97da2ddc5577bb8f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import { createReducer } from "@reduxjs/toolkit";
import { pullAllWith, remove, uniqBy } from "lodash";
import apis from "../../apis";
import {
  siteAddNotifications,
  siteAddProgress,
  siteBootstrap,
  siteChangeSidebar,
  siteRedirectToAuth,
  siteRemoveNotifications,
  siteRemoveProgress,
  siteUpdateBadges,
  siteUpdateInitialization,
  siteUpdateOffline,
} from "../actions/site";

interface Site {
  // Initialization state or error message
  initialized: boolean | string;
  auth: boolean;
  progress: Server.Progress[];
  notifications: Server.Notification[];
  sidebar: string;
  badges: Badge;
  offline: boolean;
}

const defaultSite: Site = {
  initialized: false,
  auth: true,
  progress: [],
  notifications: [],
  sidebar: "",
  badges: {
    movies: 0,
    episodes: 0,
    providers: 0,
    status: 0,
  },
  offline: false,
};

const reducer = createReducer(defaultSite, (builder) => {
  builder
    .addCase(siteBootstrap.fulfilled, (state) => {
      state.initialized = true;
    })
    .addCase(siteBootstrap.rejected, (state) => {
      state.initialized = "An Error Occurred When Initializing Bazarr UI";
    })
    .addCase(siteRedirectToAuth, (state) => {
      if (process.env.NODE_ENV !== "production") {
        apis.danger_resetApi("NEED_AUTH");
      }
      state.auth = false;
    })
    .addCase(siteUpdateInitialization, (state, action) => {
      state.initialized = action.payload;
    })
    .addCase(siteAddNotifications, (state, action) => {
      state.notifications = uniqBy(
        [...action.payload.reverse(), ...state.notifications],
        (n) => n.id
      );
    })
    .addCase(siteRemoveNotifications, (state, action) => {
      remove(state.notifications, (n) => n.id === action.payload);
    })
    .addCase(siteAddProgress, (state, action) => {
      state.progress = uniqBy(
        [...action.payload.reverse(), ...state.progress],
        (n) => n.id
      );
    })
    .addCase(siteRemoveProgress, (state, action) => {
      pullAllWith(state.progress, action.payload, (l, r) => l.id === r);
    })
    .addCase(siteChangeSidebar, (state, action) => {
      state.sidebar = action.payload;
    })
    .addCase(siteUpdateOffline, (state, action) => {
      state.offline = action.payload;
    })
    .addCase(siteUpdateBadges.fulfilled, (state, action) => {
      state.badges = action.payload;
    });
});

export default reducer;