Skip to content

Commit

Permalink
add: settings
Browse files Browse the repository at this point in the history
  • Loading branch information
leozhang007 committed Aug 25, 2020
1 parent 88135ad commit 4be7e63
Show file tree
Hide file tree
Showing 14 changed files with 402 additions and 139 deletions.
23 changes: 23 additions & 0 deletions api/article.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,27 @@ export const getComments = slug => {
method: 'GET',
url: `/api/articles/${slug}/comments`
})
}

export const createArticle = data => {
return request({
method: 'POST',
url: `/api/articles`,
data
})
}

export const editArticle = slug => {
return request({
method: 'GET',
url: `/api/articles/${slug}`,
})
}

export const updateArticle = data => {
return request({
method: 'PUT',
url: `/api/articles/${data.slug}`,
data
})
}
36 changes: 36 additions & 0 deletions api/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,40 @@ export const register = data => {
url: '/api/users',
data
})
}

export const getUser = () => {
return request({
method: 'GET',
url: '/api/user'
})
}

export const updateUser = data => {
return request({
method: 'PUT',
url: '/api/user',
data
})
}

export const getProfile = username => {
return request({
method: 'GET',
url: `/api/profiles/${username}`
})
}

export const followUser = username => {
return request({
method: 'POST',
url: `/api/profiles/${username}/follow`
})
}

export const unFollowUser = username => {
return request({
method: 'DELETE',
url: `/api/profiles/${username}/follow`
})
}
78 changes: 78 additions & 0 deletions components/articleList.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<template>
<div>
<div
class="article-preview"
v-for="article in articles"
:key="article.slug"
>
<div class="article-meta">
<nuxt-link
:to="{
name: 'profile',
params: {
username: article.author.username
}
}"
>
<img :src="article.author.image" />
</nuxt-link >
<div class="info">
<nuxt-link
:to="{
name: 'profile',
params: {
username: article.author.username
}
}"
>
{{ article.author.username }}
</nuxt-link >
<span class="date">{{ article.createdAt | date('MMM DD, YYYY') }}</span>
</div>
<button
class="btn btn-outline-primary btn-sm pull-xs-right"
:class="{
active: article.favorited
}"
@click="onFavorite(article)"
:disabled="article.favoriteDisabled"
>
<i class="ion-heart"></i> {{ article.favoritesCount }}
</button>
</div>
<nuxt-link
class="preview-link"
:to="{
name: 'article',
params: {
slug: article.slug
}
}"
>
<h1>{{ article.title }}</h1>
<p>{{ article.description }}</p>
<span>Read more...</span>
</nuxt-link>
</div>
</div>
</template>

<script>
export default {
name: 'ArticleList',
props: {
articles: {
type: Array,
required: true
},
onFavorite: {
type: Function,
required: true
}
}
}
</script>

