1
1
// import R from 'ramda'
2
2
3
- import { makeDebugger , $solver , asyncErr , ERR } from '../../utils'
3
+ import {
4
+ makeDebugger ,
5
+ $solver ,
6
+ asyncRes ,
7
+ asyncErr ,
8
+ ERR ,
9
+ githubApi ,
10
+ } from '../../utils'
4
11
import SR71 from '../../utils/network/sr71'
5
12
6
- // import S from './schema'
13
+ import S from './schema'
7
14
8
15
const sr71$ = new SR71 ( )
9
16
let sub$ = null
@@ -14,13 +21,59 @@ const debug = makeDebugger('L:WikiThread')
14
21
15
22
let store = null
16
23
17
- export function someMethod ( ) { }
24
+ const getWiki = ( ) => {
25
+ const community = store . curCommunity . raw
26
+
27
+ sr71$ . query ( S . wiki , { community} )
28
+ }
29
+
30
+ const syncWiki = readme => {
31
+ const args = {
32
+ readme,
33
+ lastSync :new Date ( ) . toISOString ( ) ,
34
+ communityId :store . curCommunity . id ,
35
+ }
36
+
37
+ sr71$ . mutate ( S . syncWiki , args )
38
+ }
39
+
40
+ export function syncWikiFromGithub ( ) {
41
+ githubApi
42
+ . searchWiki ( 'javascript' )
43
+ . then ( res => {
44
+ syncWiki ( res )
45
+ } )
46
+ . catch ( e => {
47
+ store . handleError ( githubApi . parseError ( e ) )
48
+ } )
49
+ }
50
+
51
+ export function addContributor ( user ) {
52
+ const args = {
53
+ id :store . wikiData . id ,
54
+ contributor :user ,
55
+ }
56
+ sr71$ . mutate ( S . addWikiContributor , args )
57
+ }
18
58
19
59
// ###############################
20
60
// Data & Error handlers
21
61
// ###############################
22
62
23
- const DataSolver = [ ]
63
+ const DataSolver = [
64
+ {
65
+ match :asyncRes ( 'wiki' ) ,
66
+ action :( { wiki} ) => store . markState ( { wiki} ) ,
67
+ } ,
68
+ {
69
+ match :asyncRes ( 'syncWiki' ) ,
70
+ action :( ) => getWiki ( ) ,
71
+ } ,
72
+ {
73
+ match :asyncRes ( 'addWikiContributor' ) ,
74
+ action :( ) => getWiki ( ) ,
75
+ } ,
76
+ ]
24
77
const ErrSolver = [
25
78
{
26
79
match :asyncErr ( ERR . CRAPHQL ) ,
@@ -43,10 +96,15 @@ const ErrSolver = [
43
96
]
44
97
45
98
export function init ( _store ) {
46
- if ( store ) return false
99
+ if ( store ) {
100
+ return getWiki ( )
101
+ }
102
+
47
103
store = _store
48
104
49
105
debug ( store )
50
106
if ( sub$ ) sub$ . unsubscribe ( )
51
107
sub$ = sr71$ . data ( ) . subscribe ( $solver ( DataSolver , ErrSolver ) )
108
+
109
+ getWiki ( )
52
110
}