Skip to content

Commit

Permalink
OV-421: * load template from templates page
Browse files Browse the repository at this point in the history
  • Loading branch information
Sanchousina committed Sep 27, 2024
1 parent f77509f commit 9e42994
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 9 deletions.
14 changes: 12 additions & 2 deletions frontend/src/bundles/studio/pages/studio.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ import {
getVoicesConfigs,
scenesExceedScripts,
} from '../helpers/helpers.js';
import { selectVideoDataById } from '../store/selectors.js';
import {
selectTemplateDataById,
selectVideoDataById,
} from '../store/selectors.js';
import { actions as studioActions } from '../store/studio.js';

const Studio: React.FC = () => {
Expand All @@ -64,6 +67,10 @@ const Studio: React.FC = () => {
selectVideoDataById(state, locationState?.id),
);

const templateData = useAppSelector((state) =>
selectTemplateDataById(state, locationState?.templateId),
);

const {
scenes,
scripts,
Expand Down Expand Up @@ -91,7 +98,10 @@ const Studio: React.FC = () => {
if (videoData) {
void dispatch(studioActions.loadVideoData(videoData));
}
}, [dispatch, videoData]);
if (templateData) {
void dispatch(studioActions.loadTemplate(templateData));
}
}, [dispatch, templateData, videoData]);

const handleResize = useCallback(() => {
dispatch(studioActions.changeVideoSize());
Expand Down
22 changes: 21 additions & 1 deletion frontend/src/bundles/studio/store/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { type RootState } from '~/bundles/common/types/types.js';

import {
type Script,
type Template,
type VideoGetAllItemResponseDto,
} from '../types/types.js';

Expand All @@ -13,6 +14,12 @@ const selectScrips = (state: RootState): Script[] => state.studio.scripts;
const selectVideos = (state: RootState): VideoGetAllItemResponseDto[] =>
state.home.videos;

const selectPublicTemplates = (state: RootState): Template[] =>
state.studio.templates.public;

const selectUserTemplates = (state: RootState): Template[] =>
state.studio.templates.user;

const selectTotalDuration = createSelector([selectScrips], (scripts) => {
const totalDuration = scripts.reduce(
(total, script) => total + script.duration,
Expand All @@ -29,4 +36,17 @@ const selectVideoDataById = createSelector(
},
);

export { selectTotalDuration, selectVideoDataById };
const selectTemplateDataById = createSelector(
[selectPublicTemplates, selectUserTemplates, (_, id: string): string => id],
(publicTemplates, userTemplates, id) => {
const publicTemplate = publicTemplates.find(
(template) => template.id === id,
);
if (!publicTemplate) {
return userTemplates.find((template) => template.id === id);
}
return publicTemplate;
},
);

export { selectTemplateDataById, selectTotalDuration, selectVideoDataById };
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const CreationsSection: React.FC<Properties> = ({ onClick }) => {
marginRight="20px"
width="250px"
>
<TemplateCard {...template} />
<TemplateCard {...template} id={id} />
</Box>
))}
</Box>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,34 @@ import {
Image,
Text,
} from '~/bundles/common/components/components.js';
import { useCallback, useState } from '~/bundles/common/hooks/hooks.js';
import { AppRoute } from '~/bundles/common/enums/enums.js';
import {
useCallback,
useNavigate,
useState,
} from '~/bundles/common/hooks/hooks.js';
import { IconName } from '~/bundles/common/icons/icons.js';
import { DeleteWarning } from '~/bundles/home/components/video-card/components/delete-warning.js';

import styles from './styles.module.css';

type Properties = {
id: string;
name: string;
previewUrl: string;
};

const TemplateCard: React.FC<Properties> = ({ name, previewUrl }) => {
const TemplateCard: React.FC<Properties> = ({ id, name, previewUrl }) => {
const navigate = useNavigate();
const [isWarningModalOpen, setIsWarningModalOpen] = useState(false);

const handleIconClick = useCallback((): void => {}, []);
const handleIconClick = useCallback((): void => {
navigate(AppRoute.STUDIO, {
state: {
templateId: id,
},
});
}, [id, navigate]);

const handleWarningModalClose = useCallback(() => {
setIsWarningModalOpen(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,10 @@ const TemplatesSection: React.FC = () => {
</Flex>
<SimpleGrid columns={{ sm: 2, md: 3, lg: 4 }} spacing="20px">
{templates.public.map(({ id, ...template }) => (
<TemplateCard key={id} {...template} />
<TemplateCard key={id} {...template} id={id} />
))}
{templates.user.map(({ id, ...template }) => (
<TemplateCard key={id} {...template} />
<TemplateCard key={id} {...template} id={id} />
))}
</SimpleGrid>
</Box>
Expand Down

0 comments on commit 9e42994

Please sign in to comment.