Skip to content
This repository was archived by the owner on Nov 8, 2022. It is now read-only.

Commit af9726b

Browse files
committed
refactor the network layer
1 parent af7b0e6 commit af9726b

File tree

11 files changed

+511
-36
lines changed

11 files changed

+511
-36
lines changed

.eslintrc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ module.exports = {
2222
'no-nested-ternary': 0, // TODO
2323
'no-shadow': 0, //TODO: currently just for entry
2424
'no-return-assign': 0, //TODO currently only for BookStore
25+
'prefer-promise-reject-errors': 0,
2526
'react/jsx-no-bind': 0,
2627
'react/jsx-filename-extension': [1, { extensions: ['.js', '.jsx'] }],
2728
'react/forbid-prop-types': 0,

containers/CheatSheetViewer/logic.js

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import R from 'ramda'
22

33
import Prism from 'mastani-codehighlight'
44
import { makeDebugger } from '../../utils/functions'
5+
import network from '../../utils/network'
56

67
/* eslint-disable no-unused-vars */
78
const debug = makeDebugger('L:cheatsheetViewer')
@@ -37,15 +38,42 @@ export const convertTaskTag = R.compose(
3738
R.replace(/<li>\[x\] /g, '<li class="task-done">')
3839
)
3940

41+
const CheatsheetCDN =
42+
'https://raw.githubusercontent.com/mydearxym/mastani-cheatsheets/master'
43+
4044
export function getData(which) {
4145
setTimeout(() => {
42-
cheatsheetViewer.SR71$.getCheatsheet(which)
46+
const url = `${CheatsheetCDN}/${which}.md`
47+
network.GET(url).then(res => {
48+
/* debug('GET ', res) */
49+
if (res.error) return handleError(res)
50+
51+
let source = ''
52+
try {
53+
source = transMarkDownforRender(res)
54+
} catch (err) {
55+
return handleError({ error: 'parse_error' })
56+
}
57+
handleLoaded(source)
58+
})
59+
/* cheatsheetViewer.SR71$.getCheatsheet(which) */
4360
}, 2000)
4461
cheatsheetViewer.markState({
4562
state: 'loading',
4663
})
4764
}
4865

66+
function handleError(res) {
67+
switch (res.error) {
68+
case 404:
69+
return handle404Error()
70+
case 'parse_error':
71+
return handleParseError()
72+
default:
73+
debug(res)
74+
}
75+
}
76+
4977
function handleParseError(errMsg) {
5078
cheatsheetViewer.markState({
5179
current: '',
@@ -55,8 +83,7 @@ function handleParseError(errMsg) {
5583
Prism.highlightAll()
5684
}
5785

58-
const is404 = v => R.trim(v) === '404: Not Found'
59-
function handle404() {
86+
function handle404Error() {
6087
cheatsheetViewer.markState({
6188
current: '',
6289
state: '404',
@@ -75,22 +102,5 @@ function handleLoaded(source) {
75102

76103
export function init(selectedStore) {
77104
cheatsheetViewer = selectedStore
78-
// debug('cheatsheetviewer current: ', cheatsheetViewer.current)
79-
80-
cheatsheetViewer.SR71$.cheatsheet().subscribe(res => {
81-
// console.info('res: ', res)
82-
if (is404(res)) {
83-
handle404()
84-
} else {
85-
let source = ''
86-
87-
try {
88-
source = transMarkDownforRender(res)
89-
} catch (err) {
90-
handleParseError(err)
91-
return false
92-
}
93-
handleLoaded(source)
94-
}
95-
})
105+
debug(cheatsheetViewer)
96106
}

containers/PostsViewer/index.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { inject, observer } from 'mobx-react'
99

1010
// import Link from 'next/link'
1111

12+
import { Button } from '../../components'
1213
import { makeDebugger, storeSelector } from '../../utils/functions'
1314
import * as logic from './logic'
1415
import { Wrapper } from './styles'
@@ -17,13 +18,29 @@ const debug = makeDebugger('C:PostsViewer')
1718

1819
class PostsViewerContainer extends React.Component {
1920
componentWillMount() {
20-
debug('mount')
2121
logic.init(this.props.postsViewer)
2222
}
2323

2424
render() {
2525
debug('postsViewer: ', this.props.postsViewer.data)
26-
return <Wrapper>PostsViewer container!</Wrapper>
26+
return (
27+
<Wrapper>
28+
PostsViewer container!
29+
<div>
30+
<Button type="primary" onClick={logic.createPost}>
31+
createPost (mutate)
32+
</Button>
33+
&nbsp;&nbsp;
34+
<Button type="primary" onClick={logic.postList}>
35+
postList (query)
36+
</Button>
37+
&nbsp;&nbsp;
38+
<Button type="primary" onClick={logic.cheatsheet}>
39+
cheatsheet
40+
</Button>
41+
</div>
42+
</Wrapper>
43+
)
2744
}
2845
}
2946

containers/PostsViewer/logic.js

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,59 @@
11
// import R from 'ramda'
22

33
import { makeDebugger } from '../../utils/functions'
4+
import network from '../../utils/network'
5+
import S from './schema'
46

57
/* eslint-disable no-unused-vars */
68
const debug = makeDebugger('L:PostsViewer')
79
/* eslint-enable no-unused-vars */
810

911
let postsViewer = null
1012

11-
export function someMethod() {}
13+
function handleError(res) {
14+
debug(`handleError [ ${res.error}]: ${res.details}`)
15+
}
16+
17+
// mutation 之后修改 cache
18+
// https://www.apollographql.com/docs/react/basics/mutations.html
19+
export function createPost() {
20+
const variables = {
21+
username: 'seifefefefen',
22+
nickname: 'xi2e',
23+
bio: 'fuckman',
24+
company: 'infomedia',
25+
}
26+
27+
network.mutate(S.createUser, variables).then(res => {
28+
if (res.error) return handleError(res)
29+
debug('mutate: ', res)
30+
})
31+
}
32+
33+
export function postList() {
34+
network.query(S.allUser2).then(res => {
35+
if (res.error) return handleError(res)
36+
debug('query: ', res)
37+
})
38+
}
39+
40+
const CheatsheetCDN =
41+
'https://raw.githubusercontent.com/mydearxym/mastani-cheatsheets/master'
42+
export function cheatsheet() {
43+
// const which = 'elixir' // good
44+
/* const which = 'react' // syntax error */
45+
const which = 'whatever' // not found
46+
47+
const url = `${CheatsheetCDN}/${which}.md`
48+
debug(url)
49+
50+
network.GET(url).then(res => {
51+
/* if (res.error) return handleError(res) */
52+
debug('GET ', res)
53+
})
54+
}
1255

1356
export function init(selectedStore) {
14-
debug(postsViewer)
1557
postsViewer = selectedStore
58+
debug(postsViewer)
1659
}

containers/PostsViewer/schema.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import gql from 'graphql-tag'
2+
3+
const allUser2 = gql`
4+
{
5+
allUsers2 {
6+
entries {
7+
username
8+
id
9+
nickname
10+
}
11+
totalCount
12+
pageSize
13+
}
14+
}
15+
`
16+
17+
const createUser = gql`
18+
mutation(
19+
$username: String
20+
$nickname: String
21+
$bio: String
22+
$company: String
23+
) {
24+
createUser(
25+
username: $username
26+
nickname: $nickname
27+
bio: $bio
28+
company: $company
29+
) {
30+
username
31+
bio
32+
}
33+
}
34+
`
35+
36+
const schema = {
37+
allUser2,
38+
createUser,
39+
}
40+
41+
export default schema

package.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,15 @@
9595
"dependencies": {
9696
"accepts": "^1.3.4",
9797
"antd": "2.13.5",
98+
"apollo-cache-inmemory": "^1.1.4",
99+
"apollo-client": "^2.0.4",
100+
"apollo-link": "^1.0.7",
101+
"apollo-link-error": "^1.0.3",
102+
"apollo-link-http": "^1.3.2",
103+
"apollo-link-retry": "^2.1.1",
98104
"debug": "3.1.0",
99105
"glob": "^7.1.2",
106+
"graphql-tag": "^2.6.1",
100107
"intl": "^1.2.5",
101108
"isomorphic-fetch": "^2.2.1",
102109
"mastani-codehighlight": "0.0.5",
@@ -107,6 +114,7 @@
107114
"next": "4.2.1",
108115
"path-match": "^1.2.4",
109116
"polished": "1.8.0",
117+
"promise-timeout": "^1.1.1",
110118
"prop-types": "^15.5.10",
111119
"ramda": "0.25.0",
112120
"randomcolor": "^0.5.3",

stores/PostsViewerStore/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ const PostsViewerStore = t
1818
return getParent(self)
1919
},
2020

21+
get SR71$() {
22+
return self.root.SR71$
23+
},
24+
2125
get data() {
2226
return self.root.posts.current
2327
},

0 commit comments

Comments
 (0)