Millorant pobles
This commit is contained in:
parent
49e5c21482
commit
4b577f9094
@ -1,4 +1,4 @@
|
|||||||
import React, { Suspense } from 'react';
|
import React, { Suspense, useEffect, useState, useRef } from 'react';
|
||||||
import { Meteor } from 'meteor/meteor';
|
import { Meteor } from 'meteor/meteor';
|
||||||
import { BrowserRouter, Routes, Route, Link } from 'react-router-dom';
|
import { BrowserRouter, Routes, Route, Link } from 'react-router-dom';
|
||||||
import { Login } from './Login';
|
import { Login } from './Login';
|
||||||
@ -7,50 +7,56 @@ import { PoblesCollection } from '/imports/api/pobles.js';
|
|||||||
import { useSubscribe, useTracker, useFind } from 'meteor/react-meteor-data/suspense';
|
import { useSubscribe, useTracker, useFind } from 'meteor/react-meteor-data/suspense';
|
||||||
|
|
||||||
|
|
||||||
const AfegirPoblacio = () => {
|
const Pobles = () => {
|
||||||
useSubscribe('pobles');
|
|
||||||
|
|
||||||
|
const [pobleSeleccionat, setPobleSeleccionat] = useState(null);
|
||||||
|
useSubscribe('pobles');
|
||||||
const pobles = useTracker("pobles", () => PoblesCollection.find().fetchAsync());
|
const pobles = useTracker("pobles", () => PoblesCollection.find().fetchAsync());
|
||||||
|
|
||||||
return <Suspense fallback={<>Carregant...</>} >
|
const QuadreInfo_Poble = () => {
|
||||||
<form action={d => {
|
|
||||||
|
return <>
|
||||||
|
<h1>{pobleSeleccionat._id}</h1>
|
||||||
|
<form action={d => {
|
||||||
try {
|
try {
|
||||||
Meteor.callAsync('afigPoble',
|
Meteor.callAsync('editaOAfigPoble',
|
||||||
{
|
{
|
||||||
createdAt: new Date(),
|
createdAt: new Date(),
|
||||||
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
|
||||||
).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="nomPoblacio">Població: </label><input name="nomPoble" type="text" /><br />
|
<label htmlFor="nomPoble">Població: </label><input name="nomPoble" type="text" defaultValue={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="Crear la Població" />
|
<input type='submit' value="Crear la Població" />
|
||||||
</form>
|
</form></>;
|
||||||
|
};
|
||||||
|
|
||||||
|
return <Suspense fallback={<>Carregant...</>} >
|
||||||
|
{pobleSeleccionat && <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}</li>)
|
.map(pob => <li key={`pob_${pob._id}`}>{pob.nomPoble}<button onClick={() => {setPobleSeleccionat(pob)}}>Edita</button><button>×</button></li>)
|
||||||
}</ul>
|
}</ul>
|
||||||
</Suspense>;
|
</Suspense>;
|
||||||
};
|
};
|
||||||
|
|
||||||
const Loguejat = () => {
|
const Loguejat = () => {
|
||||||
const userId = useUserId();
|
|
||||||
|
|
||||||
// const user = await Meteor.userAsync();
|
const userId = useUserId();
|
||||||
|
|
||||||
return userId ? <div>
|
return userId ? <div>
|
||||||
|
|
||||||
<Link to="/afegir_poblacio" >Afegir Població</Link>
|
<Link to="/pobles" >Pobles</Link>
|
||||||
|
|
||||||
<button onClick={() => Meteor.logout()} >Logout</button>
|
<button onClick={() => Meteor.logout()} >Logout</button>
|
||||||
|
|
||||||
@ -87,7 +93,7 @@ export const App = () => (
|
|||||||
<BrowserRouter>
|
<BrowserRouter>
|
||||||
<Routes>
|
<Routes>
|
||||||
<Route path="/" element={ <Loguejat /> } />
|
<Route path="/" element={ <Loguejat /> } />
|
||||||
<Route path="/afegir_poblacio" element={ <AfegirPoblacio /> } />
|
<Route path="/pobles" element={ <Pobles /> } />
|
||||||
<Route path="/login" element={ <Login /> } />
|
<Route path="/login" element={ <Login /> } />
|
||||||
</Routes>
|
</Routes>
|
||||||
</BrowserRouter>
|
</BrowserRouter>
|
||||||
|
|||||||
@ -182,7 +182,7 @@ Meteor.startup(async () => {
|
|||||||
|
|
||||||
Meteor.methods({
|
Meteor.methods({
|
||||||
|
|
||||||
'afigPoble': async function (poble) {
|
'editaOAfigPoble': async function (poble) {
|
||||||
try {
|
try {
|
||||||
if (poble.nomPoble) {
|
if (poble.nomPoble) {
|
||||||
return await PoblesCollection.insertAsync({
|
return await PoblesCollection.insertAsync({
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user