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 all 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, 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),
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
81 changes: 61 additions & 20 deletions 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 @@ -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,
Expand All @@ -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;
Expand Down Expand Up @@ -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 (
<Grid.Row key={'row' + index}>
<Grid.Column>
Expand Down