Skip to content

Commit

Permalink
Nuzhy/dataManager (#763)
Browse files Browse the repository at this point in the history
* fix: update tradeManager to dataManager

* fix: remove unused function

* fix: update purchaseManager to dataManager

* fix: update contractManager to dataManager

* fix: remove unused managers

* fix: pr comments addressed

* fix: refactor data-manager
  • Loading branch information
Nuzhy-Deriv authored Aug 7, 2024
1 parent 9b88e2d commit 073eafe
Show file tree
Hide file tree
Showing 32 changed files with 316 additions and 371 deletions.
59 changes: 0 additions & 59 deletions src/javascript/app/common/contract_manager.js

This file was deleted.

4 changes: 2 additions & 2 deletions src/javascript/app/common/currency.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const tradeManager = require('./trade_manager').default;
const dataManager = require('./data_manager').default;
const CurrencyBase = require('../../_common/base/currency_base');
const localize = require('../../_common/localize').localize;

Expand All @@ -23,7 +23,7 @@ const getCurrencyList = (currencies) => {
currency_list.fiat.push({ value: currency, text: currency_name });
}
});
tradeManager.set({
dataManager.setTrade({
currency_list,
});

Expand Down
165 changes: 165 additions & 0 deletions src/javascript/app/common/data_manager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
import {
triggerBarrierChange,
triggerContractChange,
triggerPurchaseChange,
triggerTimeChange,
triggerTradeChange,
} from '../hooks/events';

const changeTypeMap = {
trade : 'tradeChange',
purchase: 'purchaseChange',
contract: 'contractChange',
};

class DataManager {
constructor() {
this.data = {
trade : {},
purchase: {},
contract: {},
};
}

set(data, data_type, optional_trigger) {
if (typeof data === 'object') {
const oldValues = {};
const newValues = {};
Object.entries(data).forEach(([key, value]) => {
if (this.data[data_type][key] !== value) {
oldValues[key] = this.data[data_type][key];
newValues[key] = value;
}
this.data[data_type][key] = value;
});
if (Object.keys(newValues).length > 0) {
// Trigger a custom event with old and new values
window.dispatchEvent(new CustomEvent(changeTypeMap[data_type], {
detail: { oldValues, newValues },
}));

switch (data_type) {
case 'trade':
if (optional_trigger === 'barrier') {
triggerBarrierChange();
} else if (optional_trigger === 'time') {
triggerTimeChange();
} else {
triggerTradeChange();
}
break;

case 'purchase':
triggerPurchaseChange();
break;

case 'contract':
triggerContractChange();
break;

default:
break;
}

}
}
}

get(key, data_type) {
return this.data[data_type][key];
}

getAll(data_type) {
return { ...this.data[data_type] };
}

clear(data_type) {
const oldValues = { ...this.data[data_type] };
this.data[data_type] = {};

window.dispatchEvent(new CustomEvent(changeTypeMap[data_type], {
detail: { oldValues, newValues: {} },
}));
if (data_type === 'trade') {
triggerTradeChange();
}
if (data_type === 'purchase') {
triggerPurchaseChange();
}
if (data_type === 'contract') {
triggerContractChange();
}
}

has(key, data_type) {
return Object.prototype.hasOwnProperty.call(this.data[data_type], key);
}

// methods for trade
setTrade(data, optional_trigger) {
this.set(data, 'trade', optional_trigger);
}

getTrade(key) {
return this.get(key, 'trade');
}

getAllTrades() {
return this.getAll('trade');
}

clearTrades() {
this.clear('trade');
}

hasTrade(key) {
return this.has(key, 'trade');
}

// methods for purchase
setPurchase(data) {
this.set(data, 'purchase');
}

getPurchase(key) {
return this.get(key, 'purchase');
}

getAllPurchases() {
return this.getAll('purchase');
}

clearPurchases() {
this.clear('purchase');
}

hasPurchase(key) {
return this.has(key, 'purchase');
}

// methods for contract
setContract(data) {
this.set(data, 'contract');
}

getContract(key) {
return this.get(key, 'contract');
}

getAllContracts() {
return this.getAll('contract');
}

clearContracts() {
this.clear('contract');
}

hasContract(key) {
return this.has(key, 'contract');
}

}

const dataManager = new DataManager();

export default dataManager;
33 changes: 3 additions & 30 deletions src/javascript/app/common/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react';
import { Tooltip } from '@deriv-com/quill-ui';
import parse from 'html-react-parser';
import moment from 'moment';
import tradeManager from './trade_manager';
import dataManager from './data_manager';
import { getLocalTime } from '../base/clock';
import common_independent from '../pages/trade/common_independent';

Expand All @@ -24,32 +24,6 @@ const TimeTooltipWrapper = (element, time) => {
);
};

const handleNumeric = (event, regex_string) => {
let input_value = event.target.value;
const regex = new RegExp(regex_string) || /[^0-9.]/g;

input_value = input_value
.split('')
.filter((char, index, array) => {
const tempValue = array.slice(0, index + 1).join('');
return !regex.test(tempValue);
})
.join('');

if (input_value.match(/[+-]/g) && input_value.match(/[+-]/g).length > 1) {
input_value = input_value.replace(/[+-]/g, '');
}

const decimal_count = (input_value.match(/\./g) || []).length;
if (decimal_count > 1) {
input_value = input_value.replace(/\./g, (match, offset) =>
offset === input_value.indexOf('.') ? match : ''
);
}

return input_value;
};

const setMinMaxTime = (selector, check_end_time) => {
const $date_start = $('#date_start');
const $time_start = $('#time_start');
Expand Down Expand Up @@ -125,17 +99,16 @@ const setMinMaxTimeObj = (options) => {
}
}

tradeManager.set({
dataManager.setTrade({
starttime_obj,
expirytime_obj,
},'time');
}, 'time');
};

export {
parseData,
triggerClick,
TimeTooltipWrapper,
handleNumeric,
setMinMaxTime,
};

59 changes: 0 additions & 59 deletions src/javascript/app/common/purchase_manager.js

This file was deleted.

Loading

0 comments on commit 073eafe

Please sign in to comment.