|
| 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 | +); |
0 commit comments