Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 | import { useState, useEffect } from 'react';
import { useNavigate, useParams } from 'react-router-dom';
import { ArrowLeft, Bug, Key, Save, ListChecks } from 'lucide-react';
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
import api from '../../services/api';
import toast from 'react-hot-toast';
import { useOrganizationStore } from '../../store/organizationStore';
import CronPresetPicker from '../components/CronPresetPicker';
export default function ScheduleEdit() {
const navigate = useNavigate();
const { id } = useParams();
const { currentOrganization } = useOrganizationStore();
const orgId = currentOrganization?.id;
const queryClient = useQueryClient();
const isNew = id === 'new';
const [cronExpression, setCronExpression] = useState('*/30 * * * *');
const [selectedCrawlerId, setSelectedCrawlerId] = useState('');
const [selectedApiKeyId, setSelectedApiKeyId] = useState('');
const { data: schedule } = useQuery({
queryKey: ['schedule', id],
queryFn: async () => {
if (isNew) return null;
const response = await api.get(`/schedules/${id}`);
return response.data.data;
},
enabled: !isNew,
});
useEffect(() => {
if (schedule) {
setCronExpression(schedule.cronExpression);
setSelectedCrawlerId(schedule.configId);
setSelectedApiKeyId(schedule.apiKeyId || '');
}
}, [schedule]);
const { data: crawlers, isLoading: loadingCrawlers } = useQuery({
queryKey: ['crawlers', orgId],
queryFn: async () => {
const response = await api.get('/crawlers');
return response.data.data || [];
},
enabled: !!orgId,
});
const { data: apiKeys } = useQuery({
queryKey: ['api-keys', orgId],
queryFn: async () => {
const response = await api.get('/api-keys');
return response.data.data || [];
},
enabled: !!orgId,
});
const saveSchedule = useMutation({
mutationFn: async () => {
const payload = {
configId: selectedCrawlerId,
cronExpression,
apiKeyId: selectedApiKeyId || null,
organizationId: orgId,
};
if (isNew) {
return api.post('/schedules', payload);
} else {
return api.put(`/schedules/${id}`, payload);
}
},
onSuccess: () => {
toast.success(isNew ? 'Planification créée' : 'Planification mise à jour');
queryClient.invalidateQueries(['schedules', orgId]);
navigate(`/${orgId}/backoffice/schedules`);
},
onError: (err) => {
toast.error(err.response?.data?.message || 'Erreur');
},
});
const selectedCrawler = crawlers?.find(c => (c._id || c.id) === selectedCrawlerId);
return (
<div className="space-y-6">
<div className="flex items-center">
<button
onClick={() => navigate(`/${orgId}/backoffice/schedules`)}
className="mr-4 p-2 text-gray-600 hover:text-gray-900 hover:bg-gray-100 rounded-lg"
>
<ArrowLeft className="w-6 h-6" />
</button>
<div>
<h2 className="text-2xl font-bold text-gray-900">
{isNew ? 'Nouvelle planification' : 'Modifier la planification'}
</h2>
<p className="text-gray-600">
{schedule ? `${schedule.cronExpression}` : 'Programmer un crawl récurrent'}
</p>
</div>
</div>
<div className="bg-white rounded-lg shadow p-6">
<div className="flex items-center mb-4">
<Bug className="w-6 h-6 text-indigo-600 mr-2" />
<h3 className="text-lg font-medium text-gray-900">Crawler à exécuter</h3>
</div>
{loadingCrawlers ? (
<div className="animate-pulse h-10 bg-gray-100 rounded-lg" />
) : (
<select
value={selectedCrawlerId}
onChange={(e) => setSelectedCrawlerId(e.target.value)}
className="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-indigo-500"
>
<option value="">Sélectionnez un crawler...</option>
{crawlers?.map((c) => (
<option key={c._id || c.id} value={c._id || c.id}>
{c.name} {c.description ? `— ${c.description}` : ''}
</option>
))}
</select>
)}
</div>
<div className="bg-white rounded-lg shadow p-6">
<CronPresetPicker value={cronExpression} onChange={setCronExpression} />
</div>
<div className="bg-white rounded-lg shadow p-6">
<div className="flex items-center mb-4">
<Key className="w-6 h-6 text-amber-600 mr-2" />
<h3 className="text-lg font-medium text-gray-900">Clé API</h3>
</div>
<select
value={selectedApiKeyId}
onChange={(e) => setSelectedApiKeyId(e.target.value)}
className="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-indigo-500"
>
<option value="">Sélectionnez une clé...</option>
{apiKeys?.map((ak) => (
<option key={ak._id || ak.id} value={ak._id || ak.id}>
{ak.name} — {ak.keyPrefix}...
</option>
))}
</select>
<p className="text-xs text-gray-500 mt-2">
La régénération d'une clé mettra automatiquement à jour les planifications qui l'utilisent.
</p>
</div>
{!isNew && (
<button
onClick={() => navigate(`/${orgId}/backoffice/jobs?scheduleId=${id}`)}
className="w-full flex items-center justify-center gap-2 px-4 py-3 border border-indigo-200 text-indigo-700 bg-indigo-50 rounded-lg hover:bg-indigo-100"
>
<ListChecks className="w-5 h-5" />
Voir les exécutions de cette planification
</button>
)}
<div className="flex justify-end gap-3">
<button
onClick={() => navigate(`/${orgId}/backoffice/schedules`)}
className="px-4 py-2 border border-gray-300 text-gray-700 rounded-lg hover:bg-gray-50"
>
Annuler
</button>
<button
onClick={() => saveSchedule.mutate()}
disabled={saveSchedule.isLoading || !selectedCrawlerId || !selectedApiKeyId}
className="flex items-center px-4 py-2 bg-indigo-600 text-white rounded-lg hover:bg-indigo-700 disabled:opacity-50"
>
<Save className="w-5 h-5 mr-2" />
{saveSchedule.isLoading ? 'Sauvegarde...' : (isNew ? 'Créer' : 'Enregistrer')}
</button>
</div>
</div>
);
}
|