Update the store after setting an authorized address

[MAILPOET-2681]
This commit is contained in:
Amine Ben hammou
2020-04-03 08:39:45 +02:00
committed by Veljko V
parent 9877ed3573
commit af4225bc87
2 changed files with 14 additions and 6 deletions

View File

@@ -7,7 +7,7 @@ import { GlobalContext } from 'context';
const mailPoetApiVersion = (window as any).mailpoet_api_version as string; const mailPoetApiVersion = (window as any).mailpoet_api_version as string;
const handleSave = async (address: string|null) => MailPoet.Ajax.post({ const handleSave = async (address: string | null) => MailPoet.Ajax.post({
api_version: mailPoetApiVersion, api_version: mailPoetApiVersion,
endpoint: 'settings', endpoint: 'settings',
action: 'setAuthorizedFromAddress', action: 'setAuthorizedFromAddress',
@@ -16,7 +16,7 @@ const handleSave = async (address: string|null) => MailPoet.Ajax.post({
}, },
}); });
const getErrorMessage = (error: any|null): string => { const getErrorMessage = (error: any | null): string => {
if (!error) { if (!error) {
return MailPoet.I18n.t('setFromAddressEmailUnknownError'); return MailPoet.I18n.t('setFromAddressEmailUnknownError');
} }
@@ -33,7 +33,7 @@ const getErrorMessage = (error: any|null): string => {
const getSuccessMessage = (): JSX.Element => ( const getSuccessMessage = (): JSX.Element => (
<p> <p>
{ ReactStringReplace( {ReactStringReplace(
MailPoet.I18n.t('setFromAddressEmailSuccess'), MailPoet.I18n.t('setFromAddressEmailSuccess'),
/\[link\](.*?)\[\/link\]/g, /\[link\](.*?)\[\/link\]/g,
(match) => ( (match) => (
@@ -66,9 +66,10 @@ const removeUnauthorizedEmailNotices = () => {
type Props = { type Props = {
onRequestClose: () => void, onRequestClose: () => void,
setAuthorizedAddress: (address: string) => any,
}; };
const SetFromAddressModal = ({ onRequestClose }: Props) => { const SetFromAddressModal = ({ onRequestClose, setAuthorizedAddress }: Props) => {
const [address, setAddress] = useState(null); const [address, setAddress] = useState(null);
const { notices } = React.useContext<any>(GlobalContext); const { notices } = React.useContext<any>(GlobalContext);
@@ -125,6 +126,7 @@ const SetFromAddressModal = ({ onRequestClose }: Props) => {
} }
try { try {
await handleSave(address); await handleSave(address);
setAuthorizedAddress(address);
onRequestClose(); onRequestClose();
removeUnauthorizedEmailNotices(); removeUnauthorizedEmailNotices();
notices.success(getSuccessMessage(), { timeout: false }); notices.success(getSuccessMessage(), { timeout: false });

View File

@@ -16,8 +16,13 @@ export default function KeyActivation() {
const verifyPremiumKey = useAction('verifyPremiumKey'); const verifyPremiumKey = useAction('verifyPremiumKey');
const installPremiumPlugin = useAction('installPremiumPlugin'); const installPremiumPlugin = useAction('installPremiumPlugin');
const activatePremiumPlugin = useAction('activatePremiumPlugin'); const activatePremiumPlugin = useAction('activatePremiumPlugin');
const [senderAddress] = useSetting('sender', 'address'); const [senderAddress, setSenderAddress] = useSetting('sender', 'address');
const [unauthorizedAddresses] = useSetting('authorized_emails_addresses_check'); const [unauthorizedAddresses, setUnauthorizedAddresses] = useSetting('authorized_emails_addresses_check');
const setAuthorizedAddress = async (address: string) => {
await setSenderAddress(address);
await setUnauthorizedAddresses(null);
};
const verifyKey = async (event) => { const verifyKey = async (event) => {
if (!state.key) { if (!state.key) {
@@ -94,6 +99,7 @@ export default function KeyActivation() {
{state.showFromAddressModal && ( {state.showFromAddressModal && (
<SetFromAddressModal <SetFromAddressModal
onRequestClose={() => setState({ showFromAddressModal: false })} onRequestClose={() => setState({ showFromAddressModal: false })}
setAuthorizedAddress={setAuthorizedAddress}
/> />
)} )}
</div> </div>