159 lines
6.0 KiB
JavaScript
159 lines
6.0 KiB
JavaScript
import React, { Suspense, useEffect, useState, useRef, lazy } from 'react';
|
|
import { Meteor } from 'meteor/meteor';;
|
|
import { NecessitatsCollection } from '/imports/api/necessitats.js';
|
|
import { PoblesCollection } from '../api/pobles';
|
|
import { TipusCollection } from '../api/tipus';
|
|
import { useSubscribe, useTracker, useFind } from 'meteor/react-meteor-data/suspense';
|
|
import { Roles } from 'meteor/roles';
|
|
// import { useUserId } from 'meteor/react-meteor-accounts';
|
|
// import Select from 'react-select'
|
|
|
|
import CreatableSelect from 'react-select/creatable';
|
|
import AsyncCreatableSelect from 'react-select/async-creatable';
|
|
|
|
|
|
|
|
|
|
export const Necessitats = () => {
|
|
|
|
// const [permes, setPermes] = useState(false);
|
|
const [esEditor, setEsEditor] = useState(false);
|
|
const userId = Meteor.userId();
|
|
|
|
useEffect(() => {
|
|
(async () => {
|
|
const comprovaAdmin = await Roles.userIsInRoleAsync(userId, ["admin"]);
|
|
setEsEditor(comprovaAdmin);
|
|
})();
|
|
}, []);
|
|
|
|
// console.log("isAdmin: ", isAdmin) ;
|
|
|
|
const [necessitatSeleccionada, setNecessitatSeleccionada] = useState(null);
|
|
|
|
useSubscribe('necessitats');
|
|
const necessitats = useTracker("necessitats", () => NecessitatsCollection.find().fetchAsync());
|
|
|
|
useSubscribe('pobles');
|
|
const pobles = useTracker("pobles", () => PoblesCollection.find().fetchAsync());
|
|
|
|
useSubscribe('tipus');
|
|
const tipus = useTracker("tipus", () => TipusCollection.find().fetchAsync());
|
|
|
|
|
|
const filterColors = (inputValue) => {
|
|
return colourOptions.filter((i) =>
|
|
i.label.toLowerCase().includes(inputValue.toLowerCase())
|
|
);
|
|
};
|
|
|
|
|
|
const QuadreInfo_Necessitats = () => {
|
|
|
|
return <div style={{
|
|
display: `inline-block`,
|
|
border: `1px solid #6666`,
|
|
padding: `.5rem`,
|
|
borderRadius: `.3em`,
|
|
backgroundColor: `lightcyan`
|
|
}}>
|
|
{ esEditor && <div style={{
|
|
color: `red`
|
|
}}>ADMINISTRADOR</div> }
|
|
{necessitatSeleccionada && <h1>{necessitatSeleccionada._id}</h1>}
|
|
<h1>Necessitats</h1>
|
|
|
|
<form
|
|
action={d => {
|
|
if (d.get('selTipus'))
|
|
try {
|
|
Meteor.callAsync('editaOAfigNecessitat', {
|
|
...necessitatSeleccionada || [],
|
|
titol: d.get('titol'),
|
|
tipus: d.get('selTipus') || "",
|
|
poble: d.get('selPoble')
|
|
})
|
|
.then(() => setNecessitatSeleccionada(null))
|
|
.catch(err => console.error(err))
|
|
;
|
|
} catch (err) {
|
|
alert(err);
|
|
console.error(err);
|
|
}
|
|
}}
|
|
>
|
|
<label htmlFor="tipus">Tipus: </label>
|
|
{/* Si
|
|
l'usuari és ADMINISTRADOR o té permisos de responsabilitat, podrà crear nous Tipus al CREAR o a l'EDITAR
|
|
si no
|
|
tansols podrà assignar a un tipus predefinit o al de ALTRES
|
|
*/}
|
|
<AsyncCreatableSelect
|
|
name="selTipus"
|
|
defaultOptions={tipus.map((v,i) => ({value: v, label: v.titol})) }
|
|
onCreateOption={(inputValue) => Meteor.callAsync('afigTipus', {titol: inputValue})}
|
|
// loadOptions={tipus.map((v,i) => ({value: v, label: v.titol}))}
|
|
/>
|
|
|
|
{/* <select name="selTipus">
|
|
<option value="moble">Moble</option>
|
|
<option value="maquinari">Maquinari</option>
|
|
<option value="menjar">Menjar i beguda</option>
|
|
<option value="infantil">Infantils</option>
|
|
<option value="higiene">Higiene</option>
|
|
</select> */}
|
|
|
|
<br />
|
|
|
|
<label htmlFor="titol">Títol: </label>
|
|
<input type="text" name='titol' />
|
|
|
|
<br /><br />
|
|
|
|
<label htmlFor="poble">Poble: </label>
|
|
|
|
<CreatableSelect name="selPoble" options={pobles.map((v,i) => ({value: v, label: v.nomPoble})) } />
|
|
{/* <select name="selPoble">
|
|
{
|
|
pobles.map((v,i) => <option key={`optPob${i}`}>{v.nomPoble}</option>)
|
|
}
|
|
</select> */}
|
|
|
|
<br />
|
|
|
|
<input type='submit' value="Enviar" />
|
|
|
|
{necessitatSeleccionada && esEditor && <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));
|
|
setNecessitatSeleccionada(null);
|
|
}
|
|
}}>Elimina</button>}
|
|
</form>
|
|
</div>;
|
|
};
|
|
|
|
|
|
return <Suspense fallback={<>Carregant...</>} >
|
|
|
|
<QuadreInfo_Necessitats />
|
|
|
|
<br /><br />
|
|
|
|
{/* <AsyncCreatableSelect
|
|
cacheOptions
|
|
defaultOptions={tipus.map((v,i) => ({value: v, label: v.titol})) }
|
|
loadOptions={tipus.map((v,i) => ({value: v, label: v.titol}))}
|
|
/> */}
|
|
|
|
{/* <SelectSearch options={options} value="sv" name="language" placeholder="Choose your language" /> */}
|
|
|
|
|
|
{/* <ul>{
|
|
necessitats
|
|
.sort((a,b) => a.nomPoble?.toLowerCase() > b.nomPoble?.toLowerCase())
|
|
.map(pob => <li key={`pob_${pob._id}`}>{pob.nomPoble}{esEditor && <button onClick={() => {setPobleSeleccionat(pob)}}>Edita</button>}</li>)
|
|
}</ul> */}
|
|
</Suspense>;
|
|
}; |