From b1b87a5d7e84230f7204aeb24d21830e7a482f44 Mon Sep 17 00:00:00 2001 From: Steven Thompson <44806974+thompsonsj@users.noreply.github.com> Date: Thu, 1 Jun 2023 18:01:21 +0100 Subject: [PATCH] feat(afterchange): respect an env variable to always update (#22) * style(afterchange): remove console logs * feat(afterchange): respect an env variable to always update --- README.md | 13 ++++++++++++- src/hooks/collections/afterChange.ts | 16 ++++++---------- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index aca2230..18b71e3 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/hooks/collections/afterChange.ts b/src/hooks/collections/afterChange.ts index 32d3afc..b3c10c7 100644 --- a/src/hooks/collections/afterChange.ts +++ b/src/hooks/collections/afterChange.ts @@ -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 @@ -116,8 +115,6 @@ const performAfterChange = async ({ return doc } - console.log(getLocalizedFields({ fields: localizedFields, type: 'html'})) - /** * Prepare JSON objects * @@ -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') { @@ -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() @@ -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 {