diff --git a/src/api/BlogPosts.ts b/src/api/BlogPosts.ts index fd2b1c5..2bd0d08 100644 --- a/src/api/BlogPosts.ts +++ b/src/api/BlogPosts.ts @@ -27,10 +27,10 @@ export class BlogPostsApi { // eslint-disable-next-line no-useless-constructor constructor( private readonly client: ApiClient ) {} - async create( body: CreateBody ): Promise< BlogPost > { + async create( blogPost: BlogPost ): Promise< BlogPost > { const response = ( await this.client.post( '/liberated_data', { meta: { - guid: body.guid, + guid: blogPost.guid, }, } ) ) as ApiPost; return fromApiResponse( response ); diff --git a/src/model/subject/BlogPost.ts b/src/model/subject/BlogPost.ts index a9c714e..4d773e5 100644 --- a/src/model/subject/BlogPost.ts +++ b/src/model/subject/BlogPost.ts @@ -1,7 +1,7 @@ import { Subject, SubjectType } from '@/model/subject/Subject'; -import { DateField } from '@/model/field/DateField'; -import { TextField } from '@/model/field/TextField'; -import { HtmlField } from '@/model/field/HtmlField'; +import { DateField, newDateField } from '@/model/field/DateField'; +import { newTextField, TextField } from '@/model/field/TextField'; +import { HtmlField, newHtmlField } from '@/model/field/HtmlField'; export interface BlogPost extends Subject { type: SubjectType.BlogPost; @@ -10,6 +10,18 @@ export interface BlogPost extends Subject { content: HtmlField; } +export function newBlogPost( sourceUrl: string ): BlogPost { + return { + id: 0, + transformedId: 0, + type: SubjectType.BlogPost, + guid: sourceUrl, + date: newDateField(), + title: newTextField(), + content: newHtmlField(), + }; +} + export function validateBlogPost( blogPost: BlogPost ): boolean { const fields = [ blogPost.title, blogPost.date, blogPost.content ]; let isValid = true; diff --git a/src/ui/blueprints/useSubjectForBlueprint.ts b/src/ui/blueprints/useSubjectForBlueprint.ts index 940a00f..aa0ad60 100644 --- a/src/ui/blueprints/useSubjectForBlueprint.ts +++ b/src/ui/blueprints/useSubjectForBlueprint.ts @@ -2,6 +2,7 @@ import { Blueprint } from '@/model/blueprint/Blueprint'; import { Subject, SubjectType } from '@/model/subject/Subject'; import { useEffect, useState } from 'react'; import { useSessionContext } from '@/ui/session/SessionProvider'; +import { newBlogPost } from '@/model/subject/BlogPost'; // Create or load a Subject to preview the Blueprint's results. // If a Subject already exists for the Blueprint's source URL, we use that Subject, @@ -17,26 +18,28 @@ export function useSubjectForBlueprint( return; } async function loadSubject( bp: Blueprint ) { - let p: Subject | null; + let subj: Subject | null; switch ( bp.type ) { case SubjectType.BlogPost: - p = await apiClient!.blogPosts.findByGuid( bp.sourceUrl ); + subj = await apiClient!.blogPosts.findByGuid( + bp.sourceUrl + ); break; default: throw Error( `unknown blueprint type ${ bp.type }` ); } - if ( ! p ) { + if ( ! subj ) { switch ( bp.type ) { case SubjectType.BlogPost: - p = await apiClient!.blogPosts.create( { - guid: bp.sourceUrl, - } ); + subj = await apiClient!.blogPosts.create( + newBlogPost( bp.sourceUrl ) + ); break; default: throw Error( `unknown blueprint type ${ bp.type }` ); } } - setSubject( p ); + setSubject( subj ); } loadSubject( blueprint ).catch( console.error ); }, [ blueprint, apiClient ] );