Skip to content

Commit e2a6fc0

Browse files
committed
cleanup q vs query
since `q` is the url parameter of the search string, it's better to not have local variables called `q` which represent the whole query. also, where possible, make local variables of the search string be "searchStr/searchAll" instead of "q/all" to make the difference b/t variables and current query parameters more obvious
1 parent 410ea08 commit e2a6fc0

File tree

9 files changed

+47
-35
lines changed

9 files changed

+47
-35
lines changed

cal/src/CalSearch.vue

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,17 @@ export default {
3535
};
3636
},
3737
computed: {
38-
q() {
38+
searchStr() {
3939
return this.$route.query.q;
4040
},
4141
offset() {
4242
return this.$route.query.offset || 0;
4343
},
44+
searchAll() {
45+
// return undefined if the 'all' parameter doesnt exist
46+
// so we don't wind up with all=false in the browser bar.
47+
return this.$route.query.all || undefined;
48+
},
4449
itemCount() {
4550
return this.events.length;
4651
},
@@ -55,9 +60,12 @@ export default {
5560
}
5661
},
5762
methods: {
63+
// expects the query object as a parameter.
5864
// emits the 'pageLoaded' event when done.
59-
updateSearch({q, offset, all: searchAll}) {
60-
return fetchSearch(q, parseInt(offset || 0), searchAll).then((page) => {
65+
updateSearch(urlQuery) {
66+
// unpack the desired query parameter:
67+
const {q: searchStr, offset, all: searchAll} = urlQuery;
68+
return fetchSearch(searchStr, parseInt(offset || 0), searchAll).then((page) => {
6169
this.events = page.data.events;
6270
this.pageNum = page.data.pageNum;
6371
this.fullCount = page.data.fullCount;
@@ -73,7 +81,7 @@ export default {
7381
</script>
7482
<template>
7583
<h3 class="c-divder c-divder--center">
76-
<div>Found {{fullCount}} {{pluralized}} containing "{{q}}"</div>
84+
<div>Found {{fullCount}} {{pluralized}} containing "{{searchStr}}"</div>
7785
<div v-if="totalPages > 1">Showing page {{pageNum}} of {{totalPages}}</div>
7886
</h3>
7987
<EventSummary

cal/src/EventDetails.vue

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ export default {
5555
// record the event data
5656
// and remember the calendar start
5757
if (from.name === 'events') {
58-
const q = from.query;
59-
if (q.start) {
60-
vm.calStart = q.start;
58+
const { start } = from.query;
59+
if (start) {
60+
vm.calStart = start;
6161
}
6262
}
6363
// done loading.

cal/src/Menu.vue

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,21 @@ export default {
2323
},
2424
computed: {
2525
activeMenu() {
26-
const q = this.$route.query;
27-
return q.menu;
26+
return this.$route.query.menu;
2827
},
2928
activeKids() {
3029
return this.menu[this.activeMenu].kids;
3130
}
3231
},
3332
methods: {
3433
activate(name) {
35-
const q = { ...this.$route.query };
36-
if (q.menu === name) {
37-
delete q.menu;
34+
const query = { ...this.$route.query };
35+
if (query.menu === name) {
36+
delete query.menu;
3837
} else {
39-
q.menu = name;
38+
query.menu = name;
4039
}
41-
this.$router.replace({query: q});
40+
this.$router.replace({query});
4241
},
4342
caretFor(name) {
4443
const icon = name === this.activeMenu ? 'caretDown' : 'caretRight';

cal/src/Toolbar.vue

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ export default {
2424
const next = this.returnLink || {name: "events"};
2525
return this.$router.push(next);
2626
} else {
27-
const q = { ...this.$route.query };
28-
q.expanded = this.expanded != name ? name : undefined;
29-
return this.$router.replace({query: q})
27+
const query = { ...this.$route.query };
28+
query.expanded = this.expanded != name ? name : undefined;
29+
return this.$router.replace({query});
3030
}
3131
},
3232
icon(key) {

cal/src/calList.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,10 @@ function buildPage(start, end, days) {
4545
// ( given calList.updateRange: if today is a monday
4646
// we see monday-sunday; and then shift to the next/previous monday. )
4747
function shiftRange(router, start, dir) {
48-
const q = { ...router.currentRoute.query }; // *copy* the query object.
48+
const query = { ...router.currentRoute.query }; // *copy* the query object.
4949
const next = start.add(dir, 'week');
50-
q.start = next.format("YYYY-MM-DD"); // add/replace the start.
51-
router.push({query: q});
50+
query.start = next.format("YYYY-MM-DD"); // add/replace the start.
51+
router.push({query});
5252
}
5353

5454
// ---------------------------------------------------------------------

cal/src/calSearch.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ import dayjs from 'dayjs'
55
import dataPool from './support/dataPool.js'
66
import siteConfig from './siteConfig.js'
77

8-
export async function fetchSearch(q, offset, searchAll) {
9-
const result = await dataPool.getSearch(q, offset, searchAll);
10-
return buildPage(q, offset, result);
8+
export async function fetchSearch(searchStr, offset, searchAll) {
9+
const result = await dataPool.getSearch(searchStr, offset, searchAll);
10+
return buildPage(searchStr, offset, result);
1111
}
1212

1313
// ---------------------------------------------------------------------
1414
// internal functions
1515
// ---------------------------------------------------------------------
16-
function buildPage(q, offset, res) {
16+
function buildPage(searchStr, offset, res) {
1717
//
1818
const { events } = res;
1919
const { limit, fullcount } = res.pagination;
@@ -22,8 +22,8 @@ function buildPage(q, offset, res) {
2222
return {
2323
page: {
2424
title: multiplePages ?
25-
`${q} - Page ${pageNum} - searching ${siteConfig.title}` :
26-
`${q} - searching ${siteConfig.title}`,
25+
`${searchStr} - Page ${pageNum} - searching ${siteConfig.title}` :
26+
`${searchStr} - searching ${siteConfig.title}`,
2727
banner: siteConfig.defaultListBanner,
2828
},
2929
data: {

cal/src/support/dataPool.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ export default {
7272
return data;
7373
},
7474
// searching
75-
async getSearch(q, offset, searchAll) {
76-
const url = buildUrl(API_SEARCH_URL, { q, o: offset, all: searchAll });
75+
async getSearch(searchStr, offset, searchAll) {
76+
const url = buildUrl(API_SEARCH_URL, { q: searchStr, o: offset, all: searchAll });
7777
console.log(`fetching ${url}`);
7878
const resp = await fetch(url);
7979
const data = await resp.json(); // data => { events: [], pagination: {} }

cal/src/tools/InputText.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export default {
1111
label: String,
1212
attrs: Object,
1313
model: Object, // containing "inputText"
14-
selectText: Boolean
14+
shouldSelect: Boolean
1515
},
1616
computed: {
1717
id() {
@@ -22,7 +22,7 @@ export default {
2222
mounted() {
2323
// console.log("mounted", this.$refs.inputItem);
2424
this.$refs.inputItem.focus();
25-
if (this.selectText) {
25+
if (this.shouldSelect) {
2626
this.$refs.inputItem.select();
2727
}
2828
},

cal/src/tools/SearchTool.vue

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,13 @@ export default {
1111
placeholder: "Search event titles...",
1212
},
1313
model: {
14+
// the text in the input box should be filled with the query search string.
1415
inputText: this.$route.query.q,
1516
},
16-
selectText: !!this.$route.query.q,
17-
all: this.$route.query.all,
17+
// if there is an existing inputText, select it.
18+
shouldSelect: !!this.$route.query.q,
19+
// the v-model attribute (below) keeps this in sync with the checkbox state
20+
searchAll: this.$route.query.all,
1821
}
1922
},
2023
methods: {
@@ -24,7 +27,9 @@ export default {
2427
console.log(`searching for ${inputText}`);
2528
this.$emit("changeRoute", { name: 'search', query: {
2629
q: inputText,
27-
all: this.all || undefined,
30+
// if the checkbox is 'false' report 'undefined'
31+
// that makes the url query string be blank instead of "all=false"
32+
all: this.searchAll || undefined,
2833
}});
2934
}
3035
},
@@ -35,10 +40,10 @@ export default {
3540
<form class="c-search" method="dialog">
3641
<!-- -->
3742
<div class="c-search__controls">
38-
<InputText name="search" label="Search" :attrs :model :selectText/>
43+
<InputText name="search" label="Search" :attrs :model :shouldSelect/>
3944
<span class="c-search__past">
4045
<label for="all">Include past events </label>
41-
<input type="checkbox" id="all" v-model="all"/>
46+
<input type="checkbox" id="all" v-model="searchAll"/>
4247
</span>
4348
</div>
4449
<!--

0 commit comments

Comments
 (0)