From 1172c55fdff52a4d64acb9111ba42b9df673f17c Mon Sep 17 00:00:00 2001 From: Matthias Barde Date: Tue, 12 Sep 2023 13:10:54 +0200 Subject: [PATCH 1/6] feat: Add option for email fields to set logged in users email as default --- .../FromSchemaExtender.js | 19 +++++++++++- src/components/FormView.jsx | 30 ++++++++++++++++++- 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/src/components/FieldTypeSchemaExtenders/FromSchemaExtender.js b/src/components/FieldTypeSchemaExtenders/FromSchemaExtender.js index 033e82e..0f2e50f 100644 --- a/src/components/FieldTypeSchemaExtenders/FromSchemaExtender.js +++ b/src/components/FieldTypeSchemaExtenders/FromSchemaExtender.js @@ -22,11 +22,20 @@ const messages = defineMessages({ defaultMessage: 'If selected, a copy of email will alse be sent to this address.', }, + userEmailAsDefault: { + id: 'userEmailAsDefault', + defaultMessage: 'Use email of logged in user as default', + }, + userEmailAsDefault_description: { + id: 'userEmailAsDefault_description', + defaultMessage: + 'If selected and user is logged in, his/her email address will be set as default value.', + }, }); export const FromSchemaExtender = (intl) => { return { - fields: ['use_as_reply_to', 'use_as_bcc'], + fields: ['use_as_reply_to', 'use_as_bcc', 'user_email_as_default'], properties: { use_as_reply_to: { title: intl.formatMessage(messages.useAsReplyTo), @@ -40,6 +49,14 @@ export const FromSchemaExtender = (intl) => { type: 'boolean', default: false, }, + user_email_as_default: { + title: intl.formatMessage(messages.userEmailAsDefault), + description: intl.formatMessage( + messages.userEmailAsDefault_description, + ), + type: 'boolean', + default: false, + }, }, required: [], }; diff --git a/src/components/FormView.jsx b/src/components/FormView.jsx index db65cea..8b2bec8 100644 --- a/src/components/FormView.jsx +++ b/src/components/FormView.jsx @@ -1,5 +1,7 @@ -import React from 'react'; +import React, { useEffect } from 'react'; import { useIntl, defineMessages } from 'react-intl'; +import { useSelector, useDispatch } from 'react-redux'; +import jwtDecode from 'jwt-decode'; import { Segment, Message, @@ -10,6 +12,7 @@ import { } from 'semantic-ui-react'; import { getFieldName } from 'volto-form-block/components/utils'; import Field from 'volto-form-block/components/Field'; +import { getUser } from '@plone/volto/actions'; import config from '@plone/volto/registry'; /* Style */ @@ -53,6 +56,31 @@ const FormView = ({ const intl = useIntl(); const FieldSchema = config.blocks.blocksConfig.form.fieldSchema; + const userId = useSelector((state) => + state.userSession.token ? jwtDecode(state.userSession.token).sub : '', + ); + const curUserEmail = useSelector( + (state) => state.users?.user?.email || false, + ); + const dispatch = useDispatch(); + useEffect(() => { + if (userId?.length > 0 && curUserEmail === false) dispatch(getUser(userId)); + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [userId]); + + if (curUserEmail?.length > 0) { + data.subblocks.forEach((subblock) => { + if ( + ['email', 'from'].includes(subblock.field_type) && + subblock.user_email_as_default === true + ) { + const name = getFieldName(subblock.label, subblock.id); + if (!formData.hasOwnProperty(name)) + formData[name] = { value: curUserEmail }; + } + }); + } + const isValidField = (field) => { return formErrors?.indexOf(field) < 0; }; From 25e8fe43368715c06bb2722d0a618f3294fff557 Mon Sep 17 00:00:00 2001 From: Matthias Barde Date: Tue, 12 Sep 2023 13:14:59 +0200 Subject: [PATCH 2/6] update locales --- locales/de/LC_MESSAGES/volto.po | 10 ++++++++++ locales/en/LC_MESSAGES/volto.po | 10 ++++++++++ locales/es/LC_MESSAGES/volto.po | 10 ++++++++++ locales/eu/LC_MESSAGES/volto.po | 10 ++++++++++ locales/fr/LC_MESSAGES/volto.po | 10 ++++++++++ locales/it/LC_MESSAGES/volto.po | 10 ++++++++++ locales/ja/LC_MESSAGES/volto.po | 10 ++++++++++ locales/nl/LC_MESSAGES/volto.po | 10 ++++++++++ locales/pt/LC_MESSAGES/volto.po | 10 ++++++++++ locales/pt_BR/LC_MESSAGES/volto.po | 10 ++++++++++ locales/ro/LC_MESSAGES/volto.po | 10 ++++++++++ locales/volto.pot | 12 +++++++++++- 12 files changed, 121 insertions(+), 1 deletion(-) diff --git a/locales/de/LC_MESSAGES/volto.po b/locales/de/LC_MESSAGES/volto.po index 11917bb..3c21ea3 100644 --- a/locales/de/LC_MESSAGES/volto.po +++ b/locales/de/LC_MESSAGES/volto.po @@ -477,3 +477,13 @@ msgstr "Ergebnis" # defaultMessage: Title msgid "title" msgstr "Titel" + +#: components/FieldTypeSchemaExtenders/FromSchemaExtender +# defaultMessage: Use email of logged in user as default +msgid "userEmailAsDefault" +msgstr "Mit E-Mail von eingeloggtem Benutzer vorausfüllen" + +#: components/FieldTypeSchemaExtenders/FromSchemaExtender +# defaultMessage: If selected and user is logged in, his/her email address will be set as default value. +msgid "userEmailAsDefault_description" +msgstr "Wenn Benutzer eingeloggt ist, wird dessen E-Mail-Adresse automatisch in das Feld eingetragen." diff --git a/locales/en/LC_MESSAGES/volto.po b/locales/en/LC_MESSAGES/volto.po index 813422c..0b538b4 100644 --- a/locales/en/LC_MESSAGES/volto.po +++ b/locales/en/LC_MESSAGES/volto.po @@ -477,3 +477,13 @@ msgstr "" # defaultMessage: Title msgid "title" msgstr "Title" + +#: components/FieldTypeSchemaExtenders/FromSchemaExtender +# defaultMessage: Use email of logged in user as default +msgid "userEmailAsDefault" +msgstr "" + +#: components/FieldTypeSchemaExtenders/FromSchemaExtender +# defaultMessage: If selected and user is logged in, his/her email address will be set as default value. +msgid "userEmailAsDefault_description" +msgstr "" diff --git a/locales/es/LC_MESSAGES/volto.po b/locales/es/LC_MESSAGES/volto.po index b4a9c21..039789e 100644 --- a/locales/es/LC_MESSAGES/volto.po +++ b/locales/es/LC_MESSAGES/volto.po @@ -486,3 +486,13 @@ msgstr "Resultado" # defaultMessage: Title msgid "title" msgstr "Título" + +#: components/FieldTypeSchemaExtenders/FromSchemaExtender +# defaultMessage: Use email of logged in user as default +msgid "userEmailAsDefault" +msgstr "" + +#: components/FieldTypeSchemaExtenders/FromSchemaExtender +# defaultMessage: If selected and user is logged in, his/her email address will be set as default value. +msgid "userEmailAsDefault_description" +msgstr "" diff --git a/locales/eu/LC_MESSAGES/volto.po b/locales/eu/LC_MESSAGES/volto.po index 7bfb027..bbde2be 100644 --- a/locales/eu/LC_MESSAGES/volto.po +++ b/locales/eu/LC_MESSAGES/volto.po @@ -479,3 +479,13 @@ msgstr "emaitza" # defaultMessage: Title msgid "title" msgstr "Izenburua" + +#: components/FieldTypeSchemaExtenders/FromSchemaExtender +# defaultMessage: Use email of logged in user as default +msgid "userEmailAsDefault" +msgstr "" + +#: components/FieldTypeSchemaExtenders/FromSchemaExtender +# defaultMessage: If selected and user is logged in, his/her email address will be set as default value. +msgid "userEmailAsDefault_description" +msgstr "" diff --git a/locales/fr/LC_MESSAGES/volto.po b/locales/fr/LC_MESSAGES/volto.po index 80c5e6a..d59b9bd 100644 --- a/locales/fr/LC_MESSAGES/volto.po +++ b/locales/fr/LC_MESSAGES/volto.po @@ -477,3 +477,13 @@ msgstr "" # defaultMessage: Title msgid "title" msgstr "" + +#: components/FieldTypeSchemaExtenders/FromSchemaExtender +# defaultMessage: Use email of logged in user as default +msgid "userEmailAsDefault" +msgstr "" + +#: components/FieldTypeSchemaExtenders/FromSchemaExtender +# defaultMessage: If selected and user is logged in, his/her email address will be set as default value. +msgid "userEmailAsDefault_description" +msgstr "" diff --git a/locales/it/LC_MESSAGES/volto.po b/locales/it/LC_MESSAGES/volto.po index f29a01f..5a1f0b6 100644 --- a/locales/it/LC_MESSAGES/volto.po +++ b/locales/it/LC_MESSAGES/volto.po @@ -477,3 +477,13 @@ msgstr "risultato" # defaultMessage: Title msgid "title" msgstr "Titolo" + +#: components/FieldTypeSchemaExtenders/FromSchemaExtender +# defaultMessage: Use email of logged in user as default +msgid "userEmailAsDefault" +msgstr "" + +#: components/FieldTypeSchemaExtenders/FromSchemaExtender +# defaultMessage: If selected and user is logged in, his/her email address will be set as default value. +msgid "userEmailAsDefault_description" +msgstr "" diff --git a/locales/ja/LC_MESSAGES/volto.po b/locales/ja/LC_MESSAGES/volto.po index d69f80c..b8a1e6c 100644 --- a/locales/ja/LC_MESSAGES/volto.po +++ b/locales/ja/LC_MESSAGES/volto.po @@ -477,3 +477,13 @@ msgstr "" # defaultMessage: Title msgid "title" msgstr "" + +#: components/FieldTypeSchemaExtenders/FromSchemaExtender +# defaultMessage: Use email of logged in user as default +msgid "userEmailAsDefault" +msgstr "" + +#: components/FieldTypeSchemaExtenders/FromSchemaExtender +# defaultMessage: If selected and user is logged in, his/her email address will be set as default value. +msgid "userEmailAsDefault_description" +msgstr "" diff --git a/locales/nl/LC_MESSAGES/volto.po b/locales/nl/LC_MESSAGES/volto.po index d69f80c..b8a1e6c 100644 --- a/locales/nl/LC_MESSAGES/volto.po +++ b/locales/nl/LC_MESSAGES/volto.po @@ -477,3 +477,13 @@ msgstr "" # defaultMessage: Title msgid "title" msgstr "" + +#: components/FieldTypeSchemaExtenders/FromSchemaExtender +# defaultMessage: Use email of logged in user as default +msgid "userEmailAsDefault" +msgstr "" + +#: components/FieldTypeSchemaExtenders/FromSchemaExtender +# defaultMessage: If selected and user is logged in, his/her email address will be set as default value. +msgid "userEmailAsDefault_description" +msgstr "" diff --git a/locales/pt/LC_MESSAGES/volto.po b/locales/pt/LC_MESSAGES/volto.po index d69f80c..b8a1e6c 100644 --- a/locales/pt/LC_MESSAGES/volto.po +++ b/locales/pt/LC_MESSAGES/volto.po @@ -477,3 +477,13 @@ msgstr "" # defaultMessage: Title msgid "title" msgstr "" + +#: components/FieldTypeSchemaExtenders/FromSchemaExtender +# defaultMessage: Use email of logged in user as default +msgid "userEmailAsDefault" +msgstr "" + +#: components/FieldTypeSchemaExtenders/FromSchemaExtender +# defaultMessage: If selected and user is logged in, his/her email address will be set as default value. +msgid "userEmailAsDefault_description" +msgstr "" diff --git a/locales/pt_BR/LC_MESSAGES/volto.po b/locales/pt_BR/LC_MESSAGES/volto.po index b293dc4..e8b54be 100644 --- a/locales/pt_BR/LC_MESSAGES/volto.po +++ b/locales/pt_BR/LC_MESSAGES/volto.po @@ -483,3 +483,13 @@ msgstr "" # defaultMessage: Title msgid "title" msgstr "título" + +#: components/FieldTypeSchemaExtenders/FromSchemaExtender +# defaultMessage: Use email of logged in user as default +msgid "userEmailAsDefault" +msgstr "" + +#: components/FieldTypeSchemaExtenders/FromSchemaExtender +# defaultMessage: If selected and user is logged in, his/her email address will be set as default value. +msgid "userEmailAsDefault_description" +msgstr "" diff --git a/locales/ro/LC_MESSAGES/volto.po b/locales/ro/LC_MESSAGES/volto.po index d69f80c..b8a1e6c 100644 --- a/locales/ro/LC_MESSAGES/volto.po +++ b/locales/ro/LC_MESSAGES/volto.po @@ -477,3 +477,13 @@ msgstr "" # defaultMessage: Title msgid "title" msgstr "" + +#: components/FieldTypeSchemaExtenders/FromSchemaExtender +# defaultMessage: Use email of logged in user as default +msgid "userEmailAsDefault" +msgstr "" + +#: components/FieldTypeSchemaExtenders/FromSchemaExtender +# defaultMessage: If selected and user is logged in, his/her email address will be set as default value. +msgid "userEmailAsDefault_description" +msgstr "" diff --git a/locales/volto.pot b/locales/volto.pot index 7c923c3..54c25ee 100644 --- a/locales/volto.pot +++ b/locales/volto.pot @@ -1,7 +1,7 @@ msgid "" msgstr "" "Project-Id-Version: Plone\n" -"POT-Creation-Date: 2023-05-29T11:05:12.094Z\n" +"POT-Creation-Date: 2023-09-12T11:11:32.690Z\n" "Last-Translator: Plone i18n \n" "Language-Team: Plone i18n \n" "MIME-Version: 1.0\n" @@ -479,3 +479,13 @@ msgstr "" # defaultMessage: Title msgid "title" msgstr "" + +#: components/FieldTypeSchemaExtenders/FromSchemaExtender +# defaultMessage: Use email of logged in user as default +msgid "userEmailAsDefault" +msgstr "" + +#: components/FieldTypeSchemaExtenders/FromSchemaExtender +# defaultMessage: If selected and user is logged in, his/her email address will be set as default value. +msgid "userEmailAsDefault_description" +msgstr "" From 51b6dfa7ca3ea92bdcd6c3646286d6846541ab04 Mon Sep 17 00:00:00 2001 From: Matthias Barde Date: Tue, 12 Dec 2023 11:09:35 +0100 Subject: [PATCH 3/6] fix: wording in FromSchemaExtender.js Co-authored-by: Piero Nicolli --- src/components/FieldTypeSchemaExtenders/FromSchemaExtender.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/components/FieldTypeSchemaExtenders/FromSchemaExtender.js b/src/components/FieldTypeSchemaExtenders/FromSchemaExtender.js index 0f2e50f..f533e57 100644 --- a/src/components/FieldTypeSchemaExtenders/FromSchemaExtender.js +++ b/src/components/FieldTypeSchemaExtenders/FromSchemaExtender.js @@ -29,7 +29,7 @@ const messages = defineMessages({ userEmailAsDefault_description: { id: 'userEmailAsDefault_description', defaultMessage: - 'If selected and user is logged in, his/her email address will be set as default value.', + 'If selected and user is logged in, their email address will be set as default value.', }, }); From 329b80636fc4a7c299cb1eac1c4252490afe19cd Mon Sep 17 00:00:00 2001 From: Matthias Barde Date: Tue, 12 Dec 2023 11:10:16 +0100 Subject: [PATCH 4/6] fix: remove unnecessarily explicit check Co-authored-by: Piero Nicolli --- src/components/FormView.jsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/components/FormView.jsx b/src/components/FormView.jsx index 8b2bec8..c315597 100644 --- a/src/components/FormView.jsx +++ b/src/components/FormView.jsx @@ -72,7 +72,7 @@ const FormView = ({ data.subblocks.forEach((subblock) => { if ( ['email', 'from'].includes(subblock.field_type) && - subblock.user_email_as_default === true + subblock.user_email_as_default ) { const name = getFieldName(subblock.label, subblock.id); if (!formData.hasOwnProperty(name)) From ed8cf118ff36a380d8293536ae2de17b7549d660 Mon Sep 17 00:00:00 2001 From: Matthias Barde Date: Tue, 12 Dec 2023 11:29:59 +0100 Subject: [PATCH 5/6] fix: use onChangeFormData to change form data in the state --- src/components/FormView.jsx | 56 ++++++++++++++++++++++--------------- 1 file changed, 34 insertions(+), 22 deletions(-) diff --git a/src/components/FormView.jsx b/src/components/FormView.jsx index c315597..7d001b7 100644 --- a/src/components/FormView.jsx +++ b/src/components/FormView.jsx @@ -41,6 +41,29 @@ const messages = defineMessages({ }, }); +const getFieldsToSendWithValue = (subblock) => { + const FieldSchema = config.blocks.blocksConfig.form.fieldSchema; + + var fields_to_send = []; + var fieldSchemaProperties = FieldSchema(subblock)?.properties; + for (var key in fieldSchemaProperties) { + if (fieldSchemaProperties[key].send_to_backend) { + fields_to_send.push(key); + } + } + + var fields_to_send_with_value = Object.assign( + {}, + ...fields_to_send.map((field) => { + return { + [field]: subblock[field], + }; + }), + ); + + return fields_to_send_with_value; +}; + const FormView = ({ formState, formErrors, @@ -54,8 +77,6 @@ const FormView = ({ id, }) => { const intl = useIntl(); - const FieldSchema = config.blocks.blocksConfig.form.fieldSchema; - const userId = useSelector((state) => state.userSession.token ? jwtDecode(state.userSession.token).sub : '', ); @@ -75,8 +96,15 @@ const FormView = ({ subblock.user_email_as_default ) { const name = getFieldName(subblock.label, subblock.id); - if (!formData.hasOwnProperty(name)) - formData[name] = { value: curUserEmail }; + if (!formData.hasOwnProperty(name)) { + const fields_to_send_with_value = getFieldsToSendWithValue(subblock); + onChangeFormData( + subblock.id, + name, + curUserEmail, + fields_to_send_with_value, + ); + } } }); } @@ -144,24 +172,8 @@ const FormView = ({ ))} {data.subblocks?.map((subblock, index) => { let name = getFieldName(subblock.label, subblock.id); - - var fields_to_send = []; - var fieldSchemaProperties = FieldSchema(subblock)?.properties; - for (var key in fieldSchemaProperties) { - if (fieldSchemaProperties[key].send_to_backend) { - fields_to_send.push(key); - } - } - - var fields_to_send_with_value = Object.assign( - {}, - ...fields_to_send.map((field) => { - return { - [field]: subblock[field], - }; - }), - ); - + const fields_to_send_with_value = + getFieldsToSendWithValue(subblock); return ( From 1c44b11df132d63ea90f46e212b14625601a4501 Mon Sep 17 00:00:00 2001 From: Matthias Barde Date: Tue, 12 Dec 2023 11:53:56 +0100 Subject: [PATCH 6/6] fix: make sure to only dispatch if it was not already loaded --- src/components/FormView.jsx | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/components/FormView.jsx b/src/components/FormView.jsx index 7d001b7..663a13f 100644 --- a/src/components/FormView.jsx +++ b/src/components/FormView.jsx @@ -80,12 +80,13 @@ const FormView = ({ const userId = useSelector((state) => state.userSession.token ? jwtDecode(state.userSession.token).sub : '', ); + const isUserLoaded = useSelector((state) => state.users?.get?.loaded); const curUserEmail = useSelector( (state) => state.users?.user?.email || false, ); const dispatch = useDispatch(); useEffect(() => { - if (userId?.length > 0 && curUserEmail === false) dispatch(getUser(userId)); + if (userId?.length > 0 && !isUserLoaded) dispatch(getUser(userId)); // eslint-disable-next-line react-hooks/exhaustive-deps }, [userId]);