Skip to content

Commit

Permalink
feat(afterchange): respect an env variable to always update (#22)
Browse files Browse the repository at this point in the history
* style(afterchange): remove console logs

* feat(afterchange): respect an env variable to always update
  • Loading branch information
thompsonsj authored Jun 1, 2023
1 parent 561e8e2 commit b1b87a5
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,18 @@ export default buildConfig({
Plugin options:

- `projectId` (required): the id of the project in CrowdIn.
- `directoryId` (optional): define a parent directory in your CrowdIn project to sync translations.
- `directoryId` (optional): define a parent directory in your CrowdIn project to sync translations.

### Environment variables

Set `PAYLOAD_CROWDIN_SYNC_ALWAYS_UPDATE=true` to update all localized fields in CrowdIn when an article is created/updated.

By default, updates will only be sent to CrowdIn in the following scenarios.

- At least one of the localized text fields has changed: any change to a localized `text` field updates the compiled `fields.json` that is sent to CrowdIn.
- A `richText` field is changed. Individual `richText` fields will only be updated on CrowdIn if the content has changed - each field has its own file on CrowdIn.

It is useful to have a convenient way of forcing all localized fields to update at once. For example, if the plugin is activated on an existing install, it is convenient to trigger all updates on CrowdIn for a given article without having to change every `richText` field or one of the `text` fields.

## Details

Expand Down
16 changes: 6 additions & 10 deletions src/hooks/collections/afterChange.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { CollectionAfterChangeHook, CollectionConfig, Field, GlobalConfig, GlobalAfterChangeHook, PayloadRequest } from 'payload/types';
import { CrowdinPluginRequest, FieldWithName } from '../../types'
import { CrowdinPluginRequest } from '../../types'
import { findOrCreateArticleDirectory, payloadCreateCrowdInFile, payloadUpdateCrowdInFile, getCrowdinFile } from '../../api/payload'
import { buildCrowdinHtmlObject, buildCrowdinJsonObject, convertSlateToHtml, fieldChanged, getLocalizedFields } from '../../utilities'
import { buildCrowdinHtmlObject, buildCrowdinJsonObject, convertSlateToHtml, fieldChanged } from '../../utilities'
import deepEqual from 'deep-equal'
import dot from "dot-object"

/**
* Update CrowdIn collections and make updates in CrowdIn
Expand Down Expand Up @@ -116,8 +115,6 @@ const performAfterChange = async ({
return doc
}

console.log(getLocalizedFields({ fields: localizedFields, type: 'html'}))

/**
* Prepare JSON objects
*
Expand Down Expand Up @@ -201,13 +198,12 @@ const performAfterChange = async ({
doc: previousDoc,
fields: localizedFields,
})
console.log(currentCrowdinHtmlData)

Object.keys(currentCrowdinHtmlData).forEach(async name => {
const crowdinFile = await getCrowdinFile(name, articleDirectory.id, req.payload)
const currentValue = currentCrowdinHtmlData[name]
const prevValue = prevCrowdinHtmlData[name]
if (!fieldChanged(prevValue, currentValue, 'richText')) {
console.log(`${name} not changed`)
if (!fieldChanged(prevValue, currentValue, 'richText') && process.env.PAYLOAD_CROWDIN_SYNC_ALWAYS_UPDATE !== 'true') {
return
}
if (typeof crowdinFile === 'undefined') {
Expand Down Expand Up @@ -236,7 +232,7 @@ const performAfterChange = async ({
// as the asynchronous operations will run twice almost instantaneously
// on create.
if (operation === 'create') {
if (!deepEqual(currentCrowdinJsonData, prevCrowdinJsonData) && Object.keys(currentCrowdinJsonData).length !== 0) {
if ((!deepEqual(currentCrowdinJsonData, prevCrowdinJsonData) && Object.keys(currentCrowdinJsonData).length !== 0) || process.env.PAYLOAD_CROWDIN_SYNC_ALWAYS_UPDATE === 'true') {
await createJsonFile()
}
await createOrUpdateHtmlSource()
Expand All @@ -246,7 +242,7 @@ const performAfterChange = async ({
// and update if necessary
if (operation === 'update') {
const crowdinJsonFile = await getCrowdinFile('fields', articleDirectory.id, req.payload)
if (!deepEqual(currentCrowdinJsonData, prevCrowdinJsonData)) {
if (!deepEqual(currentCrowdinJsonData, prevCrowdinJsonData) || process.env.PAYLOAD_CROWDIN_SYNC_ALWAYS_UPDATE === 'true') {
if (typeof crowdinJsonFile === 'undefined') {
await createJsonFile()
} else {
Expand Down

0 comments on commit b1b87a5

Please sign in to comment.