summaryrefslogtreecommitdiffhomepage
path: root/frontend/src/@redux/reducers/series.ts
blob: 1ce6dbc25f72a2ea7f8dd5adc0f54e860d050a09 (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
90
91
92
93
94
95
96
import { createReducer } from "@reduxjs/toolkit";
import {
  episodesMarkBlacklistDirty,
  episodesMarkDirtyById,
  episodesMarkHistoryDirty,
  episodesRemoveById,
  episodesUpdateBlacklist,
  episodesUpdateHistoryByRange,
  episodeUpdateById,
  episodeUpdateBySeriesId,
  seriesMarkDirtyById,
  seriesMarkWantedDirtyById,
  seriesRemoveById,
  seriesRemoveWantedById,
  seriesUpdateAll,
  seriesUpdateById,
  seriesUpdateByRange,
  seriesUpdateWantedById,
  seriesUpdateWantedByRange,
} from "../actions";
import { AsyncUtility, ReducerUtility } from "../utils";
import {
  createAsyncEntityReducer,
  createAsyncItemReducer,
  createAsyncListReducer,
} from "../utils/factory";

interface Series {
  seriesList: Async.Entity<Item.Series>;
  wantedEpisodesList: Async.Entity<Wanted.Episode>;
  episodeList: Async.List<Item.Episode>;
  historyList: Async.Entity<History.Episode>;
  blacklist: Async.Item<Blacklist.Episode[]>;
}

const defaultSeries: Series = {
  seriesList: AsyncUtility.getDefaultEntity("sonarrSeriesId"),
  wantedEpisodesList: AsyncUtility.getDefaultEntity("sonarrEpisodeId"),
  episodeList: AsyncUtility.getDefaultList("sonarrEpisodeId"),
  historyList: AsyncUtility.getDefaultEntity("id"),
  blacklist: AsyncUtility.getDefaultItem(),
};

const reducer = createReducer(defaultSeries, (builder) => {
  createAsyncEntityReducer(builder, (s) => s.seriesList, {
    range: seriesUpdateByRange,
    ids: seriesUpdateById,
    removeIds: seriesRemoveById,
    all: seriesUpdateAll,
  });

  builder.addCase(seriesMarkDirtyById, (state, action) => {
    const series = state.seriesList;
    const dirtyIds = action.payload.map(String);

    ReducerUtility.markDirty(series, dirtyIds);

    // Update episode list
    const episodes = state.episodeList;
    const dirtyIdsSet = new Set(dirtyIds);
    const dirtyEpisodeIds = episodes.content
      .filter((v) => dirtyIdsSet.has(v.sonarrSeriesId.toString()))
      .map((v) => String(v.sonarrEpisodeId));

    ReducerUtility.markDirty(episodes, dirtyEpisodeIds);
  });

  createAsyncEntityReducer(builder, (s) => s.wantedEpisodesList, {
    range: seriesUpdateWantedByRange,
    ids: seriesUpdateWantedById,
    removeIds: seriesRemoveWantedById,
    dirty: seriesMarkWantedDirtyById,
  });

  createAsyncEntityReducer(builder, (s) => s.historyList, {
    range: episodesUpdateHistoryByRange,
    dirty: episodesMarkHistoryDirty,
  });

  createAsyncItemReducer(builder, (s) => s.blacklist, {
    all: episodesUpdateBlacklist,
    dirty: episodesMarkBlacklistDirty,
  });

  createAsyncListReducer(builder, (s) => s.episodeList, {
    ids: episodeUpdateBySeriesId,
  });

  createAsyncListReducer(builder, (s) => s.episodeList, {
    ids: episodeUpdateById,
    removeIds: episodesRemoveById,
    dirty: episodesMarkDirtyById,
  });
});

export default reducer;