Skip to content

Commit 5d457bc

Browse files
KeitoTadashievavirsedamsarcevmarc2332
authored
Update purchase and auction dialog funcionality (#430)
* feat: update purchase and auction popups styles * remove styles * feat(dapp): add missing features * feat(dapp): update hooks usage * update dialogs * set default checkbox and hook * coments * fix function usage * refactor function and move to utils * chore: Add magic break line again --------- Co-authored-by: evavirseda <evirseda@boxfish.studio> Co-authored-by: msarcev <mario.sarcevic@iota.org> Co-authored-by: marc2332 <mespinsanz@gmail.com>
1 parent 1e85829 commit 5d457bc

File tree

3 files changed

+62
-4
lines changed

3 files changed

+62
-4
lines changed

dapp/src/components/dialogs/PurchaseNameDialog.tsx

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import {
77
Button,
88
ButtonType,
9+
Checkbox,
910
Dialog,
1011
DialogBody,
1112
DialogContent,
@@ -14,12 +15,16 @@ import {
1415
Header,
1516
LoadingIndicator,
1617
Panel,
18+
Select,
19+
SelectOption,
1720
} from '@iota/apps-ui-kit';
1821
import { useCurrentAccount, useIotaClient, useSignAndExecuteTransaction } from '@iota/dapp-kit';
1922
import { useMutation, useQueryClient } from '@tanstack/react-query';
23+
import { useState } from 'react';
2024

2125
import { NameUpdate, queryKey, useUpdateNameTransaction } from '@/hooks';
2226
import { useBalance } from '@/hooks/useBalance';
27+
import { useCoreConfig } from '@/hooks/useCoreConfig';
2328
import { useNameRecord } from '@/hooks/useNameRecord';
2429
import {
2530
GAS_BALANCE_TOO_LOW_ID,
@@ -28,7 +33,7 @@ import {
2833
} from '@/lib/constants';
2934
import { formatNanosToIota } from '@/lib/utils';
3035
import { denormalizeName } from '@/lib/utils/format/formatNames';
31-
import { getDefaultExpirationDate } from '@/lib/utils/getDefaultExpirationDate';
36+
import { getTargetExpirationDate } from '@/lib/utils/names';
3237

3338
type PurchaseNameProps = {
3439
name: string;
@@ -41,11 +46,21 @@ export function PurchaseNameDialog({ name, open, setOpen, onPurchase }: Purchase
4146
const queryClient = useQueryClient();
4247
const client = useIotaClient();
4348
const account = useCurrentAccount();
49+
const { data: coreConfig } = useCoreConfig();
50+
51+
const [renewYears, setRenewYears] = useState<number>(1);
52+
const [isDisplayName, setIsDisplayName] = useState<boolean>(false);
53+
4454
const {
4555
data: nameRecordData,
4656
isLoading: isNameRecordLoading,
4757
error: nameRecordError,
48-
} = useNameRecord(name);
58+
} = useNameRecord(name, {
59+
price: {
60+
years: renewYears,
61+
isRegistration: true,
62+
},
63+
});
4964

5065
const price = nameRecordData?.type === 'available' ? nameRecordData?.price : 0;
5166
const isConnected = !!account?.address;
@@ -57,7 +72,8 @@ export function PurchaseNameDialog({ name, open, setOpen, onPurchase }: Purchase
5772
type: 'register-name',
5873
name: name,
5974
price: price,
60-
years: 1,
75+
years: renewYears,
76+
setDefault: isDisplayName,
6177
});
6278
}
6379

@@ -107,6 +123,13 @@ export function PurchaseNameDialog({ name, open, setOpen, onPurchase }: Purchase
107123

108124
if (!isConnected) return null;
109125

126+
const RENEW_OPTIONS: SelectOption[] = coreConfig?.max_years
127+
? Array.from({ length: coreConfig?.max_years }, (_, i) => ({
128+
id: String(i + 1),
129+
label: `${i + 1} Year${i ? 's' : ''}`,
130+
}))
131+
: [];
132+
110133
const totalBalance = Number(coinBalance?.totalBalance) || 0;
111134
const totalGas = Number(updateNameData?.gasSummary?.totalGas) || 0;
112135
const totalPrice = nameRecordData?.type === 'available' ? nameRecordData.price + totalGas : 0;
@@ -126,7 +149,8 @@ export function PurchaseNameDialog({ name, open, setOpen, onPurchase }: Purchase
126149
const canRegister = canPay && !hasErrors && !isLoading && !isSendingTransaction;
127150

128151
const cleanName = denormalizeName(name);
129-
const expirationDate = getDefaultExpirationDate();
152+
153+
const expirationDate = getTargetExpirationDate(renewYears);
130154

131155
return (
132156
<Dialog open={open} onOpenChange={setOpen}>
@@ -142,8 +166,27 @@ export function PurchaseNameDialog({ name, open, setOpen, onPurchase }: Purchase
142166
</span>
143167
</div>
144168
</Panel>
169+
<div className="px-md py-sm border-t border-names-neutral-6">
170+
<Select
171+
value={renewYears.toString()}
172+
options={RENEW_OPTIONS}
173+
onValueChange={(value) => {
174+
setRenewYears(parseInt(value, 10));
175+
}}
176+
placeholder="Select renewal period"
177+
/>
178+
</div>
145179
</div>
146180
<div className="flex flex-col w-full gap-y-md">
181+
<Panel bgColor="bg-names-neutral-10">
182+
<div className="flex flex-row gap-x-sm w-full p-md">
183+
<Checkbox
184+
isChecked={isDisplayName}
185+
onCheckedChange={(e) => setIsDisplayName(e.target.checked)}
186+
label="Set name as Display Name"
187+
/>
188+
</div>
189+
</Panel>
147190
<div className="flex flex-row gap-x-sm w-full">
148191
<DisplayStats label="Registration Expires" value={expirationDate} />
149192
<DisplayStats

dapp/src/hooks/useUpdateNameTransaction.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ export type NameUpdate =
7171
name: string;
7272
price: number;
7373
years: number;
74+
setDefault: boolean;
7475
};
7576

7677
export function useUpdateNameTransaction({ address, updates }: UseUpdateNameTransactionOptions) {
@@ -150,6 +151,14 @@ export function useUpdateNameTransaction({ address, updates }: UseUpdateNameTran
150151
years: update.years,
151152
coin,
152153
});
154+
if (update.setDefault) {
155+
iotaNamesTx.setTargetAddress({
156+
nft: nft,
157+
address: address,
158+
isSubname: false,
159+
});
160+
iotaNamesTx.setDefault(update.name);
161+
}
153162
iotaNamesTx.transaction.transferObjects([nft, coin], address);
154163
break;
155164
}

dapp/src/lib/utils/names.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44
import { GRACE_PERIOD_MS, isSubname, NameRecord } from '@iota/iota-names-sdk';
55

66
import type { RegistrationNft } from '../interfaces/registration.interfaces';
7+
import { formatExpirationDate } from './format/formatExpirationDate';
8+
9+
export function getTargetExpirationDate(renewYears: number): string {
10+
const today = new Date();
11+
return formatExpirationDate(new Date(today.setFullYear(today.getFullYear() + renewYears)));
12+
}
713

814
export function isNameRecordExpired(nameRecord: NameRecord | RegistrationNft) {
915
return nameRecord.expirationTimestampMs < Date.now();

0 commit comments

Comments
 (0)