Skip to content

Commit 7b6f0f2

Browse files
author
guosw
committed
pref: Project optimization in progress
1 parent a6f0209 commit 7b6f0f2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+765
-1041
lines changed

.eslintrc.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ module.exports = {
175175
'no-path-concat': 'off', // 可以直接使用 __dirname
176176
'prefer-promise-reject-errors': 0,
177177
"react-hooks/rules-of-hooks": "off",
178-
'react-hooks/exhaustive-deps': 'off'
178+
'react-hooks/exhaustive-deps': 'off',
179+
'jsx-a11y/anchor-is-valid': 'off'
179180
}
180181
}

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"@svgr/webpack": "4.3.2",
88
"@typescript-eslint/eslint-plugin": "1.13.0",
99
"@typescript-eslint/parser": "1.13.0",
10-
"antd": "^3.23.0",
10+
"antd": "^3.26.6",
1111
"axios": "^0.19.0",
1212
"babel-eslint": "10.0.2",
1313
"babel-jest": "^24.8.0",

server/controllers/article.js

+21-20
Original file line numberDiff line numberDiff line change
@@ -232,25 +232,21 @@ class ArticleController {
232232

233233
if (validator) {
234234
const { fileNameList } = ctx.request.body
235-
const existList = [] // 存在的文件名列表
236-
const noExistList = []
237235
const list = await Promise.all(
238236
fileNameList.map(async fileName => {
239237
const filePath = `${uploadPath}/${fileName}`
240-
const result = decodeFile(filePath)
241-
const title = result.title || fileName.replace(/\.md/, '')
242-
const article = await ArticleModel.findOne({ where: { title } })
238+
const file = decodeFile(filePath)
239+
const title = file.title || fileName.replace(/\.md/, '')
240+
const article = await ArticleModel.findOne({ where: { title }, attributes: ['id'] })
241+
const result = { fileName, title }
243242
if (article) {
244-
existList.push({ fileName, articleId: article.id, title: article.title })
245-
} else {
246-
const params = { fileName, title: result.title }
247-
params.title ? noExistList.unshift(params) : noExistList.push(params)
243+
result.exist = true
244+
result.articleId = article.id
248245
}
249-
return article
246+
return result
250247
})
251248
)
252-
253-
ctx.body = { existList, noExistList }
249+
ctx.body = list
254250
}
255251
}
256252

