summaryrefslogtreecommitdiffhomepage
path: root/frontend/src/@redux/hooks/movies.ts
blob: 485e93451ab0b5d12b1335fab6b71289a660ee39 (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
import { useCallback, useMemo } from "react";
import { useEntityItemById, useEntityToList } from "../../utilities";
import {
  movieUpdateBlacklist,
  movieUpdateById,
  movieUpdateWantedById,
} from "../actions";
import { useAutoDirtyUpdate, useAutoUpdate } from "./async";
import { useReduxAction, useReduxStore } from "./base";

export function useMovieEntities() {
  const entities = useReduxStore((d) => d.movies.movieList);

  useAutoDirtyUpdate(entities, movieUpdateById);

  return entities;
}

export function useMovies() {
  const rawMovies = useMovieEntities();
  const content = useEntityToList(rawMovies.content);
  const movies = useMemo<Async.List<Item.Movie>>(() => {
    return {
      ...rawMovies,
      keyName: rawMovies.content.keyName,
      content,
    };
  }, [rawMovies, content]);
  return movies;
}

export function useMovieBy(id: number) {
  const movies = useMovieEntities();
  const action = useReduxAction(movieUpdateById);

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

  const movie = useEntityItemById(movies, id.toString());

  useAutoUpdate(movie, update);
  return movie;
}

export function useWantedMovies() {
  const items = useReduxStore((d) => d.movies.wantedMovieList);

  useAutoDirtyUpdate(items, movieUpdateWantedById);
  return items;
}

export function useBlacklistMovies() {
  const update = useReduxAction(movieUpdateBlacklist);
  const items = useReduxStore((d) => d.movies.blacklist);

  useAutoUpdate(items, update);
  return items;
}

export function useMoviesHistory() {
  const items = useReduxStore((s) => s.movies.historyList);

  return items;
}