chore: improve typings

This commit is contained in:
Arvid Norlin
2024-06-10 11:51:22 +02:00
parent af205451e6
commit 85aab88aec
5 changed files with 45 additions and 39 deletions

View File

@@ -21,7 +21,9 @@ import styles from "./overviewTable.module.css"
import {
ComparisonLevel,
overviewTableActionsEnum,
OverviewTableProps,
OverviewTableReducerAction,
} from "@/types/components/loyalty/blocks"
import { User } from "@/types/user"
@@ -54,15 +56,7 @@ const titleTranslations = {
],
}
enum actions {
SET_SELECTED_LEVEL_A_MOBILE = "SET_SELECTED_LEVEL_A_MOBILE",
SET_SELECTED_LEVEL_B_MOBILE = "SET_SELECTED_LEVEL_B_MOBILE",
SET_SELECTED_LEVEL_A_DESKTOP = "SET_SELECTED_LEVEL_A_DESKTOP",
SET_SELECTED_LEVEL_B_DESKTOP = "SET_SELECTED_LEVEL_B_DESKTOP",
SET_SELECTED_LEVEL_C_DESKTOP = "SET_SELECTED_LEVEL_C_DESKTOP",
}
function getLevelByTier(tier: number) {
function getLevelByTier(tier: membershipLevels) {
return levelsData.levels.find(
(level) => level.tier === tier
) as ComparisonLevel
@@ -109,29 +103,29 @@ function getInitialState(user?: User) {
}
}
function reducer(state: any, action: any) {
function reducer(state: any, action: OverviewTableReducerAction) {
switch (action.type) {
case actions.SET_SELECTED_LEVEL_A_MOBILE:
case overviewTableActionsEnum.SET_SELECTED_LEVEL_A_MOBILE:
return {
...state,
selectedLevelAMobile: action.payload,
}
case actions.SET_SELECTED_LEVEL_B_MOBILE:
case overviewTableActionsEnum.SET_SELECTED_LEVEL_B_MOBILE:
return {
...state,
selectedLevelBMobile: action.payload,
}
case actions.SET_SELECTED_LEVEL_A_DESKTOP:
case overviewTableActionsEnum.SET_SELECTED_LEVEL_A_DESKTOP:
return {
...state,
selectedLevelADesktop: action.payload,
}
case actions.SET_SELECTED_LEVEL_B_DESKTOP:
case overviewTableActionsEnum.SET_SELECTED_LEVEL_B_DESKTOP:
return {
...state,
selectedLevelBDesktop: action.payload,
}
case actions.SET_SELECTED_LEVEL_C_DESKTOP:
case overviewTableActionsEnum.SET_SELECTED_LEVEL_C_DESKTOP:
return {
...state,
selectedLevelCDesktop: action.payload,
@@ -146,10 +140,13 @@ export default function OverviewTable({ user }: OverviewTableProps) {
const [selectionState, dispatch] = useReducer(reducer, user, getInitialState)
function handleSelectChange(actionType: string) {
function handleSelectChange(actionType: overviewTableActionsEnum) {
return (key: Key) => {
if (typeof key === "number") {
dispatch({ payload: getLevelByTier(key), type: actionType })
dispatch({
payload: getLevelByTier(key),
type: actionType,
})
}
}
}
@@ -179,7 +176,9 @@ export default function OverviewTable({ user }: OverviewTableProps) {
label={intl.formatMessage({ id: "Level" })}
items={levelOptions}
value={selectionState.selectedLevelAMobile.tier}
onSelect={handleSelectChange(actions.SET_SELECTED_LEVEL_A_MOBILE)}
onSelect={handleSelectChange(
overviewTableActionsEnum.SET_SELECTED_LEVEL_A_MOBILE
)}
/>
<Image
className={styles.icon}
@@ -204,7 +203,9 @@ export default function OverviewTable({ user }: OverviewTableProps) {
label={intl.formatMessage({ id: "Level" })}
items={levelOptions}
value={selectionState.selectedLevelBMobile.tier}
onSelect={handleSelectChange(actions.SET_SELECTED_LEVEL_B_MOBILE)}
onSelect={handleSelectChange(
overviewTableActionsEnum.SET_SELECTED_LEVEL_B_MOBILE
)}
/>
<Image
className={styles.icon}
@@ -240,7 +241,7 @@ export default function OverviewTable({ user }: OverviewTableProps) {
items={levelOptions}
value={selectionState.selectedLevelADesktop.tier}
onSelect={handleSelectChange(
actions.SET_SELECTED_LEVEL_A_DESKTOP
overviewTableActionsEnum.SET_SELECTED_LEVEL_A_DESKTOP
)}
/>
<Image
@@ -267,7 +268,7 @@ export default function OverviewTable({ user }: OverviewTableProps) {
items={levelOptions}
value={selectionState.selectedLevelBDesktop.tier}
onSelect={handleSelectChange(
actions.SET_SELECTED_LEVEL_B_DESKTOP
overviewTableActionsEnum.SET_SELECTED_LEVEL_B_DESKTOP
)}
/>
<Image
@@ -294,7 +295,7 @@ export default function OverviewTable({ user }: OverviewTableProps) {
items={levelOptions}
value={selectionState.selectedLevelCDesktop.tier}
onSelect={handleSelectChange(
actions.SET_SELECTED_LEVEL_C_DESKTOP
overviewTableActionsEnum.SET_SELECTED_LEVEL_C_DESKTOP
)}
/>
<Image

View File

@@ -1,3 +1,5 @@
import { serverClient } from "@/lib/trpc/server"
import Link from "@/components/TempDesignSystem/Link"
import Subtitle from "@/components/TempDesignSystem/Text/Subtitle"
import Title from "@/components/TempDesignSystem/Text/Title"
@@ -27,15 +29,16 @@ function DynamicComponentBlock({ component, user }: DynamicComponentProps) {
}
}
export default function DynamicContent({
export default async function DynamicContent({
dynamicContent,
user,
}: DynamicContentProps) {
const displayHeader = !!(
dynamicContent.title ||
dynamicContent.subtitle ||
dynamicContent.title
)
const user = await serverClient().user.get()
return (
<section className={styles.container}>
{displayHeader ? (

View File

@@ -1,5 +1,3 @@
import { serverClient } from "@/lib/trpc/server"
import JsonToHtml from "@/components/JsonToHtml"
import DynamicContentBlock from "@/components/Loyalty/Blocks/DynamicContent"
import Shortcuts from "@/components/MyPages/Blocks/Shortcuts"
@@ -10,8 +8,6 @@ import type { BlocksProps } from "@/types/components/loyalty/blocks"
import { LoyaltyBlocksTypenameEnum } from "@/types/components/loyalty/enums"
export async function Blocks({ blocks }: BlocksProps) {
const user = await serverClient().user.get()
console.log({ user })
return blocks.map((block) => {
switch (block.__typename) {
case LoyaltyBlocksTypenameEnum.LoyaltyPageBlocksContent:
@@ -24,12 +20,7 @@ export async function Blocks({ blocks }: BlocksProps) {
</section>
)
case LoyaltyBlocksTypenameEnum.LoyaltyPageBlocksDynamicContent:
return (
<DynamicContentBlock
dynamicContent={block.dynamic_content}
user={user}
/>
)
return <DynamicContentBlock dynamicContent={block.dynamic_content} />
case LoyaltyBlocksTypenameEnum.LoyaltyPageBlocksShortcuts:
return (
<Shortcuts

View File

@@ -1,5 +1,3 @@
import { get } from "http"
import { serverClient } from "@/lib/trpc/server"
import Header from "@/components/MyPages/Blocks/Header"

View File

@@ -1,4 +1,5 @@
import { Lang } from "@/constants/languages"
import { membershipLevels } from "@/constants/membershipLevels"
import {
Block,
CardsGrid,
@@ -16,7 +17,6 @@ export type BlocksProps = {
export type DynamicContentProps = {
dynamicContent: DynamicContent["dynamic_content"]
user?: User
}
export type DynamicComponentProps = {
@@ -40,7 +40,7 @@ export type OverviewTableTitleProps = { texts: OverviewTableTitleTranslation[] }
export type OverviewTableProps = { user?: User }
export type Level = {
tier: number
tier: membershipLevels
name: string
requiredPoints: number
requiredNights?: number
@@ -55,7 +55,7 @@ export type LevelCardProps = {
}
export type ComparisonLevel = {
tier: number
tier: membershipLevels
name: string
description: string
requirement: string
@@ -103,3 +103,16 @@ export type BenefitTableHeaderProps = {
name: string
description: string
}
export enum overviewTableActionsEnum {
SET_SELECTED_LEVEL_A_MOBILE = "SET_SELECTED_LEVEL_A_MOBILE",
SET_SELECTED_LEVEL_B_MOBILE = "SET_SELECTED_LEVEL_B_MOBILE",
SET_SELECTED_LEVEL_A_DESKTOP = "SET_SELECTED_LEVEL_A_DESKTOP",
SET_SELECTED_LEVEL_B_DESKTOP = "SET_SELECTED_LEVEL_B_DESKTOP",
SET_SELECTED_LEVEL_C_DESKTOP = "SET_SELECTED_LEVEL_C_DESKTOP",
}
export type OverviewTableReducerAction = {
type: overviewTableActionsEnum
payload: ComparisonLevel
}