@@ -279,12 +275,16 @@ class ArticleController {
279275
static async uploadConfirm(ctx) {
280276
const validator = ctx.validate(ctx.request.body, {
281277
authorId: Joi.number(),
282-
insertList: Joi.array(),
283-
updateList: Joi.array()
278+
uploadList: Joi.array()
284279
})
285280
if (validator) {
286-
const { insertList, updateList, authorId } = ctx.request.body
281+
const { uploadList, authorId } = ctx.request.body
287282
await findOrCreateFilePath(uploadPath) // 检查目录
283+
// const insertList = []
284+
// const updateList = []
285+
// uploadList.forEach(file => {
286+
// file.exist ? updateList.push(file) : insertList.push(file)
287+
// })
288288

289289
const _parseList = list => {
290290
return list.map(item => {
@@ -304,16 +304,17 @@ class ArticleController {
304304
})
305305
}
306306

307-
const list1 = _parseList(insertList)
308-
const list2 = _parseList(updateList)
307+
const list = _parseList(uploadList)
308+
const updateList = list.filter(d => d.articleId !== 'undefined')
309+
const insertList = list.filter(d => d.articleId === 'undefined')
309310

310311
// 插入文章
311312
const insertResultList = await Promise.all(
312-
list1.map(data => ArticleModel.create(data, { include: [TagModel, CategoryModel] }))
313+
insertList.map(data => ArticleModel.create(data, { include: [TagModel, CategoryModel] }))
313314
)
314315

315316
const updateResultList = await Promise.all(
316-
list2.map(async data => {
317+
updateList.map(async data => {
317318
const { title, content, categories = [], tags = [], articleId } = data
318319
await ArticleModel.update({ title, content }, { where: { id: articleId } })
319320
await TagModel.destroy({ where: { articleId } })
@@ -324,7 +325,7 @@ class ArticleController {
324325
})
325326
)
326327

327-
ctx.body = { insertList: insertResultList, updateList: updateResultList }
328+
ctx.body = { message: 'success', insertList: insertResultList, updateList: updateResultList }
328329
}
329330
}
330331

server/controllers/user.js

+1-10
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,6 @@ class UserController {
124124
// username: user.username, role, userId: id, token
125125
const token = createToken({ userId: githubInfo.id, role: target.role }) // 生成 token
126126

127-
// ctx.client(200, 'success', {
128-
// github: githubInfo,
129-
// username: target.username,
130-
// userId: target.id,
131-
// role: target.role,
132-
// token
133-
// })
134127
ctx.body = {
135128
github: githubInfo,
136129
username: target.username,
@@ -139,7 +132,6 @@ class UserController {
139132
token
140133
}
141134
} else {
142-
// ctx.client(403, 'github 授权码已失效!')
143135
ctx.throw(403, 'github 授权码已失效!')
144136
}
145137
}
@@ -163,8 +155,7 @@ class UserController {
163155
} else {
164156
const user = await UserModel.findOne({ where: { username } })
165157
if (user && !user.github) {
166-
// ctx.client(403, '用户名已被占用')
167-
ctx.client(403, '用户名已被占用')
158+
ctx.throw(403, '用户名已被占用')
168159
} else {
169160
const saltPassword = await encrypt(password)
170161
await UserModel.create({ username, password: saltPassword, email })

src/components/Discuss/index.jsx

+6-4
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,16 @@ import { DISCUSS_AVATAR } from '@/config'
77
// methods
88
import axios from '@/utils/axios'
99
import { calcCommentsCount } from '@/utils'
10-
import { login, register, loginout } from '@/redux/user/actions'
11-
import { switchSignModal } from '@/redux/app/actions'
10+
import { loginout } from '@/redux/modal/user'
1211

1312
// components
1413
import SvgIcon from '@/components/SvgIcon'
1514
import { Comment, Avatar, Form, Button, Divider, Input, Icon, Menu, Dropdown, message, Modal } from 'antd'
1615
import List from './list' // 评论列表
1716
import AppAvatar from '@/components/Avatar'
1817

18+
import useBus from '@/hooks/useBus'
19+
1920
const { TextArea } = Input
2021

2122
const Editor = ({ onChange, onSubmit, submitting, value, articleId }) => (
@@ -37,6 +38,7 @@ const Editor = ({ onChange, onSubmit, submitting, value, articleId }) => (
3738

3839
function Discuss(props) {
3940
const dispatch = useDispatch()
41+
const bus = useBus()
4042
const userInfo = useSelector(state => state.user)
4143
const { username, role } = userInfo
4244

@@ -60,12 +62,12 @@ function Discuss(props) {
6062
function handleMenuClick(e) {
6163
switch (e.key) {
6264
case 'login':
63-
dispatch(switchSignModal('login', true))
65+
bus.emit('openSignModal', 'login')
6466

6567
break
6668

6769
case 'register':
68-
dispatch(switchSignModal('register', true))
70+
bus.emit('openSignModal', 'register')
6971

7072
break
7173

src/components/GithubLogining/index.jsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { useDispatch } from 'react-redux'
44
// ..
55
import { Spin } from 'antd'
66
import { decodeQuery } from '@/utils'
7-
import { login } from '@/redux/user/actions'
7+
import { login } from '@/redux/modal/user'
88
import { get, remove } from '@/utils/storage'
99

1010
function AppLoading(props) {

src/components/Public/SignModal/index.jsx

+16-10
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
import React, { useState, useEffect } from 'react'
22
import { Form, Icon, Input, Button, Modal } from 'antd'
3+
import { useLocation } from 'react-router-dom'
34

45
import { GITHUB } from '@/config'
5-
import { save, remove } from '@/utils/storage'
6+
import { save } from '@/utils/storage'
7+
8+
// redux
9+
import { login, register } from '@/redux/modal/user'
10+
import { useDispatch } from 'react-redux'
611

712
// hooks
813
import { useListener } from '@/hooks/useBus'
@@ -24,9 +29,12 @@ function FormItem(props) {
2429
}
2530

2631
function SignModal(props) {
32+
const dispatch = useDispatch() // dispatch hooks
33+
const location = useLocation() // location
2734
const [visible, setVisible] = useState(false)
2835
const [type, setType] = useState('login')
2936
const { getFieldDecorator } = props.form
37+
3038
useListener('openSignModal', type => {
3139
props.form.resetFields()
3240
setType(type)
@@ -37,17 +45,15 @@ function SignModal(props) {
3745
e.preventDefault()
3846
props.form.validateFieldsAndScroll((errors, values) => {
3947
if (errors) return
40-
console.log(values)
41-
if (type === 'login') {
42-
// dispatch(login(values))
43-
} else if (type === 'register') {
44-
// dispatch(register(values))
45-
}
48+
const action = type === 'login' ? login : register
49+
dispatch(action(values)).then(() => {
50+
setVisible(false) // type = login | register
51+
})
4652
})
4753
}
4854

4955
function githubLogin() {
50-
const { pathname, search } = props.location
56+
const { pathname, search } = location
5157
save('prevRouter', `${pathname}${search}`)
5258
window.location.href = `${GITHUB.url}?client_id=${GITHUB.client_id}`
5359
}
@@ -97,14 +103,14 @@ function SignModal(props) {
97103
})(<Input placeholder='请输入密码' type='password' />)}
98104
</FormItem>
99105
<FormItem label='确认密码'>
100-
{getFieldDecorator('password', {
106+
{getFieldDecorator('confirm', {
101107
rules: [
102108
{ required: true, message: 'Password is required' },
103109
{ validator: compareToFirstPassword }
104110
]
105111
})(<Input placeholder='确认密码' type='password' />)}
106112
</FormItem>
107-
<FormItem label='密码'>
113+
<FormItem label='邮箱'>
108114
{getFieldDecorator('email', {
109115
rules: [
110116
{ type: 'email', message: 'The input is not valid E-mail!' },

0 commit comments

Comments
 (0)