summaryrefslogtreecommitdiffhomepage
path: root/frontend/src/components/modals/MovieUploadModal.tsx
blob: 37785aada0098e273aae21fe2d44fa58bc56e278 (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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import { faTrash } from "@fortawesome/free-solid-svg-icons";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import React, {
  FunctionComponent,
  useCallback,
  useMemo,
  useState,
} from "react";
import { Button, Container, Form } from "react-bootstrap";
import { Column, Row } from "react-table";
import { dispatchTask } from "../../@modules/task";
import { createTask } from "../../@modules/task/utilites";
import { useProfileBy, useProfileItemsToLanguages } from "../../@redux/hooks";
import { MoviesApi } from "../../apis";
import { BuildKey } from "../../utilites";
import { FileForm } from "../inputs";
import { LanguageSelector } from "../LanguageSelector";
import { SimpleTable } from "../tables";
import BaseModal, { BaseModalProps } from "./BaseModal";
import { useModalInformation } from "./hooks";

interface PendingSubtitle {
  file: File;
  language: Language.Info;
  forced: boolean;
}

export const TaskGroupName = "Uploading Subtitles...";

const MovieUploadModal: FunctionComponent<BaseModalProps> = (props) => {
  const modal = props;

  const { payload, closeModal } = useModalInformation<Item.Movie>(
    modal.modalKey
  );

  const profile = useProfileBy(payload?.profileId);

  const availableLanguages = useProfileItemsToLanguages(profile);

  const [pending, setPending] = useState<PendingSubtitle[]>([]);

  const filelist = useMemo(() => pending.map((v) => v.file), [pending]);

  const setFiles = useCallback(
    (files: File[]) => {
      const list: PendingSubtitle[] = files.map((v) => ({
        file: v,
        forced: availableLanguages[0].forced ?? false,
        language: availableLanguages[0],
      }));
      setPending(list);
    },
    [availableLanguages]
  );

  const upload = useCallback(() => {
    if (payload === null || pending.length === 0) {
      return;
    }

    const { radarrId } = payload;

    const tasks = pending.map((v) => {
      const { file, language, forced } = v;

      return createTask(
        file.name,
        radarrId,
        MoviesApi.uploadSubtitles.bind(MoviesApi),
        radarrId,
        {
          file: file,
          forced,
          hi: false,
          language: language.code2,
        }
      );
    });

    dispatchTask(TaskGroupName, tasks, "Uploading...");
    setFiles([]);
    closeModal();
  }, [payload, closeModal, pending, setFiles]);

  const modify = useCallback(
    (row: Row<PendingSubtitle>, info?: PendingSubtitle) => {
      setPending((pd) => {
        const newPending = [...pd];
        if (info) {
          newPending[row.index] = info;
        } else {
          newPending.splice(row.index, 1);
        }
        return newPending;
      });
    },
    []
  );

  const columns = useMemo<Column<PendingSubtitle>[]>(
    () => [
      {
        id: "name",
        Header: "File",
        accessor: (d) => d.file.name,
      },
      {
        Header: "Forced",
        accessor: "forced",
        Cell: ({ row, value, update }) => {
          const { original, index } = row;
          return (
            <Form.Check
              custom
              id={BuildKey(index, original.file.name, "forced")}
              checked={value}
              onChange={(v) => {
                const newInfo = { ...row.original };
                newInfo.forced = v.target.checked;
                update && update(row, newInfo);
              }}
            ></Form.Check>
          );
        },
      },
      {
        Header: "Language",
        accessor: "language",
        className: "w-25",
        Cell: ({ row, update, value }) => {
          return (
            <LanguageSelector
              options={availableLanguages}
              value={value}
              onChange={(lang) => {
                if (lang && update) {
                  const newInfo = { ...row.original };
                  newInfo.language = lang;
                  update(row, newInfo);
                }
              }}
            ></LanguageSelector>
          );
        },
      },
      {
        accessor: "file",
        Cell: ({ row, update }) => {
          return (
            <Button
              size="sm"
              variant="light"
              onClick={() => {
                update && update(row);
              }}
            >
              <FontAwesomeIcon icon={faTrash}></FontAwesomeIcon>
            </Button>
          );
        },
      },
    ],
    [availableLanguages]
  );

  const canUpload = pending.length > 0;

  const footer = (
    <Button disabled={!canUpload} onClick={upload}>
      Upload
    </Button>
  );

  return (
    <BaseModal title={`Upload - ${payload?.title}`} footer={footer} {...modal}>
      <Container fluid className="flex-column">
        <Form>
          <Form.Group>
            <FileForm
              emptyText="Select..."
              disabled={canUpload || availableLanguages.length === 0}
              multiple
              value={filelist}
              onChange={setFiles}
            ></FileForm>
          </Form.Group>
        </Form>
        <div hidden={!canUpload}>
          <SimpleTable
            columns={columns}
            data={pending}
            responsive={false}
            update={modify}
          ></SimpleTable>
        </div>
      </Container>
    </BaseModal>
  );
};

export default MovieUploadModal;