<style>
</style>
2 changes: 1 addition & 1 deletion nuxt.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ module.exports = {
component: resolve(__dirname, 'pages/settings/'),
},
{
path: '/editor',
path: '/editor/:slug?',
name: 'editor',
component: resolve(__dirname, 'pages/editor/'),
},
Expand Down
4 changes: 2 additions & 2 deletions pages/article/components/article-comments.vue
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ export default {
}
},
async mounted () {
const { data } = await getComments(this.article.slug)
this.comments = data.comments
const { comments } = await getComments(this.article.slug)
this.comments = comments
}
}
</script>
Expand Down
3 changes: 1 addition & 2 deletions pages/article/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ export default {
ArticleComments
},
async asyncData ({ params }) {
const { data } = await getArticle(params.slug)
const { article } = data
const { article } = await getArticle(params.slug)
const md = new MarkdownIt()
article.body = md.render(article.body)
return {
Expand Down
74 changes: 66 additions & 8 deletions pages/editor/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,32 @@
<div class="row">

<div class="col-md-10 offset-md-1 col-xs-12">
<form>
<form @submit.prevent="onSubmit">
<fieldset>
<fieldset class="form-group">
<input type="text" class="form-control form-control-lg" placeholder="Article Title">
<input v-model="article.title" type="text" class="form-control form-control-lg" placeholder="Article Title">
</fieldset>
<fieldset class="form-group">
<input type="text" class="form-control" placeholder="What's this article about?">
<input v-model="article.description" type="text" class="form-control" placeholder="What's this article about?">
</fieldset>
<fieldset class="form-group">
<textarea class="form-control" rows="8" placeholder="Write your article (in markdown)"></textarea>
<textarea v-model="article.body" class="form-control" rows="8" placeholder="Write your article (in markdown)"></textarea>
</fieldset>
<fieldset class="form-group">
<input type="text" class="form-control" placeholder="Enter tags"><div class="tag-list"></div>
<input @keyup.enter="addTag" v-model="articleTag" type="text" class="form-control" placeholder="Enter tags"><div class="tag-list"></div>
<div class="tag-list">
<span class="tag-default tag-pill" v-for="tag in article.tagList" :key="tag">
<i class="ion-close-round" @click="removeTag(tag)"></i>
{{tag}}
</span>
</div>
</fieldset>
<button class="btn btn-lg pull-xs-right btn-primary" type="button">
Publish Article
<button
class="btn btn-lg pull-xs-right btn-primary"
type="button"
@click="onSubmit"
>
Publish Article
</button>
</fieldset>
</form>
Expand All @@ -31,9 +41,57 @@
</template>

<script>
import { createArticle, editArticle, updateArticle } from '@/api/article'
export default {
middleware: 'authenticated',
name: 'EditorIndex'
name: 'EditorIndex',
async asyncData ({ store, env, params, query }) {
if (params.slug) {
const { article } = await editArticle(params.slug)
return {
article
}
} else {
return {}
}
},
data () {
return {
article: {
slug: '',
title: '',
description: '',
body: '',
tagList: []
},
articleTag: '',
errors: {},
}
},
methods: {
async onSubmit () {
const request = this.article.slug ? updateArticle : createArticle;
const { article } = await request(this.article);
if (article) {
this.$router.push({
name: "article",
params: { slug: article.slug },
});
}
},
addTag () {
if (!this.article.tagList.includes(this.articleTag)) {
this.article.tagList.push(this.articleTag);
this.articleTag = "";
}
},
removeTag(tag) {
this.article.tagList = this.article.tagList.filter(
(vals) => vals !== tag
);
},
}
}
</script>

Expand Down
67 changes: 9 additions & 58 deletions pages/home/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -66,60 +66,7 @@
</ul>
</div>

<div
class="article-preview"
v-for="article in articles"
:key="article.slug"
>
<div class="article-meta">
<nuxt-link
:to="{
name: 'profile',
params: {
username: article.author.username
}
}"
>
<img :src="article.author.image" />
</nuxt-link >
<div class="info">
<nuxt-link
:to="{
name: 'profile',
params: {
username: article.author.username
}
}"
>
{{ article.author.username }}
</nuxt-link >
<span class="date">{{ article.createdAt | date('MMM DD, YYYY') }}</span>
</div>
<button
class="btn btn-outline-primary btn-sm pull-xs-right"
:class="{
active: article.favorited
}"
@click="onFavorite(article)"
:disabled="article.favoriteDisabled"
>
<i class="ion-heart"></i> {{ article.favoritesCount }}
</button>
</div>
<nuxt-link
class="preview-link"
:to="{
name: 'article',
params: {
slug: article.slug
}
}"
>
<h1>{{ article.title }}</h1>
<p>{{ article.description }}</p>
<span>Read more...</span>
</nuxt-link>
</div>
<article-list :articles="articles" :onFavorite="onFavorite" />

<!-- 分页列表 -->
<nav>
Expand Down Expand Up @@ -180,26 +127,30 @@
import { getArticles, getYourFeedArticles, addFavorite, deleteFavorite } from '@/api/article'
import { getTags } from '@/api/tag'
import { mapState } from 'vuex'
import ArticleList from '@/components/articleList'
export default {
name: 'HomeIndex',
components: {
ArticleList
},
async asyncData ({ query }) {
const page = Number.parseInt(query.page || 1)
const limit = 20
const tab = query.tab || 'global_feed'
const tag = query.tag
const loadArticles = tab === 'your_feed' ? getYourFeedArticles : getArticles
const [ articleRes, tagRes ] = await Promise.all([
const [ data, tagData ] = await Promise.all([
loadArticles({
limit,
offset: (page - 1) * 2,
tag
}),
getTags()
])
const { articles, articlesCount } = articleRes.data
const { tags } = tagRes.data
const { articles, articlesCount } = data
const { tags } = tagData
articles.forEach(article => article.favoriteDisabled = false)
Expand Down
7 changes: 6 additions & 1 deletion pages/layout/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@
</nuxt-link>
</li>
<li class="nav-item">
<nuxt-link to="/profile/123" class="nav-link" >
<nuxt-link :to="{
name: 'profile',
params: {
username: user.username
}
}" class="nav-link" >
<img class="user-pic" :src="user.image">
{{ user.username }}
</nuxt-link>
Expand Down
Loading

0 comments on commit 4be7e63

Please sign in to comment.