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

114 lines
3.9 KiB
TypeScript

import { IconPlus, IconTrash } from "@tabler/icons-react";
import type { ContactChannel } from "@/lib/cinemas";
/**
* แก้ช่องทางติดต่อของการ์ด "สอบถามข้อมูลและจองตั๋ว" ต่อโรง
* แต่ละแถว = label / ค่าที่แสดง / ลิงก์ (ถ้าไม่มีลิงก์ หน้าเว็บแสดงเป็นข้อความธรรมดา)
*/
const SUGGESTED_LABELS = [
"Website",
"Facebook",
"Instagram",
"Line",
"Call",
"อีเมล",
"จองตั๋ว",
];
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]";
type Props = {
contacts: ContactChannel[];
onChange: (next: ContactChannel[]) => void;
inputClass: string;
labelClass: string;
};
export function ContactsEditor({ contacts, onChange, inputClass, labelClass }: Props) {
function updateAt(index: number, patch: Partial<ContactChannel>) {
onChange(
contacts.map((item, i) => (i === index ? { ...item, ...patch } : item)),
);
}
return (
<div className="flex flex-col gap-2">
<span className="text-xs text-[#999]">ช่องทางติดต่อ / จองตั๋ว</span>
{contacts.length === 0 ? (
<span className="text-[11px] text-[#777]">
ยังไม่มีช่องทางติดต่อ — หน้าเว็บจะขึ้น “ยังไม่ระบุช่องทางติดต่อ”
</span>
) : null}
{contacts.map((contact, 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}>
ป้ายชื่อ
<input
className={inputClass}
value={contact.label}
placeholder="Website"
list="contact-label-options"
onChange={(event) => updateAt(index, { label: event.target.value })}
/>
</label>
<label className={labelClass}>
ข้อความที่แสดง
<input
className={inputClass}
value={contact.value}
placeholder="www.housesamyan.com"
onChange={(event) => updateAt(index, { value: event.target.value })}
/>
</label>
<label className={labelClass}>
ลิงก์ (ไม่บังคับ)
<input
className={inputClass}
value={contact.href ?? ""}
placeholder="https://… หรือ tel:+66…"
onChange={(event) =>
updateAt(index, {
href: event.target.value.trim() === "" ? null : event.target.value,
})
}
/>
</label>
<div className="flex items-end">
<button
type="button"
className={smallButtonClass}
aria-label={`ลบช่องทางที่ ${index + 1}`}
onClick={() => onChange(contacts.filter((_, i) => i !== index))}
>
<IconTrash size={14} />
</button>
</div>
</div>
))}
<datalist id="contact-label-options">
{SUGGESTED_LABELS.map((label) => (
<option key={label} value={label} />
))}
</datalist>
<button
type="button"
className={`${smallButtonClass} w-fit`}
onClick={() => onChange([...contacts, { label: "", value: "", href: null }])}
>
<IconPlus size={14} />
เพิ่มช่องทางติดต่อ
</button>
</div>
);
}