-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvue.js
152 lines (151 loc) · 5.35 KB
/
vue.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
const app = Vue.createApp({
data(){
return {
/* Datos Tablas */
miembros: [],
partidoR: [],
partidoD: [],
partidoID: [],
mostEnganged: [],
leastEnganged:[],
mostLoyalty:[],
leastLoyalty:[],
/* Estados */
states:[],
/* Boton Read More - Read Less */
read: "Read More",
isShow: true,
/* Checkbox - Select */
checkboxes: [],
selected: "All",
}
},
created(){
let url = "https://api.propublica.org/congress/v1/113/house/members.json"
let init={
method: "GET",
headers: {
"X-API-Key":"eexrqveyxeppR7sxXHvRE2eEmCqg8snJrLoAuAju"
}
}
if(document.title.includes("Senate")) {
url = "https://api.propublica.org/congress/v1/113/senate/members.json"
}
fetch(url, init)
.then(response => response.json())
.then(data => {
this.miembros = data.results[0].members
this.mostEnganged = this.diezPct(this.ordenar(this.miembros, "missed_votes_pct", true), "missed_votes_pct")
this.leastEnganged = this.diezPct(this.ordenar(this.miembros, "missed_votes_pct", false), "missed_votes_pct")
this.mostLoyalty = this.diezPct(this.ordenar(this.miembros, "votes_with_party_pct", false), "votes_with_party_pct")
this.leastLoyalty = this.diezPct(this.ordenar(this.miembros, "votes_with_party_pct", true), "votes_with_party_pct")
this.miembrosPartidos()
})
.catch(err => alert(err.message));
},
methods:{
toggleShow() {
this.isShow = !this.isShow
this.isShow ? this.read = "Read More" : this.read = "Read Less"
},
ordenar(array,prop,esAscendente) {
let copiaArr = [...array]
copiaArr.sort((a, b) => {
if (a[prop] < b[prop]) {
if (esAscendente) {
return -1
} else {
return 1
}
}
if (a[prop] > b[prop]) {
if (esAscendente) {
return 1
} else {
return -1
}
}
return 0
})
return copiaArr
},
diezPct(array, prop) {
let posicion = parseInt(array.length * .1)
let arrReducido = array.splice(0, posicion + 1)
let ultimoValor = arrReducido[arrReducido.length - 1][prop]
array.forEach(element => {
if (element[prop] > 0) {
if (element[prop] === ultimoValor) {
arrReducido.push(element)
}
}
})
return arrReducido
},
miembrosPartidos(){
this.partidoR = this.miembros.filter(miembro =>miembro.party === "R")
this.partidoD = this.miembros.filter(miembro => miembro.party === "D")
this.partidoID = this.miembros.filter(miembro => miembro.party === "ID")
},
},
computed:{
estados() {
this.miembros.forEach(member => {
if (!this.states.includes(member.state)) {
this.states.push(member.state)
}
})
return this.states.sort()
},
memberFiltrados(){
let check = this.miembros.filter(member => this.checkboxes.includes(member.party) || this.checkboxes.length === 0);
if(this.selected === "All") {
return check;
} else {
return check.filter(member => member.state === this.selected)
}
},
statesFilter(){
this.states = this.miembros.filter(miembro => miembro.state)
},
//necesito me recorra el array del partido
//me sume los porcentajes de votos y los divida por la cantidad de miembros del partido
sumVotesR(){
let porcentaje = 0;
this.partidoR.forEach(member => {
let porcentajeMember = member.votes_with_party_pct
porcentaje = porcentaje + porcentajeMember;
})
porcentaje = porcentaje / this.partidoR.length
if (isNaN(porcentaje)) {
return 0;
}
return porcentaje
},
sumVotesD() {
let porcentaje = 0;
this.partidoD.forEach(member => {
let porcentajeMember = member.votes_with_party_pct
porcentaje = porcentaje + porcentajeMember;
})
porcentaje = porcentaje / this.partidoD.length
if (isNaN(porcentaje)) {
return 0;
}
return porcentaje
},
sumVotesID() {
let porcentaje = 0;
this.partidoID.forEach(member => {
let porcentajeMember = member.votes_with_party_pct
porcentaje = porcentaje + porcentajeMember;
})
porcentaje = porcentaje / this.partidoID.length
if (isNaN(porcentaje)) {
return 0;
}
return porcentaje
}
}
})
app.mount("#app")