- 
                Notifications
    You must be signed in to change notification settings 
- Fork 5.5k
New Components - contentstack #15037
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Merged
      
      
    
  
     Merged
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            4 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      
    File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| import contentstack from "../../contentstack.app.mjs"; | ||
| import { | ||
| parseArray, parseEntry, | ||
| } from "../../common/utils.mjs"; | ||
|  | ||
| const createDocLink = "https://www.contentstack.com/docs/developers/apis/content-management-api#create-an-entry"; | ||
| const updateDocLink = "https://www.contentstack.com/docs/developers/apis/content-management-api#update-an-entry"; | ||
|  | ||
| export default { | ||
| props: { | ||
| contentstack, | ||
| contentType: { | ||
| propDefinition: [ | ||
| contentstack, | ||
| "contentType", | ||
| ], | ||
| reloadProps: true, | ||
| }, | ||
| }, | ||
| async additionalProps() { | ||
| if (!this.contentType) { | ||
| return {}; | ||
| } | ||
| try { | ||
| return await this.buildFieldProps(this.contentType); | ||
| } catch { | ||
| return { | ||
| entryObj: { | ||
| type: "object", | ||
| label: "Entry", | ||
| description: `Enter the entry object as JSON. [See the documentation](${this.isUpdate() | ||
| ? updateDocLink | ||
| : createDocLink}) for more information.`, | ||
| }, | ||
| }; | ||
| } | ||
| }, | ||
| methods: { | ||
| getType(field) { | ||
| if (field.data_type === "boolean") { | ||
| return "boolean"; | ||
| } | ||
| if (field.data_type === "json") { | ||
| return "object"; | ||
| } | ||
| return field.multiple | ||
| ? "string[]" | ||
| : "string"; | ||
| }, | ||
| isUpdate() { | ||
| return false; | ||
| }, | ||
| async getOptions(field) { | ||
| if (field.data_type === "reference") { | ||
| const referenceContentType = field.reference_to[0]; | ||
| const { entries } = await this.contentstack.listEntries({ | ||
| contentType: referenceContentType, | ||
| }); | ||
| return entries?.map(({ | ||
| uid: value, title: label, | ||
| }) => ({ | ||
| value, | ||
| label: label ?? value, | ||
| })) || []; | ||
| } | ||
| if (field.data_type === "file") { | ||
| const { assets } = await this.contentstack.listAssets(); | ||
| return assets?.map(({ | ||
| uid: value, title: label, | ||
| }) => ({ | ||
| value, | ||
| label: label ?? value, | ||
| })) || []; | ||
| } | ||
| return undefined; | ||
| }, | ||
| async buildFieldProps(contentType) { | ||
| const props = {}; | ||
| const { content_type: { schema } } = await this.contentstack.getContentType({ | ||
| contentType, | ||
| }); | ||
| for (const field of schema) { | ||
| props[field.uid] = { | ||
| type: this.getType(field), | ||
| label: field.display_name ?? field.uid, | ||
| description: `Value of field ${field.display_name}. Field type: \`${field.data_type}\``, | ||
| optional: this.isUpdate() | ||
| ? true | ||
| : !field.mandatory, | ||
| options: await this.getOptions(field), | ||
| }; | ||
| } | ||
| return props; | ||
| }, | ||
| async buildEntry() { | ||
| if (this.entryObj) { | ||
| return parseEntry(this.entryObj); | ||
| } | ||
| const { content_type: { schema } } = await this.contentstack.getContentType({ | ||
| contentType: this.contentType, | ||
| }); | ||
| const entry = {}; | ||
| for (const field of schema) { | ||
| if (!this[field.uid]) { | ||
| continue; | ||
| } | ||
| if (field.data_type === "reference") { | ||
| if (field.multiple) { | ||
| const referenceField = parseArray(this[field.uid]); | ||
| entry[field.uid] = referenceField?.map((value) => ({ | ||
| uid: value, | ||
| _content_type_uid: field.reference_to[0], | ||
| })); | ||
| } else { | ||
| entry[field.uid] = { | ||
| uid: this[field.uid], | ||
| _content_type_uid: field.reference_to[0], | ||
| }; | ||
| } | ||
| continue; | ||
| } | ||
| entry[field.uid] = this[field.uid]; | ||
| } | ||
| return parseEntry(entry); | ||
| }, | ||
| }, | ||
| }; | 
        
          
          
            33 changes: 33 additions & 0 deletions
          
          33 
        
  components/contentstack/actions/create-entry/create-entry.mjs
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| import common from "../common/entries.mjs"; | ||
|  | ||
| export default { | ||
| ...common, | ||
| key: "contentstack-create-entry", | ||
| name: "Create Entry", | ||
| description: "Creates a new entry in Contentstack. [See the documentation](https://www.contentstack.com/docs/developers/apis/content-management-api#create-an-entry).", | ||
| version: "0.0.1", | ||
| type: "action", | ||
| props: { | ||
| ...common.props, | ||
| locale: { | ||
| propDefinition: [ | ||
| common.props.contentstack, | ||
| "locale", | ||
| ], | ||
| }, | ||
| }, | ||
| async run({ $ }) { | ||
| const response = await this.contentstack.createEntry({ | ||
| $, | ||
| contentType: this.contentType, | ||
| params: { | ||
| locale: this.locale, | ||
| }, | ||
| data: { | ||
| entry: await this.buildEntry(), | ||
| }, | ||
| }); | ||
| $.export("$summary", `Created entry "${response.entry.title}" with UID ${response.entry.uid}`); | ||
| return response; | ||
| }, | ||
| }; | ||
        
          
          
            73 changes: 73 additions & 0 deletions
          
          73 
        
  components/contentstack/actions/publish-entry/publish-entry.mjs
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| import contentstack from "../../contentstack.app.mjs"; | ||
| import { parseArray } from "../../common/utils.mjs"; | ||
|  | ||
| export default { | ||
| key: "contentstack-publish-entry", | ||
| name: "Publish Entry", | ||
| description: "Publishes a specific entry using its UID. [See the documentation](https://www.contentstack.com/docs/developers/apis/content-management-api#publish-entry)", | ||
| version: "0.0.1", | ||
| type: "action", | ||
| props: { | ||
| contentstack, | ||
| contentType: { | ||
| propDefinition: [ | ||
| contentstack, | ||
| "contentType", | ||
| ], | ||
| }, | ||
| entryId: { | ||
| propDefinition: [ | ||
| contentstack, | ||
| "entryId", | ||
| (c) => ({ | ||
| contentType: c.contentType, | ||
| }), | ||
| ], | ||
| }, | ||
| environments: { | ||
| propDefinition: [ | ||
| contentstack, | ||
| "environments", | ||
| ], | ||
| }, | ||
| locales: { | ||
| propDefinition: [ | ||
| contentstack, | ||
| "locale", | ||
| ], | ||
| type: "string[]", | ||
| label: "Locale", | ||
| description: "The code of the language in which you want your entry to be localized in", | ||
| }, | ||
| scheduledAt: { | ||
| type: "string", | ||
| label: "Scheduled At", | ||
| description: "The date/time in the ISO format to publish the entry. Example: `2016-10-07T12:34:36.000Z`", | ||
| optional: true, | ||
| }, | ||
| }, | ||
| async run({ $ }) { | ||
| const { entry } = await this.contentstack.getEntry({ | ||
| $, | ||
| contentType: this.contentType, | ||
| entryId: this.entryId, | ||
| }); | ||
|         
                  michelle0927 marked this conversation as resolved.
              Show resolved
            Hide resolved | ||
|  | ||
| const response = await this.contentstack.publishEntry({ | ||
| $, | ||
| contentType: this.contentType, | ||
| entryId: this.entryId, | ||
| data: { | ||
| entry: { | ||
| environments: parseArray(this.environments), | ||
| locales: parseArray(this.locales), | ||
| }, | ||
| locale: entry.locale, | ||
| version: entry._version, | ||
| scheduled_at: this.scheduledAt, | ||
| }, | ||
| }); | ||
|         
                  michelle0927 marked this conversation as resolved.
              Show resolved
            Hide resolved | ||
| $.export("$summary", `Successfully published entry with UID ${this.entryId}`); | ||
| return response; | ||
| }, | ||
| }; | ||
        
          
          
            50 changes: 50 additions & 0 deletions
          
          50 
        
  components/contentstack/actions/update-entry/update-entry.mjs
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| import common from "../common/entries.mjs"; | ||
|  | ||
| export default { | ||
| ...common, | ||
| key: "contentstack-update-entry", | ||
| name: "Update Entry", | ||
| description: "Updates an existing Contentstack entry. [See the documentation](https://www.contentstack.com/docs/developers/apis/content-management-api#update-an-entry).", | ||
| version: "0.0.1", | ||
| type: "action", | ||
| props: { | ||
| ...common.props, | ||
| entryId: { | ||
| propDefinition: [ | ||
| common.props.contentstack, | ||
| "entryId", | ||
| (c) => ({ | ||
| contentType: c.contentType, | ||
| }), | ||
| ], | ||
| }, | ||
| locale: { | ||
| propDefinition: [ | ||
| common.props.contentstack, | ||
| "locale", | ||
| ], | ||
| optional: true, | ||
| }, | ||
| }, | ||
| methods: { | ||
| ...common.methods, | ||
| isUpdate() { | ||
| return true; | ||
| }, | ||
| }, | ||
| async run({ $ }) { | ||
| const response = await this.contentstack.updateEntry({ | ||
| $, | ||
| contentType: this.contentType, | ||
| entryId: this.entryId, | ||
| params: { | ||
| locale: this.locale, | ||
| }, | ||
| data: { | ||
| entry: await this.buildEntry(), | ||
| }, | ||
| }); | ||
| $.export("$summary", `Entry ${this.entryId} updated successfully`); | ||
| return response; | ||
| }, | ||
|         
                  michelle0927 marked this conversation as resolved.
              Show resolved
            Hide resolved | ||
| }; | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| export function parseArray(value) { | ||
| if (!value) { | ||
| return undefined; | ||
| } | ||
| if (typeof value === "string") { | ||
| try { | ||
| return JSON.parse(value); | ||
| } catch { | ||
| throw new Error(`Could not parse as array: ${value}`); | ||
| } | ||
| } | ||
| return value; | ||
| } | ||
|  | ||
| export function parseEntry(entry) { | ||
| if (!entry) { | ||
| return undefined; | ||
| } | ||
| if (typeof entry === "string") { | ||
| try { | ||
| return JSON.parse(entry); | ||
| } catch { | ||
| throw new Error("Could not parse entry as JSON"); | ||
| } | ||
| } | ||
| const parsedEntry = {}; | ||
| for (const [ | ||
| key, | ||
| value, | ||
| ] of Object.entries(entry)) { | ||
| if (typeof value === "string") { | ||
| try { | ||
| parsedEntry[key] = JSON.parse(value); | ||
| } catch { | ||
| parsedEntry[key] = value; | ||
| } | ||
| } else { | ||
| parsedEntry[key] = value; | ||
| } | ||
| } | ||
| return parsedEntry; | ||
| } | 
      
      Oops, something went wrong.
        
    
  
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.