Files
web-alterflixhub/app/components/admin/TransportEditor.tsx
T
2026-11-01 17:24:44 +07:00

117 lines
3.9 KiB
TypeScript

import { IconPlus, IconTrash } from "@tabler/icons-react";
import type { TransportMode } from "@/lib/cinemas";
import { DEFAULT_LOCALE, MESSAGES } from "@/lib/i18n/messages";
/**
* แก้ไขข้อมูลการเดินทางของโรงหนัง (รถยนต์ส่วนตัว / รถไฟฟ้า / รถประจำทาง)
* เรียงตามลำดับในอาร์เรย์ transport ของ content.json
*/
const ADMIN_MESSAGES = MESSAGES[DEFAULT_LOCALE];
const ICON_OPTIONS: Array<TransportMode["icon_name"]> = [
"private_car",
"metro",
"bus",
];
type TransportEditorProps = {
value: TransportMode[];
onChange: (next: TransportMode[]) => void;
inputClass: string;
labelClass: string;
};
const smallButtonClass =
"flex min-h-9 items-center gap-1 rounded-lg border border-[#303030] px-2 text-xs text-[#999] transition hover:border-[#A92626] hover:text-[#D47272]";
export function TransportEditor({
value,
onChange,
inputClass,
labelClass,
}: TransportEditorProps) {
function updateAt(index: number, patch: Partial<TransportMode>) {
onChange(value.map((item, i) => (i === index ? { ...item, ...patch } : item)));
}
return (
<div className="flex flex-col gap-2">
<span className="text-xs text-[#999]">การเดินทาง</span>
{value.length === 0 ? (
<span className="text-[11px] text-[#777]">
ยังไม่มีข้อมูล — หน้าเว็บจะซ่อนบล็อกการเดินทาง
</span>
) : null}
{value.map((route, index) => (
<div
key={index}
className="grid gap-2 rounded-lg border border-[#242424] p-2 sm:grid-cols-[130px_minmax(0,1fr)_minmax(0,1fr)_auto]"
>
<label className={labelClass}>
วิธีเดินทาง
<select
className={inputClass}
value={route.icon_name}
onChange={(event) =>
updateAt(index, {
icon_name: event.target.value as TransportMode["icon_name"],
})
}
>
{ICON_OPTIONS.map((icon) => (
<option key={icon} value={icon}>
{ADMIN_MESSAGES.transport[icon]}
</option>
))}
</select>
</label>
<label className={labelClass}>
รายละเอียด (TH)
<input
className={inputClass}
value={route.value}
placeholder="MRT สถานีสามย่าน"
onChange={(event) => updateAt(index, { value: event.target.value })}
/>
</label>
<label className={labelClass}>
รายละเอียด (EN)
<input
className={inputClass}
value={route.value_en ?? ""}
placeholder="MRT Sam Yan station"
onChange={(event) =>
updateAt(index, { value_en: event.target.value })
}
/>
</label>
<div className="flex items-end">
<button
type="button"
className={smallButtonClass}
aria-label={`ลบวิธีเดินทางลำดับที่ ${index + 1}`}
onClick={() => onChange(value.filter((_, i) => i !== index))}
>
<IconTrash size={14} />
</button>
</div>
</div>
))}
<button
type="button"
className={`${smallButtonClass} w-fit`}
onClick={() =>
onChange([...value, { icon_name: "bus", value: "", value_en: "" }])
}
>
<IconPlus size={14} />
เพิ่มวิธีเดินทาง
</button>
</div>
);
}