201 lines
8.7 KiB
JavaScript
201 lines
8.7 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';
|
|
import { BarraNav } from "./BarraNav/BarraNav";
|
|
|
|
|
|
|
|
|
|
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 QuadreInfo_Necessitats = () => {
|
|
|
|
return <div style={{
|
|
display: `inline-block`,
|
|
border: `1px solid #6666`,
|
|
padding: `.5rem`,
|
|
borderRadius: `.3em`,
|
|
backgroundColor: `lightcyan`
|
|
}}>
|
|
|
|
{necessitatSeleccionada && <h2>{necessitatSeleccionada._id}</h2>}
|
|
<h1>Necessitats</h1>
|
|
|
|
<form
|
|
action={d => {
|
|
if (d.get('selTipus'))
|
|
try {
|
|
const tipusSeleccionat = tipus.find(t => t._id === d.get('selTipus')) || "";
|
|
const pobleSeleccionat = pobles.find(p => p._id === d.get('selPoble')) || "";
|
|
Meteor.callAsync('afigNecessitat', {
|
|
...necessitatSeleccionada || [],
|
|
titol: d.get('taTitol'),
|
|
tipus: tipusSeleccionat,
|
|
poble: pobleSeleccionat,
|
|
descrip: d.get('taDescripcio'),
|
|
contacte: {
|
|
nom: d.get('inContacte'),
|
|
tel: d.get('inTelefon'),
|
|
email: d.get('inEMail'),
|
|
adr: d.get('inAdreça')
|
|
}
|
|
})
|
|
.then(() => setNecessitatSeleccionada(null))
|
|
.catch(err => console.error(err))
|
|
;
|
|
} catch (err) {
|
|
alert(err);
|
|
console.error(err);
|
|
}
|
|
}}
|
|
>
|
|
|
|
<label htmlFor="taTitol">Títol</label> <br />
|
|
<textarea name='taTitol' placeholder='Títol de la necessitat...' defaultValue={ necessitatSeleccionada ? necessitatSeleccionada.titol : ""} ></textarea>
|
|
|
|
<br /><br />
|
|
|
|
<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"
|
|
formatCreateLabel={(inputValue) => "Crear nou tipus..."}
|
|
defaultOptions={tipus.map((v,i) => ({value: v._id, label: v.titol})).sort((a,b) => a.label.toLowerCase() > b.label.toLowerCase()) }
|
|
onCreateOption={(inputValue) => Meteor.callAsync('afigTipus', {titol: inputValue})}
|
|
defaultValue={ necessitatSeleccionada ? { value: necessitatSeleccionada.tipus._id, label: necessitatSeleccionada.tipus.titol} : ""}
|
|
// 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="poble">Poble</label>
|
|
|
|
<CreatableSelect name="selPoble" options={pobles.map((v,i) => ({value: v._id, label: v.nomPoble})) }
|
|
defaultValue={ necessitatSeleccionada ? { value: necessitatSeleccionada.poble._id, label: necessitatSeleccionada.poble.nomPoble} : ""}
|
|
/>
|
|
{/* <select name="selPoble">
|
|
{
|
|
pobles.map((v,i) => <option key={`optPob${i}`}>{v.nomPoble}</option>)
|
|
}
|
|
</select> */}
|
|
|
|
<br />
|
|
|
|
<label htmlFor="taDescripcio">Descripció: </label> <br />
|
|
<textarea name="taDescripcio" id="taDescripcio" placeholder='Observacions i detalls particulars...'
|
|
defaultValue={ necessitatSeleccionada ? necessitatSeleccionada?.descrip : ""}
|
|
></textarea> <br />
|
|
|
|
<br />
|
|
|
|
<fieldset style={{
|
|
borderRadius: `.4em`,
|
|
border: `1px solid #6666`
|
|
}}>
|
|
<legend>Contacte</legend>
|
|
<input required type="text" name="inContacte" id="inContacte" placeholder='Nom'
|
|
defaultValue={ necessitatSeleccionada ? necessitatSeleccionada.contacte?.nom : ""}
|
|
/> <br />
|
|
<input required type="text" name="inTelefon" id="inTelefon" placeholder='Telèfon'
|
|
defaultValue={ necessitatSeleccionada ? necessitatSeleccionada.contacte?.tel : ""}
|
|
/> <br />
|
|
<input required type="text" name="inEMail" id="inEMail" placeholder='Correu electrònic'
|
|
defaultValue={ necessitatSeleccionada ? necessitatSeleccionada.contacte?.email : ""}
|
|
/> <br />
|
|
<input type="text" name="inAdreça" id="inAdreça" placeholder='Adreça'
|
|
defaultValue={ necessitatSeleccionada ? necessitatSeleccionada.contacte?.adr : ""}
|
|
/> <br />
|
|
</fieldset>
|
|
|
|
<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 />
|
|
|
|
<ul style={{
|
|
display: `flex`,
|
|
flexWrap: `wrap`,
|
|
justifyContent: `space-evenly`,
|
|
rowGap: `.3em`,
|
|
alignContent: `space-around`
|
|
}}>{
|
|
necessitats
|
|
.sort((a,b) => a.titol?.toLowerCase() > b.titol?.toLowerCase())
|
|
.map(nec => <li
|
|
key={`nec_${nec._id}`}
|
|
style={{
|
|
display: `block`,
|
|
border: `1px solid #6666`,
|
|
borderRadius: `.4em`,
|
|
padding: `4px`,
|
|
listStyle: `none`,
|
|
backgroundColor: `${'lightgreen' || 'lightcoral'}`
|
|
}}
|
|
>
|
|
{nec.titol} {esEditor && <button onClick={() => {setNecessitatSeleccionada(nec)}}>Edita</button>}</li>)
|
|
}</ul>
|
|
</Suspense>;
|
|
}; |