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