Skip to content

Commit 3cc7242

Browse files
Merge pull request #380 from BinaryStudioAcademy/task/OV-349-add-logic-to-modal-warning
OV-349: Add warning modal when the scenes exceed the scripts
2 parents 03e99f0 + 9f99009 commit 3cc7242

File tree

3 files changed

+45
-3
lines changed

3 files changed

+45
-3
lines changed

frontend/src/bundles/studio/helpers/helpers.ts

+1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ export { getNewItemIndexBySpan } from './get-new-item-index.js';
44
export { calculateTotalMilliseconds } from './get-total-time.js';
55
export { getVoicesConfigs } from './get-voices-configs.helper.js';
66
export { reorderItemsByIndexes } from './reorder-items.js';
7+
export { scenesExceedScripts } from './scenes-exceed-scripts.js';
78
export { setItemsSpan } from './set-items-span.js';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { type Scene, type Script } from '../types/types.js';
2+
3+
const INITIAL_DURATION = 0;
4+
5+
const scenesExceedScripts = (scenes: Scene[], scripts: Script[]): boolean => {
6+
const totalScenesDuration = scenes.reduce(
7+
(accumulator, scene) => accumulator + scene.duration,
8+
INITIAL_DURATION,
9+
);
10+
const totalScriptsDuration = scripts.reduce(
11+
(accumulator, script) => accumulator + script.duration,
12+
INITIAL_DURATION,
13+
);
14+
15+
return totalScenesDuration > totalScriptsDuration;
16+
};
17+
18+
export { scenesExceedScripts };

frontend/src/bundles/studio/pages/studio.tsx

+26-3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
useLocation,
2424
useNavigate,
2525
useRef,
26+
useState,
2627
} from '~/bundles/common/hooks/hooks.js';
2728
import { IconName } from '~/bundles/common/icons/icons.js';
2829
import { notificationService } from '~/bundles/common/services/services.js';
@@ -33,6 +34,7 @@ import {
3334
Timeline,
3435
VideoMenu,
3536
VideoNameInput,
37+
WarningModal,
3638
} from '../components/components.js';
3739
import {
3840
SCRIPT_AND_AVATAR_ARE_REQUIRED,
@@ -42,12 +44,13 @@ import {
4244
VIDEO_SUBMIT_NOTIFICATION_ID,
4345
} from '../constants/constants.js';
4446
import { NotificationMessage, NotificationTitle } from '../enums/enums.js';
45-
import { getVoicesConfigs } from '../helpers/helpers.js';
47+
import { getVoicesConfigs, scenesExceedScripts } from '../helpers/helpers.js';
4648
import { selectVideoDataById } from '../store/selectors.js';
4749
import { actions as studioActions } from '../store/studio.js';
4850

4951
const Studio: React.FC = () => {
5052
const { state: locationState } = useLocation();
53+
const [isModalOpen, setIsModalOpen] = useState(false);
5154

5255
const videoData = useAppSelector((state) =>
5356
selectVideoDataById(state, locationState?.id),
@@ -60,6 +63,14 @@ const Studio: React.FC = () => {
6063
const dispatch = useAppDispatch();
6164
const navigate = useNavigate();
6265

66+
const handleOpenModal = useCallback(() => {
67+
setIsModalOpen(true);
68+
}, []);
69+
70+
const handleCloseModal = useCallback(() => {
71+
setIsModalOpen(false);
72+
}, []);
73+
6374
useEffect((): void => {
6475
if (videoData) {
6576
void dispatch(studioActions.loadVideoData(videoData));
@@ -70,11 +81,10 @@ const Studio: React.FC = () => {
7081
dispatch(studioActions.changeVideoSize());
7182
}, [dispatch]);
7283

73-
const handleSubmit = useCallback(() => {
84+
const handleConfirmSubmit = useCallback(() => {
7485
// TODO: REPLACE LOGIC WITH MULTIPLE SCENES
7586
const scene = scenes[0];
7687
const script = scripts[0];
77-
7888
if (!scene?.avatar || !script) {
7989
notificationService.warn({
8090
id: SCRIPT_AND_AVATAR_ARE_REQUIRED,
@@ -106,6 +116,14 @@ const Studio: React.FC = () => {
106116
});
107117
}, [dispatch, navigate, scenes, scripts]);
108118

119+
const handleSubmit = useCallback(() => {
120+
if (scenesExceedScripts(scenes, scripts)) {
121+
handleOpenModal();
122+
} else {
123+
handleConfirmSubmit();
124+
}
125+
}, [handleConfirmSubmit, handleOpenModal, scenes, scripts]);
126+
109127
useEffect(() => {
110128
return () => {
111129
dispatch(studioActions.resetStudio());
@@ -171,6 +189,11 @@ const Studio: React.FC = () => {
171189
display="flex"
172190
flexDirection="column"
173191
>
192+
<WarningModal
193+
isOpen={isModalOpen}
194+
onClose={handleCloseModal}
195+
onSubmit={handleConfirmSubmit}
196+
/>
174197
<Header
175198
center={
176199
<Button

0 commit comments

Comments
 (0)