Skip to content

Commit

Permalink
chore: fully working UI
Browse files Browse the repository at this point in the history
  • Loading branch information
prince-deriv committed Jul 3, 2024
1 parent 4eeb0ce commit 770fbb2
Show file tree
Hide file tree
Showing 12 changed files with 254 additions and 143 deletions.
8 changes: 4 additions & 4 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@
"@binary-com/binary-document-uploader": "^2.4.4",
"@binary-com/binary-style": "^0.2.26",
"@binary-com/webtrader-charts": "^0.6.1",
"@deriv-com/quill-ui": "^1.11.7",
"@deriv-com/quill-ui": "^1.12.2",
"@livechat/customer-sdk": "4.0.2",
"canvas-toBlob": "1.0.0",
"classnames": "2.2.5",
Expand Down
4 changes: 2 additions & 2 deletions src/javascript/app/pages/trade/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const Symbols = require('./symbols');
const Tick = require('./tick');
const contractsElement = require('./contracts.jsx');
const marketsElement = require('./markets.jsx');
const MarketDropdownElement = require('./markets/markets-dropdown.jsx');
const MarketSelectorElement = require('./markets/market-selector.jsx');
const TabsElement = require('../bottom/tabs.jsx');
const formatMoney = require('../../common/currency').formatMoney;
const ActiveSymbols = require('../../common/active_symbols');
Expand Down Expand Up @@ -63,7 +63,7 @@ const commonTrading = (() => {

// All other Quill refactored components
TabsElement.init();
MarketDropdownElement.init();
MarketSelectorElement.init();
};

/*
Expand Down
4 changes: 2 additions & 2 deletions src/javascript/app/pages/trade/contract.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ const Contract = (() => {
}
barrier = form_barrier.barrier_category;

contracts.available.forEach((current_obj) => {
contracts?.available.forEach((current_obj) => {
const contract_category = current_obj.contract_category;
// for callput and callputequals, populate duration for both
if (form === contract_category || (/callput/.test(form) && /callput/.test(contract_category))) {
Expand Down Expand Up @@ -202,7 +202,7 @@ const Contract = (() => {
const getContractCategories = () => {
const contracts = Contract.contracts().contracts_for;
const contract_categories = {};
contracts.available.forEach((current_obj) => {
contracts?.available?.forEach((current_obj) => {
const contract_category = current_obj.contract_category;
const contract_barrier = current_obj.barrier_category;
const contract_display = current_obj.contract_category_display;
Expand Down
132 changes: 49 additions & 83 deletions src/javascript/app/pages/trade/contracts.jsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import React from 'react';
import ReactDOM from 'react-dom';
import PropTypes from 'prop-types';
import Defaults from './defaults';
import { Divider, DropdownItem, DropdownTitle } from '@deriv-com/quill-ui';
import Defaults, { PARAM_NAMES } from './defaults';
import { getElementById } from '../../../_common/common_functions';
import { localize } from '../../../_common/localize';
import { triggerContractChange } from '../../hooks/events';

class Contracts extends React.Component {
export class Contracts extends React.Component {
constructor (props) {
super(props);
const { contracts, contracts_tree, selected } = props;
Expand Down Expand Up @@ -38,31 +38,6 @@ class Contracts extends React.Component {
}
};

openDropDown = () => {
if (this.state.contracts_tree.length < 1) return;
this.positionDropDown();
this.setState({ open: true });
};

closeDropDown = () => {
this.setState({ open: false });
const el_dropdown = this.references.wrapper;
// reposition dropdown after the animation is finished.
setTimeout(() => el_dropdown.removeAttribute('style'), 500);
};

positionDropDown = () => {
const el_dropdown = this.references.wrapper;
const pos = el_dropdown.getBoundingClientRect();

if ((pos.x + pos.width + 10) > window.innerWidth) {
// 10 is padding right for the element
el_dropdown.style.left = `${window.innerWidth - (pos.x + pos.width + 10)}px`;
} else if ((pos.x + pos.width + 10) !== window.innerWidth) {
el_dropdown.removeAttribute('style');
}
};

saveRef = (name, node) => { this.references[name] = node; };

getCurrentType = () => {
Expand Down Expand Up @@ -93,14 +68,17 @@ class Contracts extends React.Component {
};

onContractClick = (formname) => {
this.closeDropDown();
if (formname === this.state.formname) { return; }
// Notify for changes on contract.
this.el_contract.value = formname;
const event = new Event('change');
this.el_contract.dispatchEvent(event);

this.setState({ formname });
};

saveSelectedContract = (formname) => {
Defaults.set(PARAM_NAMES.CONTRACT_NAME,this.state.contracts[formname]);

triggerContractChange();
};
Expand All @@ -110,65 +88,53 @@ class Contracts extends React.Component {
const {
contracts,
contracts_tree,
open,
formname,
} = this.state;
const is_mobile = window.innerWidth <= 767;

return (
<div className='contracts'>
<div
className={`contract_current ${contracts_tree.length < 1 ? 'disabled' : ''}`}
onClick={this.openDropDown}
>
<span className='type'>
{this.getCurrentType()}
<span className={`arrow_down ${contracts_tree.length < 1 ? 'invisible' : ''}`} />
</span>
<span className='contract'>{this.getCurrentContract()}</span>
</div>
<div
className={`contracts_dropdown ${open ? '' : 'hidden'}`}
ref={this.saveRef.bind(null, 'wrapper')}
>
<div className={`mobile_close invisible ${open && is_mobile ? '' : 'disabled'}`}>
<span>{localize('Select Trade Type')}</span>
<span className='close' onClick={this.closeDropDown} />
</div>
<div className='list'>
{ contracts_tree.map((contract, idx) => {
if (typeof contract === 'object') {
return (
<div className='contract' key={idx}>
<div className='contract_type'>{contracts[contract[0]]}</div>
<div className='contract_subtypes'>
{contract[1].map((subtype, i) =>
<div
className={`sub ${subtype === formname ? 'active' : ''}`}
key={i}
onClick={this.onContractClick.bind(null, subtype)}
>
{contracts[subtype]}
</div>
)}
</div>
</div>
);
}
<div className='quill-market-dropdown-container'>
<div className='quill-market-dropdown-item-container'>
{ contracts_tree.map((contract, idx) => {
if (typeof contract === 'object') {
return (
<div className='contract' key={idx}>
<div className='contract_type'>{contracts[contract]}</div>
<div className='contract_subtypes'>
<div
className={`sub ${contract === formname ? 'active' : ''}`}
onClick={this.onContractClick.bind(null, contract)}
>
{contracts[contract]}
</div>
</div>
</div>
<React.Fragment key={idx}>
<DropdownTitle label={contracts[contract[0]]} />
{contract[1].map((subtype, i) => {
if (subtype === formname){
this.saveSelectedContract(formname);
}

return (
<DropdownItem
key={i}
onClick={this.onContractClick.bind(null, subtype)}
label={contracts[subtype]}
selected={subtype === formname}
size='sm'
/>);
})}
<Divider />
</React.Fragment>
);
})}
</div>
}

if (contract === formname){
this.saveSelectedContract(formname);
}

return (
<React.Fragment key={idx}>
<DropdownTitle label={contracts[contract]} />
<DropdownItem
onClick={this.onContractClick.bind(null, contract)}
label={contracts[contract]}
selected={contract === formname}
size='sm'
/>
<Divider />
</React.Fragment>
);
})}
</div>
</div>
);
Expand Down
2 changes: 2 additions & 0 deletions src/javascript/app/pages/trade/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ const Defaults = (() => {
SELECTED_TICK : 'selected_tick',
TIME_START : 'time_start',
UNDERLYING : 'underlying',
MARKET_NAME : 'market_name',
CONTRACT_NAME : 'contract_name',
};
const getDefault = (key) => {
const p_value = params[key] || Url.param(key);
Expand Down
85 changes: 85 additions & 0 deletions src/javascript/app/pages/trade/markets/market-selector.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import React, { useEffect, useRef, useState } from 'react';
import ReactDOM from 'react-dom';
import { CustomDropdown } from '@deriv-com/quill-ui';
import { MarketsDropdown, getMarketName } from './markets-dropdown.jsx';
import { getElementById } from '../../../../_common/common_functions';
import { localize } from '../../../../_common/localize';
import { useContractChange, useMarketChange } from '../../../hooks/events.js';
import Defaults, { PARAM_NAMES } from '../defaults.js';

const MarketSelector = () => {
const [marketLabel, setMarketLabel] = useState(getMarketName());
const [isMarketDropdownOpen, setMarketDropdownOpen] = useState(false);

Check failure on line 12 in src/javascript/app/pages/trade/markets/market-selector.jsx

View workflow job for this annotation

GitHub Actions / build_and_deploy_preview_link

'isMarketDropdownOpen' is assigned a value but never used
const [isContractDropdownOpen, setIsContractDropdownOpen] = useState(false);
const [tradeTypeLabel, setTradeTypeLabel] = useState('');
const hasContractChange = useContractChange();
const hasMarketChange = useMarketChange();

const contractDropdownRef = useRef(null);

const handleOutsideClick = (event) => {
if (
contractDropdownRef.current &&
!contractDropdownRef.current.contains(event.target)
) {
setIsContractDropdownOpen(false);
}
};

useEffect(() => {
document.addEventListener('mousedown', handleOutsideClick);
document.addEventListener('touchstart', handleOutsideClick);

return () => {
document.removeEventListener('mousedown', handleOutsideClick);
document.removeEventListener('touchstart', handleOutsideClick);
};
}, []);

useEffect(() => {
const contractName = decodeURIComponent(
Defaults.get(PARAM_NAMES.CONTRACT_NAME)
);
setTradeTypeLabel(contractName);
setIsContractDropdownOpen(false);
}, [hasContractChange]);

useEffect(() => {
setMarketLabel(getMarketName());
setMarketDropdownOpen(false);
}, [hasMarketChange]);

return (
<div className='quill-market-selector-container'>
<CustomDropdown
containerClassName='quill-market-selector-dropdown'
label={localize('Market')}
value={marketLabel}
>
<MarketsDropdown />
</CustomDropdown>
<div ref={contractDropdownRef} className='quill-market-selector-dropdown'>
<CustomDropdown
label={localize('Trade types')}
value={tradeTypeLabel}
onClickDropdown={() => setIsContractDropdownOpen((e) => !e)}
/>
<div
id='contract_component'
className={`contract-dropdown ${
isContractDropdownOpen && 'contract-dropdown-open'
}`}
/>
</div>
</div>
);
};

export const init = () => {
ReactDOM.render(
<MarketSelector />,
getElementById('markets-dropdown-container')
);
};

export default init;
Loading

0 comments on commit 770fbb2

Please sign in to comment.