|
1 | 1 | import React, { useEffect } from 'react' |
| 2 | +import { useRouter } from 'next/router' |
2 | 3 | import { Flex, Box, Type } from 'blockstack-ui' |
3 | 4 | import { connect } from 'react-redux' |
| 5 | +import isNaN from 'lodash/isNaN' |
4 | 6 |
|
5 | 7 | import { selectMaker, selectAppList, selectCurrentApp, selectCompetionStatus } from '@stores/maker/selectors' |
6 | 8 | import { fetchApps, selectAppAction } from '@stores/maker/actions' |
7 | 9 | import { selectApiServer, selectUser } from '@stores/apps/selectors' |
| 10 | +import { MakerContainer, MakerContentBox, MakerStickyStatusBox } from '@components/maker/styled' |
8 | 11 | import { Page } from '@components/page' |
9 | 12 | import Head from '@containers/head' |
10 | 13 | import Maker from '@components/maker' |
11 | | -import { MakerContainer, MakerContentBox, MakerStickyStatusBox } from '@components/maker/styled' |
12 | 14 |
|
13 | | -const MakerPortal = ({ apiServer, user, maker, loading, errorMessage, appList, selectedApp, dispatch }) => { |
| 15 | +const MakerPortal = ({ params, apiServer, user, maker, errorMessage, appList, selectedApp, competionStatus, dispatch }) => { |
14 | 16 |
|
15 | | -const MakerPortal = ({ query, params, apiServer, user, maker, errorMessage, appList, selectedApp, competionStatus, dispatch }) => { |
16 | | - console.log('Rendering component') |
| 17 | + const hasAppId = params && !!params.appId |
17 | 18 |
|
18 | | - const app = selectedApp |
| 19 | + const router = useRouter() |
19 | 20 |
|
20 | | - function handleChangingApp (event) { |
21 | | - event.persist() |
22 | | - const id = event.target.value |
23 | | - dispatch(selectAppAction(id)) |
| 21 | + console.log(router) |
| 22 | + const updateMakerRoute = id => { |
| 23 | + router.push( |
| 24 | + { |
| 25 | + pathname: '/maker', |
| 26 | + query: { |
| 27 | + params: { appId: id } |
| 28 | + }, |
| 29 | + as: `/maker/${id}` |
| 30 | + }, |
| 31 | + { |
| 32 | + shallow: true |
| 33 | + } |
| 34 | + ) |
| 35 | + } |
| 36 | + |
| 37 | + const getAppId = () => { |
| 38 | + |
| 39 | + const id = parseInt(params.appId, 10) |
| 40 | + if (isNaN(id)) return null |
| 41 | + return id |
24 | 42 | } |
25 | 43 |
|
26 | | - if (loading || !app) { |
| 44 | + useEffect(() => { |
| 45 | + async function fetchAppsOnInit () { |
| 46 | + await fetchApps({ apiServer, user })(dispatch) |
| 47 | + |
| 48 | + if (hasAppId) { |
| 49 | + dispatch(selectAppAction(getAppId())) |
| 50 | + } |
| 51 | + } |
| 52 | + fetchAppsOnInit() |
| 53 | + }, []) |
| 54 | + |
| 55 | + if (maker.loading || !selectedApp) { |
27 | 56 | return ( |
28 | 57 | <Page innerPadding={0} wrap> |
29 | 58 | <Flex> |
30 | 59 | <Box width={1}> |
31 | | - <Type fontSize={5} my={7} textAlign="center">{loading ? 'Loading...' : errorMessage}</Type> |
| 60 | + <Type fontSize={5} my={7} textAlign="center">{maker.loading ? 'Loading...' : errorMessage}</Type> |
32 | 61 | </Box> |
33 | 62 | </Flex> |
34 | 63 | </Page> |
35 | 64 | ) |
36 | 65 | } |
37 | 66 |
|
| 67 | + // if (!hasAppId) { |
| 68 | + // if (appList.length === 0) throw new Error('wlkdsfsldfl') |
| 69 | + // const { id } = appList[0] |
| 70 | + // console.log('has no app id take first', id) |
| 71 | + // updateMakerRoute(id) |
| 72 | + // dispatch(selectAppAction(id)) |
| 73 | + // } |
| 74 | + |
| 75 | + function handleChangingApp (event) { |
| 76 | + event.persist() |
| 77 | + const id = event.target.value |
| 78 | + dispatch(selectAppAction(id)) |
| 79 | + updateMakerRoute(id) |
| 80 | + } |
| 81 | + |
| 82 | + |
38 | 83 | return ( |
39 | 84 | <Page innerPadding={0} wrap> |
40 | | - <Head title={app.name} /> |
| 85 | + <Head title={selectedApp.name} /> |
41 | 86 | <MakerContainer> |
42 | 87 | <Box> |
43 | | - <select onChange={handleChangingApp}> |
44 | | - {appList.map(({ name, id }) => ( |
45 | | - <option key={id} value={id}>{name}</option> |
46 | | - ))} |
47 | | - </select> |
| 88 | + { |
| 89 | + appList.length && |
| 90 | + <select onChange={handleChangingApp} value={getAppId()}> |
| 91 | + {appList.map(({ name, id }) => ( |
| 92 | + <option key={id} value={id}>{name}</option> |
| 93 | + ))} |
| 94 | + </select> |
| 95 | + } |
48 | 96 | </Box> |
49 | 97 | <Type fontSize={3} fontWeight={500} mx={[4, 6]} py={6} px={[20, 0]}> |
50 | | - {app.name} |
| 98 | + {selectedApp.name} |
51 | 99 | </Type> |
52 | 100 | <Flex flexDirection={['column', 'column', 'row-reverse']} maxWidth={[null, null, 1140]} alignItems="flex-start"> |
53 | 101 | <MakerStickyStatusBox> |
54 | 102 | <Maker.Status |
55 | | - app={app} |
| 103 | + app={selectedApp} |
56 | 104 | apiServer={apiServer} |
57 | 105 | status={competionStatus} |
58 | 106 | /> |
59 | 107 | </MakerStickyStatusBox> |
60 | 108 | <Box> |
61 | 109 | <MakerContentBox> |
62 | 110 | <Maker.PaymentDetails |
63 | | - app={app} |
| 111 | + app={selectedApp} |
64 | 112 | user={user} |
65 | 113 | apiServer={apiServer} |
66 | 114 | dispatch={dispatch} |
67 | | - // onPaymentDetailsComplete={() => dispatch(setPaymentDetailsComplete())} |
68 | 115 | /> |
69 | 116 | </MakerContentBox> |
70 | 117 | <MakerContentBox> |
71 | 118 | <Maker.KYC |
72 | | - app={app} |
| 119 | + app={selectedApp} |
73 | 120 | user={user} |
74 | 121 | apiServer={apiServer} |
75 | | - // onKycComplete={() => dispatch(setKycComplete())} |
76 | 122 | /> |
77 | 123 | </MakerContentBox> |
78 | 124 | <MakerContentBox> |
79 | 125 | <Maker.ParticipationAgreement |
80 | | - app={app} |
| 126 | + app={selectedApp} |
81 | 127 | user={user} |
82 | 128 | apiServer={apiServer} |
83 | | - // onLegalComplete={() => dispatch(setLegalComplete())} |
84 | 129 | /> |
85 | 130 | </MakerContentBox> |
86 | 131 | </Box> |
|
0 commit comments