Skip to content

Commit

Permalink
Add newBlogPost()
Browse files Browse the repository at this point in the history
  • Loading branch information
psrpinto committed Oct 24, 2024
1 parent cf137eb commit 3b7d422
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/api/BlogPosts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
18 changes: 15 additions & 3 deletions src/model/subject/BlogPost.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down
17 changes: 10 additions & 7 deletions src/ui/blueprints/useSubjectForBlueprint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 ] );
Expand Down

0 comments on commit 3b7d422

Please sign in to comment.