Skip to content

Commit

Permalink
doc(contentful): doc to add new model
Browse files Browse the repository at this point in the history
  • Loading branch information
dpinol committed Jun 10, 2021
1 parent 8774e7c commit f95705d
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 6 deletions.
25 changes: 25 additions & 0 deletions packages/botonic-plugin-contentful/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -298,3 +298,28 @@ To define an empty day (such as bank holidays), just create the _Day Schedule_ b

**Exceptional Schedule**
For days with exceptional schedule (such as sales days), create the _Day Schedule_ and specify the special _Hour range_.

# Extending this plugin

## How to add a new TopContent
- Don't panic. You can take [this](https://github.com/hubtype/botonic/pull/1417) as an example
- Add enum entry to NonMessageTopContentType or MessageTopContentType
- Implement a content class derived from TopContents to contents.ts
- Add the new model to QA contentful space. To test custom fields, add a string
field named "customFieldText"
- Implement a delivery function to CMS interface
- Implement a class derived from TopContentDelivery in src/contentful/contents
- Integrate the previous class in ManageContentful as done for other types
- Implement a delivery function to all classes that implement CMS interface
- Implement a <NewContent>Builder class derived from TopContentBuilder in src/cms/factories/content-factories.ts.
Implement a Rnd<NewContent>Builder class derived from <NewContent>Builder at src/cms/test-helpers/builders.ts
- Write integration tests using the builder classes in tests/contentful/contents which validates delivery of:
1. Minimal content (all content's optional fields in blank)
2. Full content. All content's optional fields filled. For complex contents, you may need
different tests for each subcase.
3. In contentful.com, add to the new model a text field named customFieldText.
Fill the customFields on one of the tests contents. Write a test which validates that when
delivered, the content common.customFields field only contains the field "customFieldText"
- Add new field types to CONTENT_FIELDS in manage-cms/fields.ts


6 changes: 3 additions & 3 deletions packages/botonic-plugin-contentful/src/cms/contents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,13 @@ export class Asset {
}

/**
* Any content deliverable from a CMS
* Any content deliverable from a CMS.
* They are immutable, which allows sharing and caching them.
*/
export abstract class Content implements Stringable {
protected constructor(readonly contentType: ContentType) {}

/** @return message if any issue detected */
/** @return error message if any issue detected */
validate(): string | undefined {
return undefined
}
Expand Down Expand Up @@ -157,7 +158,6 @@ export abstract class MessageContent extends TopContent {
/**
* When any {@link keywords} is detected on a user input, we can use display the {@link shortText} for users
* to confirm their interest on this content
* TODO move contentId o ContentType here?
*/
export class CommonFields implements Stringable {
readonly shortText: string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ import {
Text,
} from '../contents'

/**
* Builder for Contents (which are immutable) which allow:
* - Setting the optional fields individually and in any order
* - Easing the implementation of the RndXXXBuilder classes at src/cms/test-helpers/builders.ts
*/
abstract class ContentBuilder {
protected constructor(public id: string, public name: string) {}

Expand All @@ -28,9 +33,6 @@ abstract class ContentBuilder {
abstract build(): Content
}

/** @deprecated use ContentBuilder */
export type ModelBuilder = ContentBuilder

export abstract class TopContentBuilder extends ContentBuilder {
shortText?: string
keywords: string[] = []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ import {
TopContentId,
} from '../index'

/**
* The Builder classes below create Content instances with minimal effort
* to speedup the implementation of unit tests by setting random values for the
* non specified fields.
* They are exported by the plugin so that the can be used in bots' unit tests
*/
export function rndStr(): string {
return Math.random().toString()
}
Expand Down
8 changes: 8 additions & 0 deletions packages/botonic-plugin-contentful/src/manage-cms/fields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,10 @@ export class I18nField {
constructor(readonly name: ContentFieldType, readonly value: string) {}
}

/**
* Only contains i18nalizable fields (used by tools to detect which fields need to be imported/deleted)
* TODO add all fields
*/
const FIELDS_PER_CONTENT_TYPE: { [type: string]: ContentFieldType[] } = {
[ContentType.BUTTON]: [ContentFieldType.TEXT],
[ContentType.CAROUSEL]: [],
Expand All @@ -199,6 +203,10 @@ const FIELDS_PER_CONTENT_TYPE: { [type: string]: ContentFieldType[] } = {
[ContentType.URL]: [ContentFieldType.URL],
}

/**
* Adds common fields to FIELDS_PER_CONTENT_TYPE
* IMPORTANT @see FIELDS_PER_CONTENT_TYPE
*/
export function getFieldsForContentType(
contentType: ContentType
): ContentFieldType[] {
Expand Down

0 comments on commit f95705d

Please sign in to comment.