-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathSave.tsx
201 lines (179 loc) · 5.92 KB
/
Save.tsx
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
import React, { useEffect } from "react";
import { css } from "@emotion/react";
import {
basicButtonStyle, backOrContinueStyle, ariaLive, errorBoxStyle,
navigationButtonStyle, spinningStyle,
} from "../cssStyles";
import { LuLoader, LuCheckCircle, LuAlertCircle, LuChevronLeft, LuSave, LuCheck } from "react-icons/lu";
import { useAppDispatch, useAppSelector } from "../redux/store";
import {
selectHasChanges,
selectSegments,
selectTracks,
setHasChanges as videoSetHasChanges,
selectValidSegments,
validateSegments,
} from "../redux/videoSlice";
import { postVideoInformation, selectStatus, selectError } from "../redux/workflowPostSlice";
import { CallbackButton, PageButton } from "./Finish";
import { useTranslation } from "react-i18next";
import {
setHasChanges as metadataSetHasChanges,
selectHasChanges as metadataSelectHasChanges,
selectCatalogs,
} from "../redux/metadataSlice";
import {
selectSubtitles, selectHasChanges as selectSubtitleHasChanges,
setHasChanges as subtitleSetHasChanges,
} from "../redux/subtitleSlice";
import { serializeSubtitle } from "../util/utilityFunctions";
import { useTheme } from "../themes";
import { ThemedTooltip } from "./Tooltip";
import { IconType } from "react-icons";
import { setEnd } from "../redux/endSlice";
/**
* Shown if the user wishes to save.
* Informs the user about saving and displays a save button
*/
const Save: React.FC = () => {
const { t } = useTranslation();
const postWorkflowStatus = useAppSelector(selectStatus);
const postError = useAppSelector(selectError);
const theme = useTheme();
const metadataHasChanges = useAppSelector(metadataSelectHasChanges);
const hasChanges = useAppSelector(selectHasChanges);
const subtitleHasChanges = useAppSelector(selectSubtitleHasChanges);
const dispatch = useAppDispatch();
dispatch(validateSegments());
const validSegments = useAppSelector(selectValidSegments);
const saveStyle = css({
display: "flex",
flexDirection: "column",
justifyContent: "center",
alignItems: "center",
gap: "30px",
});
const render = () => {
// Post (successful) save
if (postWorkflowStatus === "success"
&& !hasChanges && !metadataHasChanges && !subtitleHasChanges) {
return (
<>
<LuCheckCircle css={{ fontSize: 80 }} />
<div>{t("save.success-text")}</div>
<CallbackButton />
</>
);
// Pre save
} else {
return (
<>
<span css={{ maxWidth: "500px" }}>
{validSegments ? t("save.info-text") : t("save.invalid-text")}
</span>
<div css={backOrContinueStyle}>
<PageButton pageNumber={0} label={t("various.goBack-button")} Icon={LuChevronLeft} />
{validSegments && <SaveButton />}
</div>
</>
);
}
};
return (
<div css={saveStyle}>
<h1>{validSegments ? t("save.headline-text") : t("save.invalid-headline-text")}</h1>
{render()}
<div css={errorBoxStyle(postWorkflowStatus === "failed", theme)} role="alert">
<span>{t("various.error-text")}</span><br />
{postError ? t("various.error-details-text", { errorMessage: postError }) : t("various.error-text")}<br />
</div>
</div>
);
};
/**
* Button that sends a post request to save current changes
*/
export const SaveButton: React.FC<{
basicIcon?: IconType
text?: string
isTransitionToEnd?: boolean
}> = ({
basicIcon = LuSave,
text,
isTransitionToEnd = false,
}) => {
const { t } = useTranslation();
// Initialize redux variables
const dispatch = useAppDispatch();
const segments = useAppSelector(selectSegments);
const tracks = useAppSelector(selectTracks);
const subtitles = useAppSelector(selectSubtitles);
const metadata = useAppSelector(selectCatalogs);
const workflowStatus = useAppSelector(selectStatus);
const theme = useTheme();
// Update based on current fetching status
let Icon = basicIcon;
let spin = false;
let tooltip = null;
if (workflowStatus === "failed") {
Icon = LuAlertCircle;
spin = false;
tooltip = t("save.confirmButton-failed-tooltip");
} else if (workflowStatus === "success") {
Icon = LuCheck;
spin = false;
tooltip = t("save.confirmButton-success-tooltip");
} else if (workflowStatus === "loading") {
Icon = LuLoader;
spin = true;
tooltip = t("save.confirmButton-attempting-tooltip");
}
const ariaSaveUpdate = () => {
if (workflowStatus === "success") {
return t("save.success-tooltip-aria");
}
};
const prepareSubtitles = () =>
Object.entries(subtitles).map(([id, { deleted, cues, tags }]) => ({
id,
subtitle: deleted ? "" : serializeSubtitle(cues),
tags: deleted ? [] : tags,
deleted,
}));
const save = () => {
dispatch(postVideoInformation({
segments: segments,
tracks: tracks,
subtitles: prepareSubtitles(),
metadata: metadata,
}));
};
// Let users leave the page without warning after a successful save
useEffect(() => {
if (workflowStatus === "success") {
if (isTransitionToEnd) {
dispatch(setEnd({ hasEnded: true, value: "success" }));
}
dispatch(videoSetHasChanges(false));
dispatch(metadataSetHasChanges(false));
dispatch(subtitleSetHasChanges(false));
}
}, [dispatch, workflowStatus]);
return (
<ThemedTooltip title={tooltip == null ? tooltip = "" : tooltip}>
<div css={[basicButtonStyle(theme), navigationButtonStyle(theme)]}
role="button" tabIndex={0}
onClick={save}
onKeyDown={(event: React.KeyboardEvent<HTMLDivElement>) => {
if (event.key === " " || event.key === "Enter") {
save();
}
}}>
<Icon css={spin ? spinningStyle : undefined} />
<span>{text ?? t("save.confirm-button")}</span>
<div css={ariaLive} aria-live="polite" aria-atomic="true">{ariaSaveUpdate()}</div>
</div>
</ThemedTooltip>
);
};
export default Save;