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 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 { NecessitatsCollection } from '/imports/api/necessitats.js';
|
||||||
import { PoblesCollection } from '../api/pobles';
|
import { PoblesCollection } from '../api/pobles';
|
||||||
|
import { CodisCollection } from '../api/codis';
|
||||||
// import { TipusCollection } from '../api/tipus';
|
// import { TipusCollection } from '../api/tipus';
|
||||||
import { useSubscribe, useTracker, useFind } from 'meteor/react-meteor-data/suspense';
|
import { useSubscribe, useTracker, useFind } from 'meteor/react-meteor-data/suspense';
|
||||||
import { Roles } from 'meteor/roles';
|
import { Roles } from 'meteor/roles';
|
||||||
@ -12,6 +14,12 @@ import CreatableSelect from 'react-select/creatable';
|
|||||||
// import AsyncCreatableSelect from 'react-select/async-creatable';
|
// import AsyncCreatableSelect from 'react-select/async-creatable';
|
||||||
// import { BarraNav } from "./BarraNav/BarraNav";
|
// 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 = () => {
|
export const Codis = () => {
|
||||||
|
|
||||||
@ -33,6 +41,9 @@ export const Codis = () => {
|
|||||||
useSubscribe('necessitats');
|
useSubscribe('necessitats');
|
||||||
const necessitats = useTracker("necessitats", () => NecessitatsCollection.find().fetchAsync());
|
const necessitats = useTracker("necessitats", () => NecessitatsCollection.find().fetchAsync());
|
||||||
|
|
||||||
|
useSubscribe('codis');
|
||||||
|
const codis = useTracker("codis", () => CodisCollection.find().fetchAsync());
|
||||||
|
|
||||||
useSubscribe('pobles');
|
useSubscribe('pobles');
|
||||||
const pobles = useTracker("pobles", () => PoblesCollection.find().fetchAsync());
|
const pobles = useTracker("pobles", () => PoblesCollection.find().fetchAsync());
|
||||||
const ambits = pobles?.map(p => p?.ambitAssociat);
|
const ambits = pobles?.map(p => p?.ambitAssociat);
|
||||||
@ -60,6 +71,7 @@ export const Codis = () => {
|
|||||||
return <>
|
return <>
|
||||||
<label htmlFor="selRol">Rol: </label>
|
<label htmlFor="selRol">Rol: </label>
|
||||||
<Select
|
<Select
|
||||||
|
required
|
||||||
key={Math.random()}
|
key={Math.random()}
|
||||||
name="selRol"
|
name="selRol"
|
||||||
// filterOption={(opts) => necessitats.find(nec => nec.tipus === tipusSeleccionat._id)}
|
// filterOption={(opts) => necessitats.find(nec => nec.tipus === tipusSeleccionat._id)}
|
||||||
@ -98,6 +110,10 @@ export const Codis = () => {
|
|||||||
|
|
||||||
const QuadreInfo_Codis = () => {
|
const QuadreInfo_Codis = () => {
|
||||||
|
|
||||||
|
const [actCond, setActCond] = useState(true);
|
||||||
|
const [codiGenerat, setCodiGenerat] = useState(generaCodi()?.toUpperCase());
|
||||||
|
|
||||||
|
|
||||||
return <div style={{
|
return <div style={{
|
||||||
display: `inline-block`,
|
display: `inline-block`,
|
||||||
border: `1px solid #6666`,
|
border: `1px solid #6666`,
|
||||||
@ -109,17 +125,49 @@ export const Codis = () => {
|
|||||||
{ambitSeleccionat && <h1>{ambitSeleccionat?.value}</h1>}
|
{ambitSeleccionat && <h1>{ambitSeleccionat?.value}</h1>}
|
||||||
<h1>Codis</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
|
<form
|
||||||
action={d => {
|
action={d => {
|
||||||
// if (d.get('selTipus'))
|
// if (d.get('selTipus'))
|
||||||
try {
|
try {
|
||||||
Meteor.callAsync('editaOAfigNecessitat', {
|
Meteor.callAsync('editaOAfigCodi', {
|
||||||
...tipusSeleccionat || [],
|
...codiGenerat || [],
|
||||||
titol: d.get('titol'),
|
codi: codiGenerat,
|
||||||
tipus: d.get('selTipus') || "",
|
ambit: d.get('selAmbit') || "",
|
||||||
poble: d.get('selPoble')
|
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))
|
.then(() => setAmbitSeleccionat(null))
|
||||||
.catch(err => console.error(err))
|
.catch(err => console.error(err))
|
||||||
@ -138,6 +186,7 @@ export const Codis = () => {
|
|||||||
*/}
|
*/}
|
||||||
|
|
||||||
<Select
|
<Select
|
||||||
|
required
|
||||||
name="selAmbit"
|
name="selAmbit"
|
||||||
// formatCreateLabel={(inputValue) => "Crear nou tipus..."}
|
// formatCreateLabel={(inputValue) => "Crear nou tipus..."}
|
||||||
// filterOption={filterAmbit}
|
// filterOption={filterAmbit}
|
||||||
@ -178,37 +227,75 @@ export const Codis = () => {
|
|||||||
|
|
||||||
<SelectorDeRol {... {ambitSeleccionat}} />
|
<SelectorDeRol {... {ambitSeleccionat}} />
|
||||||
|
|
||||||
<br />
|
|
||||||
|
|
||||||
<label htmlFor="actiu">Actiu? </label>
|
|
||||||
<input type="checkbox" name='actiu' />
|
|
||||||
|
|
||||||
<br /><br />
|
<br /><br />
|
||||||
|
|
||||||
<label htmlFor="periode_validesa">Periode de validesa: </label>
|
<label htmlFor="periode_validesa">Periode de validesa: </label>
|
||||||
<input type="date" name="periode_validesa" />
|
|
||||||
|
|
||||||
{/* <select name="selPoble">
|
<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="s.
|
||||||
|
elPoble">
|
||||||
{
|
{
|
||||||
pobles.map((v,i) => <option key={`optPob${i}`}>{v.nomPoble}</option>)
|
pobles.map((v,i) => <option key={`optPob${i}`}>{v.nomPoble}</option>)
|
||||||
}
|
}
|
||||||
</select> */}
|
</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();
|
ev.preventDefault();
|
||||||
if (confirm(`Vas a eliminar el poble "${pobleSeleccionat.nomPoble}". És una operació irreversible. Procedir?`)) {
|
if (confirm(`Vas a eliminar el poble "${pobleSeleccionat.nomPoble}". És una operació irreversible. Procedir?`)) {
|
||||||
Meteor.callAsync('eliminaPoble', pobleSeleccionat._id).catch(err => console.error(err));
|
Meteor.callAsync('eliminaPoble', pobleSeleccionat._id).catch(err => console.error(err));
|
||||||
setNecessitatSeleccionada(null);
|
setNecessitatSeleccionada(null);
|
||||||
}
|
}
|
||||||
}}>Elimina</button>}
|
}}>Elimina</button>} */}
|
||||||
</form>
|
</form>
|
||||||
</div>;
|
</div>;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const
|
||||||
|
today = new Date(),
|
||||||
|
lastWeek = new Date(today.getFullYear(), today.getMonth(), today.getDate() - 7)
|
||||||
|
;
|
||||||
|
|
||||||
|
console.log("codis: ", codis);
|
||||||
|
|
||||||
|
|
||||||
return <Suspense fallback={<>Carregant...</>} >
|
return <Suspense fallback={<>Carregant...</>} >
|
||||||
|
|
||||||
@ -232,10 +319,11 @@ export const Codis = () => {
|
|||||||
rowGap: `.3em`,
|
rowGap: `.3em`,
|
||||||
alignContent: `space-around`
|
alignContent: `space-around`
|
||||||
}}>{
|
}}>{
|
||||||
ambits
|
codis
|
||||||
.sort((a,b) => a.toLowerCase() > b.toLowerCase())
|
// .reverse()
|
||||||
.map(amb => <li
|
// .sort((a,b) => a?.codi?.editedAt < b?.codi?.editedAt)
|
||||||
key={amb}
|
.map(cod => <li
|
||||||
|
key={cod._id}
|
||||||
style={{
|
style={{
|
||||||
display: `block`,
|
display: `block`,
|
||||||
border: `1px solid #6666`,
|
border: `1px solid #6666`,
|
||||||
@ -245,8 +333,17 @@ export const Codis = () => {
|
|||||||
backgroundColor: `${'lightgreen' || 'lightcoral'}`
|
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>)
|
</li>)
|
||||||
}</ul>
|
}</ul>
|
||||||
|
|
||||||
|
{/* <InfiniteCalendar
|
||||||
|
width={400}
|
||||||
|
height={600}
|
||||||
|
selected={today}
|
||||||
|
disabledDays={[0,6]}
|
||||||
|
minDate={lastWeek}
|
||||||
|
/> */}
|
||||||
</Suspense>;
|
</Suspense>;
|
||||||
};
|
};
|
||||||
@ -1,23 +1,23 @@
|
|||||||
import React from 'react';
|
// import React from 'react';
|
||||||
import { useFind, useSubscribe } from 'meteor/react-meteor-data';
|
// import { useFind, useSubscribe } from 'meteor/react-meteor-data';
|
||||||
import { LinksCollection } from '../api/links';
|
// import { LinksCollection } from '../api/links';
|
||||||
|
|
||||||
export const Info = () => {
|
// export const Info = () => {
|
||||||
const isLoading = useSubscribe('links');
|
// const isLoading = useSubscribe('links');
|
||||||
const links = useFind(() => LinksCollection.find());
|
// const links = useFind(() => LinksCollection.find());
|
||||||
|
|
||||||
if(isLoading()) {
|
// if(isLoading()) {
|
||||||
return <div>Loading...</div>;
|
// return <div>Loading...</div>;
|
||||||
}
|
// }
|
||||||
|
|
||||||
return (
|
// return (
|
||||||
<div>
|
// <div>
|
||||||
<h2>Learn Meteor!</h2>
|
// <h2>Learn Meteor!</h2>
|
||||||
<ul>{links.map(
|
// <ul>{links.map(
|
||||||
link => <li key={link._id}>
|
// link => <li key={link._id}>
|
||||||
<a href={link.url} target="_blank">{link.title}</a>
|
// <a href={link.url} target="_blank">{link.title}</a>
|
||||||
</li>
|
// </li>
|
||||||
)}</ul>
|
// )}</ul>
|
||||||
</div>
|
// </div>
|
||||||
);
|
// );
|
||||||
};
|
// };
|
||||||
|
|||||||
@ -6,6 +6,7 @@ import { Roles } from 'meteor/roles';
|
|||||||
import { ROLS_GLOBALS, ROLS_DE_POBLE } from '../imports/roles';
|
import { ROLS_GLOBALS, ROLS_DE_POBLE } from '../imports/roles';
|
||||||
import { NecessitatsCollection } from '../imports/api/necessitats';
|
import { NecessitatsCollection } from '../imports/api/necessitats';
|
||||||
import { TipusCollection } from '../imports/api/tipus';
|
import { TipusCollection } from '../imports/api/tipus';
|
||||||
|
import { CodisCollection } from '../imports/api/codis';
|
||||||
|
|
||||||
async function insertPoble({ nomPoble, cp, comarca }) {
|
async function insertPoble({ nomPoble, cp, comarca }) {
|
||||||
await PoblesCollection.insertAsync({ nomPoble, cp, comarca, createdAt: new Date() });
|
await PoblesCollection.insertAsync({ nomPoble, cp, comarca, createdAt: new Date() });
|
||||||
@ -201,6 +202,27 @@ Meteor.startup(async () => {
|
|||||||
return TipusCollection.find();
|
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) {
|
Meteor.publish('usuaris', async function (uid) {
|
||||||
|
|
||||||
const esAdmin = await Roles.userIsInRoleAsync(this.userId, "admin", null);
|
const esAdmin = await Roles.userIsInRoleAsync(this.userId, "admin", null);
|
||||||
@ -379,6 +401,31 @@ Meteor.methods({
|
|||||||
console.error(e);
|
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