import React, { Suspense, useEffect, useState, useRef, lazy } from 'react'; import { Meteor } from 'meteor/meteor'; import { PoblesCollection } from '/imports/api/pobles.js'; import { useSubscribe, useTracker, useFind } from 'meteor/react-meteor-data/suspense'; import { Roles } from 'meteor/roles'; // import { useUserId } from 'meteor/react-meteor-accounts'; import { BarraNav } from "./BarraNav/BarraNav"; import { Link } from 'react-router-dom'; import { NecessitatsCollection } from '../api/necessitats'; export const Pobles = () => { const [pobleSeleccionat, setPobleSeleccionat] = useState(null); const [creantPoble, setCreantPoble] = useState(false); useSubscribe('pobles'); useSubscribe('usuaris'); // const pobles = useTracker("pobles", () => PoblesCollection.find().fetchAsync()); const pobles = useFind(PoblesCollection, [{}, {sort: {nomPoble: 1}}]); const [esEditor, setEsEditor] = useState(false); const userId = Meteor.userId(); // console.log("isAdmin: ", isAdmin) ; // (async () => { useEffect(() => { (async () => { const comprovaAdmin = await Roles.userIsInRoleAsync(userId, ["admin"]); setEsEditor(comprovaAdmin); })(); }, []); // })(); const BotoPobleAmbIndicadors = ({pob}) => { useSubscribe('necessitats', pob.ambitAssociat); const necessitats = useFind(NecessitatsCollection, [{"poble.ambitAssociat": pob.ambitAssociat}]); const [monitors, setMonitors] = useState(false); const [encarregats, setEncarregats] = useState(false); const [voluntaris, setVoluntaris] = useState(false); // let mon, mons, enc, encs, vol, vols; (async () => { const mons = await Roles.getUsersInRoleAsync("monitor_de_poble", pob?.ambitAssociat); // console.log("monitors: ", monitors); setMonitors(mons?.fetch().length || false); const encs = await Roles.getUsersInRoleAsync("encarregat_de_tasques", pob?.ambitAssociat); // console.log("monitors: ", monitors); setEncarregats(encs?.fetch().length || false); const vols = await Roles.getUsersInRoleAsync("voluntari_de_poble", pob?.ambitAssociat); // console.log("monitors: ", monitors); setVoluntaris(vols?.fetch().length || false); })(); return
  • {// Indicadors de Responsables
    { monitors ? {monitors} : false } { encarregats ? {encarregats} : false } { voluntaris ? {voluntaris} : false }
    } {// Indicadors de Necessitats
    { necessitats.length ? {necessitats.length } : false }
    } {// Botó de creació de nou poble {pob.nomPoble} {esEditor && } }
  • ; } const QuadreInfo_Poble = () => { const refInAmbitAssignat = useRef(); return
    {pobleSeleccionat &&

    {pobleSeleccionat._id}

    }
    { try { Meteor.callAsync('editaOAfigPoble', { ...pobleSeleccionat || [], nomPoble: d.get('nomPoble'), cp: d.get('cp') || "", comarca: d.get('comarca') || "", ambitAssociat: d.get('ambit-associat') }).then(() => setPobleSeleccionat(null)) .catch(err => console.error(err)); } catch (err) { alert(err); console.error(err); } }} > refInAmbitAssignat.current.value = ev.target.value.replace(/ +/g, '-').toLowerCase()} />


    { esEditor && <>

    Àmbit associat: {/* // nomPoble.replace(/ +/g, '-').toLowerCase()}/> */}

    }
    ; }; // const Indicador = ({valor, ambit, }) => { // }; return <>

    Pobles

    { esEditor && (pobleSeleccionat || creantPoble) && } {esEditor && } ; };