Skip to content

Commit

Permalink
fix(apps): cms - fix for video field having undefined as input, schem…
Browse files Browse the repository at this point in the history
…a cleanup.
  • Loading branch information
johnnycrich committed May 16, 2024
1 parent cd185de commit 86d7483
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 60 deletions.
85 changes: 44 additions & 41 deletions apps/cms/admin/components/video-field/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
CommonFieldConfig,
} from '@keystone-6/core/types';
import { graphql } from '@keystone-6/core';
import { GraphQLResolveInfo } from 'graphql';

type VideoInput = {
file: string | null;
Expand Down Expand Up @@ -32,7 +33,9 @@ const VideoFieldInput = graphql.inputObject({
const VideoFieldOutput = graphql.object<VideoOutput>()({
name: 'VideoFieldOutput',
fields: {
file: graphql.field({ type: graphql.String }),
file: graphql.field({
type: graphql.String,
}),
label: graphql.field({
type: graphql.String,
}),
Expand All @@ -45,6 +48,16 @@ const VideoFieldOutput = graphql.object<VideoOutput>()({
export type VideoFieldConfig<ListTypeInfo extends BaseListTypeInfo> =
CommonFieldConfig<ListTypeInfo>;

const resolveInput = (val: VideoInput) => {
return {
file: val?.file || undefined,
label: val?.label || undefined,
caption: val?.caption || undefined,
thumbUrl: val?.thumbUrl || undefined,
thumbSmUrl: val?.thumbSmUrl || undefined,
};
};

export function video<ListTypeInfo extends BaseListTypeInfo>({
...config
}: VideoFieldConfig<ListTypeInfo> = {}): FieldTypeFunc<ListTypeInfo> {
Expand Down Expand Up @@ -91,58 +104,48 @@ export function video<ListTypeInfo extends BaseListTypeInfo>({
input: {
create: {
arg: graphql.arg({ type: VideoFieldInput }),
resolve(val, context) {
console.log('CREATE FILE:', val);

if (val === undefined) {
return undefined;
}

resolve(val) {
return {
file: val?.file || '',
label: val?.label || '',
caption: val?.caption || '',
thumbUrl: val?.thumbUrl || '',
thumbSmUrl: val?.thumbSmUrl || '',
file: val?.file || undefined,
label: val?.label || undefined,
caption: val?.caption || undefined,
thumbUrl: val?.thumbUrl || undefined,
thumbSmUrl: val?.thumbSmUrl || undefined,
};
},
},
update: {
arg: graphql.arg({ type: VideoFieldInput }),
resolve(val) {
if (val === null) return null;
if (val === undefined) return undefined;
console.log('UPDATE FILE:', val);

return {
file: val.file,
label: val?.label || '',
caption: val?.caption || '',
thumbUrl: val?.thumbUrl || '',
thumbSmUrl: val?.thumbSmUrl || '',
file: val?.file || undefined,
label: val?.label || undefined,
caption: val?.caption || undefined,
thumbUrl: val?.thumbUrl || undefined,
thumbSmUrl: val?.thumbSmUrl || undefined,
};
},
},
},
// output: graphql.field({
// type: VideoFieldOutput,
// async resolve({
// value: { file, label, caption, thumbUrl, thumbSmUrl },
// }) {
// const captionResolved = caption ? caption : undefined;
// if (file === null || label === null) {
// return null;
// }
// console.log('OUTPUT FILE:', label);
// return {
// file,
// label,
// caption: captionResolved,
// thumbUrl,
// thumbSmUrl,
// };
// },
// }),
output: graphql.field({
type: VideoFieldOutput,
async resolve({
value: { file, label, caption, thumbUrl, thumbSmUrl },
}) {
const captionResolved = caption ? caption : undefined;
if (file === null || label === null) {
return null;
}

return {
file,
label,
caption: captionResolved,
thumbUrl,
thumbSmUrl,
};
},
}),

views: './admin/components/video-field/views',

Expand Down
5 changes: 2 additions & 3 deletions apps/cms/admin/components/video-field/views.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,10 @@ export function Field({
}: FieldProps<typeof controller>) {
const VideoGridInstance = new VideoGrid();

console.log(value, itemValue);

return (
<>
<FieldContainer as="fieldset" className="flex flex-col p-2 max-w-[12rem]">
<FieldLabel as="legend">{field.label}</FieldLabel>
{value.file && value.file.length > 5 && value.thumbSmUrl.length > 5 && (
<>
<img src={value.thumbSmUrl} width={75} height={75} />
Expand All @@ -47,7 +46,7 @@ export function Field({
VideoGridInstance.GetData();
}}
>
{value ? 'Change' : 'Select'} Video
{value && value.file ? 'Change' : 'Select'} Video
</LoadingButton>
{VideoGridInstance._useStore().error && (
<p className="p-4 text-red font-bold block">Something went wrong.</p>
Expand Down
12 changes: 3 additions & 9 deletions apps/cms/admin/schema/elab/lists/initiative.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ import { cloudinaryImage } from '../../../components/cloudinary';
import { Social } from '../social';
import { video } from '../../../components/video-field';
import { azureStorageFile } from '../../../components/fields-azure/src';
import { azConfig, azConfigCustom } from '../../azure';
import { stars } from '../../../components/2-stars-field';
import { azConfigCustom } from '../../azure';

const Initiative: Lists.Initiative = list({
access: allowAll,
Expand All @@ -33,23 +32,18 @@ const Initiative: Lists.Initiative = list({
...group({
label: 'Intro Video/Slideshow',
fields: {
rating: stars({
ui: {
description: 'A star rating, with a scale of 5',
},
}),
introVideo: video({ label: 'Intro Video' }),
captions: azureStorageFile({
azureStorageConfig: azConfigCustom('captions'),
label: 'Captions File',
}),
/* video: text({
video: text({
label: 'Intro Video ID',
ui: {
description:
'Vimeo video ID. If specified, takes precedence over slideshow',
},
}), */
}),
videoThumbnail: cloudinaryImage({
label: 'Intro Video Thumbnail',
cloudinary: {
Expand Down
8 changes: 8 additions & 0 deletions apps/cms/admin/schema/elab/lists/slide.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,15 @@ const Slide: Lists.Slide = list({
'This is optional and different from the description of the image.',
},
}),

video: video(),
videoId: text({
label: 'Vimeo ID',
ui: {
description: 'Use the image field to specify thumbnail.',
},
}),

captions: azureStorageFile({
azureStorageConfig: azConfigCustom('captions'),
label: 'Video Captions File',
Expand Down
4 changes: 2 additions & 2 deletions apps/cms/admin/schema/elab/lists/studioProject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ const StudioProject: Lists.StudioProject = list({
'Vimeo video ID, if applicable. If specified, "Watch the film" button will appear on page',
},
}),
video: video({ label: 'Watch the film' }),
video: video({ label: 'Video' }),
captions: azureStorageFile({
azureStorageConfig: azConfigCustom('captions'),
label: 'Video Captions File',
Expand All @@ -122,7 +122,7 @@ const StudioProject: Lists.StudioProject = list({
label: 'Trailer video ID',
ui: { description: 'Vimeo video ID for trailer, if applicable.' },
}),
trailerVideo: video({ label: 'Watch the film' }),
trailerVideo: video({ label: 'Trailer' }),
trailerCaptions: azureStorageFile({
azureStorageConfig: azConfigCustom('captions'),
}),
Expand Down
14 changes: 10 additions & 4 deletions apps/cms/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -647,9 +647,9 @@ type Initiative {
id: ID!
name: String
intro: Initiative_intro_Document
rating: Int
introVideo: VideoFieldOutput
captions: AzureStorageFileFieldOutput
video: String
videoThumbnail: CloudinaryImage_File
videoCaption: String
slides(where: SlideWhereInput! = {}, orderBy: [SlideOrderByInput!]! = [], take: Int, skip: Int! = 0, cursor: SlideWhereUniqueInput): [Slide!]
Expand Down Expand Up @@ -717,6 +717,7 @@ input InitiativeWhereInput {
NOT: [InitiativeWhereInput!]
id: IDFilter
name: StringFilter
video: StringFilter
videoCaption: StringFilter
slides: SlideManyRelationFilter
news: NewsItemManyRelationFilter
Expand Down Expand Up @@ -749,17 +750,17 @@ input ResearchProjectManyRelationFilter {
input InitiativeOrderByInput {
id: OrderDirection
name: OrderDirection
rating: OrderDirection
video: OrderDirection
videoCaption: OrderDirection
ogDescription: OrderDirection
}

input InitiativeUpdateInput {
name: String
intro: JSON
rating: Int
introVideo: VideoFieldInput
captions: AzureStorageFileFieldInput
video: String
videoThumbnail: Upload
videoCaption: String
slides: SlideRelateToManyForUpdateInput
Expand Down Expand Up @@ -820,9 +821,9 @@ input InitiativeUpdateArgs {
input InitiativeCreateInput {
name: String
intro: JSON
rating: Int
introVideo: VideoFieldInput
captions: AzureStorageFileFieldInput
video: String
videoThumbnail: Upload
videoCaption: String
slides: SlideRelateToManyForCreateInput
Expand Down Expand Up @@ -2005,6 +2006,7 @@ type Slide {
altText: String
caption: String
video: VideoFieldOutput
videoId: String
captions: AzureStorageFileFieldOutput
order: Int
}
Expand All @@ -2024,6 +2026,7 @@ input SlideWhereInput {
eventSlides: EventManyRelationFilter
altText: StringFilter
caption: StringFilter
videoId: StringFilter
order: IntNullableFilter
}

Expand All @@ -2037,6 +2040,7 @@ input SlideOrderByInput {
id: OrderDirection
altText: OrderDirection
caption: OrderDirection
videoId: OrderDirection
order: OrderDirection
}

Expand All @@ -2050,6 +2054,7 @@ input SlideUpdateInput {
altText: String
caption: String
video: VideoFieldInput
videoId: String
captions: AzureStorageFileFieldInput
order: Int
}
Expand All @@ -2076,6 +2081,7 @@ input SlideCreateInput {
altText: String
caption: String
video: VideoFieldInput
videoId: String
captions: AzureStorageFileFieldInput
order: Int
}
Expand Down
3 changes: 2 additions & 1 deletion apps/cms/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,14 @@ model Initiative {
id String @id @default(cuid())
name String @unique @default("Initiative Name")
intro Json @default("[{\"type\":\"paragraph\",\"children\":[{\"text\":\"\"}]}]")
rating Int?
introVideo_file String
introVideo_label String @default("")
introVideo_caption String?
introVideo_thumbUrl String @default("")
introVideo_thumbSmUrl String @default("")
captions_filename String?
captions_filesize Int?
video String @default("")
videoThumbnail Json?
videoCaption String @default("")
slides Slide[] @relation("Initiative_slides")
Expand Down Expand Up @@ -303,6 +303,7 @@ model Slide {
video_caption String?
video_thumbUrl String @default("")
video_thumbSmUrl String @default("")
videoId String @default("")
captions_filename String?
captions_filesize Int?
order Int? @default(0)
Expand Down

0 comments on commit 86d7483

Please sign in to comment.