diff --git a/SafeSpace-Customer-UI/SafeSpace-Customer-UI/src/app/state/AppProvider.jsx b/SafeSpace-Customer-UI/SafeSpace-Customer-UI/src/app/state/AppProvider.jsx index 78f606f..bb92997 100644 --- a/SafeSpace-Customer-UI/SafeSpace-Customer-UI/src/app/state/AppProvider.jsx +++ b/SafeSpace-Customer-UI/SafeSpace-Customer-UI/src/app/state/AppProvider.jsx @@ -236,8 +236,8 @@ function LocalProvider({ children }) { // ===================================================================== // Chế độ API (VITE_USE_API=true) -// Luồng backend đã có → gọi API. Luồng backend chưa có (trả kho, lịch trả kho, -// thông báo, đổi tên hiển thị) → lưu trong trình duyệt theo từng tài khoản ("overlay"). +// Hồ sơ, mật khẩu, hỗ trợ và trả kho gọi API thật. +// Thông báo phía khách và dữ liệu phụ dùng overlay riêng theo từng tài khoản. // ===================================================================== function readSession() { const s = readJson(SESSION_KEY); @@ -308,7 +308,7 @@ function ApiProvider({ children }) { const [reservationDetails, contractDetails, ticketDetails] = await Promise.all([ Promise.all(reservations.filter((r) => r.status !== "CANCELLED").map((r) => api.reservation(r.reservationId).catch(() => null))), Promise.all(contracts.map((c) => api.contract(c.contractId).catch(() => ({ ...c, payments: [], renewals: [] })))), - Promise.all(tickets.map((t) => api.ticket(t.ticketId).catch(() => t))), + Promise.all(tickets.map((t) => api.ticket(t.ticketId))), ]); if (!refs.current.session) return; const next = { @@ -352,11 +352,25 @@ function ApiProvider({ children }) { [endSession], ); + const loadProfile = useCallback(async () => { + const current = refs.current.session; + if (!current) return; + const profile = await api.getProfile(); + if (refs.current.session?.token !== current.token) return; + const next = { ...refs.current.session, user: { ...refs.current.session.user, ...profile } }; + refs.current.session = next; + writeJson(SESSION_KEY, next); + setSession(next); + // Tên lấy từ máy chủ; bỏ tên cũ chỉ lưu trong trình duyệt. + updateOverlay(o => ({ ...o, name: null })); + }, [updateOverlay]); + const loadAll = useCallback(async () => { setLoadError(null); try { await loadCatalog(); if (refs.current.session) { + await loadProfile(); if (isAdminRoles(refs.current.session.roles) || isOpsRoles(refs.current.session.roles) || isBusinessRoles(refs.current.session.roles)) setRemote(EMPTY_REMOTE); else await loadCustomer(); } @@ -364,7 +378,7 @@ function ApiProvider({ children }) { if (e instanceof ApiError && e.status === 401) endSession("expired"); else setLoadError(e?.message || "Không tải được dữ liệu."); } - }, [loadCatalog, loadCustomer, endSession]); + }, [loadCatalog, loadCustomer, loadProfile, endSession]); useEffect(() => { loadAll(); @@ -402,12 +416,13 @@ function ApiProvider({ children }) { refs.current.overlay = saved; setOverlay(saved); setSession(next); + await loadProfile(); if (isAdminRoles(next.roles) || isOpsRoles(next.roles) || isBusinessRoles(next.roles)) { refs.current.remote = EMPTY_REMOTE; setRemote(EMPTY_REMOTE); } else await loadCustomer(); }, - [loadCustomer], + [loadCustomer, loadProfile], ); const login = useCallback( @@ -607,20 +622,25 @@ function ApiProvider({ children }) { return { ok: true }; } case "CHANGE_PASSWORD": { - await api.changePassword({ currentPassword: p.current_password, newPassword: p.new_password }); - notify("Đã đổi mật khẩu thành công."); + try { + await api.changePassword({ currentPassword: p.current, newPassword: p.next }); + } catch (e) { + if (e instanceof ApiError && e.data?.code === "BAD_PASSWORD") return fail(e.message, "BAD_PASSWORD"); + throw e; + } return { ok: true }; } case "UPDATE_NAME": { - const profile = await api.updateProfile({ fullName: p.name, phone: session?.user?.phone || null }); - if (session) { - const updatedUser = { ...session.user, fullName: profile.fullName }; - const updatedSession = { ...session, user: updatedUser }; + const profile = await api.updateProfile({ fullName: String(p.name || "").trim() }); + const current = refs.current.session; + if (current) { + const updatedUser = { ...current.user, ...profile }; + const updatedSession = { ...current, user: updatedUser }; writeJson(SESSION_KEY, updatedSession); refs.current.session = updatedSession; setSession(updatedSession); } - runLocal(type, p); + updateOverlay(o => ({ ...o, name: null })); return { ok: true, name: profile.fullName }; } case "REQUEST_RETURN": { diff --git a/SafeSpace-Customer-UI/SafeSpace-Customer-UI/src/features/customer/profile/CustomerProfilePage.jsx b/SafeSpace-Customer-UI/SafeSpace-Customer-UI/src/features/customer/profile/CustomerProfilePage.jsx index 38b326d..420cde8 100644 --- a/SafeSpace-Customer-UI/SafeSpace-Customer-UI/src/features/customer/profile/CustomerProfilePage.jsx +++ b/SafeSpace-Customer-UI/SafeSpace-Customer-UI/src/features/customer/profile/CustomerProfilePage.jsx @@ -1,4 +1,4 @@ -import React, { useState } from "react"; +import React, { useRef, useState } from "react"; import { Button, Dialog, Field, PageHeader } from "../../../shared/components/UI.jsx"; import { go } from "../../../app/router.js"; import { maskPhone } from "../../../shared/utils/format.js"; @@ -12,13 +12,28 @@ export default function CustomerProfilePage() { const [pw, setPw] = useState({ current: "", next: "", confirm: "" }); const [pwErrors, setPwErrors] = useState({}); const [dialog, setDialog] = useState(null); + const [savingName, setSavingName] = useState(false); + const [savingPassword, setSavingPassword] = useState(false); + const namePending = useRef(false); + const passwordPending = useRef(false); const saveName = async (e) => { e.preventDefault(); - const res = await dispatch("UPDATE_NAME", { name }); - if (!res.ok) return setNameError(res.error); - setName(res.name); - setDialog({ title: "Đã lưu tên", message: "Tên hiển thị của bạn đã được cập nhật." }); + if (namePending.current) return; + const fullName = name.trim(); + if (fullName.length < 2 || fullName.length > 100) return setNameError("Tên hiển thị phải từ 2 đến 100 ký tự."); + namePending.current = true; + setSavingName(true); + setNameError(""); + try { + const res = await dispatch("UPDATE_NAME", { name: fullName }); + if (!res.ok) return setNameError(res.error); + setName(res.name); + setDialog({ title: "Đã lưu tên", message: "Tên hiển thị của bạn đã được cập nhật." }); + } finally { + namePending.current = false; + setSavingName(false); + } }; const setPwField = (k, v) => { @@ -27,6 +42,7 @@ export default function CustomerProfilePage() { }; const changePassword = async (e) => { e.preventDefault(); + if (passwordPending.current) return; const err = {}; if (!pw.current) err.current = "Vui lòng nhập mật khẩu hiện tại."; if (!pw.next) err.next = "Vui lòng nhập mật khẩu mới."; @@ -38,10 +54,18 @@ export default function CustomerProfilePage() { err.confirmHint = "Nhập lại đúng mật khẩu mới để tiếp tục."; } if (Object.keys(err).length) return setPwErrors(err); - const res = await dispatch("CHANGE_PASSWORD", { current: pw.current, next: pw.next }); - if (!res.ok) return setPwErrors(res.code === "BAD_PASSWORD" ? { current: res.error } : res.code === "UNSUPPORTED" ? { form: res.error } : { next: res.error }); - setPw({ current: "", next: "", confirm: "" }); - setDialog({ title: "Đổi mật khẩu thành công", message: "Mật khẩu mới của bạn đã được cập nhật." }); + passwordPending.current = true; + setSavingPassword(true); + setPwErrors({}); + try { + const res = await dispatch("CHANGE_PASSWORD", { current: pw.current, next: pw.next }); + if (!res.ok) return setPwErrors(res.code === "BAD_PASSWORD" ? { current: res.error } : { form: res.error }); + setPw({ current: "", next: "", confirm: "" }); + setDialog({ title: "Đổi mật khẩu thành công", message: "Mật khẩu mới của bạn đã được cập nhật." }); + } finally { + passwordPending.current = false; + setSavingPassword(false); + } }; return ( @@ -51,7 +75,7 @@ export default function CustomerProfilePage() {

Thông tin cá nhân

- { setName(e.target.value); setNameError(""); }} /> + { setName(e.target.value); setNameError(""); }} /> @@ -61,24 +85,24 @@ export default function CustomerProfilePage() {

Thông báo

Nhận nhắc lịch hẹn, hạn hợp đồng và cập nhật thanh toán trong ứng dụng.

- +

Đổi mật khẩu

- setPwField("current", e.target.value)} /> + setPwField("current", e.target.value)} /> - setPwField("next", e.target.value)} /> + setPwField("next", e.target.value)} /> - setPwField("confirm", e.target.value)} /> + setPwField("confirm", e.target.value)} /> {pwErrors.confirmHint &&

{pwErrors.confirmHint}

} {!supports.changePassword &&

Đổi mật khẩu đang tạm ngưng trên hệ thống. Liên hệ quản trị viên SafeSpace nếu cần đặt lại mật khẩu.

} {pwErrors.form &&

{pwErrors.form}

} - +