Skip to content

Commit

Permalink
khanhpv - fix style shopmodal, shopitem, post request
Browse files Browse the repository at this point in the history
  • Loading branch information
khanhpv5 committed May 16, 2024
1 parent 44f0e75 commit f3605b0
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 24 deletions.
30 changes: 22 additions & 8 deletions packages/extension-koni-ui/src/components/Modal/Shop/ShopModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,20 @@ function Component ({ className, energyConfig,
inventoryItemMapByGameItemId[i.gameItemId] = i;
});

const getShopItem = (gi: GameItem, disabled = false): ShopItemInfo => {
const getShopItem = (gi: GameItem, disabled = false): {
gameId: number;
itemGroup: string;
usable: false | boolean | undefined;
gameItemId: string;
itemGroupLevel: number;
inventoryQuantity: number | undefined;
price: number;
name: string;
limit: number | undefined;
icon: string | undefined;
description: string;
disabled: boolean
} => {
const limit = gi.maxBuy || undefined;
const inventoryQuantity = inventoryItemMapByGameItemId[gi.id]?.quantity || undefined;

Expand All @@ -68,7 +81,8 @@ function Component ({ className, energyConfig,
itemGroupLevel: gi.itemGroupLevel,
price: gi.price,
disabled: disabled || (!!limit && limit > 0 && limit === inventoryQuantity) || (!!gi.itemGroup && inventoryQuantity === 1),
usable: !!inventoryQuantity && inventoryQuantity > 0 && inventoryItemMapByGameItemId[gi.id]?.usable
usable: !!inventoryQuantity && inventoryQuantity > 0 && inventoryItemMapByGameItemId[gi.id]?.usable,
icon: gi.icon
};
};

Expand Down Expand Up @@ -111,6 +125,7 @@ function Component ({ className, energyConfig,
}, [energyConfig, gameId, gameInventoryItemList, gameItemMap]);
const onBuy = useCallback((gameItemId: string) => {
setIsLoading(true);

if (gameItemId === 'buy-energy-id') {
apiSDK.buyEnergy()
.then(() => {
Expand All @@ -119,7 +134,7 @@ function Component ({ className, energyConfig,
type: 'success'
});
})
.catch((error) => {
.catch((error: Error) => {
notify({
message: error.message,
type: 'error'
Expand All @@ -136,7 +151,7 @@ function Component ({ className, energyConfig,
type: 'success'
});
})
.catch((error) => {
.catch((error: Error) => {
notify({
message: error.message,
type: 'error'
Expand All @@ -157,11 +172,10 @@ function Component ({ className, energyConfig,
type: 'success'
});
})
.catch((e) => {
console.log('onUse error', e);
.catch((e: Error) => {
notify({
message: 'Error',
type: e.error
message: e.message,
type: 'error'
});
}).finally(() => {
setIsLoading(false);
Expand Down
15 changes: 9 additions & 6 deletions packages/extension-koni-ui/src/components/Shop/ShopItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ function Component (props: Props): React.ReactElement<Props> {
description,
disabled,
gameItemId,
icon,
inventoryQuantity,
limit,
name, onBuy, price, usable, onUse } = props;
name, onBuy, onUse, price, usable } = props;

const _onBuy = useCallback(() => {
onBuy(gameItemId, 1);
Expand All @@ -41,24 +42,24 @@ function Component (props: Props): React.ReactElement<Props> {
>
<Image
className={'item-icon'}
src={DefaultLogosMap.subwallet}
src={icon || DefaultLogosMap.subwallet}
width={40}
/>

<div className={'__middle-part'}>
<div>{name}</div>
<div>description: {description}</div>
<div className={'__website-name h5-text'}>{name}</div>
<div className={'__website-domain cdescription'}> {description !== null && 'Description: ' + description}</div>
{
!!limit && (
<div>Limit: {limit}</div>
)
}

<div>Price: {price}</div>
<div className={'__website-domain description'}>Price: <strong> {price}</strong></div>

{
!!inventoryQuantity && (
<div>Quantity: {inventoryQuantity}</div>
<div className={'__website-domain description'}>Quantity: <strong> {inventoryQuantity}</strong></div>
)
}

Expand All @@ -68,6 +69,7 @@ function Component (props: Props): React.ReactElement<Props> {
usable && (
<Button
onClick={_onUse}
size={'xs'}
>
Use
</Button>
Expand All @@ -77,6 +79,7 @@ function Component (props: Props): React.ReactElement<Props> {
<Button
disabled={disabled}
onClick={_onBuy}
size={'xs'}
>
Buy
</Button>
Expand Down
19 changes: 9 additions & 10 deletions packages/extension-koni-ui/src/connector/booka/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,26 +157,25 @@ export class BookaSdk {
return undefined;
}
}
private async postRequest<T>(url: string, body: any): Promise<T> {

private async postRequest<T, U>(url: string, body: U): Promise<T> {
try {
const response = await fetch(url, {
method: 'POST',
headers: this.getRequestHeader(),
body: JSON.stringify(body)
});
if (response.status === 200 || response.status === 304) {
return (await response.json()) as T;
}
if (response.status === 400) {
const errorResponse = await response.json();
body: JSON.stringify(body),
});

if (response && response.status === 200 || response.status === 304) {
return await response.json();
} else {
const errorResponse = await response.json() as { error: string };
throw new Error(errorResponse.error || 'Bad request');
}
throw new Error(`HTTP error! status: ${response.status}`);
} catch (error) {
throw error;
}
}


private async reloadAccount () {
const account = this.account;
Expand Down
1 change: 1 addition & 0 deletions packages/extension-koni-ui/src/connector/booka/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export interface GameItem {
itemGroup: string,
itemGroupLevel: number,
effectDuration: number,
icon?: string
}

export enum GameInventoryItemStatus {
Expand Down

0 comments on commit f3605b0

Please sign in to comment.