aboutsummaryrefslogtreecommitdiffhomepage
path: root/frontend/src/components/forms/TimeOffsetForm.tsx
blob: 1a7739dd9aff4c853b0d235b5fb197bac8c27b76 (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
import { FunctionComponent } from "react";
import { Button, Divider, Group, NumberInput, Stack } from "@mantine/core";
import { useForm } from "@mantine/form";
import { faMinus, faPlus } from "@fortawesome/free-solid-svg-icons";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { useSubtitleAction } from "@/apis/hooks";
import { useModals, withModal } from "@/modules/modals";
import { task } from "@/modules/task";
import FormUtils from "@/utilities/form";

const TaskName = "Changing Time";

function convertToAction(h: number, m: number, s: number, ms: number) {
  return `shift_offset(h=${h},m=${m},s=${s},ms=${ms})`;
}

interface Props {
  selections: FormType.ModifySubtitle[];
  onSubmit?: VoidFunction;
}

const TimeOffsetForm: FunctionComponent<Props> = ({ selections, onSubmit }) => {
  const { mutateAsync } = useSubtitleAction();
  const modals = useModals();

  const form = useForm({
    initialValues: {
      positive: true,
      hour: 0,
      min: 0,
      sec: 0,
      ms: 0,
    },
    validate: {
      hour: FormUtils.validation((v) => v >= 0, "Hour must be larger than 0"),
      min: FormUtils.validation((v) => v >= 0, "Minute must be larger than 0"),
      sec: FormUtils.validation((v) => v >= 0, "Second must be larger than 0"),
      ms: FormUtils.validation(
        (v) => v >= 0,
        "Millisecond must be larger than 0",
      ),
    },
  });

  const enabled =
    form.values.hour > 0 ||
    form.values.min > 0 ||
    form.values.sec > 0 ||
    form.values.ms > 0;

  return (
    <form
      onSubmit={form.onSubmit(({ positive, hour, min, sec, ms }) => {
        let action: string;
        if (positive) {
          action = convertToAction(hour, min, sec, ms);
        } else {
          action = convertToAction(-hour, -min, -sec, -ms);
        }

        selections.forEach((s) =>
          task.create(s.path, TaskName, mutateAsync, {
            action,
            form: s,
          }),
        );

        onSubmit?.();
        modals.closeSelf();
      })}
    >
      <Stack>
        <Group align="end" gap="xs" wrap="nowrap">
          <Button
            color="gray"
            variant="filled"
            style={{ overflow: "visible" }}
            onClick={() =>
              form.setValues((f) => ({ ...f, positive: !f.positive }))
            }
          >
            <FontAwesomeIcon
              icon={form.values.positive ? faPlus : faMinus}
            ></FontAwesomeIcon>
          </Button>
          <NumberInput
            min={0}
            label="hour"
            {...form.getInputProps("hour")}
          ></NumberInput>
          <NumberInput
            min={0}
            label="min"
            {...form.getInputProps("min")}
          ></NumberInput>
          <NumberInput
            min={0}
            label="sec"
            {...form.getInputProps("sec")}
          ></NumberInput>
          <NumberInput
            min={0}
            label="ms"
            {...form.getInputProps("ms")}
          ></NumberInput>
        </Group>
        <Divider></Divider>
        <Button disabled={!enabled} type="submit">
          Start
        </Button>
      </Stack>
    </form>
  );
};

export const TimeOffsetModal = withModal(TimeOffsetForm, "time-offset", {
  title: "Change Time",
});

export default TimeOffsetForm;