Skip to content

Commit 0da120a

Browse files
more syntactically pleasing way to handle store modules
Signed-off-by: Arnav Gupta <[email protected]>
1 parent 478c50a commit 0da120a

File tree

12 files changed

+194
-138
lines changed

12 files changed

+194
-138
lines changed

package.json

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,30 +11,30 @@
1111
},
1212
"dependencies": {
1313
"axios": "^0.18.0",
14-
"vue": "^2.5.17",
15-
"vue-class-component": "^6.0.0",
14+
"vue": "^2.5.18",
15+
"vue-class-component": "^6.1.0",
1616
"vue-property-decorator": "^7.2.0",
17-
"vue-router": "^3.0.1",
17+
"vue-router": "^3.0.2",
1818
"vuex": "^3.0.1",
19-
"vuex-module-decorators": "^0.9.2"
19+
"vuex-module-decorators": "^0.9.7"
2020
},
2121
"devDependencies": {
22-
"@types/jest": "^23.1.4",
22+
"@types/jest": "^23.3.10",
2323
"@vue/cli-plugin-babel": "^3.2.0",
2424
"@vue/cli-plugin-e2e-cypress": "^3.2.0",
25-
"@vue/cli-plugin-eslint": "^3.2.0",
25+
"@vue/cli-plugin-eslint": "^3.2.1",
2626
"@vue/cli-plugin-typescript": "^3.2.0",
2727
"@vue/cli-plugin-unit-jest": "^3.2.0",
2828
"@vue/cli-service": "^3.2.0",
29-
"@vue/eslint-config-prettier": "^4.0.0",
29+
"@vue/eslint-config-prettier": "^4.0.1",
3030
"@vue/eslint-config-typescript": "^3.2.0",
31-
"@vue/test-utils": "^1.0.0-beta.20",
31+
"@vue/test-utils": "^1.0.0-beta.27",
3232
"babel-core": "7.0.0-bridge.0",
3333
"babel-eslint": "^10.0.1",
34-
"eslint": "^5.8.0",
35-
"eslint-plugin-vue": "^5.0.0-0",
36-
"ts-jest": "^23.0.0",
37-
"typescript": "^3.0.0",
38-
"vue-template-compiler": "^2.5.17"
34+
"eslint": "^5.10.0",
35+
"eslint-plugin-vue": "^5.0.0",
36+
"ts-jest": "^23.10.5",
37+
"typescript": "^3.2.2",
38+
"vue-template-compiler": "^2.5.21"
3939
}
4040
}

src/components/AppNavbar.vue

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,16 @@
1717
<i class="ion-gear-a"></i>&nbsp;Settings
1818
</a>
1919
</li>
20-
<li v-if="!username" class="nav-item">
21-
<router-link class="nav-link" to="/register">
22-
Sign up
20+
<li v-if="username" class="nav-item">
21+
<router-link class="nav-link" :to="'/@' + username">
22+
{{ username }}
2323
</router-link>
2424
</li>
2525
<li v-if="!username" class="nav-item">
26-
<router-link class="nav-link" to="/login">
27-
Sign in
28-
</router-link>
26+
<router-link class="nav-link" to="/register"> Sign up </router-link>
27+
</li>
28+
<li v-if="!username" class="nav-item">
29+
<router-link class="nav-link" to="/login"> Sign in </router-link>
2930
</li>
3031
</ul>
3132
</div>
@@ -34,12 +35,12 @@
3435

