Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option for email fields to set logged in users email as default #72

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions locales/de/LC_MESSAGES/volto.po
Original file line number Diff line number Diff line change
Expand Up @@ -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."
10 changes: 10 additions & 0 deletions locales/en/LC_MESSAGES/volto.po
Original file line number Diff line number Diff line change
Expand Up @@ -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 ""
10 changes: 10 additions & 0 deletions locales/es/LC_MESSAGES/volto.po
Original file line number Diff line number Diff line change
Expand Up @@ -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 ""
10 changes: 10 additions & 0 deletions locales/eu/LC_MESSAGES/volto.po
Original file line number Diff line number Diff line change
Expand Up @@ -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 ""
10 changes: 10 additions & 0 deletions locales/fr/LC_MESSAGES/volto.po
Original file line number Diff line number Diff line change
Expand Up @@ -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 ""
10 changes: 10 additions & 0 deletions locales/it/LC_MESSAGES/volto.po
Original file line number Diff line number Diff line change
Expand Up @@ -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 ""
10 changes: 10 additions & 0 deletions locales/ja/LC_MESSAGES/volto.po
Original file line number Diff line number Diff line change
Expand Up @@ -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 ""
10 changes: 10 additions & 0 deletions locales/nl/LC_MESSAGES/volto.po
Original file line number Diff line number Diff line change
Expand Up @@ -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 ""
10 changes: 10 additions & 0 deletions locales/pt/LC_MESSAGES/volto.po
Original file line number Diff line number Diff line change
Expand Up @@ -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 ""
10 changes: 10 additions & 0 deletions locales/pt_BR/LC_MESSAGES/volto.po
Original file line number Diff line number Diff line change
Expand Up @@ -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 ""
10 changes: 10 additions & 0 deletions locales/ro/LC_MESSAGES/volto.po
Original file line number Diff line number Diff line change
Expand Up @@ -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 ""
12 changes: 11 additions & 1 deletion locales/volto.pot
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>\n"
"Language-Team: Plone i18n <[email protected]>\n"
"MIME-Version: 1.0\n"
Expand Down Expand Up @@ -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 ""
19 changes: 18 additions & 1 deletion src/components/FieldTypeSchemaExtenders/FromSchemaExtender.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.',
mbarde marked this conversation as resolved.
Show resolved Hide resolved
},
});

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),
Expand All @@ -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: [],
};
Expand Down
30 changes: 29 additions & 1 deletion src/components/FormView.jsx
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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 */
Expand Down Expand Up @@ -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));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we have this info in redux already, if the user is logged in, right? No need to dispatch again if we do have it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the user is logged in we can retrieve the userId via (state) => jwtDecode(state.userSession.token).sub, but this is not necessarily the same as the users email address (depends on the corresponding setting of the Plone instance).
For additional user info (like email) we have to dispatch getUser.
Via 1c44b11 I make sure it is not dispatched, if another component already did it.

// 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
mbarde marked this conversation as resolved.
Show resolved Hide resolved
) {
const name = getFieldName(subblock.label, subblock.id);
if (!formData.hasOwnProperty(name))
formData[name] = { value: curUserEmail };
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I recommend against doing it this way. It is considered a bad practice to mutate props and also to mutate state while rendering. All state changes should be done in a useEffect callback and in this specific situation should be done using the onChangeFormData function in order to properly handle state changes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the feedback! Resolved via ed8cf11

In order to use onChangeFormData I had to compute fields_to_send_with_value. This is done at an other location in the component as well. So I decided to create a reusable function getFieldsToSendWithValue for it.

}
});
}

const isValidField = (field) => {
return formErrors?.indexOf(field) < 0;
};
Expand Down