Skip to content

Commit

Permalink
YouTubeVideoBlock: Fix youtubeIdentifier Type (#2121)
Browse files Browse the repository at this point in the history
- Fix type of youtubeIdentifier (was required, actually is optional)
- Don't show validation error if youtubeIdentifier is empty
- Allow emptying youtubeIdentifier

---------

Co-authored-by: Johannes Obermair <48853629+johnnyomair@users.noreply.github.com>
  • Loading branch information
thomasdax98 and johnnyomair committed May 28, 2024
1 parent fdf9fa7 commit 93a84b6
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 8 deletions.
7 changes: 7 additions & 0 deletions .changeset/real-plants-rescue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@comet/blocks-api": patch
---

Fix type of `youtubeIdentifier` in `YouTubeVideoBlock`

Previously, it was incorrectly typed as required. Now it's optional.
2 changes: 1 addition & 1 deletion demo/api/block-meta.json
Original file line number Diff line number Diff line change
Expand Up @@ -2061,7 +2061,7 @@
{
"name": "youtubeIdentifier",
"kind": "String",
"nullable": false
"nullable": true
},
{
"name": "aspectRatio",
Expand Down
4 changes: 3 additions & 1 deletion packages/admin/blocks-admin/src/blocks/YouTubeVideoBlock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ const isValidYouTubeIdentifier = (value: string) => {
};

const validateIdentifier = (value?: string) => {
if (!value) return undefined;

return value && isValidYouTubeIdentifier(value) ? undefined : (
<FormattedMessage id="comet.blocks.youTubeVideo.validation" defaultMessage="Should be a valid YouTube URL or identifier" />
);
Expand Down Expand Up @@ -56,7 +58,7 @@ export const YouTubeVideoBlock: BlockInterface<YouTubeVideoBlockData, State, You
<SelectPreviewComponent>
<BlocksFinalForm
onSubmit={(newState) => {
updateState({ ...state, ...newState });
updateState(newState);
}}
initialValues={state}
>
Expand Down
4 changes: 2 additions & 2 deletions packages/api/blocks-api/block-meta.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
{
"name": "youtubeIdentifier",
"kind": "String",
"nullable": false
"nullable": true
},
{
"name": "aspectRatio",
Expand Down Expand Up @@ -80,7 +80,7 @@
{
"name": "youtubeIdentifier",
"kind": "String",
"nullable": false
"nullable": true
},
{
"name": "aspectRatio",
Expand Down
4 changes: 2 additions & 2 deletions packages/api/blocks-api/src/blocks/youtube-video.block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ enum AspectRatio {
}

class YouTubeVideoBlockData extends BlockData {
@BlockField()
@BlockField({ nullable: true })
youtubeIdentifier?: string;

@BlockField({ type: "enum", enum: AspectRatio })
Expand All @@ -28,7 +28,7 @@ class YouTubeVideoBlockData extends BlockData {
class YouTubeVideoBlockInput extends BlockInput {
@IsOptional()
@IsString()
@BlockField()
@BlockField({ nullable: true })
youtubeIdentifier?: string;

@IsEnum(AspectRatio)
Expand Down
4 changes: 2 additions & 2 deletions packages/api/cms-api/block-meta.json
Original file line number Diff line number Diff line change
Expand Up @@ -976,7 +976,7 @@
{
"name": "youtubeIdentifier",
"kind": "String",
"nullable": false
"nullable": true
},
{
"name": "aspectRatio",
Expand Down Expand Up @@ -1007,7 +1007,7 @@
{
"name": "youtubeIdentifier",
"kind": "String",
"nullable": false
"nullable": true
},
{
"name": "aspectRatio",
Expand Down
2 changes: 2 additions & 0 deletions packages/site/cms-site/src/blocks/YouTubeVideoBlock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import styled from "styled-components";

import { YouTubeVideoBlockData } from "../blocks.generated";
import { withPreview } from "../iframebridge/withPreview";
import { PreviewSkeleton } from "../previewskeleton/PreviewSkeleton";
import { PropsWithData } from "./PropsWithData";

interface VideoContainerProps {
Expand Down Expand Up @@ -47,6 +48,7 @@ const parseYoutubeIdentifier = (value: string): string | undefined => {

export const YouTubeVideoBlock = withPreview(
({ data: { youtubeIdentifier, autoplay, loop, showControls, aspectRatio } }: PropsWithData<YouTubeVideoBlockData>) => {
if (!youtubeIdentifier) return <PreviewSkeleton type="media" hasContent={false} />;
const identifier = parseYoutubeIdentifier(youtubeIdentifier);

const searchParams = new URLSearchParams();
Expand Down

0 comments on commit 93a84b6

Please sign in to comment.