Generem i afegim codis
This commit is contained in:
parent
4ce2ee304d
commit
c82cd4f545
@ -1,7 +1,9 @@
|
||||
import React, { Suspense, useEffect, useState, useRef, lazy } from 'react';
|
||||
import { Meteor } from 'meteor/meteor';;
|
||||
import { Meteor } from 'meteor/meteor';
|
||||
import { Random } from 'meteor/random';
|
||||
import { NecessitatsCollection } from '/imports/api/necessitats.js';
|
||||
import { PoblesCollection } from '../api/pobles';
|
||||
import { CodisCollection } from '../api/codis';
|
||||
// import { TipusCollection } from '../api/tipus';
|
||||
import { useSubscribe, useTracker, useFind } from 'meteor/react-meteor-data/suspense';
|
||||
import { Roles } from 'meteor/roles';
|
||||
@ -12,6 +14,12 @@ import CreatableSelect from 'react-select/creatable';
|
||||
// import AsyncCreatableSelect from 'react-select/async-creatable';
|
||||
// import { BarraNav } from "./BarraNav/BarraNav";
|
||||
|
||||
// import InfiniteCalendar from 'react-infinite-calendar';
|
||||
import 'react-infinite-calendar/styles.css';
|
||||
|
||||
function generaCodi() {
|
||||
return Random.id().slice(0, 5);
|
||||
}
|
||||
|
||||
export const Codis = () => {
|
||||
|
||||
@ -33,6 +41,9 @@ export const Codis = () => {
|
||||
useSubscribe('necessitats');
|
||||
const necessitats = useTracker("necessitats", () => NecessitatsCollection.find().fetchAsync());
|
||||
|
||||
useSubscribe('codis');
|
||||
const codis = useTracker("codis", () => CodisCollection.find().fetchAsync());
|
||||
|
||||
useSubscribe('pobles');
|
||||
const pobles = useTracker("pobles", () => PoblesCollection.find().fetchAsync());
|
||||
const ambits = pobles?.map(p => p?.ambitAssociat);
|
||||
@ -60,6 +71,7 @@ export const Codis = () => {
|
||||
return <>
|
||||
<label htmlFor="selRol">Rol: </label>
|
||||
<Select
|
||||
required
|
||||
key={Math.random()}
|
||||
name="selRol"
|
||||
// filterOption={(opts) => necessitats.find(nec => nec.tipus === tipusSeleccionat._id)}
|
||||
@ -97,6 +109,10 @@ export const Codis = () => {
|
||||
}
|
||||
|
||||
const QuadreInfo_Codis = () => {
|
||||
|
||||
const [actCond, setActCond] = useState(true);
|
||||
const [codiGenerat, setCodiGenerat] = useState(generaCodi()?.toUpperCase());
|
||||
|
||||
|
||||
return <div style={{
|
||||
display: `inline-block`,
|
||||
@ -109,17 +125,49 @@ export const Codis = () => {
|
||||
{ambitSeleccionat && <h1>{ambitSeleccionat?.value}</h1>}
|
||||
<h1>Codis</h1>
|
||||
|
||||
<p>Tria el rol que adquirirà qui utilitze el codi.</p>
|
||||
{/* <button
|
||||
onClick={ev => {
|
||||
ev.preventDefault();
|
||||
ev.stopPropagation();
|
||||
|
||||
setCodiGenerat(generaCodi());
|
||||
}}
|
||||
>Genera el codi</button> */}
|
||||
|
||||
<div
|
||||
style={{
|
||||
fontWeight: `bolder`,
|
||||
textAlign: `center`,
|
||||
transform: `scale(2)`
|
||||
}}
|
||||
>
|
||||
<span
|
||||
style={{
|
||||
backgroundColor: `white`,
|
||||
border: `3px solid black`,
|
||||
padding: `0 .2em`,
|
||||
margin: `1rem auto`,
|
||||
}}
|
||||
>{codiGenerat}</span>
|
||||
</div>
|
||||
<br />
|
||||
|
||||
<p>Tria el rol que adquirirà qui utilitze el codi anterior una vegada activat</p>
|
||||
|
||||
|
||||
<form
|
||||
action={d => {
|
||||
// if (d.get('selTipus'))
|
||||
try {
|
||||
Meteor.callAsync('editaOAfigNecessitat', {
|
||||
...tipusSeleccionat || [],
|
||||
titol: d.get('titol'),
|
||||
tipus: d.get('selTipus') || "",
|
||||
poble: d.get('selPoble')
|
||||
Meteor.callAsync('editaOAfigCodi', {
|
||||
...codiGenerat || [],
|
||||
codi: codiGenerat,
|
||||
ambit: d.get('selAmbit') || "",
|
||||
rol: d.get('selRol'),
|
||||
act_abs: d.get('activ_abs'),
|
||||
act_cond: d.get('activ_cond'),
|
||||
periode_validesa_ini: d.get('periode_validesa_ini'),
|
||||
periode_validesa_fi: d.get('periode_validesa_fi')
|
||||
})
|
||||
.then(() => setAmbitSeleccionat(null))
|
||||
.catch(err => console.error(err))
|
||||
@ -138,6 +186,7 @@ export const Codis = () => {
|
||||
*/}
|
||||
|
||||
<Select
|
||||
required
|
||||
name="selAmbit"
|
||||
// formatCreateLabel={(inputValue) => "Crear nou tipus..."}
|
||||
// filterOption={filterAmbit}
|
||||
@ -178,37 +227,75 @@ export const Codis = () => {
|
||||
|
||||
<SelectorDeRol {... {ambitSeleccionat}} />
|
||||
|
||||
<br />
|
||||
|
||||
<label htmlFor="actiu">Actiu? </label>
|
||||
<input type="checkbox" name='actiu' />
|
||||
|
||||
<br /><br />
|
||||
|
||||
<label htmlFor="periode_validesa">Periode de validesa: </label>
|
||||
<input type="date" name="periode_validesa" />
|
||||
|
||||
<div style={{
|
||||
border: `1px solid #cccc`,
|
||||
borderRadius: `.4em`,
|
||||
margin: `1rem`,
|
||||
padding: `.6rem`,
|
||||
backgroundColor: `lightslategray`
|
||||
}}>
|
||||
<p>TEMPS REAL:</p>
|
||||
<label htmlFor="activ_abs"
|
||||
style={{
|
||||
color: `red`
|
||||
}}
|
||||
>Activació absoluta: </label>
|
||||
<input type="checkbox" name='activ_abs' />
|
||||
|
||||
<br />
|
||||
|
||||
{/* L'activació condicionada desactivada fa que no siguen 'required' les dates d'inici i final */}
|
||||
|
||||
<label htmlFor="activa_cond">Activació condicionada: </label>
|
||||
<input type="checkbox" name='activ_cond' checked={actCond}
|
||||
onChange={(ev) => setActCond(!actCond)}
|
||||
/>
|
||||
<p
|
||||
style={{
|
||||
color: `red`
|
||||
}}
|
||||
>ATENCIÓ al format! <b>(mm/dd/aaaa)</b></p>
|
||||
|
||||
Des de: <input required={actCond} type="date" name="periode_validesa_ini" />
|
||||
<br />
|
||||
Fins a: <input required={actCond} type="date" name="periode_validesa_fi" />
|
||||
<br />
|
||||
</div>
|
||||
|
||||
{/* <select name="selPoble">
|
||||
{/* <select name="s.
|
||||
elPoble">
|
||||
{
|
||||
pobles.map((v,i) => <option key={`optPob${i}`}>{v.nomPoble}</option>)
|
||||
}
|
||||
</select> */}
|
||||
|
||||
<br />
|
||||
|
||||
<input type='submit' value="Enviar" />
|
||||
|
||||
{ambitSeleccionat && esEditor && <button onClick={ev => {
|
||||
|
||||
|
||||
<input type='submit' value="Donar d'alta el codi" />
|
||||
|
||||
{/* {ambitSeleccionat && 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>}
|
||||
}}>Elimina</button>} */}
|
||||
</form>
|
||||
</div>;
|
||||
};
|
||||
|
||||
const
|
||||
today = new Date(),
|
||||
lastWeek = new Date(today.getFullYear(), today.getMonth(), today.getDate() - 7)
|
||||
;
|
||||
|
||||
console.log("codis: ", codis);
|
||||
|
||||
|
||||
return <Suspense fallback={<>Carregant...</>} >
|
||||
|
||||
@ -232,10 +319,11 @@ export const Codis = () => {
|
||||
rowGap: `.3em`,
|
||||
alignContent: `space-around`
|
||||
}}>{
|
||||
ambits
|
||||
.sort((a,b) => a.toLowerCase() > b.toLowerCase())
|
||||
.map(amb => <li
|
||||
key={amb}
|
||||
codis
|
||||
// .reverse()
|
||||
// .sort((a,b) => a?.codi?.editedAt < b?.codi?.editedAt)
|
||||
.map(cod => <li
|
||||
key={cod._id}
|
||||
style={{
|
||||
display: `block`,
|
||||
border: `1px solid #6666`,
|
||||
@ -245,8 +333,17 @@ export const Codis = () => {
|
||||
backgroundColor: `${'lightgreen' || 'lightcoral'}`
|
||||
}}
|
||||
>
|
||||
{amb}{esEditor && <button onClick={() => {setAmbitSeleccionat({value: amb, label: amb})}}>Edita</button>}
|
||||
{cod.codi}
|
||||
{/* {esEditor && <button onClick={() => {setAmbitSeleccionat({value: cod, label: cod})}}>Edita</button>} */}
|
||||
</li>)
|
||||
}</ul>
|
||||
|
||||
{/* <InfiniteCalendar
|
||||
width={400}
|
||||
height={600}
|
||||
selected={today}
|
||||
disabledDays={[0,6]}
|
||||
minDate={lastWeek}
|
||||
/> */}
|
||||
</Suspense>;
|
||||
};
|
||||
@ -1,23 +1,23 @@
|
||||
import React from 'react';
|
||||
import { useFind, useSubscribe } from 'meteor/react-meteor-data';
|
||||
import { LinksCollection } from '../api/links';
|
||||
// import React from 'react';
|
||||
// import { useFind, useSubscribe } from 'meteor/react-meteor-data';
|
||||
// import { LinksCollection } from '../api/links';
|
||||
|
||||
export const Info = () => {
|
||||
const isLoading = useSubscribe('links');
|
||||
const links = useFind(() => LinksCollection.find());
|
||||
// export const Info = () => {
|
||||
// const isLoading = useSubscribe('links');
|
||||
// const links = useFind(() => LinksCollection.find());
|
||||
|
||||
if(isLoading()) {
|
||||
return <div>Loading...</div>;
|
||||
}
|
||||
// if(isLoading()) {
|
||||
// return <div>Loading...</div>;
|
||||
// }
|
||||
|
||||
return (
|
||||
<div>
|
||||
<h2>Learn Meteor!</h2>
|
||||
<ul>{links.map(
|
||||
link => <li key={link._id}>
|
||||
<a href={link.url} target="_blank">{link.title}</a>
|
||||
</li>
|
||||
)}</ul>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
// return (
|
||||
// <div>
|
||||
// <h2>Learn Meteor!</h2>
|
||||
// <ul>{links.map(
|
||||
// link => <li key={link._id}>
|
||||
// <a href={link.url} target="_blank">{link.title}</a>
|
||||
// </li>
|
||||
// )}</ul>
|
||||
// </div>
|
||||
// );
|
||||
// };
|
||||
|
||||
@ -6,6 +6,7 @@ import { Roles } from 'meteor/roles';
|
||||
import { ROLS_GLOBALS, ROLS_DE_POBLE } from '../imports/roles';
|
||||
import { NecessitatsCollection } from '../imports/api/necessitats';
|
||||
import { TipusCollection } from '../imports/api/tipus';
|
||||
import { CodisCollection } from '../imports/api/codis';
|
||||
|
||||
async function insertPoble({ nomPoble, cp, comarca }) {
|
||||
await PoblesCollection.insertAsync({ nomPoble, cp, comarca, createdAt: new Date() });
|
||||
@ -201,6 +202,27 @@ Meteor.startup(async () => {
|
||||
return TipusCollection.find();
|
||||
});
|
||||
|
||||
Meteor.publish('codis', async function (uid) {
|
||||
|
||||
const esAdmin = await Roles.userIsInRoleAsync(this.userId, "admin", null);
|
||||
const userRoles = await Roles.getRolesForUserAsync(this.userId);
|
||||
|
||||
// console.log("userRoles: ", userRoles);
|
||||
// console.log("esAdmin: ", esAdmin);
|
||||
|
||||
if (esAdmin) {
|
||||
return CodisCollection.find({});
|
||||
}
|
||||
|
||||
return false;
|
||||
|
||||
// if (uid) {
|
||||
// return Meteor.users.find({_id: uid}, {fields: {username: 1, avatarId: 1, avatarLink: 1}});
|
||||
// }
|
||||
|
||||
return Meteor.users.find({},{fields: {username: 1, avatarId: 1, avatarLink: 1}});
|
||||
});
|
||||
|
||||
Meteor.publish('usuaris', async function (uid) {
|
||||
|
||||
const esAdmin = await Roles.userIsInRoleAsync(this.userId, "admin", null);
|
||||
@ -379,6 +401,31 @@ Meteor.methods({
|
||||
console.error(e);
|
||||
}
|
||||
},
|
||||
|
||||
'editaOAfigCodi': async function (codiObj) {
|
||||
const esAdmin = await Roles.userIsInRoleAsync(Meteor.userId(), "admin");
|
||||
try {
|
||||
console.log(`CREACIÓ DE CODI sol·licitada per a "${codiObj.codi}". Comprovant si ${Meteor.userId()} és Admin: `, esAdmin);
|
||||
|
||||
if (esAdmin && codiObj.codi) {
|
||||
return await CodisCollection.upsertAsync(
|
||||
{
|
||||
_id: codiObj._id
|
||||
}, {
|
||||
$set: {
|
||||
...codiObj,
|
||||
usuari: Meteor.userId(),
|
||||
editedAt: new Date()
|
||||
}
|
||||
}
|
||||
);
|
||||
} else {
|
||||
throw new Error("El valor del codi no és vàlid");
|
||||
}
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
});
|
||||
Loading…
Reference in New Issue
Block a user