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

Jl/editing flow 2 #27176

Closed
wants to merge 2 commits into from
Closed
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
2 changes: 0 additions & 2 deletions test/data/mock-send-state.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@
}
},
"showIpfsModalOpen": false,
"selectedAccountsForDappConnection": {},
"selectedNetworksForDappConnection": {},
"showKeyringRemovalSnapModal": false,
"showWhatsNewPopup": false,
"warning": null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ export default class PermissionPageContainer extends Component {

onLeftFooterClick = () => {
const requestedPermissions = this.getRequestedPermissions();
// figure this out
if (requestedPermissions[PermissionNames.permittedChains] === undefined) {
this.goBack();
} else {
Expand Down Expand Up @@ -185,6 +186,7 @@ export default class PermissionPageContainer extends Component {
this.props.setSnapsInstallPrivacyWarningShownStatus(true);
};

// figure this out
const footerLeftActionText = requestedPermissions[
PermissionNames.permittedChains
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,6 @@ import { AccountListItem } from '..';
import { MergedInternalAccount } from '../../../selectors/selectors.types';
import { mergeAccounts } from '../account-list-menu/account-list-menu';

import {
addMorePermittedAccounts,
removePermittedAccount,
setSelectedAccountsForDappConnection,
} from '../../../store/actions';
import {
JustifyContent,
Display,
Expand All @@ -57,24 +52,25 @@ type EditAccountsModalProps = {
onClick: () => void;
onDisconnectClick: () => void;
allowedAccountTypes?: KeyringAccountType[];
approvedAccounts: string[];
activeTabOrigin: string;
currentTabHasNoAccounts: boolean;
selectedAccounts: string[];
setSelectedAccounts: (acccounts: string[]) => void;
};

export const EditAccountsModal: React.FC<EditAccountsModalProps> = ({
onClose,
onClick,
onDisconnectClick,
allowedAccountTypes = defaultAllowedAccountTypes,
approvedAccounts,
activeTabOrigin,
currentTabHasNoAccounts,
selectedAccounts,
setSelectedAccounts,
}) => {
const t = useI18nContext();
const accounts = useSelector(getUpdatedAndSortedAccounts);
const internalAccounts = useSelector(getInternalAccounts);
const dispatch = useDispatch();
const hostName = getURLHost(activeTabOrigin);
const [showAddNewAccountsModal, setShowAddNewAccountsModal] = useState(false);

Expand All @@ -90,44 +86,12 @@ export const EditAccountsModal: React.FC<EditAccountsModalProps> = ({
),
);

const connectedAccountsAddresses = connectedAccounts.map(
(account: InternalAccount) => account.address,
);

const defaultAccountsAddresses =
connectedAccountsAddresses.length > 0
? connectedAccountsAddresses
: approvedAccounts;

const [selectedAccounts, setSelectedAccounts] = useState<string[]>(
defaultAccountsAddresses,
);

const handleAccountClick = (address: string) => {
setSelectedAccounts((prevSelectedAccounts) =>
prevSelectedAccounts.includes(address)
? prevSelectedAccounts.filter((acc) => acc !== address)
: [...prevSelectedAccounts, address],
);
};

const managePermittedAccounts = (
selectedAcc: string[],
accountsAddresses: string[],
) => {
const removedAccounts = accountsAddresses.filter(
(acc) => !selectedAcc.includes(acc),
);
removedAccounts.forEach((account) => {
dispatch(removePermittedAccount(activeTabOrigin, account));
});

const newAccounts = selectedAcc.filter(
(acc) => !accountsAddresses.includes(acc),
setSelectedAccounts(
selectedAccounts.includes(address)
? selectedAccounts.filter((acc) => acc !== address)
: [...selectedAccounts, address],
);
if (newAccounts.length > 0) {
dispatch(addMorePermittedAccounts(activeTabOrigin, newAccounts));
}
};

const selectAll = () => {
Expand Down Expand Up @@ -241,14 +205,15 @@ export const EditAccountsModal: React.FC<EditAccountsModalProps> = ({
onClick={() => {
onClick();
if (currentTabHasNoAccounts) {
dispatch(
setSelectedAccountsForDappConnection(selectedAccounts),
);
// this is being communicated via setSelectedAccounts
// dispatch(
// setSelectedAccountsForDappConnection(selectedAccounts),
// );
} else {
managePermittedAccounts(
selectedAccounts,
connectedAccountsAddresses,
);
// managePermittedAccounts(
// selectedAccounts,
// connectedAccountsAddresses,
// );
}
onClose();
}}
Expand Down
72 changes: 30 additions & 42 deletions ui/components/multichain/edit-networks-modal/edit-networks-modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,15 @@ import { NetworkListItem } from '..';
import {
grantPermittedChains,
removePermittedChain,
setSelectedNetworksForDappConnection,
} from '../../../store/actions';
import { getURLHost } from '../../../helpers/utils/util';

export const EditNetworksModal = ({
onClose,
onClick,
currentTabHasNoAccounts,
selectedChainIds,
setSelectedChainIds,
combinedNetworks,
onDisconnectClick,
}) => {
Expand All @@ -58,57 +59,38 @@ export const EditNetworksModal = ({
);
const selectedPermittedChains =
connectedNetworks.length > 0 ? connectedNetworks : combinedNetworksIds;
const [selectedChains, setSelectedChains] = useState(selectedPermittedChains);

const selectAll = () => {
const newSelectedAccounts = combinedNetworks.map(
(network) => network.chainId,
);
setSelectedChains(newSelectedAccounts);
const chainIds = combinedNetworks.map((network) => network.chainId);
setSelectedChainIds(chainIds);
};

const deselectAll = () => {
setSelectedChains([]);
setSelectedChainIds([]);
};

const handleAccountClick = (chainId) => {
if (selectedChains.includes(chainId)) {
// Remove the chainId from the selectedChains
setSelectedChains(selectedChains.filter((id) => id !== chainId));
const handleNetworkClick = (chainId) => {
if (selectedChainIds.includes(chainId)) {
setSelectedChainIds(selectedChainIds.filter((id) => id !== chainId));
} else {
// Add the chainId to selectedChains
setSelectedChains([...selectedChains, chainId]);
setSelectedChainIds([...selectedChainIds, chainId]);
}
};

const allAreSelected = () => {
return combinedNetworksIds.length === selectedChains.length;
return combinedNetworksIds.length === selectedChainIds.length;
};

let checked = false;
let isIndeterminate = false;
if (allAreSelected()) {
checked = true;
isIndeterminate = false;
} else if (selectedChains.length > 0 && !allAreSelected()) {
} else if (selectedChainIds.length > 0 && !allAreSelected()) {
checked = false;
isIndeterminate = true;
}

const managePermittedChains = (chains, permittedChains, tabOrigin) => {
dispatch(grantPermittedChains(activeTabOrigin, chains));

const removedElements = permittedChains.filter(
(chain) => !chains.includes(chain),
);

// Dispatch removePermittedChains for each removed element
removedElements.forEach((chain) => {
const selectedChain = [chain];
dispatch(removePermittedChain(tabOrigin, selectedChain));
});
};

const hostName = getURLHost(activeTabOrigin);

return (
Expand Down Expand Up @@ -144,11 +126,11 @@ export const EditNetworksModal = ({
iconSrc={network?.rpcPrefs?.imageUrl}
key={network.id}
onClick={() => {
handleAccountClick(network.chainId);
handleNetworkClick(network.chainId);
}}
startAccessory={
<Checkbox
isChecked={selectedChains.includes(network.chainId)}
isChecked={selectedChainIds.includes(network.chainId)}
/>
}
/>
Expand All @@ -162,18 +144,18 @@ export const EditNetworksModal = ({
iconSrc={network?.rpcPrefs?.imageUrl}
key={network.id}
onClick={() => {
handleAccountClick(network.chainId);
handleNetworkClick(network.chainId);
}}
startAccessory={
<Checkbox
isChecked={selectedChains.includes(network.chainId)}
isChecked={selectedChainIds.includes(network.chainId)}
/>
}
showEndAccessory={false}
/>
))}
<ModalFooter>
{selectedChains.length === 0 ? (
{selectedChainIds.length === 0 ? (
<Box
display={Display.Flex}
flexDirection={FlexDirection.Column}
Expand Down Expand Up @@ -216,15 +198,17 @@ export const EditNetworksModal = ({
onClick();
onClose();
if (currentTabHasNoAccounts) {
dispatch(
setSelectedNetworksForDappConnection(selectedChains),
);
// dispatch(
// // TODO set this via hook
// setSelectedNetworksForDappConnection(selectedChains),
// );
} else {
managePermittedChains(
selectedChains,
selectedPermittedChains,
activeTabOrigin,
);
// fix this
// managePermittedChains(
// selectedChainIds,
// selectedPermittedChains,
// activeTabOrigin,
// );
}
}}
size={ButtonPrimarySize.Lg}
Expand All @@ -251,6 +235,10 @@ EditNetworksModal.propTypes = {
*/
onClick: PropTypes.func.isRequired,

selectedChainIds: PropTypes.arrayOf(PropTypes.string).isRequired,

setSelectedChainIds: PropTypes.func.isRequired,

/**
* Boolean indicating if the current tab has no associated accounts.
*/
Expand Down
Loading
Loading