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 "" diff --git a/src/components/FieldTypeSchemaExtenders/FromSchemaExtender.js b/src/components/FieldTypeSchemaExtenders/FromSchemaExtender.js index 033e82e..f533e57 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, their 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..663a13f 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 */ @@ -38,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, @@ -51,7 +77,38 @@ 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 : '', + ); + 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 && !isUserLoaded) 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 + ) { + const name = getFieldName(subblock.label, subblock.id); + if (!formData.hasOwnProperty(name)) { + const fields_to_send_with_value = getFieldsToSendWithValue(subblock); + onChangeFormData( + subblock.id, + name, + curUserEmail, + fields_to_send_with_value, + ); + } + } + }); + } const isValidField = (field) => { return formErrors?.indexOf(field) < 0; @@ -116,24 +173,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 (