change and add into components, widgets and pages
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
import { useState } from 'react';
|
||||
import { pb } from '@/services/pocketbase';
|
||||
|
||||
interface TicketPayload {
|
||||
category: string;
|
||||
ci: string;
|
||||
subject: string;
|
||||
affectedAsset: string;
|
||||
incidentTime: string;
|
||||
description: string;
|
||||
}
|
||||
|
||||
export const useCreateTicket = () => {
|
||||
const [isSubmitting, setIsSubmitting] = useState(false);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
|
||||
const createTicket = async (payload: TicketPayload): Promise<string | null> => {
|
||||
setError(null);
|
||||
setIsSubmitting(true);
|
||||
|
||||
try {
|
||||
const currentUser = pb.authStore.record || pb.authStore.model;
|
||||
if (!currentUser) throw new Error("Utilisateur non authentifié");
|
||||
|
||||
// Construction de la description enrichie avec les métadonnées techniques
|
||||
let fullDescription = payload.description;
|
||||
|
||||
if (payload.affectedAsset.trim()) {
|
||||
fullDescription = `[Équipement concerné: ${payload.affectedAsset}]\n` + fullDescription;
|
||||
}
|
||||
if (payload.category === 'Incident Critique' && payload.incidentTime) {
|
||||
fullDescription = `[Heure de l'incident: ${new Date(payload.incidentTime).toLocaleString('fr-FR')}]\n` + fullDescription;
|
||||
}
|
||||
if (payload.ci !== 'general') {
|
||||
fullDescription = `[CI: ${payload.ci}]\n` + fullDescription;
|
||||
}
|
||||
|
||||
// Enregistrement dans PocketBase
|
||||
const record = await pb.collection('aegis_tickets').create({
|
||||
author: currentUser.id,
|
||||
company: currentUser.company,
|
||||
category: payload.category,
|
||||
subject: payload.subject,
|
||||
description: fullDescription,
|
||||
status: 'Ouvert',
|
||||
});
|
||||
|
||||
return record.id; // On retourne l'ID pour la redirection
|
||||
} catch (err) {
|
||||
console.error("Erreur lors de la création du ticket :", err);
|
||||
setError("Impossible d'enregistrer le ticket sur le serveur sécurisé.");
|
||||
return null;
|
||||
} finally {
|
||||
setIsSubmitting(false);
|
||||
}
|
||||
};
|
||||
|
||||
return { createTicket, isSubmitting, error };
|
||||
};
|
||||
Reference in New Issue
Block a user