дату помним в localstorage
This commit is contained in:
@@ -29,8 +29,28 @@ import {
|
|||||||
VerificationConditionsValue,
|
VerificationConditionsValue,
|
||||||
} from './types'
|
} from './types'
|
||||||
|
|
||||||
|
// Утилиты для работы с localStorage
|
||||||
|
const getLocalStorageKey = (type: string, id: string) => `${type}_${id}`
|
||||||
|
|
||||||
|
const saveToLocalStorage = (key: string, value: string) => {
|
||||||
|
try {
|
||||||
|
localStorage.setItem(key, value)
|
||||||
|
} catch (error) {
|
||||||
|
console.warn('Failed to save to localStorage:', error)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const getFromLocalStorage = (key: string): string => {
|
||||||
|
try {
|
||||||
|
return localStorage.getItem(key) || ''
|
||||||
|
} catch (error) {
|
||||||
|
console.warn('Failed to get from localStorage:', error)
|
||||||
|
return ''
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
interface VerificationConditionsRenderProps {
|
interface VerificationConditionsRenderProps {
|
||||||
config: VerificationConditionsConfig
|
config: VerificationConditionsConfig & { id?: string }
|
||||||
value?: VerificationConditionsValue
|
value?: VerificationConditionsValue
|
||||||
onChange?: (value: VerificationConditionsValue) => void
|
onChange?: (value: VerificationConditionsValue) => void
|
||||||
onSave?: (value: VerificationConditionsValue) => Promise<void>
|
onSave?: (value: VerificationConditionsValue) => Promise<void>
|
||||||
@@ -42,9 +62,22 @@ export function VerificationConditionsRender({
|
|||||||
onChange,
|
onChange,
|
||||||
onSave,
|
onSave,
|
||||||
}: VerificationConditionsRenderProps) {
|
}: VerificationConditionsRenderProps) {
|
||||||
const [selectedDate, setSelectedDate] = useState(
|
// Инициализация даты с учетом localStorage
|
||||||
value?.date || config.selectedDate || new Date().toISOString().split('T')[0]
|
const getInitialDate = () => {
|
||||||
)
|
if (value?.date) return value.date
|
||||||
|
if (config.selectedDate) return config.selectedDate
|
||||||
|
|
||||||
|
// Попытка загрузить из localStorage
|
||||||
|
if (config.id) {
|
||||||
|
const key = getLocalStorageKey('verification_conditions_date', config.id)
|
||||||
|
const savedDate = getFromLocalStorage(key)
|
||||||
|
if (savedDate) return savedDate
|
||||||
|
}
|
||||||
|
|
||||||
|
return new Date().toISOString().split('T')[0]
|
||||||
|
}
|
||||||
|
|
||||||
|
const [selectedDate, setSelectedDate] = useState(getInitialDate)
|
||||||
const [editDialogOpen, setEditDialogOpen] = useState(false)
|
const [editDialogOpen, setEditDialogOpen] = useState(false)
|
||||||
const [editingConditions, setEditingConditions] = useState<
|
const [editingConditions, setEditingConditions] = useState<
|
||||||
Record<string, string>
|
Record<string, string>
|
||||||
@@ -101,6 +134,12 @@ export function VerificationConditionsRender({
|
|||||||
const newDate = event.target.value
|
const newDate = event.target.value
|
||||||
setSelectedDate(newDate)
|
setSelectedDate(newDate)
|
||||||
|
|
||||||
|
// Сохранение в localStorage
|
||||||
|
if (config.id) {
|
||||||
|
const key = getLocalStorageKey('verification_conditions_date', config.id)
|
||||||
|
saveToLocalStorage(key, newDate)
|
||||||
|
}
|
||||||
|
|
||||||
// Обновляем значение даты в value
|
// Обновляем значение даты в value
|
||||||
if (onChange) {
|
if (onChange) {
|
||||||
onChange({
|
onChange({
|
||||||
|
|||||||
@@ -58,6 +58,7 @@ export const verificationConditionsElementDefinition: ElementDefinition<
|
|||||||
conditionMappings: [],
|
conditionMappings: [],
|
||||||
showUnits: true,
|
showUnits: true,
|
||||||
targetCells: [],
|
targetCells: [],
|
||||||
|
id: '',
|
||||||
},
|
},
|
||||||
|
|
||||||
// Компоненты
|
// Компоненты
|
||||||
@@ -65,10 +66,26 @@ export const verificationConditionsElementDefinition: ElementDefinition<
|
|||||||
Preview: VerificationConditionsPreview,
|
Preview: VerificationConditionsPreview,
|
||||||
Render: VerificationConditionsRender,
|
Render: VerificationConditionsRender,
|
||||||
|
|
||||||
// Получение начальных значений из конфигурации
|
// Получение начальных значений из конфигурации с учетом localStorage
|
||||||
getInitialValue: config => {
|
getInitialValue: config => {
|
||||||
|
let initialDate =
|
||||||
|
config.selectedDate || new Date().toISOString().split('T')[0]
|
||||||
|
|
||||||
|
// Попытка загрузить дату из localStorage
|
||||||
|
if (config.id) {
|
||||||
|
try {
|
||||||
|
const key = `verification_conditions_date_${config.id}`
|
||||||
|
const savedDate = localStorage.getItem(key)
|
||||||
|
if (savedDate) {
|
||||||
|
initialDate = savedDate
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.warn('Failed to get date from localStorage:', error)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
date: config.selectedDate || new Date().toISOString().split('T')[0],
|
date: initialDate,
|
||||||
conditions: {},
|
conditions: {},
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -19,6 +19,8 @@ export interface VerificationConditionsConfig {
|
|||||||
showUnits?: boolean
|
showUnits?: boolean
|
||||||
// Целевые ячейки
|
// Целевые ячейки
|
||||||
targetCells?: any[]
|
targetCells?: any[]
|
||||||
|
// Идентификатор для совместимости с TemplateElement
|
||||||
|
id?: string
|
||||||
}
|
}
|
||||||
|
|
||||||
// Значение элемента
|
// Значение элемента
|
||||||
|
|||||||
Reference in New Issue
Block a user