ReactJS - Limpiar Formulario Al Grabar

<<>>
 
Vista:
sin imagen de perfil

Limpiar Formulario Al Grabar

Publicado por David (1 intervención) el 03/10/2024 02:46:04
Hola, por favor su ayuda. Tengo un formulario que al crear un registro lo guarda sin problema, pero cuando lo cierro y vuelvo a crear un registro me muestra la información anterior.

const handleSubmit = async e => {
e.preventDefault()

dispatch({ type: ACTIONS.LOADING })

if (!hcData.diagnostico) {
dispatch({
type: ACTIONS.ERROR_MISSING_REQUIRED_FIELD,
payload: 'Al menos diagnostico es requerido grabar datos',
})
// console.warn('Insuficientes datos para grabar')
return
}

try {
if (!hcRef && !state.tempEntryRef) {
console.log('>>> This is a new hc')
// This is a new hc, so we need a date of creation
const { num_id, nombres, apellidos, fecha_nacimiento } = patientData

const fecha_creado = Date.now()

await mutate(
{
...hcData,
fecha_creado,
patientData: { num_id, nombres, apellidos, fecha_nacimiento },
},
false
)

const response = await axios.post('/api/createHc', {
hcData: {
...hcData,
fecha_creado,
patientData: { num_id, nombres, apellidos, fecha_nacimiento },
},
patientId: patientData.num_id,
})

dispatch({
type: ACTIONS.SET_TEMP_ENTRY_REF,
payload: response.data.ref,
})

// trigger a revalidation (refetch) to make sure our local data is correct
await mutate()

const newHcListItem = [
response.data.fecha_creado,
response.data.diagnostico,
response.data.ref,
]

updateHcsList(newHcListItem)
e.target.reset()
} else {
// This is a hc update
const update = await axios.put('/api/updateHc', {
hcData,
ref: state.tempEntryRef ? state.tempEntryRef : hcRef,
})

// trigger a revalidation (refetch) to make sure our local data is correct
await mutate()

const updatedItem = [
update.data.fecha_creado,
update.data.diagnostico,
update.data.ref,
]

updateHcsList(
updatedItem,
state.tempEntryRef ? state.tempEntryRef : hcRef
)
}

dispatch({
type: ACTIONS.DATA_SAVE_UPDATE_SUCCESS,
payload: 'Datos grabados correctamente!',

})
} catch (error) {
// console.error(error)

dispatch({
type: ACTIONS.DATA_SAVE_UPDATE_ERROR,
payload: 'Error al grabar datos en la base de datos!',
})
}
}

No sé cómo borrar el formulario cuando guardo un nuevo registro. Agradezco tu ayuda.
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder