import React from 'react';
import { c } from 'ttag';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import { Dropdown, DropdownMenu, useApi, useModal } from 'react-components';
import { ADDRESS_TYPE, ADDRESS_STATUS } from 'proton-shared/lib/constants';
import { createNotification } from 'proton-shared/lib/state/notifications/actions';
import { deleteAddress, enableAddress, disableAddress } from 'proton-shared/lib/api/addresses';

import EditAddressModal from './EditAddressModal';

const { TYPE_ORIGINAL, TYPE_CUSTOM_DOMAIN, TYPE_PREMIUM } = ADDRESS_TYPE;
const { STATUS_DISABLED, STATUS_ENABLED } = ADDRESS_STATUS;

const AddressActions = ({ address, user, createNotification }) => {
    const { request: requestDelete } = useApi(() => deleteAddress(address.ID));
    const { request: requestEnable } = useApi(() => enableAddress(address.ID));
    const { request: requestDisable } = useApi(() => disableAddress(address.ID));
    const { isOpen, open, close } = useModal();
    const { Status, Type } = address;
    const canDelete = Type === TYPE_CUSTOM_DOMAIN;
    const canEnable =
        user.isAdmin && Status === STATUS_DISABLED && Type !== TYPE_ORIGINAL && address.Type !== TYPE_PREMIUM;
    const canDisable =
        user.isAdmin && Status === STATUS_ENABLED && Type !== TYPE_ORIGINAL && address.Type !== TYPE_PREMIUM;

    const handleDelete = async () => {
        await requestDelete();
        // TODO call event manager
        createNotification({ text: c('Success notification').t`Address deleted` });
    };

    const handleEnable = async () => {
        await requestEnable();
        // TODO call event manager
        createNotification({ text: c('Success notification').t`Address enabled` });
    };

    const handleDisable = async () => {
        await requestDisable();
        // TODO call event manager
        createNotification({ text: c('Success notification').t`Address disabled` });
    };

    const list = [
        {
            text: c('Address action').t`Edit`,
            type: 'button',
            onClick: open
        }
    ];

    if (canEnable) {
        list.push({
            text: c('Address action').t`Enable`,
            type: 'button',
            onClick: handleEnable
        });
    }

    if (canDisable) {
        list.push({
            text: c('Address action').t`Disable`,
            type: 'button',
            onClick: handleDisable
        });
    }

    if (canDelete) {
        list.push({
            text: c('Address action').t`Delete`,
            type: 'button',
            onClick: handleDelete
        });
    }

    return (
        <>
            <Dropdown className="pm-button pm-button--small" content={c('Action').t`Options`}>
                <DropdownMenu list={list} />
            </Dropdown>
            <EditAddressModal show={isOpen} onClose={close} address={address} />
        </>
    );
};

AddressActions.propTypes = {
    address: PropTypes.object.isRequired,
    user: PropTypes.object.isRequired,
    createNotification: PropTypes.func.isRequired
};

const mapStateToProps = ({ user: { data } }) => ({ user: data });
const mapDispatchToProps = { createNotification };

export default connect(
    mapStateToProps,
    mapDispatchToProps
)(AddressActions);

@@ -160,16 +160,29 @@
 , useApi
+WithoutResult
 , useMod
@@ -207,24 +207,24 @@
 omponents';%0A
-
 import %7B ADD
@@ -719,24 +719,66 @@
 ion %7D) =%3E %7B%0A
+    const %7B Status, Type, ID %7D = address;%0A
     const %7B 
@@ -802,32 +802,45 @@
 elete %7D = useApi
+WithoutResult
 (() =%3E deleteAdd
@@ -840,32 +840,24 @@
 leteAddress(
-address.
 ID));%0A    co
@@ -891,24 +891,37 @@
 e %7D = useApi
+WithoutResult
 (() =%3E enabl
@@ -925,32 +925,24 @@
 ableAddress(
-address.
 ID));%0A    co
@@ -981,16 +981,29 @@
 = useApi
+WithoutResult
 (() =%3E d
@@ -1016,24 +1016,16 @@
 Address(
-address.
 ID));%0A  
@@ -1074,46 +1074,8 @@
 ();%0A
-    const %7B Status, Type %7D = address;%0A
     
@@ -1138,32 +1138,24 @@
  canEnable =
-%0A       
  user.isAdmi
@@ -1211,32 +1211,24 @@
 ORIGINAL && 
-address.
 Type !== TYP
@@ -1260,24 +1260,16 @@
 isable =
-%0A       
  user.is
@@ -1336,16 +1336,8 @@
  && 
-address.
 Type

Update AddressActions to use api without results