Skip to content

Commit 61d8c28

Browse files
committed
refactor store naming + use stylus
1 parent c255945 commit 61d8c28

File tree

5 files changed

+100
-101
lines changed

5 files changed

+100
-101
lines changed

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
"css-loader": "^0.23.1",
3232
"extract-text-webpack-plugin": "^2.0.0-beta.3",
3333
"file-loader": "^0.8.4",
34+
"stylus": "^0.54.5",
35+
"stylus-loader": "^2.1.2",
3436
"vue-loader": "^9.2.2",
3537
"webpack": "^2.1.0-beta.20",
3638
"webpack-dev-middleware": "^1.6.1",

src/App.vue

+18-19
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,22 @@
1313
</div>
1414
</template>
1515

16-
<style>
17-
body {
18-
font-family: Roboto, Helvetica, sans-serif;
19-
}
20-
.logo {
21-
width: 30px;
22-
}
23-
.view {
24-
transition: all .35s ease;
25-
}
26-
.view-enter, .view-leave-active {
27-
opacity: 0;
28-
}
29-
a {
30-
color: #4fc08d;
31-
}
32-
a.disabled {
33-
color: #999;
34-
}
16+
<style lang="stylus">
17+
body
18+
font-family Roboto, Helvetica, sans-serif
19+
20+
.logo
21+
width 30px
22+
23+
.view
24+
transition all .35s ease
25+
26+
.view-enter, .view-leave-active
27+
opacity 0
28+
29+
a
30+
color #4fc08d
31+
32+
a.disabled
33+
color #999
3534
</style>

src/components/Spinner.vue

+32-33
Original file line numberDiff line numberDiff line change
@@ -10,41 +10,40 @@ export default {
1010
}
1111
</script>
1212

13-
<style>
14-
.spinner {
15-
opacity: 0;
16-
transition: opacity .3s ease;
17-
animation: rotator 1.4s linear infinite;
18-
animation-play-state: paused;
19-
}
13+
<style lang="stylus">
14+
$offset = 126
15+
$duration = 1.4s
2016
21-
.spinner.show {
22-
opacity: 1;
23-
animation-play-state: running;
24-
}
17+
.spinner
18+
opacity 0
19+
transition opacity .15s ease
20+
animation rotator $duration linear infinite
21+
animation-play-state paused
2522
26-
@keyframes rotator {
27-
0% { transform: scale(0.5) rotate(0deg); }
28-
100% { transform: scale(0.5) rotate(270deg); }
29-
}
23+
.spinner.show
24+
opacity 1
25+
animation-play-state running
3026
31-
.spinner .path {
32-
stroke: #4fc08d;
33-
stroke-dasharray: 126;
34-
stroke-dashoffset: 0;
35-
transform-origin: center;
36-
animation: dash 1.4s ease-in-out infinite;
37-
}
27+
@keyframes rotator
28+
0%
29+
transform scale(0.5) rotate(0deg)
30+
100%
31+
transform scale(0.5) rotate(270deg)
3832
39-
@keyframes dash {
40-
0% { stroke-dashoffset: 126; }
41-
50% {
42-
stroke-dashoffset: 32;
43-
transform:rotate(135deg);
44-
}
45-
100% {
46-
stroke-dashoffset: 126;
47-
transform:rotate(450deg);
48-
}
49-
}
33+
.spinner .path
34+
stroke #4fc08d
35+
stroke-dasharray $offset
36+
stroke-dashoffset 0
37+
transform-origin center
38+
animation dash 1.4s ease-in-out infinite
39+
40+
@keyframes dash
41+
0%
42+
stroke-dashoffset $offset
43+
50%
44+
stroke-dashoffset ($offset/2)
45+
transform rotate(135deg)
46+
100%
47+
stroke-dashoffset $offset
48+
transform rotate(450deg)
5049
</style>

src/store/index.js

+16-16
Original file line numberDiff line numberDiff line change
@@ -6,38 +6,38 @@ Vue.use(Vuex)
66

