-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6282c62
commit 83434bc
Showing
106 changed files
with
1,769 additions
and
699 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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { isAccountAddressValid } from '@/shared/helpers'; | ||
import { decodeAddress } from '@gear-js/api'; | ||
import { z } from 'zod'; | ||
|
||
const DEFAULT_VALUES = { | ||
address: '', | ||
value: '', | ||
duration: '', | ||
isCodeUploadEnabled: false, | ||
}; | ||
|
||
const ADDRESS_SCHEMA = z | ||
.string() | ||
.trim() | ||
.min(0) | ||
.refine((value) => isAccountAddressValid(value), 'Invalid address') | ||
.transform((value) => decodeAddress(value)); | ||
|
||
export { DEFAULT_VALUES, ADDRESS_SCHEMA }; |
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,3 +1,5 @@ | ||
import { useIssueVoucher } from './use-issue-voucher'; | ||
import { useBalanceSchema } from './use-balance-schema'; | ||
import { useDurationSchema } from './use-duration-schema'; | ||
|
||
export { useIssueVoucher }; | ||
export { useIssueVoucher, useBalanceSchema, useDurationSchema }; |
28 changes: 28 additions & 0 deletions
28
idea/frontend/src/features/voucher/hooks/use-balance-schema.ts
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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { useApi, useBalanceFormat } from '@gear-js/react-hooks'; | ||
import { z } from 'zod'; | ||
|
||
function useBalanceSchema() { | ||
const { api } = useApi(); | ||
const { getChainBalanceValue, getFormattedBalanceValue } = useBalanceFormat(); | ||
|
||
const getBalanceSchema = () => { | ||
if (!api) throw new Error('API is not initialized'); | ||
|
||
const decimals = api.registry.chainDecimals.toString(); | ||
const existentialDeposit = api.existentialDeposit.toString(); | ||
|
||
const minValueMessage = `Minimum value is ${getFormattedBalanceValue(existentialDeposit).toFixed()}`; | ||
const integerMessage = `Maximum amount of decimal places is ${decimals}`; | ||
|
||
return z | ||
.string() | ||
.transform((value) => getChainBalanceValue(value)) | ||
.refine((value) => value.isGreaterThanOrEqualTo(existentialDeposit), minValueMessage) | ||
.refine((value) => value.isInteger(), integerMessage) | ||
.transform((value) => value.toFixed()); | ||
}; | ||
|
||
return getBalanceSchema(); | ||
} | ||
|
||
export { useBalanceSchema }; |
18 changes: 18 additions & 0 deletions
18
idea/frontend/src/features/voucher/hooks/use-duration-schema.ts
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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { useApi } from '@gear-js/react-hooks'; | ||
import { z } from 'zod'; | ||
|
||
function useDurationSchema() { | ||
const { api } = useApi(); | ||
|
||
const getDurationSchema = () => { | ||
if (!api) throw new Error('API is not initialized'); | ||
|
||
const { minDuration, maxDuration } = api.voucher; | ||
|
||
return z.coerce.number().min(minDuration).max(maxDuration).int(); | ||
}; | ||
|
||
return getDurationSchema(); | ||
} | ||
|
||
export { useDurationSchema }; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,17 @@ | ||
import { IssueVoucher, UseVoucherCheckbox, VoucherTable, VoucherBadge } from './ui'; | ||
import { | ||
IssueVoucher, | ||
VoucherSelect, | ||
VoucherTable, | ||
VoucherBadge, | ||
UseVoucherCheckboxDeprecated, | ||
VoucherTableDeprecated, | ||
} from './ui'; | ||
|
||
export { IssueVoucher, UseVoucherCheckbox, VoucherTable, VoucherBadge }; | ||
export { | ||
IssueVoucher, | ||
VoucherSelect, | ||
VoucherTable, | ||
VoucherBadge, | ||
UseVoucherCheckboxDeprecated, | ||
VoucherTableDeprecated, | ||
}; |
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,6 +1,14 @@ | ||
import { IssueVoucher } from './issue-voucher'; | ||
import { UseVoucherCheckbox } from './use-voucher-checkbox'; | ||
import { VoucherTable } from './voucher-table'; | ||
import { VoucherSelect } from './voucher-select'; | ||
import { VoucherTable, VoucherTableDeprecated } from './voucher-table'; | ||
import { VoucherBadge } from './voucher-badge'; | ||
import { UseVoucherCheckboxDeprecated } from './use-voucher-checkbox-deprecated'; | ||
|
||
export { IssueVoucher, UseVoucherCheckbox, VoucherTable, VoucherBadge }; | ||
export { | ||
IssueVoucher, | ||
VoucherSelect, | ||
VoucherTable, | ||
VoucherBadge, | ||
UseVoucherCheckboxDeprecated, | ||
VoucherTableDeprecated, | ||
}; |
File renamed without changes.
63 changes: 63 additions & 0 deletions
63
idea/frontend/src/features/voucher/ui/issue-voucher-modal/issue-voucher-modal-deprecated.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 |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { Button, Modal } from '@gear-js/ui'; | ||
import { HexString, decodeAddress } from '@gear-js/api'; | ||
import { useBalanceFormat } from '@gear-js/react-hooks'; | ||
import { yupResolver } from '@hookform/resolvers/yup'; | ||
import { FormProvider, useForm } from 'react-hook-form'; | ||
import * as yup from 'yup'; | ||
|
||
import ApplySVG from '@/shared/assets/images/actions/apply.svg?react'; | ||
import CloseSVG from '@/shared/assets/images/actions/close.svg?react'; | ||
import { isAccountAddressValid } from '@/shared/helpers'; | ||
import { Input, ValueField } from '@/shared/ui/form'; | ||
|
||
import { useIssueVoucher } from '../../hooks'; | ||
import styles from './issue-voucher-modal.module.scss'; | ||
|
||
type Props = { | ||
programId: HexString; | ||
close: () => void; | ||
}; | ||
|
||
const defaultValues = { address: '', value: '' }; | ||
|
||
const schema = yup.object().shape({ | ||
address: yup | ||
.string() | ||
.test('is-address-valid', 'Invalid address', isAccountAddressValid) | ||
.required('This field is required'), | ||
value: yup.string().required('This field is required'), | ||
}); | ||
|
||
const resolver = yupResolver(schema); | ||
|
||
const IssueVoucherModalDeprecated = ({ programId, close }: Props) => { | ||
const { getChainBalanceValue } = useBalanceFormat(); | ||
const { issueVoucherDeprecated } = useIssueVoucher(); | ||
|
||
const methods = useForm({ defaultValues, resolver }); | ||
|
||
const handleSubmit = ({ address, value }: typeof defaultValues) => { | ||
const decodedAddress = decodeAddress(address); | ||
const chainValue = getChainBalanceValue(value).toFixed(); | ||
|
||
issueVoucherDeprecated(decodedAddress, programId, chainValue, close); | ||
}; | ||
|
||
return ( | ||
<Modal heading="Create Voucher" size="large" close={close}> | ||
<FormProvider {...methods}> | ||
<form onSubmit={methods.handleSubmit(handleSubmit)} className={styles.form}> | ||
<Input name="address" label="Account address" direction="y" block /> | ||
<ValueField name="value" label="Tokens amount:" direction="y" block /> | ||
|
||
<div className={styles.buttons}> | ||
<Button type="submit" icon={ApplySVG} size="large" text="Create" /> | ||
<Button icon={CloseSVG} color="light" size="large" text="Close" onClick={close} /> | ||
</div> | ||
</form> | ||
</FormProvider> | ||
</Modal> | ||
); | ||
}; | ||
|
||
export { IssueVoucherModalDeprecated }; |
Oops, something went wrong.