Skip to content

Commit

Permalink
Creating SignatureRequestNavigation component and extracting the UI r…
Browse files Browse the repository at this point in the history
…endering part into a single component
  • Loading branch information
VSaric committed Jan 19, 2023
1 parent 0897ecd commit d929dde
Show file tree
Hide file tree
Showing 11 changed files with 161 additions and 105 deletions.
3 changes: 2 additions & 1 deletion ui/components/app/app-components.scss
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,5 @@
@import 'detected-token/detected-token-selection-popover/index';
@import 'network-account-balance-header/index';
@import 'approve-content-card/index';
@import 'transaction-alerts/transaction-alerts'
@import 'transaction-alerts/transaction-alerts';
@import 'navigation/index';
Original file line number Diff line number Diff line change
@@ -1,122 +1,44 @@
import React, { useContext } from 'react';
import React from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { useHistory, useParams } from 'react-router-dom';
import { useHistory } from 'react-router-dom';
import {
getCurrentChainId,
getUnapprovedTransactions,
unconfirmedMessagesHashSelector,
} from '../../../../selectors';
import { transactionMatchesNetwork } from '../../../../../shared/modules/transaction.utils';
import { I18nContext } from '../../../../contexts/i18n';
import {
CONFIRM_TRANSACTION_ROUTE,
SIGNATURE_REQUEST_PATH,
} from '../../../../helpers/constants/routes';
import { CONFIRM_TRANSACTION_ROUTE } from '../../../../helpers/constants/routes';
import { clearConfirmTransaction } from '../../../../ducks/confirm-transaction/confirm-transaction.duck';
import { hexToDecimal } from '../../../../../shared/lib/metamask-controller-utils';
import Navigation from '../../navigation';

const ConfirmPageContainerNavigation = () => {
const t = useContext(I18nContext);
const dispatch = useDispatch();
const history = useHistory();
const { id } = useParams();

const unapprovedTxs = useSelector(getUnapprovedTransactions);
const unconfirmedMessages = useSelector(unconfirmedMessagesHashSelector);
const currentChainId = useSelector(getCurrentChainId);
const network = hexToDecimal(currentChainId);
const isUnapprovedTxsEmpty = Object.keys(unapprovedTxs).length === 0;

const currentNetworkUnapprovedTxs = Object.keys(unapprovedTxs)
.filter((key) =>
transactionMatchesNetwork(unapprovedTxs[key], currentChainId, network),
)
.reduce((acc, key) => ({ ...acc, [key]: unapprovedTxs[key] }), {});

const enumUnapprovedTxs = Object.keys(
isUnapprovedTxsEmpty ? unconfirmedMessages : currentNetworkUnapprovedTxs,
);

const currentPosition = enumUnapprovedTxs.indexOf(id);

const totalTx = enumUnapprovedTxs.length;
const positionOfCurrentTx = currentPosition + 1;
const nextTxId = enumUnapprovedTxs[currentPosition + 1];
const prevTxId = enumUnapprovedTxs[currentPosition - 1];
const showNavigation = enumUnapprovedTxs.length > 1;
const firstTx = enumUnapprovedTxs[0];
const lastTx = enumUnapprovedTxs[enumUnapprovedTxs.length - 1];
const enumUnapprovedTxs = Object.keys(currentNetworkUnapprovedTxs);

const onNextTx = (txId) => {
if (txId) {
dispatch(clearConfirmTransaction());
history.push(
isUnapprovedTxsEmpty
? `${CONFIRM_TRANSACTION_ROUTE}/${txId}${SIGNATURE_REQUEST_PATH}`
: `${CONFIRM_TRANSACTION_ROUTE}/${txId}`,
);
history.push(`${CONFIRM_TRANSACTION_ROUTE}/${txId}`);
}
};

return (
<div
className="confirm-page-container-navigation"
style={{
display: showNavigation ? 'flex' : 'none',
}}
>
<div
className="confirm-page-container-navigation__container"
data-testid="navigation-container"
style={{
visibility: prevTxId ? 'initial' : 'hidden',
}}
>
<button
className="confirm-page-container-navigation__arrow"
data-testid="first-page"
onClick={() => onNextTx(firstTx)}
>
<i className="fa fa-angle-double-left fa-2x" />
</button>
<button
className="confirm-page-container-navigation__arrow"
data-testid="previous-page"
onClick={() => onNextTx(prevTxId)}
>
<i className="fa fa-angle-left fa-2x" />
</button>
</div>
<div className="confirm-page-container-navigation__textcontainer">
<div className="confirm-page-container-navigation__navtext">
{positionOfCurrentTx} {t('ofTextNofM')} {totalTx}
</div>
<div className="confirm-page-container-navigation__longtext">
{t('requestsAwaitingAcknowledgement')}
</div>
</div>
<div
className="confirm-page-container-navigation__container"
style={{
visibility: nextTxId ? 'initial' : 'hidden',
}}
>
<button
className="confirm-page-container-navigation__arrow"
data-testid="next-page"
onClick={() => onNextTx(nextTxId)}
>
<i className="fa fa-angle-right fa-2x" />
</button>
<button
className="confirm-page-container-navigation__arrow"
data-testid="last-page"
onClick={() => onNextTx(lastTx)}
>
<i className="fa fa-angle-double-right fa-2x" />
</button>
</div>
</div>
<Navigation
enumUnapprovedTxsOrUnconfirmedMessages={enumUnapprovedTxs}
onNext={onNextTx}
/>
);
};

Expand Down
1 change: 0 additions & 1 deletion ui/components/app/confirm-page-container/index.scss
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
@import 'confirm-page-container-content/index';
@import 'confirm-page-container-header/index';
@import 'confirm-detail-row/index';
@import 'confirm-page-container-navigation/index';
///: BEGIN:ONLY_INCLUDE_IN(flask)
@import 'flask/index';
///: END:ONLY_INCLUDE_IN
Expand Down
1 change: 1 addition & 0 deletions ui/components/app/navigation/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from './navigation';
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.confirm-page-container-navigation {
.navigation {
display: flex;
justify-content: space-between;
font: inherit;
Expand Down
96 changes: 96 additions & 0 deletions ui/components/app/navigation/navigation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import React, { useContext } from 'react';
import { useParams } from 'react-router-dom';
import PropTypes from 'prop-types';
import { I18nContext } from '../../../contexts/i18n';

export default function Navigation({
enumUnapprovedTxsOrUnconfirmedMessages,
onNext,
}) {
const t = useContext(I18nContext);
const { id } = useParams();

const currentPosition = enumUnapprovedTxsOrUnconfirmedMessages.indexOf(id);
const totalTx = enumUnapprovedTxsOrUnconfirmedMessages.length;
const positionOfCurrentTx = currentPosition + 1;
const nextTxId = enumUnapprovedTxsOrUnconfirmedMessages[currentPosition + 1];
const prevTxId = enumUnapprovedTxsOrUnconfirmedMessages[currentPosition - 1];
const showNavigation = enumUnapprovedTxsOrUnconfirmedMessages.length > 1;
const firstTx = enumUnapprovedTxsOrUnconfirmedMessages[0];
const lastTx =
enumUnapprovedTxsOrUnconfirmedMessages[
enumUnapprovedTxsOrUnconfirmedMessages.length - 1
];

return (
<div
className="navigation"
style={{
display: showNavigation ? 'flex' : 'none',
}}
>
<div
className="navigation__container"
data-testid="navigation-container"
style={{
visibility: prevTxId ? 'initial' : 'hidden',
}}
>
<button
className="navigation__arrow"
data-testid="first-page"
onClick={() => onNext(firstTx)}
>
<i className="fa fa-angle-double-left fa-2x" />
</button>
<button
className="navigation__arrow"
data-testid="previous-page"
onClick={() => onNext(prevTxId)}
>
<i className="fa fa-angle-left fa-2x" />
</button>
</div>
<div className="navigation__textcontainer">
<div className="navigation__navtext">
{positionOfCurrentTx} {t('ofTextNofM')} {totalTx}
</div>
<div className="navigation__longtext">
{t('requestsAwaitingAcknowledgement')}
</div>
</div>
<div
className="navigation__container"
style={{
visibility: nextTxId ? 'initial' : 'hidden',
}}
>
<button
className="navigation__arrow"
data-testid="next-page"
onClick={() => onNext(nextTxId)}
>
<i className="fa fa-angle-right fa-2x" />
</button>
<button
className="navigation__arrow"
data-testid="last-page"
onClick={() => onNext(lastTx)}
>
<i className="fa fa-angle-double-right fa-2x" />
</button>
</div>
</div>
);
}

Navigation.propTypes = {
/**
* Returns the names of the enumerable string properties and methods of an object.
*/
enumUnapprovedTxsOrUnconfirmedMessages: PropTypes.array.isRequired,
/**
* The onNext handler to be passed to the Navigation component.
*/
onNext: PropTypes.func.isRequired,
};
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,24 @@ exports[`SignatureRequestOriginal should match snapshot 1`] = `
class="request-signature__navigation"
>
<div
class="confirm-page-container-navigation"
class="navigation"
style="display: none;"
>
<div
class="confirm-page-container-navigation__container"
class="navigation__container"
data-testid="navigation-container"
style="visibility: hidden;"
>
<button
class="confirm-page-container-navigation__arrow"
class="navigation__arrow"
data-testid="first-page"
>
<i
class="fa fa-angle-double-left fa-2x"
/>
</button>
<button
class="confirm-page-container-navigation__arrow"
class="navigation__arrow"
data-testid="previous-page"
>
<i
Expand All @@ -35,10 +35,10 @@ exports[`SignatureRequestOriginal should match snapshot 1`] = `
</button>
</div>
<div
class="confirm-page-container-navigation__textcontainer"
class="navigation__textcontainer"
>
<div
class="confirm-page-container-navigation__navtext"
class="navigation__navtext"
>
0
Expand All @@ -47,25 +47,25 @@ exports[`SignatureRequestOriginal should match snapshot 1`] = `
0
</div>
<div
class="confirm-page-container-navigation__longtext"
class="navigation__longtext"
>
requests waiting to be acknowledged
</div>
</div>
<div
class="confirm-page-container-navigation__container"
class="navigation__container"
style="visibility: hidden;"
>
<button
class="confirm-page-container-navigation__arrow"
class="navigation__arrow"
data-testid="next-page"
>
<i
class="fa fa-angle-right fa-2x"
/>
</button>
<button
class="confirm-page-container-navigation__arrow"
class="navigation__arrow"
data-testid="last-page"
>
<i
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
TEXT_ALIGN,
} from '../../../helpers/constants/design-system';
import { NETWORK_TYPES } from '../../../../shared/constants/network';
import { ConfirmPageContainerNavigation } from '../confirm-page-container';
import SignatureRequestNavigation from '../signature-request/signature-request-navigation';
import SignatureRequestOriginalWarning from './signature-request-original-warning';

export default class SignatureRequestOriginal extends Component {
Expand Down Expand Up @@ -297,7 +297,7 @@ export default class SignatureRequestOriginal extends Component {
return (
<div className="request-signature__container">
<div className="request-signature__navigation">
<ConfirmPageContainerNavigation />
<SignatureRequestNavigation />
</div>
<div className="request-signature__account">
<NetworkAccountBalanceHeader
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from './signature-request-navigation';
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import React from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { useHistory } from 'react-router-dom';
import { unconfirmedMessagesHashSelector } from '../../../../selectors';
import {
CONFIRM_TRANSACTION_ROUTE,
SIGNATURE_REQUEST_PATH,
} from '../../../../helpers/constants/routes';
import { clearConfirmTransaction } from '../../../../ducks/confirm-transaction/confirm-transaction.duck';
import Navigation from '../../navigation';

const SignatureRequestNavigation = () => {
const dispatch = useDispatch();
const history = useHistory();

const unconfirmedMessages = useSelector(unconfirmedMessagesHashSelector);
const enumUnconfirmedMessages = Object.keys(unconfirmedMessages);

const onNextMessage = (txId) => {
if (txId) {
dispatch(clearConfirmTransaction());
history.push(
`${CONFIRM_TRANSACTION_ROUTE}/${txId}${SIGNATURE_REQUEST_PATH}`,
);
}
};

return (
<Navigation
enumUnapprovedTxsOrUnconfirmedMessages={enumUnconfirmedMessages}
onNext={onNextMessage}
/>
);
};

export default SignatureRequestNavigation;
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
} from '../../../helpers/constants/design-system';
import NetworkAccountBalanceHeader from '../network-account-balance-header';
import { NETWORK_TYPES } from '../../../../shared/constants/network';
import { ConfirmPageContainerNavigation } from '../confirm-page-container';
import SignatureRequestNavigation from './signature-request-navigation';
import Footer from './signature-request-footer';
import Message from './signature-request-message';

Expand Down Expand Up @@ -215,7 +215,7 @@ export default class SignatureRequest extends PureComponent {

return (
<div className="signature-request">
<ConfirmPageContainerNavigation />
<SignatureRequestNavigation />
<div className="request-signature__account">
<NetworkAccountBalanceHeader
networkName={currentNetwork}
Expand Down

0 comments on commit d929dde

Please sign in to comment.