94 lines
3.9 KiB
Plaintext
94 lines
3.9 KiB
Plaintext
import { Category, Day, Project, Slot } from './types.uts'
|
|
import { formatDate } from './format.uts'
|
|
import { PROJECT_PLACEHOLDER_IMAGE } from './theme.uts'
|
|
import { mediaUrl } from './api.uts'
|
|
|
|
export function decorateCategories(categories: Category[]): Category[] {
|
|
const iconMap = ['heart', 'search', 'smile', 'service', 'user']
|
|
const imageKeys = ['daily', 'clean', 'white', 'breath', 'child']
|
|
const result: Category[] = []
|
|
for (let categoryIndex = 0; categoryIndex < categories.length; categoryIndex += 1) {
|
|
const category = categories[categoryIndex]
|
|
const rawImageKey = category.image_key == null ? '' : category.image_key as string
|
|
const imageKey = rawImageKey.length > 0 ? rawImageKey : imageKeys[categoryIndex % imageKeys.length]
|
|
const projects: Project[] = []
|
|
for (let projectIndex = 0; projectIndex < category.projects.length; projectIndex += 1) {
|
|
const project = category.projects[projectIndex]
|
|
const rawProjectImageKey = project.image_key == null ? '' : project.image_key as string
|
|
const rawProjectImage = project.image == null ? '' : project.image as string
|
|
const projectImageKey = rawProjectImageKey.length > 0 ? rawProjectImageKey : imageKey
|
|
const projectImage = rawProjectImage.length > 0 ? mediaUrl(rawProjectImage) : ''
|
|
projects.push({
|
|
id: project.id,
|
|
category_id: project.category_id,
|
|
name: project.name,
|
|
subtitle: project.subtitle,
|
|
price_text: project.price_text,
|
|
image: projectImage.length > 0 ? projectImage : PROJECT_PLACEHOLDER_IMAGE,
|
|
image_key: projectImageKey,
|
|
sortIndex: projectIndex + 1
|
|
} as Project)
|
|
}
|
|
const rawIcon = category.icon == null ? '' : category.icon as string
|
|
result.push({
|
|
id: category.id,
|
|
name: category.name,
|
|
icon: rawIcon.length > 0 ? rawIcon : iconMap[categoryIndex % iconMap.length],
|
|
image_key: imageKey,
|
|
projects
|
|
} as Category)
|
|
}
|
|
return result
|
|
}
|
|
|
|
export function fallbackCategories(): Category[] {
|
|
return [
|
|
{ id: 'cat_meniscus_injury', name: '半月板损伤', image_key: 'daily', projects: [] } as Category,
|
|
{ id: 'cat_synovitis', name: '滑膜炎', image_key: 'clean', projects: [] } as Category,
|
|
{ id: 'cat_knee_arthritis', name: '膝关节炎', image_key: 'white', projects: [] } as Category,
|
|
{ id: 'cat_lumbar_disc_herniation', name: '腰椎间盘突出', image_key: 'breath', projects: [] } as Category,
|
|
{ id: 'cat_lumbar_muscle_strain', name: '腰肌劳损', image_key: 'child', projects: [] } as Category,
|
|
{ id: 'cat_lumbar_spinal_stenosis', name: '腰椎管狭窄', image_key: 'daily', projects: [] } as Category,
|
|
{ id: 'cat_cervical_spondylosis', name: '颈椎病', image_key: 'clean', projects: [] } as Category,
|
|
{ id: 'cat_lumbar_spondylosis', name: '腰椎病', image_key: 'white', projects: [] } as Category
|
|
]
|
|
}
|
|
|
|
export function fallbackDays(projectId: string): Day[] {
|
|
const starts = ['09:30', '10:30', '13:00', '14:00', '18:00', '19:00', '20:00']
|
|
const base = new Date()
|
|
const days: Day[] = []
|
|
for (let i = 1; i <= 21; i += 1) {
|
|
const date = new Date(base.getFullYear(), base.getMonth(), base.getDate() + i)
|
|
const dateText = formatDate(date)
|
|
const dayId = scheduleDayId(projectId, dateText)
|
|
const slots: Slot[] = []
|
|
for (let idx = 0; idx < starts.length; idx += 1) {
|
|
const start = starts[idx]
|
|
slots.push({
|
|
id: slotId(dayId, start),
|
|
start_time: start,
|
|
status: 'available'
|
|
} as Slot)
|
|
}
|
|
days.push({
|
|
date: dateText,
|
|
status: 'available',
|
|
slots
|
|
} as Day)
|
|
}
|
|
return days
|
|
}
|
|
|
|
function scheduleDayId(projectId: string, dateText: string): string {
|
|
return 'day_' + projectId + '_' + compactDate(dateText)
|
|
}
|
|
|
|
function slotId(dayId: string, startTime: string): string {
|
|
return 'slot_' + dayId + '_' + startTime.replace(':', '')
|
|
}
|
|
|
|
function compactDate(dateText: string): string {
|
|
return dateText.split('-').join('')
|
|
}
|