This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 取得資料 | |
const { refetch: fetchUserProfile } = useUserProfile(userId); | |
const userProfile = await fetchUserProfile(); | |
// 建立檢查權限的邏輯 | |
const userPermissionHandler = new UserPermissionHandler(userProfile); | |
// 將要檢查的權限放到 guards 裡面 | |
const guards = new Guards(); | |
guard.addGuards([ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class UserPermissionHandler { | |
constructor(user) { | |
this.user = (user || {}); | |
} | |
get isLogin() { | |
return Boolean(this.user.token); | |
} | |
get isMobileVerified() { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const useUserProfile = (userId) => useQuery({ | |
queryKey: ['user'], | |
queryFn: async () => { | |
const userProfile = await fetchUserProfile(); | |
return new User(userProfile); | |
}, | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Promise.resolve() | |
.then(() => console.log('b')); | |
setTimeout(() => console.log('a')); | |
console.log('c'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const toUpperCase = (str) => str.toUpperCase(); | |
const getFirstLetter = (str) => str[0] || ''; | |
const getCapitalFirstLetter = (str) => getFirstLetter(toUpperCase(str)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class FailTransactionStatusDelegate { | |
constructor(transaction) { | |
this.statusString = '失敗'; | |
this.isNonRefundable = true; | |
} | |
} | |
export default FailTransactionStatusDelegate; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Transaction from './Transaction'; | |
const getTransactionInfo = (transaction) => new Transaction(transaction); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
NewerOlder