3536
<script>
3637
import { Vue, Component } from 'vue-property-decorator';
37-
import users from '@/store/modules/users'
38+
import users from '@/store/modules/users';
3839
3940
@Component
4041
export default class AppNavbar extends Vue {
4142
get username() {
42-
return users.username
43+
return users.username;
4344
}
4445
}
4546
</script>
Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,18 @@
11
<template>
22
<div class="article-preview">
33
<div class="article-meta">
4-
<a href="profile.html">
5-
<img :src="article.author.image" />
6-
</a>
4+
<a href="profile.html"> <img :src="article.author.image" /> </a>
75
<div class="info">
8-
<a
9-
href=""
10-
class="author"
11-
>{{article.author.username}}</a>
6+
<a href="" class="author">{{ article.author.username }}</a>
127
<span class="date">January 20th</span>
138
</div>
149
<button class="btn btn-outline-primary btn-sm pull-xs-right">
15-
<i class="ion-heart"></i> {{article.favoritesCount}}
10+
<i class="ion-heart"></i> {{ article.favoritesCount }}
1611
</button>
1712
</div>
18-
<a
19-
href=""
20-
class="preview-link"
21-
>
22-
<h1>
23-
{{article.title}}
24-
</h1>
25-
<p>{{article.description}}</p>
13+
<a href="" class="preview-link">
14+
<h1>{{ article.title }}</h1>
15+
<p>{{ article.description }}</p>
2616
<span>Read more...</span>
2717
</a>
2818
</div>
@@ -34,6 +24,6 @@ import { Article } from '@/store/models';
3424
3525
@Component
3626
export default class ArticlePreivew extends Vue {
37-
@Prop() article?: Article
27+
@Prop() article?: Article;
3828
}
3929
</script>

src/router.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,10 @@ export default new Router({
2626
name: 'settings',
2727
component: () => import('@/views/Settings.vue'),
2828
},
29+
{
30+
path: '/@:username',
31+
name: 'profile',
32+
component: () => import('@/views/Profile.vue'),
33+
},
2934
],
3035
});

src/store/api.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
import axios from 'axios';
2-
import { UserSubmit, UserResponse, User, ArticlesResponse } from './models';
2+
import {
3+
UserSubmit,
4+
UserResponse,
5+
User,
6+
ArticlesResponse,
7+
Profile,
8+
ProfileResponse,
9+
} from './models';
310

411
export const conduitApi = axios.create({
512
baseURL: 'https://conduit.productionready.io/api',
@@ -20,7 +27,12 @@ export async function loginUser(user: UserSubmit): Promise<User> {
2027
return (response.data as UserResponse).user;
2128
}
2229

30+
export async function fetchProfile(username: string): Promise<Profile> {
31+
const response = await conduitApi.get(`/profiles/${username}`);
32+
return (response.data as ProfileResponse).profile;
33+
}
34+
2335
export async function getGlobalFeed() {
24-
const response = await conduitApi.get('/articles')
25-
return response.data as ArticlesResponse
36+
const response = await conduitApi.get('/articles');
37+
return response.data as ArticlesResponse;
2638
}

src/store/models.d.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
export interface Profile {
32
username: string;
43
bio?: string;
@@ -33,18 +32,20 @@ export interface Author {
3332
following: boolean;
3433
}
3534

36-
3735
export interface UserSubmit {
3836
email: string;
3937
password: string;
4038
}
4139

4240
export interface UserResponse {
43-
user: User
41+
user: User;
42+
}
43+
44+
export interface ProfileResponse {
45+
profile: Profile;
4446
}
4547

4648
export interface ArticlesResponse {
4749
articles?: (Article)[] | null;
4850
articlesCount: number;
4951
}
50-

src/store/modules/articles.ts

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,34 @@
1-
import { Module, VuexModule, getModule, Mutation, Action } from "vuex-module-decorators";
2-
import store from '@/store'
1+
import {
2+
Module,
3+
VuexModule,
4+
getModule,
5+
Mutation,
6+
Action,
7+
} from 'vuex-module-decorators';
8+
import store from '@/store';
39
import { Article } from '../models';
4-
import * as api from '@/store/api'
10+
import * as api from '@/store/api';
11+
type FeedType = 'global' | 'user'
512

613
@Module({
714
dynamic: true,
815
namespaced: true,
916
name: 'articles',
10-
store
17+
store,
1118
})
1219
class ArticlesModule extends VuexModule {
13-
globalFeed: Article[] = []
14-
userFeed: Article[] = []
20+
feed: Article[] = [];
1521

1622
@Mutation
17-
setGlobalFeed(articles: Article[]) {
18-
this.globalFeed = articles
23+
setFeed(articles: Article[]) {
24+
this.feed = articles;
1925
}
2026

21-
@Action({commit: 'setGlobalFeed'})
22-
async refreshGlobalFeed() {
23-
const globalFeed = await api.getGlobalFeed()
24-
return globalFeed.articles
27+
@Action({ commit: 'setFeed' })
28+
async refreshFeed(feedType: FeedType) {
29+
const globalFeed = await api.getGlobalFeed();
30+
return globalFeed.articles;
2531
}
2632
}
2733

28-
29-
export default getModule(ArticlesModule)
34+
export default getModule(ArticlesModule);

src/store/modules/users.ts

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,42 @@
1-
import { VuexModule, Module, getModule, MutationAction, Mutation, Action } from 'vuex-module-decorators'
2-
import store from '@/store'
3-
import { User, Profile, UserSubmit } from '../models';
4-
import { loginUser } from '../api';
1+
import { getModule, Module, Mutation, MutationAction, VuexModule } from 'vuex-module-decorators';
2+
import store from '@/store';
3+
import { Profile, User, UserSubmit } from '../models';
4+
import { fetchProfile, loginUser } from '../api';
55

66
@Module({
77
namespaced: true,
88
name: 'users',
99
store,
10-
dynamic: true
10+
dynamic: true,
1111
})
1212
class UsersModule extends VuexModule {
1313
user: User | null = null
1414
profile: Profile | null = null
1515

1616
@Mutation
17-
setUser(user: User) { this.user = user }
17+
setUser(user: User) {
18+
this.user = user;
19+
}
20+
21+
@Mutation
22+
setProfile(profile: Profile) {
23+
this.profile = profile;
24+
}
1825

1926
get username() {
20-
return this.user && this.user.username || null
27+
return (this.user && this.user.username) || null;
2128
}
2229

23-
@Action({commit: 'setUser'})
30+
@MutationAction
2431
async login(userSubmit: UserSubmit) {
25-
const user = await loginUser(userSubmit)
26-
return user
32+
const user = await loginUser(userSubmit);
33+
return { user };
34+
}
35+
36+
@MutationAction
37+
async loadProfile(username: string) {
38+
const profile = await fetchProfile(username);
39+
return { profile };
2740
}
2841
}
2942

src/views/Home.vue

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@
2222
</div>
2323

2424
<ArticlePreview
25-
v-for="article in feed"
26-
:article="article"
27-
:key="article.slug"
25+
v-for="article in feed"
26+
:article="article"
27+
:key="article.slug"
2828
></ArticlePreview>
2929
</div>
3030

@@ -51,22 +51,22 @@
5151

5252
<script lang="ts">
5353
import { Vue, Component } from 'vue-property-decorator';
54-
import ArticlePreview from '@/components/article/ArticlePreview.vue'
55-
import articles from '@/store/modules/articles'
54+
import ArticlePreview from '@/components/article/ArticlePreview.vue';
55+
import articles from '@/store/modules/articles';
5656
import { Article } from '@/store/models';
5757
5858
@Component({
5959
components: {
60-
ArticlePreview
61-
}
60+
ArticlePreview,
61+
},
6262
})
63-
export default class extends Vue {
64-
feed: Article[] = []
63+
export default class extends Vue {
64+
feed: Article[] = [];
6565
6666
created() {
67-
articles.refreshGlobalFeed().then(() => {
68-
this.feed = articles.globalFeed
69-
})
67+
articles.refreshFeed('global').then(() => {
68+
this.feed = articles.feed;
69+
});
7070
}
7171
}
7272
</script>

0 commit comments

Comments
 (0)