feat(admin): รองรับตัวกรองประเภทโรงหนังเต็มรูปแบบ
- ลิสต์โรงหนัง: ช่องค้นหา (ไทย/อังกฤษ/slug) + ชิปกรองประเภทพร้อมจำนวน (ทั้งหมด/ท้องถิ่น/อินดี้/กลุ่ม/ยังไม่จัด) - ป้ายประเภทท้ายชื่อทุกแถว (ยังไม่จัด = ป้ายแดง) + empty state - ตั้งประเภททีละกลุ่ม: "ตั้งประเภทให้ N โรงที่กรองอยู่" → เลือกประเภท → ใช้ (แล้วกดบันทึกทั้งหมด) - หัวหลังบ้านสรุปจำนวนต่อประเภท - แท็บคำศัพท์: ปุ่มลัดสร้าง key `venues.<ประเภท>` สำหรับแปลชื่อตัวกรอง - ปิดลากสลับลำดับขณะกรอง + hint อธิบาย - logic ย้ายไป lib: filterAdminCinemas / setVenueForSlugs / venueCounts + unit tests (169 ผ่าน) - DESIGN.md / docs/admin-guide.md อัปเดต
This commit is contained in:
1 parent
1932098a1d
commit
1cf1398ba4
6 files changed
+377
-16
No files matched your search
@@ -6,6 +6,9 @@ import {
|
||||
hasVenue,
|
||||
listOtherCinemas,
|
||||
type Cinema,
|
||||
filterAdminCinemas,
|
||||
setVenueForSlugs,
|
||||
venueCounts,
|
||||
} from "./cinemas";
|
||||
import { filterCinemas } from "./search";
|
||||
|
||||
@@ -98,3 +101,87 @@ describe("hasVenue / filterCinemas (ชั้นกรองประเภท
|
||||
).toEqual([local]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("หลังบ้าน: ตัวกรองประเภทโรงหนัง", () => {
|
||||
const base = {
|
||||
slug: "base",
|
||||
name: "X",
|
||||
tags: [],
|
||||
location_label: "",
|
||||
coordinates: null,
|
||||
description: "",
|
||||
map_url: null,
|
||||
address_lines: [],
|
||||
transport: [],
|
||||
contacts: [],
|
||||
payments: [],
|
||||
photos: [],
|
||||
} as Cinema;
|
||||
const list: Cinema[] = [
|
||||
{
|
||||
...base,
|
||||
slug: "a-local",
|
||||
name: "Alpha",
|
||||
name_th: "อัลฟา",
|
||||
venue_type: "โรงภาพยนตร์ท้องถิ่น",
|
||||
},
|
||||
{ ...base, slug: "b-indie", name: "Beta", venue_type: "โรงภาพยนตร์อินดี้" },
|
||||
{ ...base, slug: "c-group", name: "Gamma", venue_type: "กลุ่มคนฉายหนัง" },
|
||||
{ ...base, slug: "d-none", name: "Delta" },
|
||||
];
|
||||
|
||||
it("กรอง all / unset / แต่ละประเภท", () => {
|
||||
expect(filterAdminCinemas(list, { venue: "all", query: "" })).toHaveLength(
|
||||
4,
|
||||
);
|
||||
expect(
|
||||
filterAdminCinemas(list, { venue: "unset", query: "" }).map(
|
||||
(c) => c.slug,
|
||||
),
|
||||
).toEqual(["d-none"]);
|
||||
expect(
|
||||
filterAdminCinemas(list, { venue: "โรงภาพยนตร์อินดี้", query: "" }).map(
|
||||
(c) => c.slug,
|
||||
),
|
||||
).toEqual(["b-indie"]);
|
||||
});
|
||||
|
||||
it("ค้นด้วยชื่อไทย/อังกฤษ/slug ร่วมกับตัวกรอง", () => {
|
||||
expect(
|
||||
filterAdminCinemas(list, { venue: "all", query: "อัลฟา" }).map(
|
||||
(c) => c.slug,
|
||||
),
|
||||
).toEqual(["a-local"]);
|
||||
expect(
|
||||
filterAdminCinemas(list, { venue: "โรงภาพยนตร์ท้องถิ่น", query: "beta" }),
|
||||
).toHaveLength(0);
|
||||
expect(
|
||||
filterAdminCinemas(list, { venue: "all", query: " GAMMA " })[0]?.slug,
|
||||
).toBe("c-group");
|
||||
});
|
||||
|
||||
it("setVenueForSlugs ตั้งค่าเฉพาะ slug ที่ระบุ และไม่แก้ตัวเดิม", () => {
|
||||
const next = setVenueForSlugs(list, ["d-none", "b-indie"], "กลุ่มคนฉายหนัง");
|
||||
expect(next.find((c) => c.slug === "d-none")?.venue_type).toBe(
|
||||
"กลุ่มคนฉายหนัง",
|
||||
);
|
||||
expect(next.find((c) => c.slug === "b-indie")?.venue_type).toBe(
|
||||
"กลุ่มคนฉายหนัง",
|
||||
);
|
||||
expect(next.find((c) => c.slug === "a-local")?.venue_type).toBe(
|
||||
"โรงภาพยนตร์ท้องถิ่น",
|
||||
);
|
||||
expect(list.find((c) => c.slug === "d-none")?.venue_type).toBeUndefined();
|
||||
expect(setVenueForSlugs(list, [], "โรงภาพยนตร์อินดี้")).toBe(list);
|
||||
});
|
||||
|
||||
it("venueCounts นับครบทุกช่อง", () => {
|
||||
expect(venueCounts(list)).toEqual({
|
||||
all: 4,
|
||||
unset: 1,
|
||||
โรงภาพยนตร์ท้องถิ่น: 1,
|
||||
โรงภาพยนตร์อินดี้: 1,
|
||||
กลุ่มคนฉายหนัง: 1,
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -373,6 +373,65 @@ export function hasVenue(cinema: Cinema, venue: VenueType | null): boolean {
|
||||
return venue === null || cinema.venue_type === venue;
|
||||
}
|
||||
|
||||
/** ตัวกรองประเภทในลิสต์หลังบ้าน — "all" = ทุกประเภท, "unset" = ยังไม่จัดประเภท */
|
||||
export type VenueFilterValue = VenueType | "all" | "unset";
|
||||
|
||||
/** กรองรายชื่อโรงหนังในหลังบ้านตามประเภท (ชั้นกรองที่ 1) + คำค้นชื่อ/slug */
|
||||
export function filterAdminCinemas(
|
||||
cinemas: Cinema[],
|
||||
{
|
||||
venue,
|
||||
query,
|
||||
}: {
|
||||
venue: VenueFilterValue;
|
||||
query: string;
|
||||
},
|
||||
): Cinema[] {
|
||||
const needle = query.trim().toLowerCase();
|
||||
return cinemas.filter((cinema) => {
|
||||
if (venue === "unset") {
|
||||
if (cinema.venue_type) return false;
|
||||
} else if (venue !== "all" && cinema.venue_type !== venue) {
|
||||
return false;
|
||||
}
|
||||
if (!needle) return true;
|
||||
return `${cinema.name} ${cinema.name_th ?? ""} ${cinema.slug}`
|
||||
.toLowerCase()
|
||||
.includes(needle);
|
||||
});
|
||||
}
|
||||
|
||||
/** ตั้งประเภทให้หลายโรงพร้อมกัน (คืนลิสต์ใหม่ ไม่แตะตัวเดิม) */
|
||||
export function setVenueForSlugs(
|
||||
cinemas: Cinema[],
|
||||
slugs: string[],
|
||||
venue: VenueType,
|
||||
): Cinema[] {
|
||||
if (slugs.length === 0) return cinemas;
|
||||
const target = new Set(slugs);
|
||||
return cinemas.map((cinema) =>
|
||||
target.has(cinema.slug) ? { ...cinema, venue_type: venue } : cinema,
|
||||
);
|
||||
}
|
||||
|
||||
/** นับจำนวนโรงต่อประเภท (สำหรับสรุปในหลังบ้าน) */
|
||||
export function venueCounts(
|
||||
cinemas: Cinema[],
|
||||
): Record<VenueFilterValue, number> {
|
||||
const counts: Record<VenueFilterValue, number> = {
|
||||
all: cinemas.length,
|
||||
unset: 0,
|
||||
โรงภาพยนตร์ท้องถิ่น: 0,
|
||||
โรงภาพยนตร์อินดี้: 0,
|
||||
กลุ่มคนฉายหนัง: 0,
|
||||
};
|
||||
for (const cinema of cinemas) {
|
||||
if (cinema.venue_type) counts[cinema.venue_type] += 1;
|
||||
else counts.unset += 1;
|
||||
}
|
||||
return counts;
|
||||
}
|
||||
|
||||
export function findCinemaBySlug(
|
||||
cinemas: Cinema[],
|
||||
slug: string | undefined,
|
||||
|
||||
Reference in new issue
Block a user