New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor Fiat Plugin enterAmount
API
#4186
Changes from all commits
b419384
5dffdc5
2c84fda
a797cfc
b3502fb
7e8a718
94a4603
3c62d8f
3bf0280
0e803e0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,8 +5,9 @@ import { DisablePluginMap } from '../../actions/ExchangeInfoActions' | |
import { SendScene2Params } from '../../components/scenes/SendScene2' | ||
import { HomeAddress, SepaInfo } from '../../types/FormTypes' | ||
import { GuiPlugin } from '../../types/GuiPluginTypes' | ||
import { AppParamList } from '../../types/routerTypes' | ||
import { EdgeTokenId } from '../../types/types' | ||
import { EnterAmountPoweredBy } from './scenes/FiatPluginEnterAmountScene' | ||
import { StateManager } from './hooks/useStateManager' | ||
|
||
export const asFiatDirection = asValue('buy', 'sell') | ||
export type FiatDirection = ReturnType<typeof asFiatDirection> | ||
|
@@ -21,25 +22,6 @@ export interface FiatPluginAddressFormParams { | |
onSubmit: (homeAddress: HomeAddress) => Promise<void> | ||
} | ||
|
||
export interface FiatPluginGetMethodsResponse { | ||
setStatusText: (params: { statusText: string; options?: { textType?: 'warning' | 'error' } }) => void | ||
setPoweredBy: (params: EnterAmountPoweredBy) => void | ||
setValue1: (value: string) => void | ||
setValue2: (value: string) => void | ||
} | ||
|
||
export interface FiatPluginEnterAmountParams { | ||
headerTitle: string | ||
isBuy: boolean | ||
label1: string | ||
label2: string | ||
convertValue: (sourceFieldNum: number, value: string) => Promise<string | undefined> | ||
onSubmit: (value: FiatPluginEnterAmountResponse) => Promise<void> | ||
getMethods?: (methods: FiatPluginGetMethodsResponse) => void | ||
initialAmount1?: string | ||
headerIconUri?: string | ||
} | ||
|
||
export interface FiatPluginSepaFormParams { | ||
headerTitle: string | ||
headerIconUri?: string | ||
|
@@ -97,11 +79,11 @@ export interface FiatPluginUi { | |
}> | ||
showError: (error: Error) => Promise<void> | ||
listModal: (params: FiatPluginListModalParams) => Promise<string | undefined> | ||
enterAmount: (params: FiatPluginEnterAmountParams) => void | ||
enterAmount: (params: AppParamList['guiPluginEnterAmount']) => void | ||
addressForm: (params: FiatPluginAddressFormParams) => Promise<HomeAddress> | ||
sepaForm: (params: FiatPluginSepaFormParams) => Promise<SepaInfo> | ||
sepaTransferInfo: (params: FiatPluginSepaTransferParams) => Promise<void> | ||
popScene: () => {} | ||
exitScene: () => {} | ||
send: (params: SendScene2Params) => Promise<void> | ||
// showWebView: (params: { webviewUrl: string }) => Promise<void> | ||
} | ||
|
@@ -133,3 +115,8 @@ export interface FiatPlugin { | |
} | ||
|
||
export type FiatPluginFactory = (params: FiatPluginFactoryArgs) => Promise<FiatPlugin> | ||
|
||
export interface StatefulSceneEvent<EventValue, State extends object> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I won't ask for a change, but not sure what this commit buys us. The syntax for calling the callbacks is more complex. The generics also make the code harder to read. |
||
value: EventValue | ||
stateManager: StateManager<State> | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { useRef } from 'react' | ||
|
||
import { useHandler } from '../../../hooks/useHandler' | ||
import { useState } from '../../../types/reactHooks' | ||
|
||
export class StateManager<State extends object> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why the class? Why not just instantiate an object within useRef on line 28? |
||
private _state: State | ||
private readonly onUpdate: (state: State) => void | ||
|
||
constructor(state: State, onUpdate: (state: State) => void) { | ||
this._state = state | ||
this.onUpdate = onUpdate | ||
} | ||
|
||
get state(): State { | ||
return this._state | ||
} | ||
|
||
update(state: Partial<State>): void { | ||
this._state = { ...this._state, ...state } | ||
this.onUpdate(this._state) | ||
} | ||
} | ||
|
||
export const useStateManager = <T extends object>(defaultState: T): StateManager<T> => { | ||
const [state, setState] = useState<T>(defaultState) | ||
const handleUpdate: StateManager<T>['update'] = useHandler((state: Partial<T>) => setState({ ...stateManagerRef.current.state, ...state })) | ||
const stateManagerRef = useRef<StateManager<T>>(new StateManager(state, handleUpdate)) | ||
|
||
return stateManagerRef.current | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why is the spinner now getting modified in this callback?