статусы на русском языке
All checks were successful
Auto Deploy / deploy (push) Successful in 22s

This commit is contained in:
deonisii
2026-04-17 23:49:12 +03:00
parent 4f67bca4be
commit 9b86175929
3 changed files with 103 additions and 51 deletions

View File

@@ -8,6 +8,15 @@ type SearchParams = Promise<{
status?: string; status?: string;
}>; }>;
function formatLeadNumber(id: string, createdAt: Date) {
const date = new Date(createdAt);
const y = date.getFullYear();
const m = String(date.getMonth() + 1).padStart(2, "0");
const d = String(date.getDate()).padStart(2, "0");
return `WP-${y}${m}${d}-${id.slice(-6).toUpperCase()}`;
}
export default async function AdminLeadsPage({ export default async function AdminLeadsPage({
searchParams, searchParams,
}: { }: {
@@ -64,11 +73,11 @@ export default async function AdminLeadsPage({
className="rounded-2xl border border-white/10 bg-neutral-900 px-4 py-3 outline-none" className="rounded-2xl border border-white/10 bg-neutral-900 px-4 py-3 outline-none"
> >
<option value="">Все статусы</option> <option value="">Все статусы</option>
<option value="NEW">NEW</option> <option value="NEW">Новая</option>
<option value="IN_PROGRESS">IN_PROGRESS</option> <option value="IN_PROGRESS">В работе</option>
<option value="CALL_SCHEDULED">CALL_SCHEDULED</option> <option value="CALL_SCHEDULED">Назначен звонок</option>
<option value="WON">WON</option> <option value="WON">Успешно</option>
<option value="LOST">LOST</option> <option value="LOST">Закрыта</option>
</select> </select>
<button className="rounded-2xl bg-emerald-600 px-5 py-3 font-semibold hover:bg-emerald-500"> <button className="rounded-2xl bg-emerald-600 px-5 py-3 font-semibold hover:bg-emerald-500">
@@ -81,6 +90,7 @@ export default async function AdminLeadsPage({
<thead className="border-b border-white/10 text-neutral-400"> <thead className="border-b border-white/10 text-neutral-400">
<tr> <tr>
<th className="text-left px-4 py-3">Дата</th> <th className="text-left px-4 py-3">Дата</th>
<th className="text-left px-4 py-3">Заявка</th>
<th className="text-left px-4 py-3">Компания</th> <th className="text-left px-4 py-3">Компания</th>
<th className="text-left px-4 py-3">Телефон</th> <th className="text-left px-4 py-3">Телефон</th>
<th className="text-left px-4 py-3">Email</th> <th className="text-left px-4 py-3">Email</th>
@@ -95,6 +105,9 @@ export default async function AdminLeadsPage({
<td className="px-4 py-3 whitespace-nowrap"> <td className="px-4 py-3 whitespace-nowrap">
{new Date(lead.createdAt).toLocaleString("ru-RU")} {new Date(lead.createdAt).toLocaleString("ru-RU")}
</td> </td>
<td className="px-4 py-3 whitespace-nowrap">
{formatLeadNumber(lead.id, lead.createdAt)}
</td>
<td className="px-4 py-3">{lead.company}</td> <td className="px-4 py-3">{lead.company}</td>
<td className="px-4 py-3">{lead.phone}</td> <td className="px-4 py-3">{lead.phone}</td>
<td className="px-4 py-3">{lead.email || "—"}</td> <td className="px-4 py-3">{lead.email || "—"}</td>
@@ -108,7 +121,7 @@ export default async function AdminLeadsPage({
{leads.length === 0 && ( {leads.length === 0 && (
<tr> <tr>
<td colSpan={7} className="px-4 py-8 text-center text-neutral-400"> <td colSpan={8} className="px-4 py-8 text-center text-neutral-400">
Пока заявок нет Пока заявок нет
</td> </td>
</tr> </tr>

View File

@@ -3,13 +3,30 @@
import { useState } from "react"; import { useState } from "react";
const statuses = [ const statuses = [
{ value: "NEW", label: "NEW" }, { value: "NEW", label: "Новая" },
{ value: "IN_PROGRESS", label: "IN_PROGRESS" }, { value: "IN_PROGRESS", label: "В работе" },
{ value: "CALL_SCHEDULED", label: "CALL_SCHEDULED" }, { value: "CALL_SCHEDULED", label: "Назначен звонок" },
{ value: "WON", label: "WON" }, { value: "WON", label: "Успешно" },
{ value: "LOST", label: "LOST" }, { value: "LOST", label: "Закрыта" },
] as const; ] as const;
function getStatusDot(value: string) {
switch (value) {
case "NEW":
return "bg-sky-400";
case "IN_PROGRESS":
return "bg-amber-400";
case "CALL_SCHEDULED":
return "bg-violet-400";
case "WON":
return "bg-emerald-400";
case "LOST":
return "bg-rose-400";
default:
return "bg-neutral-400";
}
}
export default function LeadStatusSelect({ export default function LeadStatusSelect({
leadId, leadId,
value, value,
@@ -21,6 +38,7 @@ export default function LeadStatusSelect({
const [isSaving, setIsSaving] = useState(false); const [isSaving, setIsSaving] = useState(false);
async function updateStatus(nextStatus: string) { async function updateStatus(nextStatus: string) {
const prev = status;
setStatus(nextStatus); setStatus(nextStatus);
setIsSaving(true); setIsSaving(true);
@@ -34,11 +52,11 @@ export default function LeadStatusSelect({
}); });
if (!response.ok) { if (!response.ok) {
setStatus(value); setStatus(prev);
alert("Не удалось обновить статус"); alert("Не удалось обновить статус");
} }
} catch { } catch {
setStatus(value); setStatus(prev);
alert("Ошибка сети"); alert("Ошибка сети");
} finally { } finally {
setIsSaving(false); setIsSaving(false);
@@ -46,17 +64,20 @@ export default function LeadStatusSelect({
} }
return ( return (
<select <div className="flex items-center gap-2">
value={status} <span className={`inline-block h-2.5 w-2.5 rounded-full ${getStatusDot(status)}`} />
disabled={isSaving} <select
onChange={(e) => updateStatus(e.target.value)} value={status}
className="rounded-xl border border-white/10 bg-black/30 px-3 py-2 text-sm outline-none" disabled={isSaving}
> onChange={(e) => updateStatus(e.target.value)}
{statuses.map((item) => ( className="rounded-xl border border-white/10 bg-black/30 px-3 py-2 text-sm outline-none"
<option key={item.value} value={item.value}> >
{item.label} {statuses.map((item) => (
</option> <option key={item.value} value={item.value}>
))} {item.label}
</select> </option>
))}
</select>
</div>
); );
} }

View File

@@ -1,41 +1,59 @@
import { NextRequest, NextResponse } from "next/server"; import { NextRequest, NextResponse } from "next/server";
import { getSessionCookieName, verifySessionToken } from "@/lib/auth"; import { getSessionCookieName, verifySessionToken } from "@/lib/auth";
function normalizeHost(host: string) {
return host.split(":")[0].toLowerCase();
}
export async function middleware(request: NextRequest) { export async function middleware(request: NextRequest) {
const { pathname, search } = request.nextUrl; const pathname = request.nextUrl.pathname;
const host = request.headers.get("host") || ""; const search = request.nextUrl.search;
const crmHost = process.env.CRM_HOST || "crm.workparking.ru"; const host = normalizeHost(request.headers.get("host") || "");
const crmHost = (process.env.CRM_HOST || "crm.workparking.ru").toLowerCase();
if (!pathname.startsWith("/admin")) {
return NextResponse.next();
}
if (host !== crmHost) {
const redirectUrl = new URL(request.url);
redirectUrl.host = crmHost;
redirectUrl.protocol = "https:";
return NextResponse.redirect(redirectUrl);
}
const cookieName = getSessionCookieName();
const token = request.cookies.get(cookieName)?.value;
const isAuthed = await verifySessionToken(token);
const isCrmHost = host === crmHost;
const isAdminPath = pathname.startsWith("/admin");
const isLoginPage = pathname === "/admin/login"; const isLoginPage = pathname === "/admin/login";
if (!isAuthed && !isLoginPage) { const token = request.cookies.get(getSessionCookieName())?.value;
const loginUrl = new URL("/admin/login", request.url); const isAuthed = await verifySessionToken(token);
loginUrl.searchParams.set("next", `${pathname}${search}`);
return NextResponse.redirect(loginUrl); // Если открыли crm.workparking.ru/ — сразу ведём в CRM
if (isCrmHost && pathname === "/") {
const url = request.nextUrl.clone();
url.pathname = isAuthed ? "/admin/leads" : "/admin/login";
url.search = "";
return NextResponse.redirect(url);
} }
if (isAuthed && isLoginPage) { // Если admin открыли не на CRM-домене — уводим на CRM без порта
return NextResponse.redirect(new URL("/admin/leads", request.url)); if (isAdminPath && !isCrmHost) {
const url = request.nextUrl.clone();
url.protocol = "https";
url.hostname = crmHost;
url.port = "";
return NextResponse.redirect(url);
}
if (isAdminPath) {
if (!isAuthed && !isLoginPage) {
const loginUrl = request.nextUrl.clone();
loginUrl.pathname = "/admin/login";
loginUrl.search = `?next=${encodeURIComponent(`${pathname}${search}`)}`;
return NextResponse.redirect(loginUrl);
}
if (isAuthed && isLoginPage) {
const url = request.nextUrl.clone();
url.pathname = "/admin/leads";
url.search = "";
return NextResponse.redirect(url);
}
} }
return NextResponse.next(); return NextResponse.next();
} }
export const config = { export const config = {
matcher: ["/admin/:path*"], matcher: ["/", "/admin/:path*"],
}; };