Skip to content

Commit

Permalink
Pass intl object to initialValue function
Browse files Browse the repository at this point in the history
The function that calls initialValue is applyBlockInitialValue. So all
functions that call applyBlockInitialValue must receive intl.
  • Loading branch information
wesleybl committed Dec 12, 2024
1 parent 4dff6bd commit 7c0f235
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 8 deletions.
2 changes: 2 additions & 0 deletions packages/volto/news/6529.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Pass `intl` object to `initialValue` function. @wesleybl

10 changes: 6 additions & 4 deletions packages/volto/src/components/manage/Blocks/Block/BlocksForm.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ const BlocksForm = (props) => {
};

const onMutateBlock = (id, value) => {
const newFormData = mutateBlock(properties, id, value);
const newFormData = mutateBlock(properties, id, value, {}, intl);
onChangeFormData(newFormData);
};

Expand All @@ -149,6 +149,8 @@ const BlocksForm = (props) => {
value,
current,
config.experimental.addBlockButton.enabled ? 1 : 0,
{},
intl,
);

const blocksFieldname = getBlocksFieldname(newFormData);
Expand All @@ -166,7 +168,7 @@ const BlocksForm = (props) => {

const onAddBlock = (type, index) => {
if (editable) {
const [id, newFormData] = addBlock(properties, type, index);
const [id, newFormData] = addBlock(properties, type, index, {}, intl);
const blocksFieldname = getBlocksFieldname(newFormData);
const blockData = newFormData[blocksFieldname][id];
newFormData[blocksFieldname][id] = applyBlockDefaults({
Expand All @@ -188,7 +190,7 @@ const BlocksForm = (props) => {
const onDeleteBlock = (id, selectPrev) => {
const previous = previousBlockId(properties, id);

const newFormData = deleteBlock(properties, id);
const newFormData = deleteBlock(properties, id, intl);
onChangeFormData(newFormData);

onSelectBlock(selectPrev ? previous : null);
Expand Down Expand Up @@ -260,7 +262,7 @@ const BlocksForm = (props) => {

for (const [n, v] of blockList) {
if (!v) {
const newFormData = deleteBlock(properties, n);
const newFormData = deleteBlock(properties, n, intl);
onChangeFormData(newFormData);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ const EditBlockWrapper = (props) => {
[id]: value || null,
},
},
intl,
});
const newValue = newFormData[blocksFieldname][id];
onChangeBlock(id, newValue);
Expand Down
30 changes: 26 additions & 4 deletions packages/volto/src/helpers/Blocks/Blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,10 @@ export function moveBlock(formData, source, destination) {
* @function deleteBlock
* @param {Object} formData Form data
* @param {string} blockId Block uid
* @param {Object} intl intl object.
* @return {Object} New form data
*/
export function deleteBlock(formData, blockId) {
export function deleteBlock(formData, blockId, intl) {
const blocksFieldname = getBlocksFieldname(formData);
const blocksLayoutFieldname = getBlocksLayoutFieldname(formData);

Expand All @@ -135,7 +136,13 @@ export function deleteBlock(formData, blockId) {
};

if (newFormData[blocksLayoutFieldname].items.length === 0) {
newFormData = addBlock(newFormData, config.settings.defaultBlockType, 0);
newFormData = addBlock(
newFormData,
config.settings.defaultBlockType,
0,
{},
intl,
);
}

return newFormData;
Expand All @@ -147,9 +154,11 @@ export function deleteBlock(formData, blockId) {
* @param {Object} formData Form data
* @param {string} type Block type
* @param {number} index Destination index
* @param {Object} blocksConfig Blocks configuration.
* @param {Object} intl intl object.
* @return {Array} New block id, New form data
*/
export function addBlock(formData, type, index, blocksConfig) {
export function addBlock(formData, type, index, blocksConfig, intl) {
const { settings } = config;
const id = uuid();
const idTrailingBlock = uuid();
Expand Down Expand Up @@ -192,6 +201,7 @@ export function addBlock(formData, type, index, blocksConfig) {
},
selected: id,
},
intl,
}),
];
}
Expand All @@ -208,6 +218,7 @@ export const applyBlockInitialValue = ({
value,
blocksConfig,
formData,
intl,
}) => {
const type = value['@type'];
blocksConfig = blocksConfig || config.blocks.blocksConfig;
Expand All @@ -217,6 +228,7 @@ export const applyBlockInitialValue = ({
id,
value,
formData,
intl,
});
const blocksFieldname = getBlocksFieldname(formData);
formData[blocksFieldname][id] = value;
Expand All @@ -231,9 +243,11 @@ export const applyBlockInitialValue = ({
* @param {Object} formData Form data
* @param {string} id Block uid to mutate
* @param {number} value Block's new value
* @param {Object} blocksConfig Blocks configuration.
* @param {Object} intl intl object.
* @return {Object} New form data
*/
export function mutateBlock(formData, id, value, blocksConfig) {
export function mutateBlock(formData, id, value, blocksConfig, intl) {
const { settings } = config;
const blocksFieldname = getBlocksFieldname(formData);
const blocksLayoutFieldname = getBlocksLayoutFieldname(formData);
Expand All @@ -260,6 +274,7 @@ export function mutateBlock(formData, id, value, blocksConfig) {
[id]: value || null,
},
},
intl,
});
if (!blockHasValue(block)) {
return newFormData;
Expand Down Expand Up @@ -288,6 +303,7 @@ export function mutateBlock(formData, id, value, blocksConfig) {
],
},
},
intl,
});
return newFormData;
}
Expand All @@ -298,6 +314,10 @@ export function mutateBlock(formData, id, value, blocksConfig) {
* @param {Object} formData Form data
* @param {string} id Insert new block before the block with this id
* @param {number} value New block's value
* @param {Object} current Current block
* @param {number} offset offset position
* @param {Object} blocksConfig Blocks configuration.
* @param {Object} intl intl object.
* @return {Array} New block id, New form data
*/
export function insertBlock(
Expand All @@ -307,6 +327,7 @@ export function insertBlock(
current = {},
offset = 0,
blocksConfig,
intl,
) {
const blocksFieldname = getBlocksFieldname(formData);
const blocksLayoutFieldname = getBlocksLayoutFieldname(formData);
Expand Down Expand Up @@ -340,6 +361,7 @@ export function insertBlock(
],
},
},
intl,
});

return [newBlockId, newFormData];
Expand Down
46 changes: 46 additions & 0 deletions packages/volto/src/helpers/Blocks/Blocks.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,52 @@ describe('Blocks', () => {
marker: true,
});
});

it('initialValue with intl', () => {
// Mock intl with formatMessage function
const intl = {
formatMessage: jest.fn(({ id }) => id),
};

const messages = {
intl: {
id: 'intl',
defaultMessage: 'intl',
},
};

config.blocks.blocksConfig.text.initialValue = ({
id,
value,
formData,
intl,
}) => {
return {
...formData.blocks[id],
intl: intl.formatMessage(messages.intl),
};
};
const [newId, form] = addBlock(
{
blocks: { a: { value: 1 }, b: { value: 2 } },
blocks_layout: { items: ['a', 'b'] },
},
'text',
1,
config.blocks.blocksConfig,
intl,
);

delete config.blocks.blocksConfig.text.initialValue;

expect(form.blocks[newId]).toStrictEqual({
'@type': 'text',
booleanField: false,
description: 'Default description',
title: 'Default title',
intl: 'intl',
});
});
});

describe('moveBlock', () => {
Expand Down

0 comments on commit 7c0f235

Please sign in to comment.