77
const store = new Vuex.Store({
88
state: {
9-
storiesPerPage: 20,
10-
topStoryIds: [],
9+
itemsPerPage: 20,
10+
activeItemIds: [],
1111
items: {}
1212
},
1313

1414
actions: {
15-
FETCH_TOP_IDS: ({ commit }) => {
15+
FETCH_IDS: ({ commit }) => {
1616
return fetchTopIds().then(ids => {
17-
commit('RECEIVE_TOP_IDS', { ids })
17+
commit('SET_ACTIVE_IDS', { ids })
1818
})
1919
},
20-
FETCH_NEWS: ({ commit, state }) => {
20+
FETCH_DISPLAYED_ITEMS: ({ commit, state }) => {
2121
const ids = getDisplayedIds(state)
2222
return fetchItems(ids).then(items => {
23-
commit('RECEIVE_ITEMS', { items })
23+
commit('SET_ITEMS', { items })
2424
})
2525
}
2626
},
2727

2828
mutations: {
29-
RECEIVE_TOP_IDS: (state, { ids }) => {
30-
state.topStoryIds = ids
29+
SET_ACTIVE_IDS: (state, { ids }) => {
30+
state.activeItemIds = ids
3131
},
32-
RECEIVE_ITEMS: (state, { items }) => {
32+
SET_ITEMS: (state, { items }) => {
3333
items.forEach(item => {
3434
Vue.set(state.items, item.id, item)
3535
})
3636
}
3737
},
3838

3939
getters: {
40-
news: state => {
40+
displayedItems: state => {
4141
const ids = getDisplayedIds(state)
4242
return ids.map(id => state.items[id]).filter(_ => _)
4343
}
@@ -47,17 +47,17 @@ const store = new Vuex.Store({
4747
// watch for realtime top IDs updates on the client
4848
if (typeof window !== 'undefined') {
4949
watchTopIds(ids => {
50-
store.commit('RECEIVE_TOP_IDS', { ids })
51-
store.dispatch('FETCH_NEWS')
50+
store.commit('SET_ACTIVE_IDS', { ids })
51+
store.dispatch('FETCH_DISPLAYED_ITEMS')
5252
})
5353
}
5454

5555
function getDisplayedIds (state) {
5656
const page = Number(state.route.params.page) || 1
57-
const { storiesPerPage, topStoryIds } = state
58-
const start = (page - 1) * storiesPerPage
59-
const end = page * storiesPerPage
60-
return topStoryIds.slice(start, end)
57+
const { itemsPerPage, activeItemIds } = state
58+
const start = (page - 1) * itemsPerPage
59+
const end = page * itemsPerPage
60+
return activeItemIds.slice(start, end)
6161
}
6262

6363
export default store

src/views/News.vue

+32-33
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<template>
22
<div>
3-
<h2>News <spinner :show="loading"></spinner></h2>
3+
<spinner :show="loading"></spinner>
44
<ul>
55
<li>
66
<router-link v-if="page > 1" :to="'/news/' + (page - 1)">prev</router-link>
@@ -28,8 +28,8 @@ import NewsItem from '../components/NewsItem.vue'
2828
2929
const fetchInitialData = store => {
3030
return store
31-
.dispatch(`FETCH_TOP_IDS`)
32-
.then(() => store.dispatch(`FETCH_NEWS`))
31+
.dispatch(`FETCH_IDS`)
32+
.then(() => store.dispatch(`FETCH_DISPLAYED_ITEMS`))
3333
}
3434
3535
export default {
@@ -43,7 +43,7 @@ export default {
4343
return {
4444
loading: false,
4545
displayPage: this.$route.params.page,
46-
displayItems: this.$store.getters.news,
46+
displayItems: this.$store.getters.displayedItems,
4747
transition: 'slide-left'
4848
}
4949
},
@@ -52,8 +52,8 @@ export default {
5252
return Number(this.$route.params.page)
5353
},
5454
maxPage () {
55-
const { storiesPerPage, topStoryIds } = this.$store.state
56-
return Math.floor(topStoryIds.length / storiesPerPage)
55+
const { itemsPerPage, activeItemIds } = this.$store.state
56+
return Math.floor(activeItemIds.length / itemsPerPage)
5757
},
5858
hasMore () {
5959
return this.page < this.maxPage
@@ -68,42 +68,41 @@ export default {
6868
watch: {
6969
'$route' (to, from) {
7070
this.loading = true
71-
this.$store.dispatch(`FETCH_NEWS`).then(() => {
71+
this.$store.dispatch(`FETCH_DISPLAYED_ITEMS`).then(() => {
7272
const toPage = Number(to.params.page)
7373
const fromPage = Number(from.params.page)
7474
this.transition = toPage > fromPage ? 'slide-left' : 'slide-right'
7575
this.displayPage = toPage
76-
this.displayItems = this.$store.getters.news.slice()
76+
this.displayItems = this.$store.getters.displayedItems
7777
this.loading = false
7878
})
7979
}
8080
}
8181
}
8282
</script>
8383

84-
<style>
85-
.news-list {
86-
position: absolute;
87-
transition: all .5s cubic-bezier(.55,0,.1,1);
88-
}
89-
.slide-left-enter, .slide-right-leave-active {
90-
opacity: 0;
91-
transform: translate(30px, 0);
92-
}
93-
.slide-left-leave-active, .slide-right-enter {
94-
opacity: 0;
95-
transform: translate(-30px, 0);
96-
}
97-
.item-move, .item-enter-active, .item-leave-active {
98-
transition: all .5s cubic-bezier(.55,0,.1,1);
99-
}
100-
.item-enter {
101-
opacity: 0;
102-
transform: translate(30px, 0);
103-
}
104-
.item-leave-active {
105-
position: absolute;
106-
opacity: 0;
107-
transform: translate(30px, 0);
108-
}
84+
<style lang="stylus">
85+
.news-list
86+
position absolute
87+
transition all .5s cubic-bezier(.55,0,.1,1)
88+
89+
.slide-left-enter, .slide-right-leave-active
90+
opacity 0
91+
transform translate(30px, 0)
92+
93+
.slide-left-leave-active, .slide-right-enter
94+
opacity 0
95+
transform translate(-30px, 0)
96+
97+
.item-move, .item-enter-active, .item-leave-active
98+
transition all .5s cubic-bezier(.55,0,.1,1)
99+
100+
.item-enter
101+
opacity 0
102+
transform translate(30px, 0)
103+
104+
.item-leave-active
105+
position absolute
106+
opacity 0
107+
transform translate(30px, 0)
109108
</style>

0 commit comments

Comments
 (0)