Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

frontend auto fetch. mixin improvements. disable btns if !userAddress #6

Merged
merged 1 commit into from
Apr 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions frontend/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import NavbarComponent from "@/components/Layout/NavbarComponent.vue";
import LoadingComponent from "@/components/Common/LoadingComponent.vue";
import FooterComponent from "@/components/Layout/FooterComponent.vue";
import mxGame from "@/mixin/game";
import {mapGetters} from "vuex";

export default {
name: "App",
Expand All @@ -22,17 +23,32 @@ export default {

components: {FooterComponent, LoadingComponent, NavbarComponent},

computed: {
...mapGetters(['gameConfig'])
},

data() {
return {
isBusy: true,
}
},

async created() {
await this.fetchOnce()
this.isBusy = false
await this.fetchOnce();
await this.fetchInterval()
}
this.isBusy = false;

// Set auto-fetch interval
this.intervalId = setInterval(() => {
const isGameEnd = this.timeLeftSeconds < Number(this.gameConfig.game_extend)
this.fetchInterval(isGameEnd);
console.log(`Auto-fetch!`)
}, 5000);
},

unmounted() {
if (this.intervalId) clearInterval(this.intervalId)
},
Comment on lines +49 to +51
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The cleanup of the auto-fetch interval in the unmounted lifecycle hook is correctly implemented, preventing potential memory leaks. However, the lifecycle hook should be beforeUnmount instead of unmounted to ensure the cleanup happens as the component is being removed, not after it has been removed.

-  unmounted() {
+  beforeUnmount() {

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
unmounted() {
if (this.intervalId) clearInterval(this.intervalId)
},
beforeUnmount() {
if (this.intervalId) clearInterval(this.intervalId)
},

};
</script>

Expand Down
6 changes: 3 additions & 3 deletions frontend/src/components/Game/BidComponent.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</div>
</div>

<div class="selected-pot">
<div class="selected-pot text-white mt-5">
<p v-if="utils.selectedPot">Selected pot: {{ getPotName(utils.selectedPot) }}</p>
<p v-else>Select a pot to place a bid.</p>
</div>
Expand All @@ -31,7 +31,7 @@
<button class="btn btn-outline-secondary" type="button" @click="setMaxBid">Max</button>
</div>

<ButtonComponent :isDisabled="!utils.selectedPot || isBusy" text="Place Bid"/>
<ButtonComponent :isDisabled="!utils.selectedPot || isBusy || !userAddress" text="Place Bid"/>
<LoadingComponent v-if="isBusy"/>
</form>
</div>
Expand All @@ -55,7 +55,7 @@ export default {
mixins: [mxChain, mxToast, mxPot, mxGame],

computed: {
...mapGetters(['minBid', 'maxBid', 'gameConfig', 'utils'])
...mapGetters(['minBid', 'maxBid', 'gameConfig', 'utils', 'userAddress'])
},

data() {
Expand Down
38 changes: 6 additions & 32 deletions frontend/src/components/Info/TimerComponent.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@
<div class="card text-center">
<div class="card-body">
<h5 class="card-title">Timer</h5>
<p v-if="timeLeft" class="card-text text-success">Time Remaining: {{ timeLeft }}</p>
<div v-if="timeLeft" class="progress">
<p v-if="timeLeftHuman" class="card-text text-success">Time Remaining: {{ timeLeftHuman }}</p>
<div v-if="timeLeftHuman" class="progress">
<div class="progress-bar" role="progressbar" :style="{ width: progressPercentage + '%' }" aria-valuenow="25"
aria-valuemin="0" aria-valuemax="100">{{ progressPercentage }}%
</div>
</div>
<p v-else class="card-text text-danger">The game has ended. Please trigger the end game process.</p>

<div class="mb-3">
<!-- Button is only shown when timeLeft is falsy, meaning the game has ended -->
<ButtonComponent v-if="!timeLeft" :isDisabled="isBusy" text="End Game" @click="onEndGame"/>
<!-- Button is only shown when timeLeftHuman is falsy, meaning the game has ended -->
<ButtonComponent v-if="!timeLeftHuman" :isDisabled="isBusy || !userAddress" text="End Game"
@click="onEndGame"/>
<LoadingComponent v-if="isBusy"/>
</div>

Expand Down Expand Up @@ -43,20 +44,7 @@ export default {
mixins: [mxChain, mxToast, mxGame],

computed: {
...mapGetters(['gameState', 'reallocationFeePool']),

timeLeft() {
if (!this.gameState) return null
const endTime = this.gameState.end_time * 1000;
const timeDiff = endTime - this.currentTime;
if (timeDiff <= 0) {
return null;
}
const hours = Math.floor((timeDiff / (1000 * 60 * 60)) % 24);
const minutes = Math.floor((timeDiff / (1000 * 60)) % 60);
const seconds = Math.floor((timeDiff / 1000) % 60);
return `${hours}h ${minutes}m ${seconds}s`;
},
...mapGetters(['gameState', 'reallocationFeePool', 'userAddress']),

progressPercentage() {
if (!this.gameState) return 0;
Expand All @@ -68,27 +56,13 @@ export default {

data() {
return {
intervalId: null,
currentTime: new Date().getTime(),
isBusy: false,
};
},

mounted() {
this.intervalId = setInterval(this.updateCurrentTime, 1000);
},

unmounted() {
clearInterval(this.intervalId);
},

methods: {
...mapActions(['fetchPlayerAllocations']),

updateCurrentTime() {
this.currentTime = new Date().getTime();
},

async onEndGame() {
this.isBusy = true
try {
Expand Down
76 changes: 61 additions & 15 deletions frontend/src/mixin/game.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,73 @@
import {mapActions, mapGetters} from "vuex";

const mxGame = {
data() {
return {
currentTime: new Date().getTime(),
};
},

computed: {
...mapGetters(['userAddress'])
...mapGetters(['userAddress', 'gameState']),

timeLeftSeconds() {
if (!this.gameState) return null;
const endTime = this.gameState.end_time * 1000;
const timeDiff = endTime - this.currentTime;
return timeDiff > 0 ? Math.floor(timeDiff / 1000) : 0;
},

timeLeftHuman() {
const timeDiff = this.timeLeftSeconds * 1000; // Convert back to milliseconds for calculation
if (timeDiff <= 0) {
return "0h 0m 0s";
}
const hours = Math.floor((timeDiff / (1000 * 60 * 60)) % 24);
const minutes = Math.floor((timeDiff / (1000 * 60)) % 60);
const seconds = Math.floor((timeDiff / 1000) % 60);
return `${hours}h ${minutes}m ${seconds}s`;
},
},

methods: {
...mapActions(['initUser', 'fetchGameConfig', 'fetchGameState', 'fetchPots', 'fetchWinningPots', 'fetchBidRange', 'fetchReallocationFeePool', 'fetchPlayerAllocations']),
...mapActions([
'initUser',
'fetchGameConfig',
'fetchGameState',
'fetchPots',
'fetchWinningPots',
'fetchBidRange',
'fetchReallocationFeePool',
'fetchPlayerAllocations',
]),

async fetchOnce() {
await this.initUser();
if (this.userAddress) await this.fetchPlayerAllocations()
await this.fetchGameConfig()
if (this.userAddress) await this.fetchPlayerAllocations();
await this.fetchGameConfig();
await this.fetchGameState();
},

async fetchInterval(gameEnd = false) {
await this.fetchPots();
await this.fetchWinningPots();
await this.fetchBidRange();
await this.fetchReallocationFeePool();
if (gameEnd) await this.fetchGameState();
},

async fetchInterval() {
await this.fetchGameState()
await this.fetchPots()
await this.fetchWinningPots()
await this.fetchBidRange()
await this.fetchReallocationFeePool()
}
}
}

export default mxGame
updateCurrentTime() {
this.currentTime = new Date().getTime();
},
},

created() {
this.intervalId = setInterval(this.updateCurrentTime, 1000);
},

unmounted() {
clearInterval(this.intervalId);
},
Comment on lines +59 to +70
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The updateCurrentTime method and its interval management in the created and unmounted hooks are well-implemented, ensuring that currentTime is regularly updated. This is crucial for the accurate calculation of time-related properties. Similar to the App.vue file, consider using beforeUnmount instead of unmounted for interval cleanup to ensure it happens before the component is fully unmounted.

-  unmounted() {
+  beforeUnmount() {

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
updateCurrentTime() {
this.currentTime = new Date().getTime();
},
},
created() {
this.intervalId = setInterval(this.updateCurrentTime, 1000);
},
unmounted() {
clearInterval(this.intervalId);
},
updateCurrentTime() {
this.currentTime = new Date().getTime();
},
},
created() {
this.intervalId = setInterval(this.updateCurrentTime, 1000);
},
beforeUnmount() {
clearInterval(this.intervalId);
},

};

export default mxGame;
Loading