@@ -0,0 +1,196 @@
|
||||
<template>
|
||||
<div
|
||||
class="th-day-row"
|
||||
:class="{
|
||||
'th-day-row--disabled': !day.enabled,
|
||||
'th-day-row--compact': compact,
|
||||
}"
|
||||
>
|
||||
<div class="th-day-row__label">
|
||||
<span class="th-day-row__badge">{{ weekday.short }}</span>
|
||||
<span class="th-day-row__name">{{ weekday.label }}</span>
|
||||
</div>
|
||||
|
||||
<div v-if="!compact" class="th-day-row__toggle">
|
||||
<a-switch
|
||||
:checked="day.enabled"
|
||||
:disabled="disabled"
|
||||
checked-children="باز"
|
||||
un-checked-children="تعطیل"
|
||||
@change="(v) => emit('update', { enabled: !!v })"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div class="th-day-row__times">
|
||||
<TradingHoursTimeField
|
||||
:model-value="day.start"
|
||||
:disabled="disabled || !day.enabled"
|
||||
@update:model-value="(v) => emit('update', { start: v })"
|
||||
/>
|
||||
<span class="th-day-row__sep">تا</span>
|
||||
<TradingHoursTimeField
|
||||
:model-value="day.end"
|
||||
:disabled="disabled || !day.enabled"
|
||||
@update:model-value="(v) => emit('update', { end: v })"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div v-if="showBar" class="th-day-row__bar-wrap">
|
||||
<div class="th-day-row__bar">
|
||||
<div class="th-day-row__bar-fill" :style="barStyle" />
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { computed } from 'vue'
|
||||
import TradingHoursTimeField from './TradingHoursTimeField.vue'
|
||||
import type { DaySchedule } from '@/pages/menuPages/configuration/tradingHoursKeys'
|
||||
|
||||
const props = withDefaults(
|
||||
defineProps<{
|
||||
weekday: { key: string; label: string; short: string }
|
||||
day: DaySchedule
|
||||
disabled?: boolean
|
||||
compact?: boolean
|
||||
showBar?: boolean
|
||||
}>(),
|
||||
{
|
||||
disabled: false,
|
||||
compact: false,
|
||||
showBar: true,
|
||||
}
|
||||
)
|
||||
|
||||
const emit = defineEmits<{
|
||||
update: [patch: Partial<DaySchedule>]
|
||||
}>()
|
||||
|
||||
function timeToMinutes(raw: string): number {
|
||||
const parts = raw.split(':')
|
||||
const h = Number(parts[0] ?? 0)
|
||||
const m = Number(parts[1] ?? 0)
|
||||
return h * 60 + m
|
||||
}
|
||||
|
||||
const barStyle = computed(() => {
|
||||
if (!props.day.enabled) {
|
||||
return { left: '0%', width: '0%' }
|
||||
}
|
||||
const start = timeToMinutes(props.day.start)
|
||||
const end = timeToMinutes(props.day.end)
|
||||
const dayMinutes = 24 * 60
|
||||
const left = Math.max(0, Math.min(100, (start / dayMinutes) * 100))
|
||||
const width = Math.max(0, Math.min(100 - left, ((Math.max(end, start) - start) / dayMinutes) * 100))
|
||||
return {
|
||||
left: `${left}%`,
|
||||
width: `${width || 2}%`,
|
||||
}
|
||||
})
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
.th-day-row {
|
||||
display: grid;
|
||||
grid-template-columns: minmax(6.5rem, 8rem) minmax(0, 1fr);
|
||||
gap: 0.75rem 1rem;
|
||||
align-items: center;
|
||||
padding: 0.85rem 1rem;
|
||||
border: 1px solid var(--admin-border);
|
||||
border-radius: 12px;
|
||||
background: var(--admin-bg-elevated);
|
||||
}
|
||||
|
||||
.th-day-row--compact {
|
||||
grid-template-columns: minmax(5rem, 6rem) minmax(0, 1fr);
|
||||
}
|
||||
|
||||
.th-day-row--disabled {
|
||||
opacity: 0.72;
|
||||
}
|
||||
|
||||
.th-day-row__label {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 0.5rem;
|
||||
}
|
||||
|
||||
.th-day-row__badge {
|
||||
display: inline-flex;
|
||||
width: 1.75rem;
|
||||
height: 1.75rem;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
border-radius: 999px;
|
||||
font-size: 0.75rem;
|
||||
font-weight: 700;
|
||||
background: color-mix(in srgb, var(--admin-accent, #d4af37) 18%, transparent);
|
||||
color: var(--admin-text);
|
||||
}
|
||||
|
||||
.th-day-row__name {
|
||||
font-size: 0.875rem;
|
||||
font-weight: 600;
|
||||
color: var(--admin-text);
|
||||
}
|
||||
|
||||
.th-day-row__toggle {
|
||||
grid-column: 1 / -1;
|
||||
}
|
||||
|
||||
@media (min-width: 768px) {
|
||||
.th-day-row {
|
||||
grid-template-columns: minmax(7rem, 8.5rem) auto minmax(0, 1fr);
|
||||
grid-template-rows: auto auto;
|
||||
}
|
||||
|
||||
.th-day-row__toggle {
|
||||
grid-column: auto;
|
||||
}
|
||||
|
||||
.th-day-row__times {
|
||||
grid-column: 3;
|
||||
}
|
||||
|
||||
.th-day-row__bar-wrap {
|
||||
grid-column: 1 / -1;
|
||||
}
|
||||
}
|
||||
|
||||
.th-day-row__times {
|
||||
display: grid;
|
||||
grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr);
|
||||
gap: 0.5rem;
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
.th-day-row__sep {
|
||||
font-size: 0.75rem;
|
||||
color: var(--admin-text-secondary);
|
||||
}
|
||||
|
||||
.th-day-row__bar-wrap {
|
||||
grid-column: 1 / -1;
|
||||
}
|
||||
|
||||
.th-day-row__bar {
|
||||
position: relative;
|
||||
height: 0.35rem;
|
||||
border-radius: 999px;
|
||||
background: color-mix(in srgb, var(--admin-border) 65%, transparent);
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
.th-day-row__bar-fill {
|
||||
position: absolute;
|
||||
top: 0;
|
||||
bottom: 0;
|
||||
border-radius: 999px;
|
||||
background: linear-gradient(
|
||||
90deg,
|
||||
color-mix(in srgb, var(--admin-accent, #d4af37) 70%, #fff),
|
||||
var(--admin-accent, #d4af37)
|
||||
);
|
||||
}
|
||||
</style>
|
||||
@@ -0,0 +1,56 @@
|
||||
<template>
|
||||
<a-time-picker
|
||||
:value="pickerValue"
|
||||
format="HH:mm"
|
||||
value-format="HH:mm:ss"
|
||||
:minute-step="5"
|
||||
:disabled="disabled"
|
||||
placeholder="انتخاب ساعت"
|
||||
class="th-time-field w-full"
|
||||
@change="onChange"
|
||||
/>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { computed } from 'vue'
|
||||
import dayjs, { type Dayjs } from 'dayjs'
|
||||
import { normalizeTimeValue } from '@/pages/menuPages/configuration/settingTypes'
|
||||
|
||||
const props = withDefaults(
|
||||
defineProps<{
|
||||
modelValue?: string
|
||||
disabled?: boolean
|
||||
}>(),
|
||||
{
|
||||
modelValue: '',
|
||||
disabled: false,
|
||||
}
|
||||
)
|
||||
|
||||
const emit = defineEmits<{
|
||||
'update:modelValue': [value: string]
|
||||
}>()
|
||||
|
||||
const pickerValue = computed(() => {
|
||||
const normalized = normalizeTimeValue(props.modelValue)
|
||||
if (!normalized) return undefined
|
||||
const parsed = dayjs(normalized, 'HH:mm:ss', true)
|
||||
return parsed.isValid() ? parsed : undefined
|
||||
})
|
||||
|
||||
function onChange(val: Dayjs | string | null) {
|
||||
if (val == null || val === '') {
|
||||
emit('update:modelValue', '')
|
||||
return
|
||||
}
|
||||
const parsed = dayjs.isDayjs(val) ? val : dayjs(String(val), 'HH:mm:ss', true)
|
||||
emit('update:modelValue', parsed.isValid() ? normalizeTimeValue(parsed.format('HH:mm:ss')) : '')
|
||||
}
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
.th-time-field :deep(.ant-picker) {
|
||||
width: 100%;
|
||||
border-radius: 10px;
|
||||
}
|
||||
</style>
|
||||
@@ -0,0 +1,410 @@
|
||||
<template>
|
||||
<div class="th-page h-full min-h-0">
|
||||
<BasicShowContent title="ساعت کاری و نمایش قیمت" class="h-full">
|
||||
<header class="th-page__header">
|
||||
<p class="th-page__intro">
|
||||
ساعت باز و بسته بودن فروشگاه و رفتار نمایش قیمت در اپ مشتری
|
||||
</p>
|
||||
<AdminFieldHelp :text="tradingHoursPageIntro" size="sm" />
|
||||
<a-button class="th-page__refresh" :loading="loading" @click="load">بروزرسانی</a-button>
|
||||
</header>
|
||||
|
||||
<a-spin :spinning="loading">
|
||||
<div class="th-page__sections">
|
||||
<section class="th-card">
|
||||
<div class="th-card__head">
|
||||
<div class="th-card__title-row">
|
||||
<BaseIcon icon="vuesax-linear:clock" size="20" class="th-card__icon" />
|
||||
<div>
|
||||
<h2 class="th-card__title">ساعت کاری فروشگاه</h2>
|
||||
<p class="th-card__desc">کنترل باز و بسته بودن خرید و فروش در طول روز</p>
|
||||
</div>
|
||||
</div>
|
||||
<div v-if="isSaving('close_price')" class="th-card__status">در حال ذخیره…</div>
|
||||
<div v-else-if="isSavedFlash('close_price')" class="th-card__status th-card__status--ok">
|
||||
ذخیره شد
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="th-card__switch-row">
|
||||
<div class="th-card__switch-copy">
|
||||
<AdminLabelWithHelp
|
||||
:label="rowMeta('close_price').title"
|
||||
:help="rowMeta('close_price').help"
|
||||
/>
|
||||
</div>
|
||||
<a-switch
|
||||
:checked="closePriceEnabled"
|
||||
checked-children="روشن"
|
||||
un-checked-children="خاموش"
|
||||
@change="(v) => setSwitch('close_price', !!v)"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div class="th-card__mode" :class="{ 'th-card__mode--disabled': !closePriceEnabled }">
|
||||
<div class="th-card__mode-head">
|
||||
<span class="th-card__mode-label">نوع برنامه</span>
|
||||
<a-radio-group
|
||||
:value="weekly.mode"
|
||||
option-type="button"
|
||||
button-style="solid"
|
||||
:disabled="!closePriceEnabled"
|
||||
@update:value="setScheduleMode"
|
||||
>
|
||||
<a-radio-button value="uniform">یکسان برای همه روزها</a-radio-button>
|
||||
<a-radio-button value="per_day">ساعت جدا برای هر روز</a-radio-button>
|
||||
</a-radio-group>
|
||||
</div>
|
||||
|
||||
<div v-if="weekly.mode === 'uniform'" class="th-uniform">
|
||||
<div class="th-uniform__pickers">
|
||||
<label class="th-field">
|
||||
<span class="th-field__label">
|
||||
<AdminLabelWithHelp
|
||||
:label="rowMeta('startDay').title"
|
||||
:help="rowMeta('startDay').help"
|
||||
/>
|
||||
</span>
|
||||
<TradingHoursTimeField
|
||||
:model-value="uniformStart"
|
||||
:disabled="!closePriceEnabled"
|
||||
@update:model-value="(v) => setUniformTimes(v, uniformEnd)"
|
||||
/>
|
||||
</label>
|
||||
<label class="th-field">
|
||||
<span class="th-field__label">
|
||||
<AdminLabelWithHelp
|
||||
:label="rowMeta('endDay').title"
|
||||
:help="rowMeta('endDay').help"
|
||||
/>
|
||||
</span>
|
||||
<TradingHoursTimeField
|
||||
:model-value="uniformEnd"
|
||||
:disabled="!closePriceEnabled"
|
||||
@update:model-value="(v) => setUniformTimes(uniformStart, v)"
|
||||
/>
|
||||
</label>
|
||||
</div>
|
||||
|
||||
<div class="th-uniform__preview">
|
||||
<p class="th-uniform__preview-title">پیشنمایش هفته</p>
|
||||
<div class="th-uniform__days">
|
||||
<TradingHoursDayRow
|
||||
v-for="def in WEEKDAY_DEFS"
|
||||
:key="def.key"
|
||||
:weekday="def"
|
||||
:day="weekly.days[def.key]"
|
||||
compact
|
||||
disabled
|
||||
@update="() => {}"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div v-else class="th-weekly">
|
||||
<p class="th-weekly__hint">
|
||||
برای هر روز میتوانید ساعت جدا بگذارید یا آن روز را کاملاً تعطیل کنید. فعلاً
|
||||
اپ از کوچکترین بازهٔ ذخیرهشده برای سازگاری استفاده میکند؛ اعمال دقیق
|
||||
روزانه در سرور بهزودی فعال میشود.
|
||||
</p>
|
||||
<div class="th-weekly__list">
|
||||
<TradingHoursDayRow
|
||||
v-for="def in WEEKDAY_DEFS"
|
||||
:key="def.key"
|
||||
:weekday="def"
|
||||
:day="weekly.days[def.key]"
|
||||
:disabled="!closePriceEnabled"
|
||||
@update="(patch) => setDaySchedule(def.key, patch)"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<section class="th-card">
|
||||
<div class="th-card__head">
|
||||
<div class="th-card__title-row">
|
||||
<BaseIcon icon="vuesax-linear:eye" size="20" class="th-card__icon" />
|
||||
<div>
|
||||
<h2 class="th-card__title">نمایش قیمت</h2>
|
||||
<p class="th-card__desc">رفتار اپ وقتی قیمت زنده نیست یا فروشگاه بسته است</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="th-display-grid">
|
||||
<article
|
||||
v-for="item in displayItems"
|
||||
:key="item.key"
|
||||
class="th-display-item"
|
||||
>
|
||||
<div class="th-display-item__head">
|
||||
<AdminLabelWithHelp :label="item.title" :help="item.help" />
|
||||
<div v-if="isSaving(item.key)" class="th-display-item__status">…</div>
|
||||
<div
|
||||
v-else-if="isSavedFlash(item.key)"
|
||||
class="th-display-item__status th-display-item__status--ok"
|
||||
>
|
||||
✓
|
||||
</div>
|
||||
</div>
|
||||
<a-switch
|
||||
:checked="isSwitchOn(draftValue(item.key))"
|
||||
checked-children="روشن"
|
||||
un-checked-children="خاموش"
|
||||
@change="(v) => setSwitch(item.key, !!v)"
|
||||
/>
|
||||
</article>
|
||||
</div>
|
||||
</section>
|
||||
</div>
|
||||
</a-spin>
|
||||
</BasicShowContent>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { computed, onMounted } from 'vue'
|
||||
import BasicShowContent from '@/components/BasicShowContext.vue'
|
||||
import AdminFieldHelp from '@/components/AdminFieldHelp.vue'
|
||||
import AdminLabelWithHelp from '@/components/AdminLabelWithHelp.vue'
|
||||
import { tradingHoursPageIntro } from '@/utils/adminHelpCopy'
|
||||
import { isSwitchOn } from '@/pages/menuPages/configuration/settingTypes'
|
||||
import { WEEKDAY_DEFS } from '@/pages/menuPages/configuration/tradingHoursKeys'
|
||||
import TradingHoursDayRow from './components/TradingHoursDayRow.vue'
|
||||
import TradingHoursTimeField from './components/TradingHoursTimeField.vue'
|
||||
import { useTradingHoursPage } from './useTradingHoursPage'
|
||||
|
||||
const {
|
||||
loading,
|
||||
closePriceEnabled,
|
||||
weekly,
|
||||
uniformStart,
|
||||
uniformEnd,
|
||||
draftValue,
|
||||
rowMeta,
|
||||
load,
|
||||
setSwitch,
|
||||
setUniformTimes,
|
||||
setScheduleMode,
|
||||
setDaySchedule,
|
||||
isSaving,
|
||||
isSavedFlash,
|
||||
} = useTradingHoursPage()
|
||||
|
||||
const displayItems = computed(() =>
|
||||
(['show_last_price', 'show_home_price_when_closed', 'show_gold_ounce_price'] as const).map(
|
||||
(key) => ({
|
||||
key,
|
||||
title: rowMeta(key).title,
|
||||
help: rowMeta(key).help,
|
||||
})
|
||||
)
|
||||
)
|
||||
|
||||
onMounted(() => {
|
||||
void load()
|
||||
})
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
.th-page__header {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
align-items: center;
|
||||
gap: 0.75rem 1rem;
|
||||
margin-bottom: 1.25rem;
|
||||
}
|
||||
|
||||
.th-page__intro {
|
||||
margin: 0;
|
||||
font-size: 0.875rem;
|
||||
color: var(--admin-text-secondary);
|
||||
}
|
||||
|
||||
.th-page__refresh {
|
||||
margin-inline-start: auto;
|
||||
}
|
||||
|
||||
.th-page__sections {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 1.25rem;
|
||||
padding-bottom: 2rem;
|
||||
}
|
||||
|
||||
.th-card {
|
||||
border: 1px solid var(--admin-border);
|
||||
border-radius: 14px;
|
||||
background: var(--admin-bg-muted);
|
||||
padding: 1rem 1rem 1.25rem;
|
||||
}
|
||||
|
||||
@media (min-width: 768px) {
|
||||
.th-card {
|
||||
padding: 1.25rem 1.5rem 1.5rem;
|
||||
}
|
||||
}
|
||||
|
||||
.th-card__head {
|
||||
display: flex;
|
||||
align-items: flex-start;
|
||||
justify-content: space-between;
|
||||
gap: 1rem;
|
||||
margin-bottom: 1rem;
|
||||
}
|
||||
|
||||
.th-card__title-row {
|
||||
display: flex;
|
||||
align-items: flex-start;
|
||||
gap: 0.75rem;
|
||||
}
|
||||
|
||||
.th-card__icon {
|
||||
color: var(--admin-accent, #d4af37);
|
||||
margin-top: 0.15rem;
|
||||
}
|
||||
|
||||
.th-card__title {
|
||||
margin: 0;
|
||||
font-size: 1rem;
|
||||
font-weight: 700;
|
||||
color: var(--admin-text);
|
||||
}
|
||||
|
||||
.th-card__desc {
|
||||
margin: 0.25rem 0 0;
|
||||
font-size: 0.8125rem;
|
||||
color: var(--admin-text-secondary);
|
||||
}
|
||||
|
||||
.th-card__status {
|
||||
font-size: 0.75rem;
|
||||
color: var(--admin-text-secondary);
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
.th-card__status--ok {
|
||||
color: #52c41a;
|
||||
}
|
||||
|
||||
.th-card__switch-row {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
gap: 1rem;
|
||||
padding: 0.85rem 1rem;
|
||||
border: 1px solid var(--admin-border);
|
||||
border-radius: 12px;
|
||||
background: var(--admin-bg-elevated);
|
||||
margin-bottom: 1rem;
|
||||
}
|
||||
|
||||
.th-card__switch-copy {
|
||||
min-width: 0;
|
||||
}
|
||||
|
||||
.th-card__mode--disabled {
|
||||
opacity: 0.55;
|
||||
pointer-events: none;
|
||||
}
|
||||
|
||||
.th-card__mode-head {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 0.75rem;
|
||||
margin-bottom: 1rem;
|
||||
}
|
||||
|
||||
@media (min-width: 768px) {
|
||||
.th-card__mode-head {
|
||||
flex-direction: row;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
}
|
||||
}
|
||||
|
||||
.th-card__mode-label {
|
||||
font-size: 0.875rem;
|
||||
font-weight: 600;
|
||||
color: var(--admin-text);
|
||||
}
|
||||
|
||||
.th-uniform__pickers {
|
||||
display: grid;
|
||||
grid-template-columns: 1fr;
|
||||
gap: 0.75rem;
|
||||
margin-bottom: 1rem;
|
||||
}
|
||||
|
||||
@media (min-width: 768px) {
|
||||
.th-uniform__pickers {
|
||||
grid-template-columns: repeat(2, minmax(0, 1fr));
|
||||
}
|
||||
}
|
||||
|
||||
.th-field {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 0.45rem;
|
||||
}
|
||||
|
||||
.th-field__label {
|
||||
font-size: 0.8125rem;
|
||||
}
|
||||
|
||||
.th-uniform__preview-title,
|
||||
.th-weekly__hint {
|
||||
margin: 0 0 0.75rem;
|
||||
font-size: 0.8125rem;
|
||||
color: var(--admin-text-secondary);
|
||||
}
|
||||
|
||||
.th-uniform__days,
|
||||
.th-weekly__list {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 0.65rem;
|
||||
}
|
||||
|
||||
.th-display-grid {
|
||||
display: grid;
|
||||
grid-template-columns: 1fr;
|
||||
gap: 0.75rem;
|
||||
}
|
||||
|
||||
@media (min-width: 768px) {
|
||||
.th-display-grid {
|
||||
grid-template-columns: repeat(3, minmax(0, 1fr));
|
||||
}
|
||||
}
|
||||
|
||||
.th-display-item {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 0.85rem;
|
||||
padding: 0.9rem 1rem;
|
||||
border: 1px solid var(--admin-border);
|
||||
border-radius: 12px;
|
||||
background: var(--admin-bg-elevated);
|
||||
min-height: 7.5rem;
|
||||
}
|
||||
|
||||
.th-display-item__head {
|
||||
display: flex;
|
||||
align-items: flex-start;
|
||||
justify-content: space-between;
|
||||
gap: 0.5rem;
|
||||
min-height: 2.5rem;
|
||||
}
|
||||
|
||||
.th-display-item__status {
|
||||
font-size: 0.75rem;
|
||||
color: var(--admin-text-secondary);
|
||||
}
|
||||
|
||||
.th-display-item__status--ok {
|
||||
color: #52c41a;
|
||||
}
|
||||
</style>
|
||||
@@ -0,0 +1,277 @@
|
||||
import { debounce } from 'lodash'
|
||||
import { computed, ref } from 'vue'
|
||||
import type { Const } from '@/model/configuration'
|
||||
import { getEditableConfiguration, setConfiguration } from '@/api/configuration'
|
||||
import {
|
||||
formatValueForSave,
|
||||
isSwitchOn,
|
||||
normalizeTimeValue,
|
||||
resolveSettingFieldType,
|
||||
resolveSettingTitle,
|
||||
helpText,
|
||||
} from '@/pages/menuPages/configuration/settingTypes'
|
||||
import {
|
||||
DEFAULT_DAY_END,
|
||||
DEFAULT_DAY_START,
|
||||
TRADING_HOURS_SETTING_KEY_SET,
|
||||
WEEKDAY_DEFS,
|
||||
defaultWeeklySchedule,
|
||||
type DaySchedule,
|
||||
type WeekdayKey,
|
||||
type WeeklyScheduleDraft,
|
||||
} from '@/pages/menuPages/configuration/tradingHoursKeys'
|
||||
|
||||
function parseWeeklyJson(raw?: string | null): WeeklyScheduleDraft | null {
|
||||
if (!raw?.trim()) return null
|
||||
try {
|
||||
const parsed = JSON.parse(raw) as Partial<WeeklyScheduleDraft>
|
||||
if (parsed.mode !== 'uniform' && parsed.mode !== 'per_day') return null
|
||||
if (!parsed.days || typeof parsed.days !== 'object') return null
|
||||
const base = defaultWeeklySchedule()
|
||||
for (const def of WEEKDAY_DEFS) {
|
||||
const row = (parsed.days as Record<string, Partial<DaySchedule>>)[def.key]
|
||||
if (!row) continue
|
||||
base.days[def.key] = {
|
||||
enabled: row.enabled !== false,
|
||||
start: normalizeTimeValue(row.start || base.days[def.key].start) || DEFAULT_DAY_START,
|
||||
end: normalizeTimeValue(row.end || base.days[def.key].end) || DEFAULT_DAY_END,
|
||||
}
|
||||
}
|
||||
base.mode = parsed.mode
|
||||
return base
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
function serializeWeekly(draft: WeeklyScheduleDraft): string {
|
||||
return JSON.stringify(draft)
|
||||
}
|
||||
|
||||
function applyUniformTimes(draft: WeeklyScheduleDraft, start: string, end: string) {
|
||||
for (const def of WEEKDAY_DEFS) {
|
||||
draft.days[def.key] = {
|
||||
...draft.days[def.key],
|
||||
start,
|
||||
end,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function useTradingHoursPage() {
|
||||
const loading = ref(true)
|
||||
const rowsByKey = ref<Record<string, Const>>({})
|
||||
const drafts = ref<Record<string, string>>({})
|
||||
const savingKeys = ref<Set<string>>(new Set())
|
||||
const savedFlashKeys = ref<Set<string>>(new Set())
|
||||
const weekly = ref<WeeklyScheduleDraft>(defaultWeeklySchedule())
|
||||
const uniformStart = ref(DEFAULT_DAY_START)
|
||||
const uniformEnd = ref(DEFAULT_DAY_END)
|
||||
|
||||
const debouncedSave = new Map<string, ReturnType<typeof debounce>>()
|
||||
const saveTimers = new Map<string, ReturnType<typeof setTimeout>>()
|
||||
|
||||
const closePriceEnabled = computed(() => isSwitchOn(draftValue('close_price')))
|
||||
|
||||
function draftValue(key: string): string {
|
||||
const row = rowsByKey.value[key]
|
||||
if (!row?.id) return ''
|
||||
return drafts.value[String(row.id)] ?? row.value ?? ''
|
||||
}
|
||||
|
||||
function rowMeta(key: string) {
|
||||
const row = rowsByKey.value[key]
|
||||
return {
|
||||
title: row ? resolveSettingTitle(row) : key,
|
||||
help: row ? helpText(row) : '',
|
||||
exists: Boolean(row?.id),
|
||||
}
|
||||
}
|
||||
|
||||
function hydrateWeeklyFromRows() {
|
||||
const start = normalizeTimeValue(draftValue('startDay') || DEFAULT_DAY_START) || DEFAULT_DAY_START
|
||||
const end = normalizeTimeValue(draftValue('endDay') || DEFAULT_DAY_END) || DEFAULT_DAY_END
|
||||
uniformStart.value = start
|
||||
uniformEnd.value = end
|
||||
|
||||
const fromJson = parseWeeklyJson(draftValue('trading_hours_by_day'))
|
||||
if (fromJson) {
|
||||
weekly.value = fromJson
|
||||
return
|
||||
}
|
||||
|
||||
weekly.value = defaultWeeklySchedule(start, end)
|
||||
}
|
||||
|
||||
async function load() {
|
||||
loading.value = true
|
||||
try {
|
||||
const rows = await getEditableConfiguration()
|
||||
const map: Record<string, Const> = {}
|
||||
const draftMap: Record<string, string> = {}
|
||||
for (const row of rows || []) {
|
||||
if (!row.key || !TRADING_HOURS_SETTING_KEY_SET.has(row.key)) continue
|
||||
map[row.key] = row
|
||||
if (row.id != null) draftMap[String(row.id)] = row.value ?? ''
|
||||
}
|
||||
rowsByKey.value = map
|
||||
drafts.value = draftMap
|
||||
hydrateWeeklyFromRows()
|
||||
} finally {
|
||||
loading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
function markSavedFlash(key: string) {
|
||||
savedFlashKeys.value.add(key)
|
||||
if (saveTimers.has(key)) clearTimeout(saveTimers.get(key))
|
||||
saveTimers.set(
|
||||
key,
|
||||
setTimeout(() => {
|
||||
savedFlashKeys.value.delete(key)
|
||||
}, 1800)
|
||||
)
|
||||
}
|
||||
|
||||
async function persistKey(key: string, rawValue: string) {
|
||||
const row = rowsByKey.value[key]
|
||||
const type = resolveSettingFieldType(row ?? { key, type: 'text' })
|
||||
const value = formatValueForSave(rawValue, type, key)
|
||||
|
||||
if (row?.id) {
|
||||
const current = row.value ?? ''
|
||||
if (value === current && drafts.value[String(row.id)] === value) return
|
||||
}
|
||||
|
||||
savingKeys.value.add(key)
|
||||
savedFlashKeys.value.delete(key)
|
||||
|
||||
try {
|
||||
const payload: Const = row?.id
|
||||
? {
|
||||
id: row.id,
|
||||
key,
|
||||
value,
|
||||
description: row.description ?? '',
|
||||
is_active: true,
|
||||
title_fa: row.title_fa ?? '',
|
||||
type,
|
||||
}
|
||||
: {
|
||||
key,
|
||||
value,
|
||||
description: '',
|
||||
is_active: true,
|
||||
title_fa: key === 'trading_hours_by_day' ? 'برنامه هفتگی ساعت کاری' : '',
|
||||
type: key === 'trading_hours_by_day' ? 'longtext' : 'text',
|
||||
}
|
||||
|
||||
const saved = await setConfiguration(payload)
|
||||
rowsByKey.value[key] = { ...rowsByKey.value[key], ...saved, key }
|
||||
if (saved.id != null) {
|
||||
drafts.value[String(saved.id)] = value
|
||||
}
|
||||
markSavedFlash(key)
|
||||
} finally {
|
||||
savingKeys.value.delete(key)
|
||||
}
|
||||
}
|
||||
|
||||
function schedulePersist(key: string, value: string, options?: { flush?: boolean }) {
|
||||
if (!debouncedSave.has(key)) {
|
||||
debouncedSave.set(
|
||||
key,
|
||||
debounce((nextValue: string) => {
|
||||
void persistKey(key, nextValue)
|
||||
}, key === 'trading_hours_by_day' ? 500 : 350)
|
||||
)
|
||||
}
|
||||
const fn = debouncedSave.get(key)!
|
||||
if (options?.flush) {
|
||||
fn.cancel()
|
||||
void persistKey(key, value)
|
||||
return
|
||||
}
|
||||
fn(value)
|
||||
}
|
||||
|
||||
function setDraftByKey(key: string, value: string, options?: { flush?: boolean }) {
|
||||
const row = rowsByKey.value[key]
|
||||
if (row?.id != null) {
|
||||
drafts.value[String(row.id)] = value
|
||||
}
|
||||
schedulePersist(key, value, options)
|
||||
}
|
||||
|
||||
function setSwitch(key: string, checked: boolean) {
|
||||
setDraftByKey(key, checked ? 'true' : 'false', { flush: true })
|
||||
}
|
||||
|
||||
function setUniformTimes(start: string, end: string) {
|
||||
const normalizedStart = normalizeTimeValue(start) || DEFAULT_DAY_START
|
||||
const normalizedEnd = normalizeTimeValue(end) || DEFAULT_DAY_END
|
||||
uniformStart.value = normalizedStart
|
||||
uniformEnd.value = normalizedEnd
|
||||
applyUniformTimes(weekly.value, normalizedStart, normalizedEnd)
|
||||
weekly.value.mode = 'uniform'
|
||||
setDraftByKey('startDay', normalizedStart, { flush: true })
|
||||
setDraftByKey('endDay', normalizedEnd, { flush: true })
|
||||
setDraftByKey('trading_hours_by_day', serializeWeekly(weekly.value), { flush: true })
|
||||
}
|
||||
|
||||
function setScheduleMode(mode: 'uniform' | 'per_day') {
|
||||
weekly.value.mode = mode
|
||||
if (mode === 'uniform') {
|
||||
applyUniformTimes(weekly.value, uniformStart.value, uniformEnd.value)
|
||||
}
|
||||
setDraftByKey('trading_hours_by_day', serializeWeekly(weekly.value), { flush: true })
|
||||
}
|
||||
|
||||
function syncLegacyBoundsFromWeekly() {
|
||||
const enabled = WEEKDAY_DEFS.map((def) => weekly.value.days[def.key]).filter((day) => day.enabled)
|
||||
if (!enabled.length) return
|
||||
const starts = [...enabled].sort((a, b) => a.start.localeCompare(b.start))
|
||||
const ends = [...enabled].sort((a, b) => a.end.localeCompare(b.end))
|
||||
uniformStart.value = starts[0].start
|
||||
uniformEnd.value = ends[ends.length - 1].end
|
||||
setDraftByKey('startDay', uniformStart.value, { flush: true })
|
||||
setDraftByKey('endDay', uniformEnd.value, { flush: true })
|
||||
}
|
||||
|
||||
function setDaySchedule(day: WeekdayKey, patch: Partial<DaySchedule>) {
|
||||
weekly.value.days[day] = {
|
||||
...weekly.value.days[day],
|
||||
...patch,
|
||||
start: normalizeTimeValue(patch.start ?? weekly.value.days[day].start) || DEFAULT_DAY_START,
|
||||
end: normalizeTimeValue(patch.end ?? weekly.value.days[day].end) || DEFAULT_DAY_END,
|
||||
}
|
||||
weekly.value.mode = 'per_day'
|
||||
setDraftByKey('trading_hours_by_day', serializeWeekly(weekly.value))
|
||||
syncLegacyBoundsFromWeekly()
|
||||
}
|
||||
|
||||
function isSaving(key: string) {
|
||||
return savingKeys.value.has(key)
|
||||
}
|
||||
|
||||
function isSavedFlash(key: string) {
|
||||
return savedFlashKeys.value.has(key)
|
||||
}
|
||||
|
||||
return {
|
||||
loading,
|
||||
closePriceEnabled,
|
||||
weekly,
|
||||
uniformStart,
|
||||
uniformEnd,
|
||||
draftValue,
|
||||
rowMeta,
|
||||
load,
|
||||
setSwitch,
|
||||
setUniformTimes,
|
||||
setScheduleMode,
|
||||
setDaySchedule,
|
||||
isSaving,
|
||||
isSavedFlash,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user