fix web appointments
This commit was merged in pull request #154.
This commit is contained in:
@@ -50,7 +50,7 @@ public class AppointmentController {
|
|||||||
.orElse(null);
|
.orElse(null);
|
||||||
|
|
||||||
Long effectiveCustomerId = customerId;
|
Long effectiveCustomerId = customerId;
|
||||||
if (role != null && role.equals("CUSTOMER")) {
|
if (role != null && (role.equals("CUSTOMER") || role.equals("ADMIN"))) {
|
||||||
User user = AuthenticationHelper.getAuthenticatedUser(userRepository);
|
User user = AuthenticationHelper.getAuthenticatedUser(userRepository);
|
||||||
effectiveCustomerId = user.getId();
|
effectiveCustomerId = user.getId();
|
||||||
}
|
}
|
||||||
@@ -88,7 +88,7 @@ public class AppointmentController {
|
|||||||
.map(authority -> authority.getAuthority().replace("ROLE_", ""))
|
.map(authority -> authority.getAuthority().replace("ROLE_", ""))
|
||||||
.orElse(null);
|
.orElse(null);
|
||||||
|
|
||||||
if (role != null && role.equals("CUSTOMER")) {
|
if (role != null && (role.equals("CUSTOMER") || role.equals("ADMIN"))) {
|
||||||
User user = AuthenticationHelper.getAuthenticatedUser(userRepository);
|
User user = AuthenticationHelper.getAuthenticatedUser(userRepository);
|
||||||
if (!request.getCustomerId().equals(user.getId())) {
|
if (!request.getCustomerId().equals(user.getId())) {
|
||||||
throw new org.springframework.security.access.AccessDeniedException("You can only create appointments for yourself");
|
throw new org.springframework.security.access.AccessDeniedException("You can only create appointments for yourself");
|
||||||
|
|||||||
@@ -195,6 +195,100 @@ function DatePicker({ value, minDate, onChange }) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function AddPetModal({ token, onClose, onAdded }) {
|
||||||
|
const [petName, setPetName] = useState("");
|
||||||
|
const [species, setSpecies] = useState("");
|
||||||
|
const [breed, setBreed] = useState("");
|
||||||
|
const [submitting, setSubmitting] = useState(false);
|
||||||
|
const [petError, setPetError] = useState(null);
|
||||||
|
|
||||||
|
async function handleSubmit(e) {
|
||||||
|
e.preventDefault();
|
||||||
|
setPetError(null);
|
||||||
|
setSubmitting(true);
|
||||||
|
|
||||||
|
try {
|
||||||
|
const res = await fetch(`${API_BASE}/api/v1/my-pets`, {
|
||||||
|
method: "POST",
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
Authorization: `Bearer ${token}`,
|
||||||
|
},
|
||||||
|
body: JSON.stringify({ petName, species, breed: breed || null }),
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!res.ok) {
|
||||||
|
const data = await res.json().catch(() => null);
|
||||||
|
throw new Error(data?.message || `Request failed (${res.status})`);
|
||||||
|
}
|
||||||
|
|
||||||
|
onAdded();
|
||||||
|
onClose();
|
||||||
|
}
|
||||||
|
|
||||||
|
catch (err) {
|
||||||
|
setPetError(err.message);
|
||||||
|
}
|
||||||
|
|
||||||
|
finally {
|
||||||
|
setSubmitting(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="appt-modal-overlay" onClick={onClose}>
|
||||||
|
<div className="appt-modal" onClick={(e) => e.stopPropagation()}>
|
||||||
|
<h3 className="profile-pet-form-title">Add a New Pet</h3>
|
||||||
|
{petError && <div className="appt-error">{petError}</div>}
|
||||||
|
<form onSubmit={handleSubmit}>
|
||||||
|
<label className="appt-label">
|
||||||
|
Name
|
||||||
|
<input
|
||||||
|
className="appt-input"
|
||||||
|
type="text"
|
||||||
|
value={petName}
|
||||||
|
onChange={(e) => setPetName(e.target.value)}
|
||||||
|
required
|
||||||
|
maxLength={50}
|
||||||
|
/>
|
||||||
|
</label>
|
||||||
|
<label className="appt-label">
|
||||||
|
Species
|
||||||
|
<input
|
||||||
|
className="appt-input"
|
||||||
|
type="text"
|
||||||
|
value={species}
|
||||||
|
onChange={(e) => setSpecies(e.target.value)}
|
||||||
|
required
|
||||||
|
maxLength={50}
|
||||||
|
placeholder="e.g. Dog, Cat, Bird"
|
||||||
|
/>
|
||||||
|
</label>
|
||||||
|
<label className="appt-label">
|
||||||
|
Breed (optional)
|
||||||
|
<input
|
||||||
|
className="appt-input"
|
||||||
|
type="text"
|
||||||
|
value={breed}
|
||||||
|
onChange={(e) => setBreed(e.target.value)}
|
||||||
|
maxLength={50}
|
||||||
|
placeholder="e.g. Golden Retriever"
|
||||||
|
/>
|
||||||
|
</label>
|
||||||
|
<div className="profile-pet-form-actions">
|
||||||
|
<button type="submit" className="appt-submit-btn" disabled={submitting}>
|
||||||
|
{submitting ? "Saving..." : "Add Pet"}
|
||||||
|
</button>
|
||||||
|
<button type="button" className="profile-pet-cancel-btn" onClick={onClose}>
|
||||||
|
Cancel
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
function AppointmentsPage() {
|
function AppointmentsPage() {
|
||||||
const { user, token, loading: authLoading } = useAuth();
|
const { user, token, loading: authLoading } = useAuth();
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
@@ -224,7 +318,9 @@ function AppointmentsPage() {
|
|||||||
const [appointments, setAppointments] = useState([]);
|
const [appointments, setAppointments] = useState([]);
|
||||||
const [loadingAppointments, setLoadingAppointments] = useState(false);
|
const [loadingAppointments, setLoadingAppointments] = useState(false);
|
||||||
|
|
||||||
const canBookAppointments = user?.role === "CUSTOMER";
|
const [showAddPetModal, setShowAddPetModal] = useState(false);
|
||||||
|
|
||||||
|
const canBookAppointments = user?.role === "CUSTOMER" || user?.role === "ADMIN";
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!authLoading && !user) {
|
if (!authLoading && !user) {
|
||||||
@@ -234,6 +330,16 @@ function AppointmentsPage() {
|
|||||||
|
|
||||||
}, [authLoading, user, router, preselectedPetId]);
|
}, [authLoading, user, router, preselectedPetId]);
|
||||||
|
|
||||||
|
const loadCustomerPets = useCallback(() => {
|
||||||
|
if (!token || !canBookAppointments) return;
|
||||||
|
fetch(`${API_BASE}/api/v1/my-pets`, {
|
||||||
|
headers: { Authorization: `Bearer ${token}` },
|
||||||
|
})
|
||||||
|
.then((r) => r.json())
|
||||||
|
.then((data) => setCustomerPets(Array.isArray(data) ? data : []))
|
||||||
|
.catch(() => {});
|
||||||
|
}, [token, canBookAppointments]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!token) {
|
if (!token) {
|
||||||
return;
|
return;
|
||||||
@@ -256,15 +362,8 @@ function AppointmentsPage() {
|
|||||||
.then((data) => setAllPets(data.content ?? []))
|
.then((data) => setAllPets(data.content ?? []))
|
||||||
.catch(() => {});
|
.catch(() => {});
|
||||||
|
|
||||||
if (canBookAppointments) {
|
loadCustomerPets();
|
||||||
fetch(`${API_BASE}/api/v1/my-pets`, {
|
}, [token, loadCustomerPets]);
|
||||||
headers: { Authorization: `Bearer ${token}` },
|
|
||||||
})
|
|
||||||
.then((r) => r.json())
|
|
||||||
.then((data) => setCustomerPets(Array.isArray(data) ? data : []))
|
|
||||||
.catch(() => {});
|
|
||||||
}
|
|
||||||
}, [token, canBookAppointments]);
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (didPreselectRef.current) {
|
if (didPreselectRef.current) {
|
||||||
@@ -392,7 +491,6 @@ function AppointmentsPage() {
|
|||||||
|
|
||||||
function getMinDate() {
|
function getMinDate() {
|
||||||
const d = new Date();
|
const d = new Date();
|
||||||
d.setDate(d.getDate() + 1);
|
|
||||||
|
|
||||||
return d.toISOString().split("T")[0];
|
return d.toISOString().split("T")[0];
|
||||||
}
|
}
|
||||||
@@ -411,12 +509,6 @@ function AppointmentsPage() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!user?.customerId) {
|
|
||||||
setError("Customer account not found. Please contact support.");
|
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (selectedPetIds.length === 0) {
|
if (selectedPetIds.length === 0) {
|
||||||
setError(isAdoptionService ? "Please select a pet to adopt." : "Please select at least one pet.");
|
setError(isAdoptionService ? "Please select a pet to adopt." : "Please select at least one pet.");
|
||||||
|
|
||||||
@@ -427,7 +519,7 @@ function AppointmentsPage() {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
const body = {
|
const body = {
|
||||||
customerId: user.customerId,
|
customerId: user.customerId || user.id,
|
||||||
storeId: Number(storeId),
|
storeId: Number(storeId),
|
||||||
serviceId: Number(serviceId),
|
serviceId: Number(serviceId),
|
||||||
employeeId: employeeId ? Number(employeeId) : undefined,
|
employeeId: employeeId ? Number(employeeId) : undefined,
|
||||||
@@ -436,12 +528,8 @@ function AppointmentsPage() {
|
|||||||
appointmentStatus: "Booked",
|
appointmentStatus: "Booked",
|
||||||
};
|
};
|
||||||
|
|
||||||
if (isCustomerPetService) {
|
if (selectedPetIds.length > 0) {
|
||||||
body.customerPetIds = selectedPetIds;
|
body.petId = selectedPetIds[0];
|
||||||
}
|
|
||||||
|
|
||||||
else {
|
|
||||||
body.petIds = selectedPetIds;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const res = await fetch(`${API_BASE}/api/v1/appointments`, {
|
const res = await fetch(`${API_BASE}/api/v1/appointments`, {
|
||||||
@@ -493,10 +581,18 @@ function AppointmentsPage() {
|
|||||||
const petSectionLabel = isAdoptionService ? "Select a Pet to Adopt" : "Select Pet(s)";
|
const petSectionLabel = isAdoptionService ? "Select a Pet to Adopt" : "Select Pet(s)";
|
||||||
const noPetsMessage = isAdoptionService
|
const noPetsMessage = isAdoptionService
|
||||||
? "No pets are currently available for adoption."
|
? "No pets are currently available for adoption."
|
||||||
: "No pets found. Please add your pets in your profile before booking.";
|
: "No pets found on your profile.";
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<main className="appt-page">
|
<main className="appt-page">
|
||||||
|
{showAddPetModal && (
|
||||||
|
<AddPetModal
|
||||||
|
token={token}
|
||||||
|
onClose={() => setShowAddPetModal(false)}
|
||||||
|
onAdded={loadCustomerPets}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
|
||||||
<section className="appt-hero">
|
<section className="appt-hero">
|
||||||
<h1 className="appt-hero-title">Schedule an Appointment</h1>
|
<h1 className="appt-hero-title">Schedule an Appointment</h1>
|
||||||
<p className="appt-hero-subtitle">Book a service for your pet or schedule a pet adoption visit</p>
|
<p className="appt-hero-subtitle">Book a service for your pet or schedule a pet adoption visit</p>
|
||||||
@@ -600,6 +696,15 @@ function AppointmentsPage() {
|
|||||||
{serviceId && (
|
{serviceId && (
|
||||||
<div className="appt-label">
|
<div className="appt-label">
|
||||||
<span>{petSectionLabel}</span>
|
<span>{petSectionLabel}</span>
|
||||||
|
{isCustomerPetService && (
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
className="appt-add-pet-btn"
|
||||||
|
onClick={() => setShowAddPetModal(true)}
|
||||||
|
>
|
||||||
|
+ Add New Pet
|
||||||
|
</button>
|
||||||
|
)}
|
||||||
{petsToShow.length === 0 ? (
|
{petsToShow.length === 0 ? (
|
||||||
<p className="appt-no-slots">{noPetsMessage}</p>
|
<p className="appt-no-slots">{noPetsMessage}</p>
|
||||||
) : isAdoptionService ? (
|
) : isAdoptionService ? (
|
||||||
|
|||||||
@@ -1351,6 +1351,44 @@ body {
|
|||||||
accent-color: orange;
|
accent-color: orange;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.appt-add-pet-btn {
|
||||||
|
display: inline-block;
|
||||||
|
margin-top: 0.5rem;
|
||||||
|
padding: 0.4rem 0.85rem;
|
||||||
|
background: none;
|
||||||
|
border: 1.5px solid orange;
|
||||||
|
border-radius: 6px;
|
||||||
|
color: orange;
|
||||||
|
font-size: 0.875rem;
|
||||||
|
font-weight: 600;
|
||||||
|
cursor: pointer;
|
||||||
|
transition: background 0.15s, color 0.15s;
|
||||||
|
}
|
||||||
|
|
||||||
|
.appt-add-pet-btn:hover {
|
||||||
|
background: orange;
|
||||||
|
color: white;
|
||||||
|
}
|
||||||
|
|
||||||
|
.appt-modal-overlay {
|
||||||
|
position: fixed;
|
||||||
|
inset: 0;
|
||||||
|
background: rgba(0, 0, 0, 0.45);
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: center;
|
||||||
|
z-index: 1000;
|
||||||
|
}
|
||||||
|
|
||||||
|
.appt-modal {
|
||||||
|
background: white;
|
||||||
|
border-radius: 12px;
|
||||||
|
padding: 2rem;
|
||||||
|
width: 100%;
|
||||||
|
max-width: 420px;
|
||||||
|
box-shadow: 0 8px 32px rgba(0, 0, 0, 0.18);
|
||||||
|
}
|
||||||
|
|
||||||
.appt-link {
|
.appt-link {
|
||||||
color: orange;
|
color: orange;
|
||||||
font-weight: 600;
|
font-weight: 600;
|
||||||
|
|||||||
@@ -108,7 +108,7 @@ export default function ProfilePage() {
|
|||||||
}, [clearPetImageObjectUrls]);
|
}, [clearPetImageObjectUrls]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (user?.role === "CUSTOMER") {
|
if (user?.role === "CUSTOMER" || user?.role === "ADMIN") {
|
||||||
loadPets();
|
loadPets();
|
||||||
}
|
}
|
||||||
}, [user, loadPets]);
|
}, [user, loadPets]);
|
||||||
@@ -419,7 +419,7 @@ export default function ProfilePage() {
|
|||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{user.role === "CUSTOMER" && (
|
{(user.role === "CUSTOMER" || user.role === "ADMIN") && (
|
||||||
<div className="profile-pets-section">
|
<div className="profile-pets-section">
|
||||||
<div className="profile-pets-header">
|
<div className="profile-pets-header">
|
||||||
<h2 className="profile-pets-title">My Pets</h2>
|
<h2 className="profile-pets-title">My Pets</h2>
|
||||||
|
|||||||
Reference in New Issue
Block a user