Files
hnh-map/frontend-nuxt/lib/UniqueList.ts
Nikolay Tatarinov fd624c2357 Refactor frontend components for improved functionality and accessibility
- Consolidated global error handling in app.vue to redirect users to the login page on API authentication failure.
- Enhanced MapView component by reintroducing event listeners for selected map and marker updates, improving interactivity.
- Updated PasswordInput and various modal components to ensure proper input handling and accessibility compliance.
- Refactored MapControls and MapControlsContent to streamline prop management and enhance user experience.
- Improved error handling in local storage operations within useMapBookmarks and useRecentLocations composables.
- Standardized input elements across forms for consistency in user interaction.
2026-03-04 14:06:27 +03:00

56 lines
1.8 KiB
TypeScript

export interface Identifiable {
id: number | string
}
export interface UniqueList<T extends Identifiable> {
elements: Record<string, T>
}
export function createUniqueList<T extends Identifiable>(): UniqueList<T> {
return { elements: {} }
}
export function uniqueListUpdate<T extends Identifiable>(
list: UniqueList<T>,
dataList: T[],
addCallback?: (it: T) => void,
removeCallback?: (it: T) => void,
updateCallback?: (oldElement: T, newElement: T) => void
): void {
const elementsToAdd = dataList.filter((it) => list.elements[String(it.id)] === undefined)
const elementsToRemove: T[] = []
for (const id of Object.keys(list.elements)) {
if (dataList.find((up) => String(up.id) === id) === undefined) {
const el = list.elements[id]
if (el) elementsToRemove.push(el)
}
}
if (removeCallback) {
elementsToRemove.forEach((it) => removeCallback(it))
}
if (updateCallback) {
dataList.forEach((newElement) => {
const oldElement = list.elements[String(newElement.id)]
if (oldElement) {
updateCallback(oldElement, newElement)
}
})
}
if (addCallback) {
elementsToAdd.forEach((it) => addCallback(it))
}
const toRemove = new Set(elementsToRemove.map((it) => String(it.id)))
list.elements = Object.fromEntries(
Object.entries(list.elements).filter(([id]) => !toRemove.has(id))
) as Record<string, T>
elementsToAdd.forEach((it) => (list.elements[String(it.id)] = it))
}
export function uniqueListGetElements<T extends Identifiable>(list: UniqueList<T>): T[] {
return Object.values(list.elements)
}
export function uniqueListById<T extends Identifiable>(list: UniqueList<T>, id: number | string): T | undefined {
return list.elements[String(id)]
}