forked from creativecommons/ccos-website-source
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomponents.js
217 lines (212 loc) · 5.58 KB
/
components.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import VueSelect from 'vue-select';
import {octokit} from './octokit';
export const IssueLabel = {
template: `
<span class="gh-label" :class="className">
{{ name }}
</span>`,
data() {
return {
labels: window.labels
}
},
props: {
name: {
type: String,
required: true
}
},
computed: {
/**
* Get the name of the class to apply to the label based on the group to
* which it belongs. Falls back to miscellaneous if the label does not
* belong to a group or a class cannot be identified.
*
* @returns {string} the name of the class to apply to the label
*/
className() {
return window.className[this.name] || 'miscellaneous'
}
}
}
export const IssueCard = {
template: `
<div class="card entry-post vertical margin-top-normal padding-normal">
<h4 class="card-title b-header margin-bottom-small">{{ issue.title }}</h4>
<a :href="issue.html_url" class="button is-text tiny site-link" target="_blank">
<span class="has-color-forest-green">
{{ issue.repository.name }}#{{ issue.number }}
</span>
<i class="icon external-link has-color-forest-green"></i>
</a>
<div class="labels margin-top-small">
<IssueLabel
v-for="(name, index) in issue.labelNames"
:key="index"
:name="name"/>
</div>
</div>`,
components: {
IssueLabel
},
props: {
issue: {
type: Object,
required: true
},
}
}
export const App = {
el: '#vue-app',
template: `
<div class="find-issues">
<div class="columns">
<div class="column is-one-quarter">
<form id="filters">
<label for="skills">
<strong>Skill set</strong><br>
Choose up to three skills that you would like to see issues for.
Leave blank if you don't have a preference.
</label>
<VueSelect
v-model="filters.skills"
id="skills"
name="skills"
:options="options.skills"
:reduce="skill => skill.toLocaleLowerCase()"
:selectable="() => filters.skills.length < 3"
multiple/>
<br/>
<label for="experience">
<strong>Experience</strong><br>
Is this your first time contributing to CC?
</label>
<VueSelect
v-model="filters.experience"
id="experience"
name="experience"
:options="options.experiences"
label="name"
:reduce="experience => experience.code"
:clearable="false"/>
<button
class="button small is-success margin-top-small"
@click.prevent="search">
Search
</button>
</form>
</div>
<div class="column">
<template v-if="issues.length">
<issue-card
v-for="issue in filteredIssues"
:key="issue.id"
:issue="issue"/>
</template>
<p
v-else
class="margin-top-normal">
No results.
</p>
</div>
</div>
</div>`,
components: {
VueSelect,
IssueCard
},
data() {
return {
options: {
skills: window.skillSet,
experiences: [
{name: 'Yes, it is', code: 'beginner'},
{name: 'No, it isn\'t', code: 'experienced'}
]
},
filters: {
skills: [],
experience: 'experienced'
},
issues: []
}
},
computed: {
/**
* Get a nested list of all the labels to search for. This is only based on
* the experience filter. The skills filter must be applied on the client
* side due to limitations of the GitHub API.
*
* This returns a list of lists. Each nested list corresponds to a single
* API query and multiple queries need to be run to get the combined set of
* valid issues.
*
* @returns {array} the array of array of labels
*/
labelsList() {
const labelsList = []
if (this.filters.experience === 'experienced') {
labelsList.push('help wanted')
} else {
labelsList.push('good first issue')
}
return labelsList
},
/**
* Get a filtered list of issues matching the chosen skill labels.
*
* @returns {array} the array of filtered issues
*/
filteredIssues() {
let filtered = this.issues
if (this.filters.skills.length) {
filtered = filtered.filter(issue => {
const joinedLabels = issue.labelNames.join(',')
return this.filters.skills.some(skill => joinedLabels.includes(skill))
})
}
return filtered
}
},
methods: {
/**
* Get a Promise for the list of issues pertaining to a given labels.
*
* @param {Array} labels - list of labels to search for
* @returns {Promise} a promise that resolves into a list of issues
*/
promise(labels = []) {
const params = {
org: 'creativecommons',
state: 'open',
filter: 'all',
per_page: 200
}
if (labels) {
params.labels = labels.join(',')
}
return octokit
.issues
.listForOrg(params)
.then(response => response.data)
},
/**
* Run the search based on the data submitted via the form and load all
* results into the `issues` attribute.
*/
search() {
this.promise(this.labelsList)
.then(issueLists => {
const issues = issueLists.flat()
issues.forEach(issue => {
issue.labelNames = issue.labels.map(label => label.name)
})
this.issues = issues
})
.catch(err => console.error(err))
}
},
mounted() {
this.search()
}
}