Skip to content

Instantly share code, notes, and snippets.

const useCheckUserPermission = () => {
const { refetch: fetchUserProfile } = useUserProfile(userId);
const userProfile = await fetchUserProfile();
const userPermissionHandler = new UserPermissionHandler(userProfile);
const guards = new Guards();
guard.addGuards([
{ isValid: userPermissionHandler.isLogin },
{ isValid: userPermissionHandler.isMobileVerified },
// 取得資料
const { refetch: fetchUserProfile } = useUserProfile(userId);
const userProfile = await fetchUserProfile();
// 建立檢查權限的邏輯
const userPermissionHandler = new UserPermissionHandler(userProfile);
// 將要檢查的權限放到 guards 裡面
const guards = new Guards();
guard.addGuards([
class Guards {
  constructor() {
    this.guards = [];
  }
  addGuards(guards) {
    if (Array.isArray(guards)) {
      this.guards = […this.guards, …guards];
    } else {
      throw new Error('guards should be a array');
class UserPermissionHandler {
 constructor(user) {
 this.user = (user || {});
 }
 get isLogin() {
   return Boolean(this.user.token);
 }
 get isMobileVerified() {
const useUserProfile = (userId) => useQuery({
  queryKey: ['user'],
 queryFn: async () => {
 const userProfile = await fetchUserProfile();
  return new User(userProfile);
  },
});
Promise.resolve()
.then(() => console.log('b'));
setTimeout(() => console.log('a'));
console.log('c');
const toUpperCase = (str) => str.toUpperCase();
const getFirstLetter = (str) => str[0] || '';
const getCapitalFirstLetter = (str) => getFirstLetter(toUpperCase(str));
class FailTransactionStatusDelegate {
constructor(transaction) {
this.statusString = '失敗';
this.isNonRefundable = true;
}
}
export default FailTransactionStatusDelegate;
import Transaction from './Transaction';
const getTransactionInfo = (transaction) => new Transaction(transaction);
import SuccessTransaction from './SuccessTransaction';
import FailTransaction from './FailTransaction';
import PendingTransaction from './PendingTransaction';
class Transaction {
constructor(transaction) {
this.id = transaction.id;
this.transactionStatusDelegate = this.getTransactionStatusDelegate(transaction);
}