Skip to content

Commit 5c80fea

Browse files
author
guosw
committed
pref: code tuning and optimization
1 parent 546a2c3 commit 5c80fea

File tree

4 files changed

+95
-77
lines changed

4 files changed

+95
-77
lines changed

src/components/Discuss/index.jsx

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { DISCUSS_AVATAR } from '@/config'
88
import axios from '@/utils/axios'
99
import { calcCommentsCount } from '@/utils'
1010
import { loginout } from '@/redux/user/actions'
11+
import useAjaxLoading from '@/hooks/useAjaxLoading'
1112

1213
// components
1314
import SvgIcon from '@/components/SvgIcon'
@@ -44,7 +45,7 @@ function Discuss(props) {
4445

4546
const { commentList, articleId } = props
4647
const [value, setValue] = useState('')
47-
const [submitting, setSubmitting] = useState(false)
48+
const [submitting, withLoading] = useAjaxLoading()
4849

4950
const renderDropdownMenu = () => {
5051
return username ? (
@@ -84,16 +85,12 @@ function Discuss(props) {
8485
if (!value) return
8586
if (!userInfo.username) return message.warn('您未登陆,请登录后再试。')
8687

87-
setSubmitting(true)
88-
89-
axios
90-
.post('/discuss', { articleId: props.articleId, content: value, userId: userInfo.userId })
91-
.then(res => {
92-
setSubmitting(false)
93-
setValue('')
94-
props.setCommentList(res.rows)
95-
})
96-
.catch(e => setSubmitting(false))
88+
withLoading(
89+
axios.post('/discuss', { articleId: props.articleId, content: value, userId: userInfo.userId })
90+
).then(res => {
91+
setValue('')
92+
props.setCommentList(res.rows)
93+
})
9794
}
9895

9996
return (

src/hooks/useAjaxLoading.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import React, { useState } from 'react'
2+
3+
/**
4+
* ajax request with loading
5+
*/
6+
export default function useRequestLoading() {
7+
const [loading, setLoading] = useState(false)
8+
9+
function withLoading(request) {
10+
if (request instanceof Promise) {
11+
return new Promise((reslove, reject) => {
12+
setLoading(true)
13+
request.then(res => {
14+
reslove(res)
15+
setLoading(false)
16+
}).catch(e => {
17+
reject(e)
18+
setLoading(false)
19+
})
20+
})
21+
}
22+
}
23+
24+
return [loading, withLoading]
25+
}
26+

src/views/web/article/index.jsx

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { useMediaQuery } from 'react-responsive'
55
// methods
66
import axios from '@/utils/axios'
77
import { translateMarkdown, calcCommentsCount } from '@/utils'
8+
import useAjaxLoading from '@/hooks/useAjaxLoading'
89

910
// components
1011
import { Drawer, Icon, Divider, Spin } from 'antd'
@@ -14,7 +15,8 @@ import Navigation from './Navigation'
1415
import Discuss from '@/components/Discuss'
1516

1617
function Article(props) {
17-
const [loading, setLoading] = useState(true)
18+
const [loading, withLoading] = useAjaxLoading()
19+
1820
const [article, setArticle] = useState({
1921
title: '',
2022
content: '',
@@ -35,21 +37,14 @@ function Article(props) {
3537
}, [])
3638

3739
useEffect(() => {
38-
const fetchData = id => {
39-
setLoading(true)
40-
axios
41-
.get(`/article/${id}`)
42-
.then(res => {
43-
res.content = translateMarkdown(res.content)
44-
setArticle(res)
45-
setLoading(false)
46-
})
47-
.catch(e => {
48-
props.history.push('/404')
49-
})
50-
}
51-
fetchData(props.match.params.id)
52-
/*eslint react-hooks/exhaustive-deps: "off"*/
40+
withLoading(axios.get(`/article/${props.match.params.id}`))
41+
.then(res => {
42+
res.content = translateMarkdown(res.content)
43+
setArticle(res)
44+
})
45+
.catch(e => {
46+
props.history.push('/404')
47+
})
5348
}, [props.match.params.id])
5449

5550
function setCommentList(list) {

src/views/web/home/index.jsx

Lines changed: 50 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useState } from 'react'
1+
import React, { useState, useMemo } from 'react'
22
import './index.less'
33

44
import { Link } from 'react-router-dom'
@@ -15,10 +15,11 @@ import SvgIcon from '@/components/SvgIcon'
1515
// hooks
1616
import useFetchList from '@/hooks/useFetchList'
1717

18-
function Preview({ list, showTitle = true }) {
18+
const LinkList = props => {
19+
const { list, showTitle = true } = props
1920
return (
2021
<ul className='preview'>
21-
{showTitle && <Divider>文章列表</Divider>}
22+
{showTitle && <Divider>快速导航</Divider>}
2223
{list.map(item => (
2324
<li key={item.id}>
2425
<Link to={`/article/${item.id}`}>{item.title}</Link>
@@ -28,41 +29,58 @@ function Preview({ list, showTitle = true }) {
2829
)
2930
}
3031

31-
function NoDataDesc({ keyword }) {
32-
return keyword ? (
33-
<span>
34-
不存在标题/内容中含有 <span className='keyword'>{keyword}</span> 的文章!
35-
</span>
32+
/**
33+
* 快速导航
34+
*/
35+
const QuickLink = props => {
36+
const isGreaterThan1300 = useMediaQuery({ query: '(min-width: 1300px)' })
37+
38+
const { list } = props
39+
40+
const [drawerVisible, setDrawerVisible] = useState(false)
41+
42+
return isGreaterThan1300 ? (
43+
<LinkList list={list} />
3644
) : (
37-
<span>暂无数据...</span>
45+
<>
46+
<div className='drawer-btn' onClick={e => setDrawerVisible(true)}>
47+
<Icon type='menu-o' className='nav-phone-icon' />
48+
</div>
49+
<Drawer
50+
title='快速导航'
51+
placement='right'
52+
closable={false}
53+
onClose={e => setDrawerVisible(false)}
54+
visible={drawerVisible}
55+
getContainer={() => document.querySelector('.app-home')}>
56+
<LinkList list={list} showTitle={false} />
57+
</Drawer>
58+
</>
3859
)
3960
}
4061

41-
function Home(props) {
42-
const [drawerVisible, setDrawerVisible] = useState(false)
43-
62+
const Home = props => {
4463
const { loading, pagination, dataList } = useFetchList({
4564
requestUrl: '/article/list',
4665
queryParams: { pageSize: HOME_PAGESIZE },
4766
fetchDependence: [props.location.search]
4867
})
4968

50-
const list = [...dataList].map(item => {
51-
const index = item.content.indexOf('<!--more-->')
52-
item.content = translateMarkdown(item.content.slice(0, index))
53-
return item
54-
})
55-
56-
const isGreaterThan1300 = useMediaQuery({
57-
query: '(min-width: 1300px)'
58-
})
69+
const list = useMemo(() => {
70+
return [...dataList].map(item => {
71+
const index = item.content.indexOf('<!--more-->')
72+
item.content = translateMarkdown(item.content.slice(0, index))
73+
return item
74+
})
75+
}, [dataList])
5976

6077
// 跳转到文章详情
6178
function jumpTo(id) {
6279
props.history.push(`/article/${id}`)
6380
}
6481

6582
const { keyword } = decodeQuery(props.location.search)
83+
6684
return (
6785
<Spin tip='Loading...' spinning={loading}>
6886
<div className='app-home'>
@@ -94,35 +112,17 @@ function Home(props) {
94112
</li>
95113
))}
96114
</ul>
97-
{list.length > 0 ? (
98-
<>
99-
{isGreaterThan1300 ? (
100-
<Preview list={list} />
101-
) : (
102-
<>
103-
<div className='drawer-btn' onClick={e => setDrawerVisible(true)}>
104-
<Icon type='menu-o' className='nav-phone-icon' />
105-
</div>
106-
<Drawer
107-
title='文章列表'
108-
placement='right'
109-
closable={false}
110-
onClose={e => setDrawerVisible(false)}
111-
visible={drawerVisible}
112-
getContainer={() => document.querySelector('.app-home')}>
113-
<Preview list={list} showTitle={false} />
114-
</Drawer>
115-
</>
116-
)}
117-
</>
118-
) : (
119-
<>
120-
{keyword && (
121-
<div className='no-data'>
122-
<Empty description={<NoDataDesc keyword={keyword} />} />
123-
</div>
124-
)}
125-
</>
115+
<QuickLink list={list} />
116+
117+
{/* 结果为空 */}
118+
{list.length === 0 && keyword && (
119+
<div className='no-data'>
120+
<Empty description={(
121+
<span>
122+
不存在标题/内容中含有 <span className='keyword'>{keyword}</span> 的文章!
123+
</span>
124+
)} />
125+
</div>
126126
)}
127127

128128
<Pagination {...pagination} onChange={

0 commit comments

Comments
 (0)