fix(wizard,quotes,dates): reliable error routing, single-project accept, Persian date/time input
Wizard: - failed saves now route to the exact step+field: a new data-field attribute on every required section (supplier_id, line_items, termsheets, project, quote_date, expiry_date, notes, proforma) plus resolveErrorTarget() that maps ApiError.field / message keywords (supplier, line item/material/unit/quantity, termsheet/schedule/milestone, project, dates, notes, file) to the offending step — the «رفع خطا» toast action scrolls/focuses the right control instead of guessing - material records are no longer created on the fly while typing (rememberMaterial no longer fires addMaterial per blur); materials/specs/units are written once during the final save, avoiding half-created catalog rows Accept flow: - quote_service no longer rewrites LineItemProjects on accept — those candidate-project rows stay 100% untouched and only item_decisions.project_id is written, matching the per-item target-project model and the UI payload (item_projects: []) Dates/time: - JalaliDatePicker: hour/minute are now editable text fields (Persian digits, clamping, 3-digit retype handling) with a controlled useEffect that only rewrites the inputs when the numeric value actually changes — no more wiping in-progress typing; date-only detection tightened to a full YYYY-MM-DD match so datetime strings are not misread Also: /api validation logging (RequestValidationError) now includes the path and error list for faster diagnosis. Verified: api + frontend image builds (python, tsc, vite) clean on top of origin/production (incl. the team's mobile layout merges).
This commit is contained in:
@@ -6,6 +6,7 @@ import {
|
||||
jalaliStrToIso,
|
||||
jalaliToGregorian,
|
||||
MONTHS,
|
||||
toAsciiDigits,
|
||||
toFa,
|
||||
WEEKDAYS,
|
||||
} from "../lib/jalali";
|
||||
@@ -29,7 +30,7 @@ function todayJalali(): [number, number, number] {
|
||||
|
||||
function isoToJalali(iso: string | null): [number, number, number] | null {
|
||||
if (!iso) return null;
|
||||
const m = String(iso).match(/^(\d{4})-(\d{2})-(\d{2})/);
|
||||
const m = String(iso).match(/^(\d{4})-(\d{2})-(\d{2})$/);
|
||||
if (m) {
|
||||
const gy = parseInt(m[1], 10);
|
||||
const gm = parseInt(m[2], 10);
|
||||
@@ -78,6 +79,10 @@ export function JalaliDatePicker({
|
||||
}: JalaliDatePickerProps) {
|
||||
const [open, setOpen] = useState(false);
|
||||
const [curJ, setCurJ] = useState<[number, number, number]>(() => isoToJalali(value) ?? todayJalali());
|
||||
const initialTime = timeOf(value);
|
||||
const [hourText, setHourText] = useState<string>(() => faNum(String(initialTime.h).padStart(2, "0")));
|
||||
const [minuteText, setMinuteText] = useState<string>(() => faNum(String(initialTime.m).padStart(2, "0")));
|
||||
|
||||
const [text, setText] = useState<string>(() => {
|
||||
if (!value) return "";
|
||||
const j = isoToJalaliStr(value);
|
||||
@@ -92,19 +97,28 @@ export function JalaliDatePicker({
|
||||
useEffect(() => {
|
||||
if (!value) {
|
||||
setText("");
|
||||
const now = nowTime();
|
||||
setHourText(faNum(String(now.h).padStart(2, "0")));
|
||||
setMinuteText(faNum(String(now.m).padStart(2, "0")));
|
||||
return;
|
||||
}
|
||||
const j = isoToJalaliStr(value);
|
||||
const t = timeOf(value);
|
||||
if (!withTime) {
|
||||
setText(j);
|
||||
} else {
|
||||
const d = new Date(value);
|
||||
if (Number.isNaN(d.getTime())) {
|
||||
setText(j);
|
||||
} else {
|
||||
const hh = String(d.getHours()).padStart(2, "0");
|
||||
const mm = String(d.getMinutes()).padStart(2, "0");
|
||||
setText(`${j} ${faNum(hh + ":" + mm)}`);
|
||||
const hh = String(t.h).padStart(2, "0");
|
||||
const mm = String(t.m).padStart(2, "0");
|
||||
setText(`${j} ${faNum(hh + ":" + mm)}`);
|
||||
|
||||
// Only update local input texts if numeric values differ, to avoid wiping in-progress typing
|
||||
const curH = parseInt(toAsciiDigits(hourText) || "0", 10);
|
||||
if (curH !== t.h) {
|
||||
setHourText(faNum(hh));
|
||||
}
|
||||
const curM = parseInt(toAsciiDigits(minuteText) || "0", 10);
|
||||
if (curM !== t.m) {
|
||||
setMinuteText(faNum(mm));
|
||||
}
|
||||
}
|
||||
const jParts = isoToJalali(value);
|
||||
@@ -116,14 +130,13 @@ export function JalaliDatePicker({
|
||||
// تاریخ انتخابشده به صورت محلی (نه UTC) برای مقایسه با سلولهای تقویم
|
||||
function selectedLocalDate(): string | null {
|
||||
if (!value) return null;
|
||||
const m = String(value).match(/^(\d{4})-(\d{2})-(\d{2})/);
|
||||
const m = String(value).match(/^(\d{4})-(\d{2})-(\d{2})$/);
|
||||
if (m) return `${m[1]}-${m[2]}-${m[3]}`;
|
||||
const d = new Date(value);
|
||||
if (Number.isNaN(d.getTime())) return null;
|
||||
return `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, "0")}-${String(d.getDate()).padStart(2, "0")}`;
|
||||
}
|
||||
const selectedIso = selectedLocalDate();
|
||||
const time = timeOf(value);
|
||||
|
||||
const grid = useMemo(() => {
|
||||
const [jy, jm] = curJ;
|
||||
@@ -163,36 +176,108 @@ export function JalaliDatePicker({
|
||||
};
|
||||
|
||||
const selectDay = (jy: number, jm: number, jd: number) => {
|
||||
const val = withTime ? buildIso(jy, jm, jd, time.h, time.m) : buildDateOnly(jy, jm, jd);
|
||||
const currentH = parseInt(toAsciiDigits(hourText) || "0", 10);
|
||||
const currentM = parseInt(toAsciiDigits(minuteText) || "0", 10);
|
||||
const validH = Math.min(23, Math.max(0, currentH));
|
||||
const validM = Math.min(59, Math.max(0, currentM));
|
||||
const val = withTime ? buildIso(jy, jm, jd, validH, validM) : buildDateOnly(jy, jm, jd);
|
||||
onChange(val);
|
||||
setText(
|
||||
`${toFa(jy)}/${toFa(String(jm).padStart(2, "0"))}/${toFa(String(jd).padStart(2, "0"))}${withTime ? " " + faNum(String(time.h).padStart(2, "0") + ":" + String(time.m).padStart(2, "0")) : ""}`
|
||||
`${toFa(jy)}/${toFa(String(jm).padStart(2, "0"))}/${toFa(String(jd).padStart(2, "0"))}${withTime ? " " + faNum(String(validH).padStart(2, "0") + ":" + String(validM).padStart(2, "0")) : ""}`
|
||||
);
|
||||
setOpen(false);
|
||||
};
|
||||
|
||||
const setTimePart = (part: "h" | "m", raw: string) => {
|
||||
const n = parseInt(raw.replace(/[۰-۹]/g, (d) => String("۰۱۲۳۴۵۶۷۸۹".indexOf(d))), 10);
|
||||
if (Number.isNaN(n)) return;
|
||||
const h = part === "h" ? Math.min(23, Math.max(0, n)) : time.h;
|
||||
const m = part === "m" ? Math.min(59, Math.max(0, n)) : time.m;
|
||||
const handleHourChange = (raw: string) => {
|
||||
let digits = toAsciiDigits(raw).replace(/\D/g, "");
|
||||
if (digits.length === 3) {
|
||||
digits = digits.slice(-1);
|
||||
} else if (digits.length > 3) {
|
||||
digits = digits.slice(0, 2);
|
||||
}
|
||||
if (!digits) {
|
||||
setHourText("");
|
||||
return;
|
||||
}
|
||||
const n = parseInt(digits, 10);
|
||||
const clamped = Math.min(23, Math.max(0, n));
|
||||
const textToShow = digits.length === 2 && n > 23 ? String(clamped) : digits;
|
||||
setHourText(faNum(textToShow));
|
||||
|
||||
const [jy, jm, jd] = curJ;
|
||||
onChange(buildIso(jy, jm, jd, h, m));
|
||||
// بهروزرسانی نمایش
|
||||
const j = value ? isoToJalaliStr(value) : isoToJalaliStr(buildIso(jy, jm, jd, h, m));
|
||||
setText(`${j} ${faNum(String(h).padStart(2, "0") + ":" + String(m).padStart(2, "0"))}`);
|
||||
const currentM = parseInt(toAsciiDigits(minuteText) || "0", 10);
|
||||
const validM = Math.min(59, Math.max(0, currentM));
|
||||
onChange(buildIso(jy, jm, jd, clamped, validM));
|
||||
const j = value ? isoToJalaliStr(value) : isoToJalaliStr(buildIso(jy, jm, jd, clamped, validM));
|
||||
setText(`${j} ${faNum(String(clamped).padStart(2, "0") + ":" + String(validM).padStart(2, "0"))}`);
|
||||
};
|
||||
|
||||
const handleHourBlur = () => {
|
||||
const digits = toAsciiDigits(hourText).replace(/\D/g, "");
|
||||
const n = digits ? parseInt(digits, 10) : 0;
|
||||
const clamped = Math.min(23, Math.max(0, n));
|
||||
setHourText(faNum(String(clamped).padStart(2, "0")));
|
||||
|
||||
const [jy, jm, jd] = curJ;
|
||||
const currentM = parseInt(toAsciiDigits(minuteText) || "0", 10);
|
||||
const validM = Math.min(59, Math.max(0, currentM));
|
||||
onChange(buildIso(jy, jm, jd, clamped, validM));
|
||||
const j = value ? isoToJalaliStr(value) : isoToJalaliStr(buildIso(jy, jm, jd, clamped, validM));
|
||||
setText(`${j} ${faNum(String(clamped).padStart(2, "0") + ":" + String(validM).padStart(2, "0"))}`);
|
||||
};
|
||||
|
||||
const handleMinuteChange = (raw: string) => {
|
||||
let digits = toAsciiDigits(raw).replace(/\D/g, "");
|
||||
if (digits.length === 3) {
|
||||
digits = digits.slice(-1);
|
||||
} else if (digits.length > 3) {
|
||||
digits = digits.slice(0, 2);
|
||||
}
|
||||
if (!digits) {
|
||||
setMinuteText("");
|
||||
return;
|
||||
}
|
||||
const n = parseInt(digits, 10);
|
||||
const clamped = Math.min(59, Math.max(0, n));
|
||||
const textToShow = digits.length === 2 && n > 59 ? String(clamped) : digits;
|
||||
setMinuteText(faNum(textToShow));
|
||||
|
||||
const [jy, jm, jd] = curJ;
|
||||
const currentH = parseInt(toAsciiDigits(hourText) || "0", 10);
|
||||
const validH = Math.min(23, Math.max(0, currentH));
|
||||
onChange(buildIso(jy, jm, jd, validH, clamped));
|
||||
const j = value ? isoToJalaliStr(value) : isoToJalaliStr(buildIso(jy, jm, jd, validH, clamped));
|
||||
setText(`${j} ${faNum(String(validH).padStart(2, "0") + ":" + String(clamped).padStart(2, "0"))}`);
|
||||
};
|
||||
|
||||
const handleMinuteBlur = () => {
|
||||
const digits = toAsciiDigits(minuteText).replace(/\D/g, "");
|
||||
const n = digits ? parseInt(digits, 10) : 0;
|
||||
const clamped = Math.min(59, Math.max(0, n));
|
||||
setMinuteText(faNum(String(clamped).padStart(2, "0")));
|
||||
|
||||
const [jy, jm, jd] = curJ;
|
||||
const currentH = parseInt(toAsciiDigits(hourText) || "0", 10);
|
||||
const validH = Math.min(23, Math.max(0, currentH));
|
||||
onChange(buildIso(jy, jm, jd, validH, clamped));
|
||||
const j = value ? isoToJalaliStr(value) : isoToJalaliStr(buildIso(jy, jm, jd, validH, clamped));
|
||||
setText(`${j} ${faNum(String(validH).padStart(2, "0") + ":" + String(clamped).padStart(2, "0"))}`);
|
||||
};
|
||||
|
||||
const pickToday = () => {
|
||||
const now = new Date();
|
||||
const [jy, jm, jd] = todayJalali();
|
||||
const nh = now.getHours();
|
||||
const nm = now.getMinutes();
|
||||
setHourText(faNum(String(nh).padStart(2, "0")));
|
||||
setMinuteText(faNum(String(nm).padStart(2, "0")));
|
||||
const val = withTime
|
||||
? buildIso(jy, jm, jd, now.getHours(), now.getMinutes())
|
||||
? buildIso(jy, jm, jd, nh, nm)
|
||||
: buildDateOnly(jy, jm, jd);
|
||||
onChange(val);
|
||||
setText(
|
||||
withTime
|
||||
? `${toFa(jy)}/${toFa(String(jm).padStart(2, "0"))}/${toFa(String(jd).padStart(2, "0"))} ${faNum(String(now.getHours()).padStart(2, "0") + ":" + String(now.getMinutes()).padStart(2, "0"))}`
|
||||
? `${toFa(jy)}/${toFa(String(jm).padStart(2, "0"))}/${toFa(String(jd).padStart(2, "0"))} ${faNum(String(nh).padStart(2, "0") + ":" + String(nm).padStart(2, "0"))}`
|
||||
: `${toFa(jy)}/${toFa(String(jm).padStart(2, "0"))}/${toFa(String(jd).padStart(2, "0"))}`,
|
||||
);
|
||||
setOpen(false);
|
||||
@@ -201,17 +286,24 @@ export function JalaliDatePicker({
|
||||
const clear = () => {
|
||||
onChange(null);
|
||||
setText("");
|
||||
const now = nowTime();
|
||||
setHourText(faNum(String(now.h).padStart(2, "0")));
|
||||
setMinuteText(faNum(String(now.m).padStart(2, "0")));
|
||||
setOpen(false);
|
||||
};
|
||||
|
||||
const onInputChange = (v: string) => {
|
||||
setText(v);
|
||||
if (!v.trim()) {
|
||||
onChange(null);
|
||||
return;
|
||||
}
|
||||
const iso = jalaliStrToIso(v);
|
||||
if (iso) {
|
||||
if (!withTime) {
|
||||
const nums = String(v)
|
||||
.split(/[/\-.]/)
|
||||
.map((x) => parseInt(String(x).replace(/[۰-۹]/g, (d) => String("۰۱۲۳۴۵۶۷۸۹".indexOf(d))), 10));
|
||||
.map((x) => parseInt(toAsciiDigits(x), 10));
|
||||
if (nums.length >= 3 && !nums.some((n) => Number.isNaN(n))) {
|
||||
const [gy, gm, gd] = jalaliToGregorian(nums[0], nums[1], nums[2]);
|
||||
onChange(`${gy}-${String(gm).padStart(2, "0")}-${String(gd).padStart(2, "0")}`);
|
||||
@@ -226,6 +318,51 @@ export function JalaliDatePicker({
|
||||
}
|
||||
};
|
||||
|
||||
const handleInputBlur = () => {
|
||||
if (!text.trim()) {
|
||||
onChange(null);
|
||||
return;
|
||||
}
|
||||
const iso = jalaliStrToIso(text);
|
||||
if (iso) {
|
||||
if (!withTime) {
|
||||
const nums = String(text)
|
||||
.split(/[/\-.]/)
|
||||
.map((x) => parseInt(toAsciiDigits(x), 10));
|
||||
if (nums.length >= 3 && !nums.some((n) => Number.isNaN(n))) {
|
||||
const [gy, gm, gd] = jalaliToGregorian(nums[0], nums[1], nums[2]);
|
||||
const dOnly = `${gy}-${String(gm).padStart(2, "0")}-${String(gd).padStart(2, "0")}`;
|
||||
onChange(dOnly);
|
||||
setText(isoToJalaliStr(dOnly));
|
||||
} else {
|
||||
onChange(iso.slice(0, 10));
|
||||
setText(isoToJalaliStr(iso));
|
||||
}
|
||||
} else {
|
||||
onChange(iso);
|
||||
const j = isoToJalaliStr(iso);
|
||||
const d = new Date(iso);
|
||||
if (!Number.isNaN(d.getTime())) {
|
||||
const hh = String(d.getHours()).padStart(2, "0");
|
||||
const mm = String(d.getMinutes()).padStart(2, "0");
|
||||
setText(`${j} ${faNum(hh + ":" + mm)}`);
|
||||
}
|
||||
}
|
||||
} else if (value) {
|
||||
const j = isoToJalaliStr(value);
|
||||
if (!withTime) {
|
||||
setText(j);
|
||||
} else {
|
||||
const d = new Date(value);
|
||||
if (!Number.isNaN(d.getTime())) {
|
||||
const hh = String(d.getHours()).padStart(2, "0");
|
||||
const mm = String(d.getMinutes()).padStart(2, "0");
|
||||
setText(`${j} ${faNum(hh + ":" + mm)}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<div ref={ref} className={cn("relative", className)}>
|
||||
<input
|
||||
@@ -238,6 +375,7 @@ export function JalaliDatePicker({
|
||||
onFocus={() => !disabled && setOpen(true)}
|
||||
onClick={() => !disabled && setOpen(true)}
|
||||
onChange={(e) => onInputChange(e.target.value)}
|
||||
onBlur={handleInputBlur}
|
||||
/>
|
||||
{open && !disabled && (
|
||||
<div
|
||||
@@ -248,6 +386,7 @@ export function JalaliDatePicker({
|
||||
withTime ? "w-80 p-3" : "w-64 p-2",
|
||||
)}
|
||||
>
|
||||
{/* 1. Header: Month / Year Navigation */}
|
||||
<div className="mb-1.5 flex items-center justify-between gap-1">
|
||||
<button type="button" className="cursor-pointer rounded px-1.5 text-sm hover:bg-slate-bg" onClick={() => nav("y+1")}>
|
||||
«
|
||||
@@ -265,6 +404,8 @@ export function JalaliDatePicker({
|
||||
»
|
||||
</button>
|
||||
</div>
|
||||
|
||||
{/* 2. Day of Week Header & Days Grid */}
|
||||
<div className="grid grid-cols-7 gap-0.5 text-center">
|
||||
{WEEKDAYS.map((w) => (
|
||||
<div key={w} className="py-0.5 text-[10px] font-bold text-muted">
|
||||
@@ -291,33 +432,40 @@ export function JalaliDatePicker({
|
||||
),
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* 3. Time Inputs Row (Positioned BELOW the calendar grid) — Hour on left, Minute on right */}
|
||||
{withTime && (
|
||||
<div className="mt-2 flex items-center justify-center gap-4 border-t border-line pt-2.5">
|
||||
<div className="mt-2 flex items-center justify-center gap-3 rounded-md border border-line/70 bg-slate-bg/40 py-1.5">
|
||||
<div className="flex items-center gap-1.5">
|
||||
<span className="text-[11.5px] font-semibold text-muted">ساعت</span>
|
||||
<input
|
||||
type="text"
|
||||
className="num-input h-8 w-14 rounded-md border border-line bg-panel px-1 text-center text-[13px] tabular-nums focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-accent/40 max-md:h-10 max-md:w-16"
|
||||
value={faNum(String(time.h).padStart(2, "0"))}
|
||||
className="num-input h-7 w-12 rounded-md border border-line bg-panel px-1 text-center text-[13px] tabular-nums focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-accent/40 max-md:h-9 max-md:w-14"
|
||||
value={minuteText}
|
||||
inputMode="numeric"
|
||||
maxLength={2}
|
||||
onChange={(e) => setTimePart("h", e.target.value)}
|
||||
onFocus={(e) => e.target.select()}
|
||||
onChange={(e) => handleMinuteChange(e.target.value)}
|
||||
onBlur={handleMinuteBlur}
|
||||
/>
|
||||
</div>
|
||||
<span className="text-muted">:</span>
|
||||
<div className="flex items-center gap-1.5">
|
||||
<span className="text-[11.5px] font-semibold text-muted">دقیقه</span>
|
||||
</div>
|
||||
<span className="font-bold text-muted">:</span>
|
||||
<div className="flex items-center gap-1.5">
|
||||
<input
|
||||
type="text"
|
||||
className="num-input h-8 w-14 rounded-md border border-line bg-panel px-1 text-center text-[13px] tabular-nums focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-accent/40 max-md:h-10 max-md:w-16"
|
||||
value={faNum(String(time.m).padStart(2, "0"))}
|
||||
className="num-input h-7 w-12 rounded-md border border-line bg-panel px-1 text-center text-[13px] tabular-nums focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-accent/40 max-md:h-9 max-md:w-14"
|
||||
value={hourText}
|
||||
inputMode="numeric"
|
||||
maxLength={2}
|
||||
onChange={(e) => setTimePart("m", e.target.value)}
|
||||
onFocus={(e) => e.target.select()}
|
||||
onChange={(e) => handleHourChange(e.target.value)}
|
||||
onBlur={handleHourBlur}
|
||||
/>
|
||||
<span className="text-[11.5px] font-semibold text-muted">ساعت</span>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
|
||||
{/* 4. Action Buttons Footer */}
|
||||
<div className="mt-2 flex justify-between border-t border-line pt-2">
|
||||
<button type="button" className="cursor-pointer rounded px-2 py-0.5 text-[11.5px] text-accent hover:bg-slate-bg" onClick={pickToday}>
|
||||
امروز
|
||||
|
||||
Reference in New Issue
Block a user