Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Nov 8, 2022. It is now read-only.

Commitaf9726b

Browse files
committed
refactor the network layer
1 parentaf7b0e6 commitaf9726b

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
importPrismfrom'mastani-codehighlight'
44
import{makeDebugger}from'../../utils/functions'
5+
importnetworkfrom'../../utils/network'
56

67
/* eslint-disable no-unused-vars */
78
constdebug=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+
constCheatsheetCDN=
42+
'https://raw.githubusercontent.com/mydearxym/mastani-cheatsheets/master'
43+
4044
exportfunctiongetData(which){
4145
setTimeout(()=>{
42-
cheatsheetViewer.SR71$.getCheatsheet(which)
46+
consturl=`${CheatsheetCDN}/${which}.md`
47+
network.GET(url).then(res=>{
48+
/* debug('GET ', res) */
49+
if(res.error)returnhandleError(res)
50+
51+
letsource=''
52+
try{
53+
source=transMarkDownforRender(res)
54+
}catch(err){
55+
returnhandleError({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+
functionhandleError(res){
67+
switch(res.error){
68+
case404:
69+
returnhandle404Error()
70+
case'parse_error':
71+
returnhandleParseError()
72+
default:
73+
debug(res)
74+
}
75+
}
76+
4977
functionhandleParseError(errMsg){
5078
cheatsheetViewer.markState({
5179
current:'',
@@ -55,8 +83,7 @@ function handleParseError(errMsg) {
5583
Prism.highlightAll()
5684
}
5785

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

76103
exportfunctioninit(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-
letsource=''
86-
87-
try{
88-
source=transMarkDownforRender(res)
89-
}catch(err){
90-
handleParseError(err)
91-
returnfalse
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*aslogicfrom'./logic'
1415
import{Wrapper}from'./styles'
@@ -17,13 +18,29 @@ const debug = makeDebugger('C:PostsViewer')
1718

1819
classPostsViewerContainerextendsReact.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+
<Buttontype="primary"onClick={logic.createPost}>
31+
createPost (mutate)
32+
</Button>
33+
&nbsp;&nbsp;
34+
<Buttontype="primary"onClick={logic.postList}>
35+
postList (query)
36+
</Button>
37+
&nbsp;&nbsp;
38+
<Buttontype="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+
importnetworkfrom'../../utils/network'
5+
importSfrom'./schema'
46

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

911
letpostsViewer=null
1012

11-
exportfunctionsomeMethod(){}
13+
functionhandleError(res){
14+
debug(`handleError [${res.error}]:${res.details}`)
15+
}
16+
17+
// mutation 之后修改 cache
18+
// https://www.apollographql.com/docs/react/basics/mutations.html
19+
exportfunctioncreatePost(){
20+
constvariables={
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)returnhandleError(res)
29+
debug('mutate: ',res)
30+
})
31+
}
32+
33+
exportfunctionpostList(){
34+
network.query(S.allUser2).then(res=>{
35+
if(res.error)returnhandleError(res)
36+
debug('query: ',res)
37+
})
38+
}
39+
40+
constCheatsheetCDN=
41+
'https://raw.githubusercontent.com/mydearxym/mastani-cheatsheets/master'
42+
exportfunctioncheatsheet(){
43+
// const which = 'elixir' // good
44+
/* const which = 'react' // syntax error */
45+
constwhich='whatever'// not found
46+
47+
consturl=`${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
exportfunctioninit(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+
importgqlfrom'graphql-tag'
2+
3+
constallUser2=gql`
4+
{
5+
allUsers2 {
6+
entries {
7+
username
8+
id
9+
nickname
10+
}
11+
totalCount
12+
pageSize
13+
}
14+
}
15+
`
16+
17+
constcreateUser=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+
constschema={
37+
allUser2,
38+
createUser,
39+
}
40+
41+
exportdefaultschema

‎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
returngetParent(self)
1919
},
2020

21+
getSR71$(){
22+
returnself.root.SR71$
23+
},
24+
2125
getdata(){
2226
returnself.root.posts.current
2327
},

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp