Skip to content

Commit

Permalink
feat(settings): choose the "from" address within all the available ad…
Browse files Browse the repository at this point in the history
…dresses

* feat(settings): choose the "from" address within all the available addresses
refactor: fixed/added types
refactor: changed the type-check of the identity to avoid relying on the translation value

* refactor(settings): onChange callback of the sender address selection

* chore: updated CODEOWNERS file

refs: IRIS-3609 (#177)
  • Loading branch information
gnekoz authored Jan 4, 2023
1 parent 349c9d6 commit ce36e6d
Show file tree
Hide file tree
Showing 12 changed files with 227 additions and 112 deletions.
2 changes: 1 addition & 1 deletion .github/CODEOWNERS
Original file line number Diff line number Diff line change
@@ -1 +1 @@
* @nubsthead @geeky-abhishek @giuliano176 @dhavaldodiya @Gammax92 @gnekoz
* @nubsthead @giuliano176 @dhavaldodiya @Gammax92 @gnekoz @Zextras/shuffled-waffles
25 changes: 17 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
"author": "Zextras Crab Onions Team <https://www.zextras.com/carbonio/>",
"maintainers": [
"Gabriele Marino <[email protected]>",
"Abhishek Kumar <[email protected]>",
"Giuliano Caregnato <[email protected]>",
"Dhaval Dodiya <[email protected]>",
"Francesco Gottardi <[email protected]>",
Expand Down Expand Up @@ -101,7 +100,7 @@
"@fontsource/roboto": "^4.5.7",
"@sentry/browser": "^6.17.7",
"@tinymce/tinymce-react": "^3.13.0",
"@zextras/carbonio-design-system": "^0.5.3",
"@zextras/carbonio-design-system": "^0.5.2",
"@zextras/carbonio-ui-preview": "^0.2.4",
"darkreader": "4.9.46",
"history": "^5.2.0",
Expand All @@ -128,7 +127,7 @@
},
"peerDependencies": {
"@reduxjs/toolkit": "^1.6.2",
"@zextras/carbonio-design-system": "^0.5.0",
"@zextras/carbonio-design-system": "^0.5.2",
"core-js": "^3.19.1",
"moment": "^2.29.1",
"node-fetch": "^2.6.6",
Expand Down
3 changes: 0 additions & 3 deletions src/network/edit-settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,6 @@ export const editSettings = (
displayName:
find(mods?.identity?.modifyList, (item) => item.id === s?.account?.id)?.prefs
.zimbraPrefIdentityName || s.account?.displayName,
name:
find(mods?.identity?.modifyList, (item) => item.id === s?.account?.id)?.prefs
.zimbraPrefFromAddress || s.account?.name,
identities: {
identity:
typeof s.account !== 'undefined'
Expand Down
43 changes: 25 additions & 18 deletions src/settings/account-wrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,42 @@ import React, { useMemo } from 'react';
import { useUserAccount } from '../store/account';
import { getT } from '../store/i18n';
import { AccountsSettings } from './accounts-settings';
import { IdentityProps } from '../../types';

const AccountWrapper = (): React.ReactElement | null => {
const accountSettings = useUserAccount();
const t = getT();
const identitiesDefault = useMemo(() => {
const temp = map(accountSettings?.identities.identity, (item, index) => ({
id: item.name === 'DEFAULT' ? '0' : (index + 1).toString(),
type: item.name === 'DEFAULT' ? t('label.primary', 'Primary') : t('label.persona', 'Persona'),
identityId: item._attrs.zimbraPrefIdentityId || '',
fromAddress: item._attrs.zimbraPrefFromAddress || '',
identityName: item._attrs.zimbraPrefIdentityName || '',
fromDisplay: item._attrs.zimbraPrefFromDisplay || '',
recoveryAccount: item._attrs.zimbraRecoveryAccount || '',
replyToDisplay: item._attrs.zimbraPrefReplyToDisplay || '',
replyToAddress: item._attrs.zimbraPrefReplyToAddress || '',
replyToEnabled: item._attrs.zimbraPrefReplyToEnabled || '',
saveToSent: item._attrs.zimbraPrefSaveToSent || '',
sentMailFolder: item._attrs.zimbraPrefSentMailFolder || '',
whenInFoldersEnabled: item._attrs.zimbraPrefWhenInFoldersEnabled || '',
whenSentToEnabled: item._attrs.zimbraPrefWhenSentToEnabled || '',
whenSentToAddresses: item._attrs.zimbraPrefWhenSentToAddresses || ''
}));
const temp = map(
accountSettings?.identities.identity,
(item, index) =>
({
id: item.name === 'DEFAULT' ? '0' : (index + 1).toString(),
flgType: item.name === 'DEFAULT' ? 'primary' : 'persona',
type:
item.name === 'DEFAULT' ? t('label.primary', 'Primary') : t('label.persona', 'Persona'),
identityId: item._attrs.zimbraPrefIdentityId || '',
fromAddress: item._attrs.zimbraPrefFromAddress || '',
identityName: item._attrs.zimbraPrefIdentityName || '',
fromDisplay: item._attrs.zimbraPrefFromDisplay || '',
recoveryAccount: item._attrs.zimbraRecoveryAccount || '',
replyToDisplay: item._attrs.zimbraPrefReplyToDisplay || '',
replyToAddress: item._attrs.zimbraPrefReplyToAddress || '',
replyToEnabled: item._attrs.zimbraPrefReplyToEnabled || '',
saveToSent: item._attrs.zimbraPrefSaveToSent || '',
sentMailFolder: item._attrs.zimbraPrefSentMailFolder || '',
whenInFoldersEnabled: item._attrs.zimbraPrefWhenInFoldersEnabled || '',
whenSentToEnabled: item._attrs.zimbraPrefWhenSentToEnabled || '',
whenSentToAddresses: item._attrs.zimbraPrefWhenSentToAddresses || ''
} as IdentityProps)
);
const result = [temp[temp.length - 1], ...temp];
result.pop();
return result;
}, [accountSettings, t]);

return identitiesDefault.length > 0 ? (
<AccountsSettings identitiesDefault={identitiesDefault} t={t} />
<AccountsSettings account={accountSettings} identitiesDefault={identitiesDefault} t={t} />
) : null;
};

Expand Down
97 changes: 84 additions & 13 deletions src/settings/accounts-settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,22 @@
import React, { useCallback, useMemo, useState, useEffect, ReactElement } from 'react';
import { Container, useSnackbar } from '@zextras/carbonio-design-system';
import { TFunction } from 'react-i18next';
import { map, includes, findIndex, reduce, find, replace, lowerFirst, isEmpty } from 'lodash';
import {
map,
includes,
findIndex,
reduce,
find,
replace,
lowerFirst,
isEmpty,
uniq,
isArray
} from 'lodash';
import { useUserSettings } from '../store/account/hooks';
import { editSettings } from '../network/edit-settings';
import { SHELL_APP_ID } from '../constants';
import { Mods, IdentityProps, CreateIdentityProps } from '../../types';
import { useAccountStore } from '../store/account/store';
import { Mods, IdentityProps, CreateIdentityProps, Account, AccountSettings } from '../../types';
import AccountsList from './components/account-settings/accounts-list';
import PrimaryAccountSettings from './components/account-settings/primary-account-settings';
import SettingsSentMessages from './components/account-settings/settings-sent-messages';
Expand All @@ -36,19 +46,71 @@ type AddModProps = {
createList?: { prefs: CreateIdentityProps }[];
};
type AccountSettingsProps = {
account: Account;
identitiesDefault: IdentityProps[];
t: TFunction;
};

type UserRightsProps = { email: string; right: string };
export const AccountsSettings = ({ identitiesDefault, t }: AccountSettingsProps): ReactElement => {

/**
* Compose a unique list of all identities' email addresses
*
* The list is composed of:
* - the email address of the current account
* - the email addresses of all the shared accounts (taken from the rights infos)
* - all the aliases
*
* @param account
* @param settings
*
* @returns a list of unique email addresses
*/
const getAvailableEmailAddresses = (account: Account, settings: AccountSettings): string[] => {
const result: string[] = [];

// Adds the email address of the primary account
result.push(account.name);

// Adds the email addresses of all the shared accounts
if (account.rights?.targets) {
account.rights?.targets.forEach((target) => {
if (target.right === 'sendAs' && target.target) {
target.target.forEach((user) => {
if (user.type === 'account' && user.email) {
user.email.forEach((email) => {
result.push(email.addr);
});
}
});
}
});
}

// Adds all the aliases
if (settings.attrs.zimbraMailAlias) {
if (isArray(settings.attrs.zimbraMailAlias)) {
result.push(...settings.attrs.zimbraMailAlias);
} else {
result.push(String(settings.attrs.zimbraMailAlias));
}
}

return uniq(result);
};

export const AccountsSettings = ({
account,
identitiesDefault,
t
}: AccountSettingsProps): ReactElement => {
const [mods, setMods] = useState<Mods>({});
const [activeDelegateView, setActiveDelegateView] = useState('0');
const [selectedIdentityId, setSelectedIdentityId] = useState(0);
const [identities, setIdentities] = useState<IdentityProps[]>(identitiesDefault);
const [delegates, setDelegates] = useState<DelegateType[]>([]);

const maxIdentities = useUserSettings().attrs.zimbraIdentityMaxNumEntries;
const settings = useUserSettings();
const maxIdentities = settings.attrs.zimbraIdentityMaxNumEntries;
const addMod = useCallback(
(arg: AddModProps) => {
const { type, modifyProp, deleteList, createList } = arg;
Expand Down Expand Up @@ -245,6 +307,11 @@ export const AccountsSettings = ({ identitiesDefault, t }: AccountSettingsProps)
const onCancel = useCallback(() => setMods({}), []);
const title: string = t('label.accounts', 'Accounts');
const isDirty = useMemo(() => !isEmpty(mods), [mods]);
const availableEmailAddresses = useMemo(
() => getAvailableEmailAddresses(account, settings),
[account, settings]
);

return (
<>
<SettingsHeader onSave={onSave} onCancel={onCancel} isDirty={isDirty} title={title} />
Expand All @@ -257,25 +324,28 @@ export const AccountsSettings = ({ identitiesDefault, t }: AccountSettingsProps)
>
<AccountsList
t={t}
account={account}
identities={identities}
setIdentities={setIdentities}
selectedIdentityId={selectedIdentityId}
setSelectedIdentityId={setSelectedIdentityId}
deleteIdentities={deleteIdentities}
createIdentities={createIdentities}
/>
{identities[selectedIdentityId]?.type === t('label.primary', 'Primary') && (
{identities[selectedIdentityId]?.flgType === 'primary' && (
<>
<PrimaryAccountSettings
t={t}
items={identities[0]}
account={account}
identity={identities[0]}
updateIdentities={updateIdentities}
/>
<SettingsSentMessages
t={t}
items={identities[selectedIdentityId]}
identity={identities[selectedIdentityId]}
isExternalAccount={false}
updateIdentities={updateIdentities}
availableEmailAddresses={availableEmailAddresses}
/>
{/* <PasswordRecoverySettings
t={t}
Expand All @@ -290,7 +360,7 @@ export const AccountsSettings = ({ identitiesDefault, t }: AccountSettingsProps)
/>
</>
)}
{identities[selectedIdentityId]?.type === t('label.persona', 'Persona') && (
{identities[selectedIdentityId]?.flgType === 'persona' && (
<>
<PersonaSettings
t={t}
Expand All @@ -299,18 +369,19 @@ export const AccountsSettings = ({ identitiesDefault, t }: AccountSettingsProps)
/>
<SettingsSentMessages
t={t}
items={identities[selectedIdentityId]}
identity={identities[selectedIdentityId]}
isExternalAccount={false}
updateIdentities={updateIdentities}
availableEmailAddresses={availableEmailAddresses}
/>
<PersonaUseSection
t={t}
items={identities[selectedIdentityId]}
identity={identities[selectedIdentityId]}
updateIdentities={updateIdentities}
/>
</>
)}
{includes(['IMAP', 'POP'], identities[selectedIdentityId]?.type) && (
{includes(['IMAP', 'POP'], identities[selectedIdentityId]?.flgType) && (
<>
{/* <ExternalAccount t={t} items={identities} />
<AdvancedSettings t={t} items={identities} />
Expand Down
9 changes: 6 additions & 3 deletions src/settings/components/account-settings/accounts-list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ import {
} from '@zextras/carbonio-design-system';
import { TFunction } from 'i18next';
import { map, filter, max, noop } from 'lodash';
import { IdentityProps, CreateIdentityProps } from '../../../../types';
import { IdentityProps, CreateIdentityProps, Account } from '../../../../types';

type AccountsListProps = {
t: TFunction;
account: Account;
identities: IdentityProps[];
setIdentities: (identities: IdentityProps[]) => void;
selectedIdentityId: number;
Expand All @@ -33,6 +34,7 @@ type AccountsListProps = {

const AccountsList = ({
t,
account,
selectedIdentityId,
identities,
setIdentities,
Expand Down Expand Up @@ -64,7 +66,7 @@ const AccountsList = ({
</Padding>
<Padding right="small">
<Text weight="regular" size="small" color="secondary">
({item.fromAddress})
({item.flgType === 'primary' ? account.name : item.fromAddress})
</Text>
</Padding>
</Container>
Expand Down Expand Up @@ -104,6 +106,7 @@ const AccountsList = ({
const newPersonaName = `New Persona ${newPersonaNextNumber || 1}`;
identities.push({
id: `${identities.length}`,
flgType: 'persona',
type: t('label.persona', 'Persona'),
identityId: createListrequestId,
fromAddress: identities[0]?.fromAddress,
Expand Down Expand Up @@ -249,7 +252,7 @@ const AccountsList = ({
onClick={(): void => onDelete()}
color="error"
type="outlined"
disabled={identities[selectedIdentityId]?.type === t('label.primary', 'Primary')}
disabled={identities[selectedIdentityId]?.flgType === 'primary'}
/>
</Row>
<Padding bottom="large" />
Expand Down
Loading

0 comments on commit ce36e6d

Please sign in to comment.