Skip to content
This repository was archived by the owner on Oct 1, 2021. It is now read-only.

Commit 2189b9f

Browse files
committed
refactor(sort order): name sort options the same way as the field names
1 parent 4f60f0e commit 2189b9f

File tree

5 files changed

+27
-22
lines changed

5 files changed

+27
-22
lines changed

api/styles.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ function getStyles(req, res) {
1010
if (owner) filter = { owner };
1111

1212
let sortOrder = "-_id";
13-
if (sort === "stars") {
13+
if (sort === "stargazers") {
1414
sortOrder = "-stargazers";
15-
} else if (sort === "update") {
15+
} else if (sort === "lastUpdate") {
1616
sortOrder = "-lastUpdate";
1717
}
1818

src/src/store/modules/styleGrid/actions.js

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export default {
1111

1212
const params = {};
1313
params.page = state.pagination.page;
14+
params.sort = state.sortOrder;
1415

1516
let url = '/api/styles/';
1617
if (state.searchQuery) {
@@ -22,15 +23,6 @@ export default {
2223
window.history.replaceState({}, `Styles by ${state.ownerFilter} | StyleBase`, `/${state.ownerFilter}`);
2324
}
2425

25-
switch (state.selectedSort) {
26-
case 1:
27-
params.sort = 'update';
28-
break;
29-
case 2:
30-
params.sort = 'stars';
31-
break;
32-
}
33-
3426
await axios
3527
.get(url, { params })
3628
.then((response) => {
@@ -64,8 +56,8 @@ export default {
6456
commit('SET_PAGE', page);
6557
dispatch('getStyles');
6658
},
67-
setSorting({ commit, dispatch }, sortOption) {
68-
commit('SET_SORTING', sortOption);
59+
setSortOrder({ commit, dispatch }, sortOption) {
60+
commit('SET_SORT_ORDER', sortOption);
6961
commit('SET_PAGE', 1);
7062
dispatch('getStyles');
7163
},
@@ -86,7 +78,7 @@ export default {
8678
resetFilters({ commit, dispatch }) {
8779
window.history.replaceState({}, document.title, '/');
8880
commit('SET_SEARCH_QUERY', '');
89-
commit('SET_SORTING', 0);
81+
commit('SET_SORT_ORDER', 0);
9082
commit('SET_OWNER_FILTER', '');
9183
commit('SET_PAGE', 1);
9284
dispatch('getStyles');

src/src/store/modules/styleGrid/mutations.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ export default {
2020
SET_OWNER_FILTER(state, filter) {
2121
state.ownerFilter = filter;
2222
},
23-
SET_SORTING(state, sorting) {
24-
state.selectedSort = sorting;
23+
SET_SORT_ORDER(state, orderId) {
24+
state.sortOrder = orderId;
2525
},
2626
SET_MODAL_VISIBILITY(state, value) {
2727
state.showStyleInfoModal = value;

src/src/store/modules/styleGrid/state.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,21 @@ export default {
99
searchQuery: '',
1010
ownerFilter: '',
1111

12-
sortOptions: ['Recently added', 'Recently updated', 'Most liked'],
13-
selectedSort: 0,
12+
sortOptions: [
13+
{
14+
id: '_id',
15+
text: 'Recently added'
16+
},
17+
{
18+
id: 'lastUpdate',
19+
text: 'Recently updated'
20+
},
21+
{
22+
id: 'stargazers',
23+
text: 'Most liked'
24+
}
25+
],
26+
sortOrder: '_id',
1427

1528
selectedStyle: {},
1629
showStyleInfoModal: false

src/src/views/Home.vue

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@
3535
<button
3636
class="link"
3737
type="button"
38-
:class="{ active: state.selectedSort === index }"
38+
:class="{ active: state.sortOrder === option.id }"
3939
:disabled="state.isLoading"
40-
@click="setSorting(index)"
40+
@click="setSortOrder(option.id)"
4141
>
42-
{{ option }}
42+
{{ option.text }}
4343
</button>
4444
</li>
4545
</ul>
@@ -118,7 +118,7 @@ export default {
118118
getStyle: 'styleGrid/getStyle',
119119
selectStyle: 'styleGrid/selectStyle',
120120
setPage: 'styleGrid/setPage',
121-
setSorting: 'styleGrid/setSorting',
121+
setSortOrder: 'styleGrid/setSortOrder',
122122
setQuery: 'styleGrid/setQuery',
123123
setOwnerFilter: 'styleGrid/setOwnerFilter',
124124
resetFilters: 'styleGrid/resetFilters'

0 commit comments

Comments
 (0)