Skip to content

Commit

Permalink
Extract logic to a function
Browse files Browse the repository at this point in the history
  • Loading branch information
psrpinto committed Oct 24, 2024
1 parent e510938 commit 0495b91
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 37 deletions.
14 changes: 14 additions & 0 deletions src/parser/blog-post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,20 @@ import { findDeepestChild } from '@/parser/util';
import { DateField, newDateField } from '@/model/field/DateField';
import { newTextField, TextField } from '@/model/field/TextField';
import { HtmlField, newHtmlField } from '@/model/field/HtmlField';
import { Field } from '@/model/field/Field';

export function parseBlogPostField( name: string, field: Field ): Field {
switch ( name ) {
case 'date':
return parseBlogPostDate( field.original );
case 'title':
return parseBlogPostTitle( field.original );
case 'content':
return parseBlogPostContent( field.original );
default:
throw Error( `unknown field type ${ field.type }` );
}
}

export function parseBlogPostDate( html: string ): DateField {
const container = document.createElement( 'div' );
Expand Down
49 changes: 12 additions & 37 deletions src/ui/blueprints/EditBlueprint.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@ import { useSessionContext } from '@/ui/session/SessionProvider';
import { BlogPostBlueprintEditor } from '@/ui/blueprints/blog-post/BlogPostBlueprintEditor';
import { ContentBus } from '@/bus/ContentBus';
import { Toolbar } from '@/ui/blueprints/Toolbar';
import {
parseBlogPostContent,
parseBlogPostDate,
parseBlogPostTitle,
} from '@/parser/blog-post';
import { parseBlogPostField } from '@/parser/blog-post';
import { SubjectType } from '@/model/subject/Subject';
import { Screens } from '@/ui/App';
import { useBlueprint } from '@/ui/blueprints/useBlueprint';
Expand Down Expand Up @@ -48,57 +44,36 @@ export function EditBlueprint() {
if ( ! blueprint || ! subject ) {
return;
}
let subjectFieldsToUpdate: object | undefined;
let parsedField: Field;
switch ( subject.type ) {
case SubjectType.BlogPost:
switch ( name ) {
case 'date':
field = parseBlogPostDate( field.original );
subjectFieldsToUpdate = {
date: field,
};
break;
case 'title':
field = parseBlogPostTitle( field.original );
subjectFieldsToUpdate = {
title: field,
};
break;
case 'content':
field = parseBlogPostContent( field.original );
subjectFieldsToUpdate = {
content: field,
};
break;
default:
throw Error( `unknown field type ${ field.type }` );
}
parsedField = parseBlogPostField( name, field );
break;
default:
throw Error( `unknown subject type ${ subject.type }` );
}
const subjectFieldsToUpdate: Record< string, Field > = {};
subjectFieldsToUpdate[ name ] = parsedField;

blueprint.fields[ name ].selector = selector;

let isBlueprintValid = true;
for ( const f of Object.values( blueprint.fields ) ) {
if ( f.selector === '' ) {
isBlueprintValid = false;
break;
}
}
blueprint.valid = isBlueprintValid;

const bp = await apiClient!.blueprints.update( blueprint );
setBlueprint( bp );

if ( subjectFieldsToUpdate ) {
const p = await apiClient!.blogPosts.update(
subject.id,
subjectFieldsToUpdate
);
setSubject( p );
void playgroundClient.goTo( '/?p=' + subject.transformedId );
}
const p = await apiClient!.blogPosts.update(
subject.id,
subjectFieldsToUpdate
);
setSubject( p );
void playgroundClient.goTo( '/?p=' + subject.transformedId );
}

let isValid = ! blueprint ? false : blueprint.valid;
Expand Down

0 comments on commit 0495b91

Please sign in to comment.