1- import React , { useEffect } from 'react'
1+ import React from 'react'
22import { useRouter } from 'next/router'
33import { Flex , Box , Type } from 'blockstack-ui'
44import { connect } from 'react-redux'
@@ -12,130 +12,111 @@ import { Page } from '@components/page'
1212import Head from '@containers/head'
1313import Maker from '@components/maker'
1414
15- const MakerPortal = ( { params, apiServer, user, maker, errorMessage, appList, selectedApp, competionStatus, dispatch } ) => {
16-
17- const hasAppId = params && ! ! params . appId
15+ const mapStateToProps = ( state ) => ( {
16+ user : selectUser ( state ) ,
17+ apiServer : selectApiServer ( state ) ,
18+ maker : selectMaker ( state ) ,
19+ appList : selectAppList ( state ) ,
20+ selectedApp : selectCurrentApp ( state ) ,
21+ completionStatus : selectCompetionStatus ( state )
22+ } )
1823
19- const router = useRouter ( )
24+ const handleChangingApp = ( event , fn ) => ( dispatch ) => {
25+ event . persist ( )
26+ const id = event . target . value
27+ dispatch ( selectAppAction ( id ) )
28+ fn ( id )
29+ }
2030
21- const updateMakerRoute = id => router . push (
22- '/maker/apps' ,
23- `/maker/apps/${ id } ` ,
24- {
25- shallow : true
26- }
27- )
31+ const getAppId = ( params ) => {
32+ if ( ! params ) return undefined
33+ const id = parseInt ( params . appId , 10 )
34+ if ( isNaN ( id ) ) return undefined
35+ return id
36+ }
2837
29- const getAppId = ( ) => {
30- if ( ! params ) return undefined
31- const id = parseInt ( params . appId , 10 )
32- if ( isNaN ( id ) ) return undefined
33- return id
34- }
38+ const LoadingPage = ( { message = 'Loading...' } ) => (
39+ < Page innerPadding = { 0 } wrap >
40+ < Flex >
41+ < Box width = { 1 } >
42+ < Type fontSize = { 5 } my = { 7 } textAlign = "center" >
43+ { message }
44+ </ Type >
45+ </ Box >
46+ </ Flex >
47+ </ Page >
48+ )
3549
36- useEffect ( ( ) => {
37- async function fetchAppsOnInit ( ) {
38- await fetchApps ( { apiServer, user } ) ( dispatch )
39- if ( hasAppId ) {
40- dispatch ( selectAppAction ( getAppId ( ) ) )
41- }
42- if ( ! hasAppId && process . browser ) {
43- setTimeout ( ( ) => {
44- console . log ( appList )
45- const { id } = appList [ 0 ]
46- dispatch ( selectAppAction ( id ) )
47- } )
48- }
49- }
50- fetchAppsOnInit ( )
51- } , [ ] )
50+ const MakerPortal = connect ( ) ( ( { maker, selectedApp, appList, appId, apiServer, completionStatus, user, dispatch } ) => {
51+ const router = useRouter ( )
5252
53- if ( maker . loading || ! selectedApp ) {
54- return (
55- < Page innerPadding = { 0 } wrap >
56- < Flex >
57- < Box width = { 1 } >
58- < Type fontSize = { 5 } my = { 7 } textAlign = "center" > { maker . loading ? 'Loading...' : errorMessage } </ Type >
59- </ Box >
60- </ Flex >
61- </ Page >
62- )
63- }
53+ const updateMakerRoute = ( id ) =>
54+ router . push ( '/maker/apps' , `/maker/apps/${ id } ` , {
55+ shallow : true
56+ } )
6457
65- function handleChangingApp ( event ) {
66- event . persist ( )
67- const id = event . target . value
68- dispatch ( selectAppAction ( id ) )
69- updateMakerRoute ( id )
70- }
58+ if ( maker . loading || ! selectedApp ) return < LoadingPage />
7159
7260 return (
7361 < Page innerPadding = { 0 } wrap >
7462 < Head title = { selectedApp . name } />
7563 < MakerContainer >
7664 < Box >
77- {
78- appList . length &&
79- < select onChange = { handleChangingApp } value = { getAppId ( ) } >
65+ { appList . length && (
66+ < select onChange = { ( e ) => handleChangingApp ( e , updateMakerRoute ) ( dispatch ) } value = { appId } >
8067 { appList . map ( ( { name, id } ) => (
81- < option key = { id } value = { id } > { name } </ option >
68+ < option key = { id } value = { id } >
69+ { name }
70+ </ option >
8271 ) ) }
8372 </ select >
84- }
73+ ) }
8574 </ Box >
8675 < Type fontSize = { 3 } fontWeight = { 500 } mx = { [ 4 , 6 ] } py = { 6 } px = { [ 20 , 0 ] } >
8776 { selectedApp . name }
8877 </ Type >
89- < Flex flexDirection = { [ 'column' , 'column' , 'row-reverse' ] } maxWidth = { [ null , null , 1140 ] } alignItems = "flex-start" >
78+ < Flex
79+ flexDirection = { [ 'column' , 'column' , 'row-reverse' ] }
80+ maxWidth = { [ 'unset' , 'unset' , 1140 ] }
81+ alignItems = "flex-start"
82+ >
9083 < MakerStickyStatusBox >
91- < Maker . Status
92- app = { selectedApp }
93- apiServer = { apiServer }
94- status = { competionStatus }
95- />
84+ < Maker . Status app = { selectedApp } apiServer = { apiServer } status = { completionStatus } />
9685 </ MakerStickyStatusBox >
9786 < Box >
9887 < MakerContentBox >
99- < Maker . PaymentDetails
100- app = { selectedApp }
101- user = { user }
102- apiServer = { apiServer }
103- dispatch = { dispatch }
104- />
88+ < Maker . PaymentDetails app = { selectedApp } user = { user } apiServer = { apiServer } dispatch = { dispatch } />
10589 </ MakerContentBox >
10690 < MakerContentBox >
107- < Maker . KYC
108- app = { selectedApp }
109- user = { user }
110- apiServer = { apiServer }
111- />
91+ < Maker . KYC app = { selectedApp } user = { user } apiServer = { apiServer } />
11292 </ MakerContentBox >
11393 < MakerContentBox >
114- < Maker . ParticipationAgreement
115- app = { selectedApp }
116- user = { user }
117- apiServer = { apiServer }
118- />
94+ < Maker . ParticipationAgreement app = { selectedApp } user = { user } apiServer = { apiServer } />
11995 </ MakerContentBox >
12096 </ Box >
12197 </ Flex >
12298 </ MakerContainer >
12399 </ Page >
124100 )
125- }
101+ } )
126102
127- MakerPortal . getInitialProps = ( x ) => {
128- console . log ( x . userApps )
129- return { params : x . query . params }
130- }
103+ MakerPortal . getInitialProps = async ( { req, reduxStore } ) => {
104+ const { params, universalCookies } = req
105+ const userCookie = universalCookies . cookies . jwt
106+ const appId = getAppId ( params )
107+ const apiServer = selectApiServer ( reduxStore . getState ( ) )
108+ await fetchApps ( { apiServer, user : { jwt : userCookie } } ) ( reduxStore . dispatch )
109+ let selectedApp = { }
110+ if ( appId ) {
111+ reduxStore . dispatch ( selectAppAction ( appId ) )
112+ selectedApp = selectCurrentApp ( reduxStore . getState ( ) )
113+ } else {
114+ const firstApp = selectAppList ( reduxStore . getState ( ) ) [ 0 ]
115+ selectedApp = firstApp
116+ }
117+ const props = mapStateToProps ( reduxStore . getState ( ) )
131118
132- const mapStateToProps = state => ( {
133- user : selectUser ( state ) ,
134- apiServer : selectApiServer ( state ) ,
135- maker : selectMaker ( state ) ,
136- appList : selectAppList ( state ) ,
137- selectedApp : selectCurrentApp ( state ) ,
138- competionStatus : selectCompetionStatus ( state )
139- } )
119+ return { appId, ...props , selectedApp, dispatch : reduxStore . dispatch }
120+ }
140121
141- export default connect ( mapStateToProps ) ( MakerPortal )
122+ export default MakerPortal
0 commit comments