feat(LOY-55): Add FilterRewardsModal * feat(LOY-55): Add rewards filtering functionality - Implement dynamic rewards filtering by category and membership level - Create FilterRewardsModal component for filtering rewards - Add useFilteredRewards hook to handle filtering logic - Update rewards schema and constants to support new filtering features - Remove hardcoded page size and replace with constant * fix(LOY-55): reuse existing tier to friend map * refactor(LOY-55): fix checkbox onChange type safety * refactor(LOY-55): Improve rewards filtering type safety and validation * refactor(LOY-55): Update filter modal border color using design token Approved-by: Christian Andolf
84 lines
2.1 KiB
TypeScript
84 lines
2.1 KiB
TypeScript
import {
|
|
RESTAURANT_REWARD_IDS,
|
|
REWARD_CATEGORIES,
|
|
REWARD_IDS,
|
|
REWARD_TYPES,
|
|
} from "@/constants/rewards"
|
|
import { dt } from "@/lib/dt"
|
|
|
|
import type { Dayjs } from "dayjs"
|
|
|
|
import type {
|
|
RestaurantRewardId,
|
|
RewardCategory,
|
|
RewardId,
|
|
RewardType,
|
|
} from "@/types/components/myPages/rewards"
|
|
import type {
|
|
Coupon,
|
|
RewardWithRedeem,
|
|
} from "@/server/routers/contentstack/reward/output"
|
|
|
|
export {
|
|
getEarliestExpirationDate,
|
|
getRewardType,
|
|
isOnSiteTierReward,
|
|
isRestaurantOnSiteTierReward,
|
|
isRestaurantReward,
|
|
isRewardCategory,
|
|
isTierType,
|
|
isValidRewardId,
|
|
redeemLocationIsOnSite,
|
|
}
|
|
|
|
function isValidRewardId(id: string): id is RewardId {
|
|
return Object.values<string>(REWARD_IDS).includes(id)
|
|
}
|
|
|
|
function isRestaurantReward(rewardId: string): rewardId is RestaurantRewardId {
|
|
return RESTAURANT_REWARD_IDS.some((id) => id === rewardId)
|
|
}
|
|
|
|
function isRewardCategory(value: string): value is RewardCategory {
|
|
return REWARD_CATEGORIES.some((category) => category === value)
|
|
}
|
|
|
|
function redeemLocationIsOnSite(
|
|
location: RewardWithRedeem["redeemLocation"]
|
|
): location is "On-site" {
|
|
return location === "On-site"
|
|
}
|
|
|
|
function isTierType(type: RewardWithRedeem["rewardType"]): type is "Tier" {
|
|
return type === "Tier"
|
|
}
|
|
|
|
function isOnSiteTierReward(reward: RewardWithRedeem): boolean {
|
|
return (
|
|
redeemLocationIsOnSite(reward.redeemLocation) &&
|
|
isTierType(reward.rewardType)
|
|
)
|
|
}
|
|
|
|
function isRestaurantOnSiteTierReward(reward: RewardWithRedeem): boolean {
|
|
return isOnSiteTierReward(reward) && isRestaurantReward(reward.reward_id)
|
|
}
|
|
|
|
function getRewardType(type?: string): RewardType | null {
|
|
return REWARD_TYPES.find((t) => t === type) ?? null
|
|
}
|
|
|
|
function getEarliestExpirationDate(coupons: Coupon[]) {
|
|
return coupons
|
|
.map(({ expiresAt }) => expiresAt)
|
|
.filter((expiresAt): expiresAt is string => !!expiresAt)
|
|
.reduce((earliestDate: Dayjs | null, expiresAt) => {
|
|
const expiresAtDate = dt(expiresAt)
|
|
if (!earliestDate) {
|
|
return expiresAtDate
|
|
}
|
|
|
|
return earliestDate.isBefore(expiresAtDate) ? earliestDate : expiresAtDate
|
|
}, null)
|
|
}
|