summaryrefslogtreecommitdiffhomepage
path: root/frontend/src/@redux/hooks/series.ts
blob: d37b7eb32b405901dfcd52c6b7da7e0639e74786 (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
97
98
99
import { useCallback, useEffect, useMemo } from "react";
import { useEntityItemById, useEntityToList } from "../../utilites";
import {
  episodesUpdateBlacklist,
  episodeUpdateById,
  episodeUpdateBySeriesId,
  seriesUpdateById,
  seriesUpdateWantedById,
} from "../actions";
import { useAutoDirtyUpdate, useAutoUpdate } from "./async";
import { useReduxAction, useReduxStore } from "./base";

export function useSerieEntities() {
  const items = useReduxStore((d) => d.series.seriesList);

  useAutoDirtyUpdate(items, seriesUpdateById);
  return items;
}

export function useSeries() {
  const rawSeries = useSerieEntities();
  const content = useEntityToList(rawSeries.content);
  const series = useMemo<Async.List<Item.Series>>(() => {
    return {
      ...rawSeries,
      keyName: rawSeries.content.keyName,
      content,
    };
  }, [rawSeries, content]);
  return series;
}

export function useSerieBy(id: number) {
  const series = useSerieEntities();
  const action = useReduxAction(seriesUpdateById);
  const serie = useEntityItemById(series, String(id));

  const update = useCallback(() => {
    if (!isNaN(id)) {
      action([id]);
    }
  }, [id, action]);

  useAutoUpdate(serie, update);
  return serie;
}

export function useEpisodesBy(seriesId: number) {
  const action = useReduxAction(episodeUpdateBySeriesId);
  const update = useCallback(() => {
    if (!isNaN(seriesId)) {
      action([seriesId]);
    }
  }, [action, seriesId]);

  const episodes = useReduxStore((d) => d.series.episodeList);

  const newContent = useMemo(() => {
    return episodes.content.filter((v) => v.sonarrSeriesId === seriesId);
  }, [seriesId, episodes.content]);

  const newList: Async.List<Item.Episode> = useMemo(
    () => ({
      ...episodes,
      content: newContent,
    }),
    [episodes, newContent]
  );

  // FIXME
  useEffect(() => {
    update();
  }, [update]);

  useAutoDirtyUpdate(episodes, episodeUpdateById);

  return newList;
}

export function useWantedSeries() {
  const items = useReduxStore((d) => d.series.wantedEpisodesList);

  useAutoDirtyUpdate(items, seriesUpdateWantedById);
  return items;
}

export function useBlacklistSeries() {
  const update = useReduxAction(episodesUpdateBlacklist);
  const items = useReduxStore((d) => d.series.blacklist);

  useAutoUpdate(items, update);
  return items;
}

export function useSeriesHistory() {
  const items = useReduxStore((s) => s.series.historyList);

  return items;
}