-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add token + amount inputs to migrate job dialog + adapt logic for fun…
…ding job
- Loading branch information
Showing
4 changed files
with
157 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 79 additions & 14 deletions
93
apps/warp-protocol/src/components/layout/migrate-jobs/migrate-job/useMigrateJobForm.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,107 @@ | ||
import { FormFunction, FormState, useForm } from '@terra-money/apps/hooks'; | ||
import { useMemo } from 'react'; | ||
import { FormFunction, FormModifier, FormState, LocalWallet, useForm, useLocalWallet } from '@terra-money/apps/hooks'; | ||
import { warp_controller } from '@terra-money/warp-sdk-v2'; | ||
import { Token, u } from '@terra-money/apps/types'; | ||
import Big from 'big.js'; | ||
import { microfy } from '@terra-money/apps/libs/formatting'; | ||
import { fetchTokenBalance } from '@terra-money/apps/queries'; | ||
|
||
export interface MigrateJobFormInput { | ||
durationDays: string; | ||
fundingAccount?: string; | ||
cwFunds?: warp_controller.CwFund[]; | ||
nativeFunds?: warp_controller.Coin[]; | ||
token?: Token; | ||
amount?: string; | ||
} | ||
|
||
interface MigrateJobFormState extends FormState<MigrateJobFormInput> { | ||
submitDisabled: boolean; | ||
amountValid?: boolean; | ||
amountError?: string; | ||
balance: u<Big>; | ||
balanceLoading: boolean; | ||
} | ||
|
||
export const useMigrateJobForm = (input?: MigrateJobFormInput) => { | ||
const initialValue = useMemo<MigrateJobFormState>( | ||
() => ({ | ||
durationDays: input?.durationDays ?? '', | ||
submitDisabled: input ? false : true, | ||
}), | ||
[input] | ||
); | ||
const dispatchTokenBalance = ( | ||
token: Token | undefined, | ||
localWallet: LocalWallet, | ||
dispatch: FormModifier<MigrateJobFormState> | ||
) => { | ||
dispatch({ | ||
amount: '', | ||
balance: Big(0) as u<Big>, | ||
balanceLoading: true, | ||
}); | ||
|
||
fetchTokenBalance(localWallet.lcd, token, localWallet.walletAddress) | ||
.then((balance) => { | ||
dispatch({ | ||
balance, | ||
balanceLoading: false, | ||
}); | ||
}) | ||
.catch(() => { | ||
dispatch({ | ||
balance: Big(0) as u<Big>, | ||
balanceLoading: false, | ||
}); | ||
}); | ||
}; | ||
|
||
const initialValue: MigrateJobFormState = { | ||
durationDays: '', | ||
fundingAccount: undefined, | ||
submitDisabled: true, | ||
token: undefined, | ||
balance: Big(0) as u<Big>, | ||
balanceLoading: false, | ||
amount: '', | ||
}; | ||
|
||
export const useMigrateJobForm = () => { | ||
const wallet = useLocalWallet(); | ||
|
||
const form: FormFunction<MigrateJobFormInput, MigrateJobFormState> = async (input, getState, dispatch) => { | ||
if (wallet.connectedWallet === undefined) { | ||
throw Error('The wallet is not connected'); | ||
} | ||
|
||
if ('token' in input) { | ||
dispatchTokenBalance(input.token, wallet, dispatch); | ||
} | ||
|
||
const state = { | ||
...getState(), | ||
...input, | ||
}; | ||
|
||
const submitDisabled = Boolean( | ||
state.durationDays === undefined || state.durationDays === null || state.durationDays.length < 1 | ||
); | ||
const uAmount = state.token && input.amount ? microfy(input.amount, state.token.decimals) : Big(0); | ||
|
||
const amountError = uAmount.gt(state.balance) ? 'The amount can not exceed the maximum balance' : undefined; | ||
|
||
const amountValid = uAmount.gt(0) && amountError === undefined; | ||
|
||
const submitDisabled = | ||
Boolean(state.token && amountValid === false) || | ||
state.durationDays === undefined || | ||
state.durationDays === null || | ||
state.durationDays.length < 1; | ||
|
||
dispatch({ | ||
...state, | ||
amountError, | ||
amountValid, | ||
submitDisabled, | ||
}); | ||
}; | ||
|
||
return useForm<MigrateJobFormInput, MigrateJobFormState>(form, initialValue); | ||
return useForm<MigrateJobFormInput, MigrateJobFormState>(form, initialValue, async (state, dispatch) => { | ||
if (wallet.connectedWallet === undefined) { | ||
throw Error('The wallet is not connected'); | ||
} | ||
|
||
if (state.token) { | ||
dispatchTokenBalance(state.token, wallet, dispatch); | ||
} | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters