Skip to content

Instantly share code, notes, and snippets.

@ladbastille
Last active December 3, 2021 04:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ladbastille/139e9db18ba9b7f83e25d9917a824c91 to your computer and use it in GitHub Desktop.
Save ladbastille/139e9db18ba9b7f83e25d9917a824c91 to your computer and use it in GitHub Desktop.
cart checkout alert code (start from line:77)
import api from '../utils/Api.js';
class BaseController {
constructor(model, view, fb, tappay) {
this.model = model;
this.view = view;
this.fb = fb;
this.tappay = tappay;
this.view.bindInputSearchPressEnter(this.redirectToIndexPageWithTag);
this.view.bindClickProfile(this.handleClickProfile.bind(this));
this.view.bindCloseCartAlert(this.handleCloseCartAlert.bind(this));
}
init() {
this.view.handleTag(this.paramsTag);
this.view.renderCount(this.model.cart.items.length);
this.view.renderCartAlert(this.model.cart.items);
this.setCartAlert(this.model.cart.items.length, this.view.cartAlert);
}
get paramsNumber() {
return this.getParams('number');
}
get paramsId() {
return this.getParams('id');
}
get paramsTag() {
return this.getParams('tag') || 'all';
}
getParams(key) {
const urlParams = new URLSearchParams(window.location.search);
return urlParams.get(key);
}
redirectToIndexPageWithTag(tag) {
window.location.href = `/?tag=${tag}`;
}
makePostCartData(cartItems, id) {
const data = {
user_id: id,
cart_items: cartItems
};
return data;
}
handleCloseCartAlert() {
this.view.cartAlert.style.display = 'none';
}
handleClickProfile() {
if (this.fb.jwtToken) {
window.location.href = '/profile.html';
} else {
this.fb
.login()
.then((response) => this.fb.handleLoginStatus(response))
.then((profile) => {
if (profile) {
window.alert('登入成功!');
api.getCart()
.then(res => {
window.localStorage.setItem('cart', JSON.stringify(res.cart_items));
});
}
})
.then(() => {
window.location.href = '/index.html';
});
}
}
store(key, expire) {
let obj = {
time: new Date().getTime(),
expire: expire,
};
// localStorage只能儲存字串,所以要先將物件轉成字串
let objStr = JSON.stringify(obj);
localStorage.setItem(key, objStr);
}
setCartAlert(cart, alert) {
// 預設60秒
const expireTime = 1000 * 60 * 1;
if (!localStorage.getItem('hasEnterBefore')) {
this.store('hasEnterBefore', expireTime);
}
const timer = setInterval(function () {
if (localStorage.getItem('hasEnterBefore')) {
let hasEnterBefore = localStorage.getItem('hasEnterBefore');
let hasEnterBeforeObj = JSON.parse(hasEnterBefore);
let timePassed = new Date().getTime() - hasEnterBeforeObj.time;
// Demo to show the countdown in console
console.log('已經經過', timePassed, '毫秒');
if (timePassed >= hasEnterBeforeObj.expire || timePassed >= expireTime) {
localStorage.removeItem('hasEnterBefore');
}
} else {
if (cart !== 0) {
alert.style.display = 'block';
}
clearInterval(timer);
}
}, 1000);
}
}
export default BaseController;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment