Skip to content

Commit 01edfb8

Browse files
module
1 parent 0c72861 commit 01edfb8

File tree

3 files changed

+166
-0
lines changed

3 files changed

+166
-0
lines changed
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
import React from 'react';
2+
import { View } from 'react-native';
3+
import { NavigationEvents } from 'react-navigation';
4+
import { connect } from 'react-redux';
5+
import { smartConnect } from '../../../../core/utils/smart-connect';
6+
import { IReduxState } from '../../../../redux/state';
7+
import { setScreenInputData } from '../../../../redux/ui/screens/input-data/actions';
8+
import { IScreenContext, IScreenModule, ISmartScreenActions, ITimerUpdateData } from '../../types';
9+
import { getStateSelectors } from '../ui-state-selectors';
10+
import { HttpClient } from '../../../../core/utils/http-client';
11+
12+
interface IExternalProps {
13+
module: IScreenModule;
14+
context: IScreenContext;
15+
actions: ISmartScreenActions;
16+
options?: {
17+
screenKey?: string;
18+
flowId?: string;
19+
};
20+
}
21+
22+
interface IReduxProps {
23+
setScreenInputData: typeof setScreenInputData;
24+
}
25+
26+
const mapStateToProps = (state: IReduxState, ownProps: IExternalProps) => {
27+
return getStateSelectors(state, ownProps.module, {
28+
flowId: ownProps?.options?.flowId,
29+
screenKey: ownProps?.options?.screenKey
30+
});
31+
};
32+
33+
const mapDispatchToProps = {
34+
setScreenInputData
35+
};
36+
37+
class TimerIntervalPriceUpdateModuleComponent extends React.Component<
38+
IReduxProps & IExternalProps
39+
> {
40+
private interval: any;
41+
private remainingTime: number;
42+
private httpClient: HttpClient;
43+
44+
public componentDidMount() {
45+
this.startTimer();
46+
}
47+
48+
// public componentDidUpdate(prevProps: IExternalProps & IReduxProps) {
49+
// const data = this.props.module?.data as ITimerUpdateData;
50+
51+
// const screenKey = this.props.options.screenKey;
52+
53+
// this.props.setScreenInputData(screenKey, {
54+
// [data.reduxKey]: 1
55+
// });
56+
// }
57+
58+
public componentWillUnmount() {
59+
this.interval && clearInterval(this.interval);
60+
}
61+
62+
private onFocus() {
63+
this.startTimer();
64+
}
65+
66+
private onWillBlur() {
67+
this.interval && clearInterval(this.interval);
68+
}
69+
70+
private async getData(data: ITimerUpdateData, endpointData: any) {
71+
try {
72+
let response;
73+
74+
switch (data.endpoint.method) {
75+
case 'POST':
76+
response = await this.httpClient.post('', endpointData);
77+
break;
78+
79+
case 'GET':
80+
response = await this.httpClient.get('');
81+
break;
82+
83+
default:
84+
break;
85+
}
86+
87+
if (response?.result?.data) {
88+
// response price
89+
}
90+
} catch (error) {
91+
// TODO: maybe do something here
92+
}
93+
}
94+
95+
private async startTimer() {
96+
const screenKey = this.props.options.screenKey;
97+
const data = this.props.module?.data as ITimerUpdateData;
98+
99+
const selector = this.props.module?.state?.selectors;
100+
101+
if (data.numberOfSeconds) {
102+
this.interval && clearInterval(this.interval);
103+
this.remainingTime = data.numberOfSeconds;
104+
this.interval = setInterval(async () => {
105+
if (this.remainingTime !== 0) {
106+
this.props.setScreenInputData(screenKey, {
107+
[data.reduxKey]: this.remainingTime
108+
});
109+
this.remainingTime--;
110+
} else {
111+
// fetch price;
112+
113+
const endpointData = data.endpoint.data;
114+
115+
Object.keys(data.endpoint.data).map(key => {
116+
if (selector.hasOwnProperty(key)) {
117+
endpointData[key] = this.props[key];
118+
}
119+
});
120+
121+
await this.getData(data, endpointData);
122+
}
123+
}, 1000); // interval is in seconds
124+
}
125+
}
126+
127+
public render() {
128+
return (
129+
<View>
130+
<NavigationEvents
131+
onWillFocus={() => this.onFocus()}
132+
onWillBlur={() => this.onWillBlur()}
133+
/>
134+
</View>
135+
);
136+
}
137+
}
138+
139+
export const TimerIntervalPriceUpdateModule = smartConnect<IExternalProps>(
140+
TimerIntervalPriceUpdateModuleComponent,
141+
[connect(mapStateToProps, mapDispatchToProps)]
142+
);

src/components/widgets/render-module.tsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import { TextLineIcon } from './components/text-line-icon/text-line-icon';
3939
import { PubSub } from '../../core/blockchain/common/pub-sub';
4040
import { PriceUpdateModule } from './components/price-update/price-update';
4141
import { AbsoluteModules } from './components/absolute-modules/absolute-modules';
42+
import { TimerIntervalPriceUpdateModule } from './components/timer-interval-price-update/timer-interval-price-update';
4243

4344
const renderModules = (
4445
modules: IScreenModule[],
@@ -292,6 +293,17 @@ export const renderModule = (
292293
);
293294
break;
294295

296+
case ModuleTypes.TIMER_INTERVAL_PRICE_UPDATE:
297+
moduleJSX = (
298+
<TimerIntervalPriceUpdateModule
299+
module={module}
300+
context={context}
301+
actions={actions}
302+
options={options}
303+
/>
304+
);
305+
break;
306+
295307
case ModuleTypes.ABSOLUTE_MODULES:
296308
moduleJSX = (
297309
<AbsoluteModules

src/components/widgets/types.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ export interface IScreenModule {
190190
| ModuleTypes.ICON_TWO_LINES
191191
| ModuleTypes.IMAGE_BANNER
192192
| ModuleTypes.INPUT
193+
| ModuleTypes.TIMER_INTERVAL_PRICE_UPDATE
193194
| ModuleTypes.MD_TEXT
194195
| ModuleTypes.MODULE_COLUMNS_WRAPPER
195196
| ModuleTypes.MODULE_SELECTABLE_WRAPPER
@@ -261,6 +262,7 @@ export enum ModuleTypes {
261262
ICON_TWO_LINES = 'icon-two-lines',
262263
IMAGE_BANNER = 'image-banner',
263264
INPUT = 'input',
265+
TIMER_INTERVAL_PRICE_UPDATE = 'timer-interval-price-update',
264266
MD_TEXT = 'md-text',
265267
MODULE_COLUMNS_WRAPPER = 'module-columns-wrapper',
266268
MODULE_SELECTABLE_WRAPPER = 'module-selectable-wrapper',
@@ -422,6 +424,16 @@ export interface IPriceUpdateData {
422424
interval: number; // miliseconds
423425
}
424426

427+
export interface ITimerUpdateData {
428+
numberOfSeconds: number;
429+
reduxKey: string;
430+
endpoint: {
431+
url: string;
432+
method: 'POST' | 'GET';
433+
data: any;
434+
};
435+
}
436+
425437
export interface IThreeLinesIconData {
426438
firstLine: IData[];
427439
secondLine: IData[];

0 commit comments

Comments
 (0)