Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 17 additions & 16 deletions frontend/src/components/Admin/AdminPerm/adminPermForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { getUsers } from "../../../services/requests/user.service";

import { Permanence } from "../../../interfaces/permanence.interface";
import { User } from "../../../interfaces/user.interface";
import { formatDateForInput } from "../../utils/datetime_utils";
import { formatDateForDB, formatDateForInput } from "../../utils/datetime_utils";

interface PermanenceFormProps {
editMode: boolean;
Expand Down Expand Up @@ -58,8 +58,8 @@ const PermanenceForm = ({
setName(editPermanence.name);
setDesc(editPermanence.description);
setLocation(editPermanence.location);
setStartAt(formatDateForInput(editPermanence.start_at));
setEndAt(formatDateForInput(editPermanence.end_at));
setStartAt(formatDateForInput(editPermanence.start_at));
setEndAt(formatDateForInput(editPermanence.end_at));
setCapacity(editPermanence.capacity);
setDifficulty(editPermanence.difficulty);
if (editPermanence.respo) {
Expand All @@ -82,29 +82,30 @@ const PermanenceForm = ({
return;
}

const respoId =
respo && !isNaN(Number(respo.userId)) ? Number(respo.userId) : null;

let respoId = respo && !isNaN(Number(respo.userId)) ? Number(respo.userId) : null;


try {
const payload = {
name,
description: desc,
location,
start_at: formatDateForInput(startAt),
end_at: formatDateForInput(endAt),
start_at: formatDateForDB(startAt), // ✅ en UTC
end_at: formatDateForDB(endAt), // ✅ en UTC
capacity,
difficulty,
respoId,
};

if (editMode && editPermanence) {
await updatePermanence(editPermanence.id, payload);
Swal.fire("Succès", "Permanence mise à jour", "success");
onCancelEdit();
} else {
await createPermanence(payload);
Swal.fire("Succès", "Permanence créée", "success");
}
if (editMode && editPermanence) {
await updatePermanence(editPermanence.id, payload);
Swal.fire("Succès", "Permanence mise à jour", "success");
onCancelEdit();
} else {
await createPermanence(payload);
Swal.fire("Succès", "Permanence créée", "success");
}

resetForm();
onRefresh();
Expand Down
32 changes: 26 additions & 6 deletions frontend/src/components/utils/datetime_utils.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,39 @@
// Pour l'input datetime-local
// Pour préremplir un <input type="datetime-local">
// On reçoit une date en UTC (string ISO venant de la DB)
// On la convertit en locale (Europe/Paris) et on renvoie une string sans timezone
export const formatDateForInput = (date?: string | null) => {
if (!date) return "";
const localDate = new Date(date);
if (isNaN(localDate.getTime())) return ""; // date invalide
const offsetDate = new Date(localDate.getTime() - localDate.getTimezoneOffset() * 60000);
return offsetDate.toISOString().slice(0, 16);
if (isNaN(localDate.getTime())) return "";

// Construire YYYY-MM-DDTHH:mm en LOCAL
const year = localDate.getFullYear();
const month = String(localDate.getMonth() + 1).padStart(2, "0");
const day = String(localDate.getDate()).padStart(2, "0");
const hours = String(localDate.getHours()).padStart(2, "0");
const minutes = String(localDate.getMinutes()).padStart(2, "0");

return `${year}-${month}-${day}T${hours}:${minutes}`;
};


// Pour envoyer vers la DB
// On reçoit une string "YYYY-MM-DDTHH:mm" venant de l'input datetime-local
// On la convertit en UTC ISO (toujours ce que ta DB attend)
export const formatDateForDB = (inputValue?: string | null) => {
if (!inputValue) return null;
const localDate = new Date(inputValue); // interprété en local
if (isNaN(localDate.getTime())) return null;
return localDate.toISOString(); // UTC
};

// Pour affichage lisible en français
// Pour affichage lisible en français (Europe/Paris)
export const formatDateForDisplay = (date?: string | null) => {
if (!date) return "";
const localDate = new Date(date);
if (isNaN(localDate.getTime())) return "";
return localDate.toLocaleString("fr-FR", {
timeZone: "Europe/Paris", // évite les surprises
timeZone: "Europe/Paris",
weekday: "long",
year: "numeric",
month: "long",
Expand Down
8 changes: 4 additions & 4 deletions frontend/src/services/requests/permanence.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ export const createPermanence = async (permanenceData: {
name: string;
description: string;
location: string;
start_at: string;
end_at: string;
start_at: string | null;
end_at: string | null;
capacity: number;
difficulty : number;
respoId: number | null;
Expand Down Expand Up @@ -88,8 +88,8 @@ export const updatePermanence = async ( permId: number, permanenceData: {
name: string;
description: string;
location: string;
start_at: string;
end_at: string;
start_at: string | null;
end_at: string | null;
capacity: number;
difficulty : number;
respoId: number | null;
Expand Down