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";
const isClickInsideRectangle = (e, element) => {
const r = element?.getBoundingClientRect();
return (
e.clientX > r.left &&
e.clientX < r.right &&
e.clientY > r.top &&
e.clientY < r.bottom
);
};
const AssignadorDeRols = ({pobles, ambitGeneral, esEditor, rols, usrSeleccionat}) => {
const [creantRol, setCreantRol] = useState(false);
const [ambitsUSel, setAmbitsUSel] = useState([]);
useEffect(() => {
(async () => {
const ambits = await Roles.getScopesForUserAsync(usrSeleccionat?._id);
setAmbitsUSel(ambits);
})();
}, []);
return
Rols
Àmbits
General:
{
rols?.map(uRol => -
{uRol}
{esEditor && }
)
}
{/*
*/}
{
ambitsUSel?.map(async aus => {
const uRol = await Roles.getRolesForUserAsync(usrSeleccionat._id, {scope: aus, onlyScoped: true});
return <>
{aus}:
{
{uRol}
{esEditor && }
}
>;
})
}
{/*
*/}
{esEditor && creantRol &&
}
{esEditor && usrSeleccionat &&
}
;
};
export const Usuaris = () => {
const refQuadreSeleccionat = useRef();
const [usrSeleccionat, setUsrSeleccionat] = useState(null);
useSubscribe('pobles');
const pobles = useTracker("pobles", () => PoblesCollection.find().fetchAsync());
const [esEditor, setEsEditor] = useState(false);
const userId = Meteor.userId();
const usuaris = useTracker("usuaris", () => {
Meteor.subscribe('usuaris');
return Meteor.users.find().fetch()//.filter(u => u._id !== Meteor.userId());
});
const rols = useTracker("rols", async () => {
return await Roles.getRolesForUserAsync(usrSeleccionat?._id);
});
// console.log("rols: ", rols);
// console.log("isAdmin: ", isAdmin) ;
// console.log("usuaris: ", usuaris);
// (async () => {
useEffect(() => {
(async () => {
const comprovaAdmin = await Roles.userIsInRoleAsync(userId, ["admin"]);
setEsEditor(comprovaAdmin);
})();
}, []);
// })();
const QuadreInfo_Usuari = () => {
// const [rols, setRols] = useState([]);
const [ambitGeneral, setAmbitGeneral] = useState(false);
let allScopes;
// useEffect( async () => {
// allScopes = await Roles.getScopesForUserAsync(usrSeleccionat?._id);
// // Roles.getRolesForUser(usrSeleccionat?._id).map(uRol => {uRol});
// // setRols(rols);
// });
return
{usrSeleccionat &&
{usrSeleccionat._id}
}
{ esEditor &&
}
;
};
return {
!isClickInsideRectangle(ev, refQuadreSeleccionat.current) && setUsrSeleccionat(null);
}}>
Carregant...>} >
Usuaris
{ esEditor && usrSeleccionat && }
{
usuaris
.sort((a,b) => a.username?.toLowerCase() > b.username?.toLowerCase())
.map(usr => -
{usr.username}{esEditor && }
)
}
{esEditor && !usrSeleccionat && }
;
};