mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-10-21 06:26:26 +00:00
8fd46c4c18
Small requests from the discussions, each built end to end: planner and map (cycling mode, date first headings, clear a day, stay times per day, off-route stops, map lock, compact unplanned markers, pending stays, cable car, transit quick picks), places (location filter, category from OSM, open in maps, geo: link, image from an attachment, enrichment on file import, places near a pin, place name language, Amap first in China), lists and costs (packed counter, inherited sharing, Markdown to-dos, payment notes, bookings CSV, my plan PDF), sharing (unplanned places, travel only, without photos), journey (photo zoom, drafts, photo order, trip suggestion, day jump), atlas (region counter, region bucket list, places per country), vacay half company holidays, studio copy and paste plus the book's own layouts, and RESTORE_FROM_BACKUP for a first start restore.
54 lines
2.1 KiB
TypeScript
54 lines
2.1 KiB
TypeScript
import { useState, type DragEvent } from 'react'
|
|
import { journeyApi } from '../../api/client'
|
|
import { getApiErrorMessage } from '../../types'
|
|
import { useToast } from '../shared/Toast'
|
|
import { useTranslation } from '../../i18n'
|
|
|
|
/** The list with the item at `from` taken out and put back at `to`. */
|
|
export function movedTo<T>(list: T[], from: number, to: number): T[] {
|
|
if (from === to || from < 0 || from >= list.length) return list
|
|
const next = [...list]
|
|
const [item] = next.splice(from, 1)
|
|
next.splice(Math.max(0, Math.min(to, next.length)), 0, item)
|
|
return next
|
|
}
|
|
|
|
/**
|
|
* The order of an entry's photos (#824): drag one onto another's place, or send
|
|
* one to the front. One request for the whole order; the order is shared with the
|
|
* other members, the share page and the PDF, so a refused write goes back.
|
|
*/
|
|
export function useEntryPhotoOrder<T extends { id: number }>(entryId: number, photos: T[], setPhotos: (next: T[]) => void) {
|
|
const { t } = useTranslation()
|
|
const toast = useToast()
|
|
const [dragIndex, setDragIndex] = useState<number | null>(null)
|
|
const [overIndex, setOverIndex] = useState<number | null>(null)
|
|
|
|
const commit = (next: T[]) => {
|
|
if (next === photos) return
|
|
const before = photos
|
|
setPhotos(next)
|
|
journeyApi.reorderEntryPhotos(entryId, next.map(p => p.id)).catch((err: unknown) => {
|
|
toast.error(getApiErrorMessage(err, t('common.error')))
|
|
setPhotos(before)
|
|
})
|
|
}
|
|
|
|
const reset = () => { setDragIndex(null); setOverIndex(null) }
|
|
|
|
const dragProps = (index: number) => ({
|
|
draggable: true,
|
|
onDragStart: (e: DragEvent) => { e.dataTransfer.effectAllowed = 'move'; setDragIndex(index) },
|
|
onDragOver: (e: DragEvent) => { if (dragIndex === null) return; e.preventDefault(); e.dataTransfer.dropEffect = 'move'; setOverIndex(index) },
|
|
onDrop: (e: DragEvent) => { e.preventDefault(); if (dragIndex !== null) commit(movedTo(photos, dragIndex, index)); reset() },
|
|
onDragEnd: reset,
|
|
})
|
|
|
|
return {
|
|
makeFirst: (index: number) => commit(movedTo(photos, index, 0)),
|
|
dragProps,
|
|
dragIndex,
|
|
overIndex,
|
|
}
|
|
}
|