89 lines
2.9 KiB
JavaScript
89 lines
2.9 KiB
JavaScript
const fs = require('fs');
|
|
let code = fs.readFileSync('src/hooks/useHospitalStore.ts', 'utf8');
|
|
|
|
// add import
|
|
code = code.replace("Laboratorio, ", "Laboratorio, OtroLaboratorio, ");
|
|
|
|
// add state array
|
|
code = code.replace("laboratorios: Laboratorio[];", "laboratorios: Laboratorio[];\n otrosLaboratorios: OtroLaboratorio[];");
|
|
|
|
// add actions
|
|
const actionsRegex = /eliminarLaboratorio: \(id: string\) => Promise<void>;/;
|
|
code = code.replace(actionsRegex, "eliminarLaboratorio: (id: string) => Promise<void>;\n agregarOtroLaboratorio: (laboratorio: Omit<OtroLaboratorio, 'id'>) => Promise<void>;\n actualizarOtroLaboratorio: (id: string, datos: Partial<OtroLaboratorio>) => Promise<void>;\n eliminarOtroLaboratorio: (id: string) => Promise<void>;");
|
|
|
|
// add initial state
|
|
code = code.replace("laboratorios: [],", "laboratorios: [],\n otrosLaboratorios: [],");
|
|
|
|
// fetch action (loadState)
|
|
code = code.replace("laboratorios: data.laboratorios || [],", "laboratorios: data.laboratorios || [],\n otrosLaboratorios: data.otrosLaboratorios || [],");
|
|
|
|
// methods
|
|
const methodsString = `
|
|
eliminarLaboratorio: async (id) => {
|
|
try {
|
|
await api.delete(\`/laboratorios/\${id}\`);
|
|
set((state) => ({
|
|
laboratorios: state.laboratorios.filter((l) => l.id !== id),
|
|
}));
|
|
} catch (error) {
|
|
console.error('Error deleting laboratorio:', error);
|
|
throw error;
|
|
}
|
|
},
|
|
|
|
agregarOtroLaboratorio: async (laboratorio) => {
|
|
try {
|
|
const response = await api.post('/otros-laboratorios', laboratorio);
|
|
set((state) => ({
|
|
otrosLaboratorios: [...state.otrosLaboratorios, response.data],
|
|
}));
|
|
} catch (error) {
|
|
console.error('Error adding otro laboratorio:', error);
|
|
throw error;
|
|
}
|
|
},
|
|
|
|
actualizarOtroLaboratorio: async (id, datos) => {
|
|
try {
|
|
await api.put(\`/otros-laboratorios/\${id}\`, datos);
|
|
set((state) => ({
|
|
otrosLaboratorios: state.otrosLaboratorios.map((l) =>
|
|
l.id === id ? { ...l, ...datos } : l
|
|
),
|
|
}));
|
|
} catch (error) {
|
|
console.error('Error updating otro laboratorio:', error);
|
|
throw error;
|
|
}
|
|
},
|
|
|
|
eliminarOtroLaboratorio: async (id) => {
|
|
try {
|
|
await api.delete(\`/otros-laboratorios/\${id}\`);
|
|
set((state) => ({
|
|
otrosLaboratorios: state.otrosLaboratorios.filter((l) => l.id !== id),
|
|
}));
|
|
} catch (error) {
|
|
console.error('Error deleting otro laboratorio:', error);
|
|
throw error;
|
|
}
|
|
},
|
|
`;
|
|
|
|
const replaceMethods = `
|
|
eliminarLaboratorio: async (id) => {
|
|
try {
|
|
await api.delete(\`/laboratorios/\${id}\`);
|
|
set((state) => ({
|
|
laboratorios: state.laboratorios.filter((l) => l.id !== id),
|
|
}));
|
|
} catch (error) {
|
|
console.error('Error deleting laboratorio:', error);
|
|
throw error;
|
|
}
|
|
},
|
|
`;
|
|
|
|
code = code.replace(replaceMethods.trim(), methodsString.trim());
|
|
fs.writeFileSync('src/hooks/useHospitalStore.ts', code);
|