gohorsejobs/frontend/fix-lines.js

83 lines
2.9 KiB
JavaScript

const fs = require('fs');
let lines = fs.readFileSync('c:/dev/gohorsejobs/frontend/src/app/dashboard/backoffice/page.tsx', 'utf8').split('\n');
// 1. Add import ConfirmModal near lucide-react (line 45)
for (let i = 0; i < lines.length; i++) {
if (lines[i].includes('import { Archive, CheckCircle, Copy, ExternalLink, PauseCircle, Plus, RefreshCw, XCircle } from "lucide-react"')) {
lines.splice(i + 1, 0, 'import { ConfirmModal } from "@/components/confirm-modal"');
break;
}
}
// 2. Add state deletePlanId near editingPlanId (around line 96)
for (let i = 0; i < lines.length; i++) {
if (lines[i].includes('const [editingPlanId, setEditingPlanId] = useState<string | null>(null)')) {
lines.splice(i + 1, 0, ' const [deletePlanId, setDeletePlanId] = useState<string | null>(null)');
break;
}
}
// 3. Replace handleDeletePlan block
let startIdx = -1;
let endIdx = -1;
for (let i = 0; i < lines.length; i++) {
if (lines[i].includes('const handleDeletePlan = async (id: string) => {')) {
startIdx = i;
}
if (startIdx !== -1 && i > startIdx && lines[i].includes('} catch (error) {')) {
// found end of try block
}
if (startIdx !== -1 && i > startIdx && lines[i].trim() === '}') {
endIdx = i;
break;
}
}
if (startIdx !== -1 && endIdx !== -1) {
const newFunc = [
' const handleDeletePlan = async (id: string) => {',
' setDeletePlanId(id)',
' }',
'',
' const confirmDeletePlan = async () => {',
' if (!deletePlanId) return',
' try {',
' await plansApi.delete(deletePlanId)',
' toast.success("Plan deleted")',
' loadBackoffice(true)',
' } catch (error) {',
' toast.error("Failed to delete plan")',
' } finally {',
' setDeletePlanId(null)',
' }',
' }'
].map(l => l + (lines[0].endsWith('\\r') ? '\\r' : ''));
lines.splice(startIdx, endIdx - startIdx + 1, ...newFunc);
}
// 4. Add ConfirmModal before last closing </div>
let lastDivIdx = -1;
for (let i = lines.length - 1; i >= 0; i--) {
if (lines[i].includes('</div>')) {
lastDivIdx = i;
break;
}
}
if (lastDivIdx !== -1) {
const modal = [
' <ConfirmModal',
' isOpen={!!deletePlanId}',
' onClose={() => setDeletePlanId(null)}',
' onConfirm={confirmDeletePlan}',
' title="Are you sure you want to delete this plan?"',
' description="This action cannot be undone."',
' />'
].map(l => l + (lines[0].endsWith('\\r') ? '\\r' : ''));
lines.splice(lastDivIdx, 0, ...modal);
}
fs.writeFileSync('c:/dev/gohorsejobs/frontend/src/app/dashboard/backoffice/page.tsx', lines.join('\n'));
console.log('done lines edit');