Edita i esborra pobles
This commit is contained in:
parent
1f1ff5c2cd
commit
6035c20831
@ -16,38 +16,44 @@ const Pobles = () => {
|
||||
const QuadreInfo_Poble = () => {
|
||||
|
||||
return <>
|
||||
<h1>{pobleSeleccionat._id}</h1>
|
||||
{pobleSeleccionat && <h1>{pobleSeleccionat._id}</h1>}
|
||||
|
||||
<form action={d => {
|
||||
try {
|
||||
Meteor.callAsync('editaOAfigPoble',
|
||||
{
|
||||
createdAt: new Date(),
|
||||
...pobleSeleccionat || [],
|
||||
nomPoble: d.get('nomPoble'),
|
||||
cp: d.get('cp') || "",
|
||||
comarca: d.get('comarca') || "",
|
||||
}, pobleSeleccionat
|
||||
).catch(err => console.error(err));
|
||||
}).then(() => setPobleSeleccionat(null))
|
||||
.catch(err => console.error(err));
|
||||
} catch (err) {
|
||||
alert(err);
|
||||
console.error(err);
|
||||
}
|
||||
}}>
|
||||
<label htmlFor="nomPoble">Població: </label><input name="nomPoble" type="text" defaultValue={pobleSeleccionat.nomPoble}/><br />
|
||||
<label htmlFor="nomPoble">Població: </label><input name="nomPoble" type="text" defaultValue={ pobleSeleccionat ? pobleSeleccionat.nomPoble : ""}/><br />
|
||||
<label htmlFor="comarca">Comarca: </label><input name="comarca" type="text" /><br />
|
||||
<label htmlFor="cp">Codi Postal: </label><input name="cp" type="text" /><br />
|
||||
<input type='submit' value="Actualitzar la Població" />
|
||||
<button onClick={() => {
|
||||
<button onClick={ev => {
|
||||
ev.preventDefault();
|
||||
if (confirm(`Vas a eliminar el poble "${pobleSeleccionat.nomPoble}". És una operació irreversible. Procedir?`)) {
|
||||
Meteor.callAsync('eliminaPoble', pobleSeleccionat._id).catch(err => console.error(err));
|
||||
setPobleSeleccionat(null);
|
||||
}
|
||||
}}>Elimina</button>
|
||||
</form></>;
|
||||
};
|
||||
|
||||
return <Suspense fallback={<>Carregant...</>} >
|
||||
{pobleSeleccionat && <QuadreInfo_Poble />}
|
||||
<QuadreInfo_Poble />
|
||||
|
||||
|
||||
<ul>{
|
||||
pobles
|
||||
.sort((a,b) => a.nomPoble.toLowerCase() > b.nomPoble.toLowerCase())
|
||||
.sort((a,b) => a.nomPoble?.toLowerCase() > b.nomPoble?.toLowerCase())
|
||||
.map(pob => <li key={`pob_${pob._id}`}>{pob.nomPoble}<button onClick={() => {setPobleSeleccionat(pob)}}>Edita</button></li>)
|
||||
}</ul>
|
||||
</Suspense>;
|
||||
|
||||
@ -185,11 +185,17 @@ Meteor.startup(async () => {
|
||||
'editaOAfigPoble': async function (poble) {
|
||||
try {
|
||||
if (poble.nomPoble) {
|
||||
return await PoblesCollection.insertAsync({
|
||||
return await PoblesCollection.upsertAsync(
|
||||
{
|
||||
_id: poble._id
|
||||
}, {
|
||||
$set: {
|
||||
...poble,
|
||||
usuari: Meteor.userId(),
|
||||
createdAt: new Date()
|
||||
});
|
||||
editedAt: new Date()
|
||||
}
|
||||
}
|
||||
);
|
||||
} else {
|
||||
throw new Error("El nom del poble no és vàlid");
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user