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

Commit d669cd7

Browse files
committed
feat(RepoThread): add RepoViewer
1 parent 097a622 commit d669cd7

File tree

18 files changed

+293
-6
lines changed

18 files changed

+293
-6
lines changed

components/GithubRepoPage/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ const GithubRepoPage = ({
5151
</DescriptionWrapper>
5252
<StatesContainers repo={repo} />
5353
<ReadmeWrapper>
54-
<MarkDownRender body={repo.readme} />
54+
<MarkDownRender body={repo.readme || ''} />
5555
</ReadmeWrapper>
5656
</BodyWrapper>
5757
<Footer>

components/RepoItem/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const debug = makeDebugger('c:RepoItem:index')
1818

1919
const RepoItem = ({ entry, active, onTitleSelect }) => (
2020
<Wrapper active={active.id && entry.id !== active.id}>
21-
<Header entry={entry} onTitleSelect={onTitleSelect} />
21+
<Header entry={entry} onTitleSelect={onTitleSelect.bind(this, entry)} />
2222

2323
<BodyDigest>{cutFrom(entry.desc, 180)}</BodyDigest>
2424

components/RepoItem/styles/footer.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,5 @@ export const PublishInfo = styled.div`
5252
font-size: 0.85rem;
5353
margin-right: 10px;
5454
align-items: center;
55+
align-self: flex-end;
5556
`

containers/Preview/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import dynamic from 'next/dynamic'
1212
import ArticleViwer from '../ArticleViwer'
1313
import AccountViewer from '../AccountViewer'
1414
import VideoViewer from '../VideoViewer'
15+
import RepoViewer from '../RepoViewer'
1516
// eiditors
1617
import AccountEditor from '../AccountEditor'
1718
import CommunityEditors from '../CommunityEditors'
@@ -95,7 +96,8 @@ const Viewer = ({ type, root, attachment }) => {
9596
}
9697
// repo
9798
case TYPE.PREVIEW_REPO_VIEW: {
98-
return <h3>PREVIEW_REPO_VIEW</h3>
99+
console.log('PREVIEW_REPO_VIEW attachment: ', attachment)
100+
return <RepoViewer attachment={attachment} />
99101
}
100102
case TYPE.PREVIEW_REPO_CREATE: {
101103
return <RepoEditor />

containers/RepoViewer/index.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
*
3+
* RepoViewer
4+
*
5+
*/
6+
7+
import React from 'react'
8+
import { inject, observer } from 'mobx-react'
9+
10+
// import { } from './styles'
11+
import { GithubRepoPage } from '../../components'
12+
13+
import { makeDebugger, storePlug } from '../../utils'
14+
15+
import * as logic from './logic'
16+
/* eslint-disable no-unused-vars */
17+
const debug = makeDebugger('C:RepoViewer')
18+
/* eslint-enable no-unused-vars */
19+
20+
class RepoViewerContainer extends React.Component {
21+
componentWillMount() {
22+
const { repoViewer, attachment } = this.props
23+
logic.init(repoViewer, attachment)
24+
}
25+
26+
render() {
27+
const { repoViewer } = this.props
28+
const { viewingData } = repoViewer
29+
30+
debug('repoViewer ---------- ', viewingData)
31+
32+
// debug('viewingData: ', viewingData)
33+
34+
return (
35+
<div>
36+
<GithubRepoPage repo={viewingData} />
37+
</div>
38+
)
39+
}
40+
}
41+
42+
export default inject(storePlug('repoViewer'))(observer(RepoViewerContainer))

containers/RepoViewer/logic.js

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// import R from 'ramda'
2+
3+
import {
4+
makeDebugger,
5+
$solver,
6+
asyncRes,
7+
asyncErr,
8+
ERR,
9+
TYPE,
10+
} from '../../utils'
11+
import SR71 from '../../utils/network/sr71'
12+
13+
import S from './schema'
14+
15+
const sr71$ = new SR71()
16+
let sub$ = null
17+
18+
/* eslint-disable no-unused-vars */
19+
const debug = makeDebugger('L:RepoViewer')
20+
/* eslint-enable no-unused-vars */
21+
22+
let store = null
23+
24+
export function loadRepo(id) {
25+
sr71$.query(S.repo, { id })
26+
}
27+
28+
const openAttachment = att => {
29+
if (!att) return false
30+
const { type } = att
31+
if (type === TYPE.PREVIEW_REPO_VIEW) {
32+
// TODO: merge default empty repo
33+
store.setViewing({ rep: att })
34+
loadRepo(att.id)
35+
}
36+
}
37+
38+
// ###############################
39+
// Data & Error handlers
40+
// ###############################
41+
42+
const DataSolver = [
43+
{
44+
match: asyncRes('repo'),
45+
action: ({ repo }) => store.setViewing({ repo }),
46+
},
47+
]
48+
const ErrSolver = [
49+
{
50+
match: asyncErr(ERR.CRAPHQL),
51+
action: ({ details }) => {
52+
debug('ERR.CRAPHQL -->', details)
53+
},
54+
},
55+
{
56+
match: asyncErr(ERR.TIMEOUT),
57+
action: ({ details }) => {
58+
debug('ERR.TIMEOUT -->', details)
59+
},
60+
},
61+
{
62+
match: asyncErr(ERR.NETWORK),
63+
action: ({ details }) => {
64+
debug('ERR.NETWORK -->', details)
65+
},
66+
},
67+
]
68+
69+
export function init(_store, attachment) {
70+
if (store) {
71+
return openAttachment(attachment)
72+
}
73+
store = _store
74+
75+
debug(store)
76+
if (sub$) sub$.unsubscribe()
77+
sub$ = sr71$.data().subscribe($solver(DataSolver, ErrSolver))
78+
openAttachment(attachment)
79+
}

containers/RepoViewer/schema.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import gql from 'graphql-tag'
2+
3+
const simpleMutation = gql`
4+
mutation($id: ID!) {
5+
post(id: $id) {
6+
id
7+
}
8+
}
9+
`
10+
const repo = gql`
11+
query($id: ID!) {
12+
repo(id: $id) {
13+
id
14+
title
15+
desc
16+
ownerName
17+
ownerUrl
18+
repoUrl
19+
homepageUrl
20+
readme
21+
starCount
22+
issuesCount
23+
prsCount
24+
forkCount
25+
watchCount
26+
primaryLanguage {
27+
color
28+
name
29+
}
30+
license
31+
releaseTag
32+
contributors {
33+
avatar
34+
htmlUrl
35+
nickname
36+
}
37+
}
38+
}
39+
`
40+
41+
const schema = {
42+
simpleMutation,
43+
repo,
44+
}
45+
46+
export default schema

containers/RepoViewer/store.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* RepoViewer store
3+
*
4+
*/
5+
6+
import { types as t, getParent } from 'mobx-state-tree'
7+
// import R from 'ramda'
8+
9+
import { markStates, makeDebugger } from '../../utils'
10+
/* eslint-disable no-unused-vars */
11+
const debug = makeDebugger('S:RepoViewer')
12+
/* eslint-enable no-unused-vars */
13+
14+
const RepoViewer = t
15+
.model('RepoViewer', {})
16+
.views(self => ({
17+
get root() {
18+
return getParent(self)
19+
},
20+
get viewingData() {
21+
return self.root.viewingData
22+
},
23+
}))
24+
.actions(self => ({
25+
setViewing(sobj) {
26+
self.root.setViewing(sobj)
27+
},
28+
markState(sobj) {
29+
markStates(sobj, self)
30+
},
31+
}))
32+
33+
export default RepoViewer

containers/RepoViewer/styles/index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import styled from 'styled-components'
2+
3+
// import Img from '../../../components/Img'
4+
// import { theme } from '../../../utils'
5+
6+
export const Wrapper = styled.div``
7+
export const Title = styled.div``
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// import React from 'react'
2+
// import { shallow } from 'enzyme'
3+
4+
// import RepoViewer from '../index'
5+
6+
describe('TODO <RepoViewer />', () => {
7+
it('Expect to have unit tests specified', () => {
8+
expect(true).toEqual(true)
9+
})
10+
})
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/*
2+
* RepoViewer store test
3+
*
4+
*/
5+
6+
// import R from 'ramda'
7+
8+
// import RepoViewer from '../index'
9+
10+
it('TODO: store test RepoViewer', () => {
11+
expect(1 + 1).toBe(2)
12+
})

containers/ReposThread/index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,12 @@ const View = ({ community, thread, entries, curView, active }) => {
4646
return (
4747
<React.Fragment>
4848
{entries.map(entry => (
49-
<RepoItem key={uid.gen()} entry={entry} active={active} />
49+
<RepoItem
50+
key={uid.gen()}
51+
entry={entry}
52+
active={active}
53+
onTitleSelect={logic.onTitleSelect}
54+
/>
5055
))}
5156
</React.Fragment>
5257
)

containers/ReposThread/logic.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,14 @@ export function loadRepos(page = 1) {
6363
store.markRoute({ page })
6464
}
6565

66-
export function onTitleSelect() {
67-
debug('onTitleSelect')
66+
export function onTitleSelect(repo) {
67+
store.setViewing({ repo })
68+
debug('onTitleSelect ---', repo)
69+
70+
dispatchEvent(EVENT.PREVIEW_OPEN, {
71+
type: TYPE.PREVIEW_REPO_VIEW,
72+
data: repo,
73+
})
6874
}
6975

7076
export function createContent() {

containers/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@ export { default as Doraemon } from './Doraemon'
1717
export { default as CommunityEditors } from './CommunityEditors'
1818
export { default as UpgradePackges } from './UpgradePackges'
1919

20+
// viewers
2021
export { default as ArticleViwer } from './ArticleViwer'
2122
export { default as AccountEditor } from './AccountEditor'
2223
export { default as AccountViewer } from './AccountViewer'
2324
export { default as VideoViewer } from './VideoViewer'
25+
export { default as RepoViewer } from './RepoViewer'
2426

2527
export { default as FavoritesCats } from './FavoritesCats'
2628
/* banners */

stores/EditorViewerStore/index.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* EditorViewerStore store
3+
*
4+
*/
5+
6+
import { types as t, getParent } from 'mobx-state-tree'
7+
// import R from 'ramda'
8+
9+
import { markStates, makeDebugger } from '../../utils'
10+
/* eslint-disable no-unused-vars */
11+
const debug = makeDebugger('S:EditorViewerStore')
12+
/* eslint-enable no-unused-vars */
13+
14+
const EditorViewerStore = t
15+
.model('EditorViewerStore', {})
16+
.views(self => ({
17+
get root() {
18+
return getParent(self)
19+
},
20+
}))
21+
.actions(self => ({
22+
markState(sobj) {
23+
markStates(sobj, self)
24+
},
25+
}))
26+
27+
export default EditorViewerStore
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/*
2+
* EditorViewerStore store test
3+
*
4+
*/
5+
6+
// import R from 'ramda'
7+
8+
// import EditorViewerStore from '../index'
9+
10+
it('TODO: test EditorViewerStore', () => {
11+
expect(1 + 1).toBe(2)
12+
})

stores/RootStore/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ import {
5151
ArticleViwerStore,
5252
AccountViewerStore,
5353
VideoViewerStore,
54+
RepoViewerStore,
5455
CommentsStore,
5556
CheatSheetPaperStore,
5657
CommunityEditorsStore,
@@ -141,6 +142,7 @@ const rootStore = t
141142
articleViwer: t.optional(ArticleViwerStore, {}),
142143
accountViewer: t.optional(AccountViewerStore, {}),
143144
videoViewer: t.optional(VideoViewerStore, {}),
145+
repoViewer: t.optional(RepoViewerStore, {}),
144146
communityEditors: t.optional(CommunityEditorsStore, {}),
145147
// user page
146148
userSettings: t.optional(UserSettingsStore, {}),

stores/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ export { default as VideoViewerStore } from '../containers/VideoViewer/store'
8080
export {
8181
default as CommunityEditorsStore,
8282
} from '../containers/CommunityEditors/store'
83+
export { default as RepoViewerStore } from '../containers/RepoViewer/store'
8384

8485
// user page
8586
export { default as UserSettingsStore } from '../containers/UserSettings/store'

0 commit comments

Comments
 (0)