Skip to content
This repository was archived by the owner on Dec 8, 2022. It is now read-only.

Commit b48bc77

Browse files
Merge pull request #246 from codewizardshq/add-shuffle
Add shuffle to VoteLeaderboardSearch
2 parents f948381 + 01454ee commit b48bc77

File tree

1 file changed

+67
-29
lines changed

1 file changed

+67
-29
lines changed

src/components/VoteLeaderboardSearch.vue

+67-29
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
</v-row>
5656
<v-row justify="center" v-else class="card-wrapper">
5757
<BallotCard
58-
v-for="(item, i) in pageData.items"
58+
v-for="(item, i) in pageData.items[pageData.page - 1]"
5959
:key="i"
6060
v-bind="item"
6161
:is-voting-disabled="isVotingDisabled"
@@ -66,6 +66,7 @@
6666
<v-pagination
6767
v-model="pageData.page"
6868
:length="pageData.totalPages"
69+
@input="nextPageOnClick"
6970
circle
7071
></v-pagination>
7172
</v-row>
@@ -106,13 +107,13 @@ export default {
106107
item: null,
107108
per: 10,
108109
pageData: {
109-
hasNext: false,
110-
hasPrev: false,
111-
nextNum: false,
110+
// hasNext: false,
111+
// hasPrev: false,
112+
// nextNum: false,
112113
page: -1,
113114
items: [],
114-
prevNum: null,
115-
totalItems: 0,
115+
// prevNum: null,
116+
// totalItems: 0,
116117
totalPages: 0
117118
}
118119
};
@@ -122,32 +123,53 @@ export default {
122123
this.item = item;
123124
this.showModal = true;
124125
},
126+
nextPageOnClick(page) {
127+
Vue.set(this.pageData, "page", page);
128+
this.updateQueryParams();
129+
},
125130
async setResult(result) {
126-
await new Promise(resolve =>
131+
await new Promise(resolve => {
127132
setTimeout(async () => {
128-
for (const [key, value] of Object.entries(result)) {
129-
Vue.set(this.pageData, key, value);
133+
// shuffle the results if no search string given
134+
let shuffled;
135+
if (this.searchText.length > 0) {
136+
shuffled = result.items;
137+
} else {
138+
shuffled = this.shuffle(result.items);
139+
}
140+
141+
// push into sub arrays
142+
let postShuffled = [];
143+
while (shuffled.length > 0) {
144+
postShuffled.push(shuffled.splice(0, 10));
130145
}
146+
147+
// set data
148+
Vue.set(this.pageData, "items", postShuffled);
149+
Vue.set(this.pageData, "totalPages", this.pageData.items.length);
150+
151+
// for (const [key, value] of Object.entries(result)) {
152+
// if (key !== "items") {
153+
// Vue.set(this.pageData, key, value);
154+
// }
155+
// }
156+
// Vue.set(this.pageData, "items", shuffled);
131157
await this.updateQueryParams();
132158
resolve();
133-
}, 1000)
134-
);
159+
}, 1000);
160+
});
135161
},
136162
async search() {
137163
if (this.searchText === "") {
138164
return this.loadPage();
139165
}
140-
this.pageData.page = 1;
166+
// this.pageData.page = 1;
141167
this.requestIndex++;
142168
this.requestCount++;
143169
const requestIndex = this.requestIndex;
144170
const searchText = this.searchText;
145171
try {
146-
const results = await voting.search(
147-
this.searchText,
148-
this.pageData.page,
149-
this.per
150-
);
172+
const results = await voting.search(this.searchText, 1, 10000);
151173
if (
152174
this.searchText === searchText &&
153175
this.requestIndex === requestIndex
@@ -172,7 +194,7 @@ export default {
172194
async loadPage() {
173195
this.requestCount++;
174196
try {
175-
const results = await voting.getBallot(this.pageData.page, this.per);
197+
const results = await voting.getBallot(1, 10000);
176198
await this.setResult(results);
177199
} catch (err) {
178200
if (err.status === 404) {
@@ -207,6 +229,20 @@ export default {
207229
} else {
208230
this.search();
209231
}
232+
},
233+
/**
234+
* Shuffles array in place.
235+
* @param {Array} a items An array containing the items.
236+
*/
237+
shuffle(a) {
238+
var j, x, i;
239+
for (i = a.length - 1; i > 0; i--) {
240+
j = Math.floor(Math.random() * (i + 1));
241+
x = a[i];
242+
a[i] = a[j];
243+
a[j] = x;
244+
}
245+
return a;
210246
}
211247
},
212248
computed: {
@@ -217,18 +253,18 @@ export default {
217253
watch: {
218254
searchText() {
219255
this.search();
220-
},
221-
["pageData.page"]() {
222-
this.refresh();
223-
},
224-
["$route.query.page"](val) {
225-
const page = parseInt(val);
226-
if (this.pageData.page === page) {
227-
return;
228-
}
229-
this.pageData.page = page;
230-
this.refresh();
231256
}
257+
// ["pageData.page"]() {
258+
// this.refresh();
259+
// },
260+
// ["$route.query.page"](val) {
261+
// const page = parseInt(val);
262+
// if (this.pageData.page === page) {
263+
// return;
264+
// }
265+
// this.pageData.page = page;
266+
// this.refresh();
267+
// }
232268
},
233269
async mounted() {
234270
this.searchText =
@@ -237,6 +273,8 @@ export default {
237273
this.$route.query.page === undefined
238274
? 1
239275
: parseInt(this.$route.query.page);
276+
277+
this.refresh();
240278
}
241279
};
242280
</script>

0 commit comments

Comments
 (0)