Skip to content

Commit

Permalink
Merge pull request #1 from Koniverse/feature/khanhpv_add_toast_buyItem
Browse files Browse the repository at this point in the history
Feature/khanhpv add toast buy item
  • Loading branch information
khanhpv195 authored May 16, 2024
2 parents 948677c + 980fc3b commit 44f0e75
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 31 deletions.
68 changes: 50 additions & 18 deletions packages/extension-koni-ui/src/components/Modal/Shop/ShopModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import { ShopItem } from '@subwallet/extension-koni-ui/components';
import { BookaSdk } from '@subwallet/extension-koni-ui/connector/booka/sdk';
import { EnergyConfig, GameInventoryItem, GameItem } from '@subwallet/extension-koni-ui/connector/booka/types';
import useNotification from '@subwallet/extension-koni-ui/hooks/common/useNotification';
import useTranslation from '@subwallet/extension-koni-ui/hooks/common/useTranslation';
import { ThemeProps } from '@subwallet/extension-koni-ui/types';
import { ShopItemInfo } from '@subwallet/extension-koni-ui/types/shop';
Expand All @@ -26,6 +27,7 @@ function Component ({ className, energyConfig,
gameInventoryItemList, gameItemMap }: Props): React.ReactElement<Props> {
const { t } = useTranslation();
const [isLoading, setIsLoading] = useState<boolean>(false);
const notify = useNotification();

const { inactiveModal } = useContext(ModalContext);

Expand Down Expand Up @@ -107,33 +109,63 @@ function Component ({ className, energyConfig,

return result;
}, [energyConfig, gameId, gameInventoryItemList, gameItemMap]);

const onBuy = useCallback((gameItemId: string) => {
setIsLoading(true);

if (gameItemId === 'buy-energy-id') {
apiSDK.buyEnergy().catch((e) => {
console.log('buyEnergy error', e);
}).finally(() => {
setIsLoading(false);
});
apiSDK.buyEnergy()
.then(() => {
notify({
message: 'Successfully buy Energy',
type: 'success'
});
})
.catch((error) => {
notify({
message: error.message,
type: 'error'
});
})
.finally(() => {
setIsLoading(false);
});
} else {
apiSDK.buyItem(+gameItemId).catch((e) => {
console.log('buyItem error', e);
}).finally(() => {
setIsLoading(false);
});
apiSDK.buyItem(+gameItemId)
.then(() => {
notify({
message: 'Successfully buy Item',
type: 'success'
});
})
.catch((error) => {
notify({
message: error.message,
type: 'error'
});
})
.finally(() => {
setIsLoading(false);
});
}
}, []);

const onUse = useCallback((gameItemId: string) => {
setIsLoading(true);

apiSDK.useInventoryItem(+gameItemId).catch((e) => {
console.log('onUse error', e);
}).finally(() => {
setIsLoading(false);
});
apiSDK.useInventoryItem(+gameItemId)
.then(() => {
notify({
message: 'Successfully use item',
type: 'success'
});
})
.catch((e) => {
console.log('onUse error', e);
notify({
message: 'Error',
type: e.error
});
}).finally(() => {
setIsLoading(false);
});
}, []);

return (
Expand Down
32 changes: 19 additions & 13 deletions packages/extension-koni-ui/src/connector/booka/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { signRaw } from '@subwallet/extension-koni-ui/messaging';
import fetch from 'cross-fetch';
import { BehaviorSubject } from 'rxjs';

export const GAME_API_HOST = process.env.GAME_API_HOST || 'https://game-api.anhmtv.xyz';
export const GAME_API_HOST = process.env.GAME_API_HOST || 'https://game-api-dev.koni.studio';
export const TELEGRAM_WEBAPP_LINK = process.env.TELEGRAM_WEBAPP_LINK || 'BookaGamesBot/swbooka';
const storage = SWStorage.instance;
const telegramConnector = TelegramConnector.instance;
Expand Down Expand Up @@ -157,20 +157,26 @@ export class BookaSdk {
return undefined;
}
}

private async postRequest<T> (url: string, body: any) {
const request = await fetch(url, {
method: 'POST',
headers: this.getRequestHeader(),
body: JSON.stringify(body)
});

if (request.status === 200 || request.status === 304) {
return (await request.json()) as unknown as T;
} else {
return undefined;
private async postRequest<T>(url: string, body: any): 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();
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

0 comments on commit 44f0e75

Please sign in to comment.