Skip to content

Commit

Permalink
[issue-163]: Update action check balance
Browse files Browse the repository at this point in the history
  • Loading branch information
anhnhu committed Nov 28, 2024
1 parent 85ec623 commit bad0308
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,29 @@ const Component = ({ accountInfo,
await new Promise((resolve) => setTimeout(resolve, 3000));
}, [navigate]);

const handleCheckBalanceAction = useCallback(async (taskId?: number) => {
if (!taskId) {
return;
}

const checkAchievement = await apiSDK.checkAchievement(taskId);

if (checkAchievement) {
if (checkAchievement.success) {
return;
}

notify({
message: t(checkAchievement.message || 'Check balance failed'),
type: 'warning'
});

return;
}

await new Promise((resolve) => setTimeout(resolve, 3000));
}, [notify, t]);

const handleShareAction = useCallback(async (action: TaskActionShare) => {
alert(`Implement share action: ${action.url}`);

Expand All @@ -228,6 +251,8 @@ const Component = ({ accountInfo,
await handleDirectAction(action as TaskActionDirect);
} else if (actionType === TaskActionComponent.SHARE) {
await handleShareAction(action as TaskActionShare);
} else if (actionType === TaskActionComponent.CHECK_BALANCE) {
await handleCheckBalanceAction(taskId);
}

// Finish the task
Expand All @@ -236,7 +261,7 @@ const Component = ({ accountInfo,
networkKey
};
})();
}, [handleDirectAction, handleOnChainAction, handleOpenScreenAction, handleShareAction, handleUrlAction]);
}, [handleDirectAction, handleOnChainAction, handleOpenScreenAction, handleShareAction, handleUrlAction, handleCheckBalanceAction]);

const doTaskAction = useCallback((task: Task) => {
const action = task.action;
Expand Down Expand Up @@ -286,7 +311,7 @@ const Component = ({ accountInfo,
};
} else if (action) {
return async () => {
await doAction(action);
await doAction(action, achievement.milestoneId);
};
}

Expand Down
15 changes: 15 additions & 0 deletions packages/extension-koni-ui/src/connector/booka/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,21 @@ export class BookaSdk {
return data as {success: boolean};
}

/**
* Check achievement
* Return {success: boolean, data: Achievement, message: string } or throw error
* @param milestoneId
*/
async checkAchievement (milestoneId: number) {
const data = await this.postRequest(`${GAME_API_HOST}/api/achievement/check`, { milestoneId });

await this.fetchAchievementList();

await this.reloadAccount();

return data as {success: boolean, message?: string, data: Achievement};
}

async fetchTaskList () {
await this.waitForSync;
const taskList = await this.getRequest<Task[]>(`${GAME_API_HOST}/api/task/history`);
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 @@ -106,6 +106,7 @@ export enum TaskActionComponent {
OPEN_SCREEN = 'task.action-open-screen',
ONCHAIN = 'task.action-onchain',
DIRECT = 'task.action-direct',
CHECK_BALANCE = 'task.action-check-balance',
}

export interface TaskAction {
Expand Down

0 comments on commit bad0308

Please sign in to comment.