|
1 | 1 | import React, { useState, useEffect } from 'react';
|
2 |
| -import { SizeMe } from 'react-sizeme'; |
| 2 | +import styled from 'styled-components'; |
3 | 3 | import some from 'lodash/some';
|
4 | 4 | import { withRouter } from 'react-router-dom';
|
5 | 5 |
|
6 | 6 | import { Row } from 'reactstrap';
|
7 |
| -import { auth } from '../../firebase'; |
8 |
| -import { Project } from '../../firebase/models'; |
| 7 | +import { auth, db } from '../../firebase'; |
9 | 8 |
|
10 | 9 | import Dashboard from './components/dashboard';
|
11 |
| -// import EditProjectButton from './components/edit-project-button'; |
12 | 10 | import BannerContent from './components/banner-content';
|
13 | 11 | import ProjectInfoContent from './components/project-info-content';
|
14 | 12 | import SocialContentSection from './components/social-content-section';
|
15 |
| -import Button from '../shared/button'; |
| 13 | +import EditProjectBanner from './components/edit-project-banner'; |
16 | 14 |
|
17 |
| -const LoadingView = () => ( |
18 |
| - <div |
19 |
| - style={{ |
20 |
| - display: 'flex', |
21 |
| - height: '70vh', |
22 |
| - flex: 1, |
23 |
| - justifyContent: 'center', |
24 |
| - alignItems: 'center', |
25 |
| - }} |
26 |
| - > |
27 |
| - Loading ... |
28 |
| - </div> |
29 |
| -); |
| 15 | +const LoadingView = styled.div` |
| 16 | + display: flex; |
| 17 | + height: 70vh; |
| 18 | + flex: 1; |
| 19 | + justify-content: center; |
| 20 | + align-items: center; |
| 21 | +`; |
30 | 22 |
|
31 |
| -const EditProjectBanner = ({ onEdit }) => ( |
32 |
| - <div |
33 |
| - style={{ |
34 |
| - backgroundColor: '#ff6e6e', |
35 |
| - color: 'white', |
36 |
| - padding: 40, |
37 |
| - fontWeight: 'bold', |
38 |
| - display: 'flex', |
39 |
| - justifyContent: 'space-around', |
40 |
| - }} |
41 |
| - > |
42 |
| - <h5 style={{ marginTop: 10 }}>You are viewing the live version of your project.</h5> |
43 |
| - <Button variant="outlined" onClick={onEdit}> |
44 |
| - Edit Project |
45 |
| - </Button> |
46 |
| - </div> |
47 |
| -); |
48 |
| - |
49 |
| -const ProjectPage = (props) => { |
| 23 | +const ProjectPage = ({ match, location }) => { |
50 | 24 | const [ loadingState, setLoadingState ] = useState(false);
|
51 | 25 | const [ showDashboardState, setShowDashboardState ] = useState(false);
|
52 | 26 | const [ projectDataState, setProjectDataState ] = useState({});
|
53 | 27 | const [ editableState, setEditableState ] = useState(false);
|
54 |
| - const [ pidState, setPidState ] = useState(undefined); |
| 28 | + const [ pidState, setPidState ] = useState(null); |
55 | 29 |
|
56 |
| - const handleMount = async () => { |
57 |
| - setLoadingState(true); |
| 30 | + useEffect(() => { |
| 31 | + const init = async () => { |
| 32 | + setLoadingState(true); |
58 | 33 |
|
59 |
| - const { shortname } = props.match.params; |
60 |
| - const projects = await db.getAllByFilter('projects')( |
61 |
| - db.where('shortname')('==')(shortname.trim()), |
62 |
| - ); |
| 34 | + const { shortname } = match.params; |
| 35 | + const projects = await db.getAllByFilter('projects')( |
| 36 | + db.where('shortname')('==')(shortname.trim()), |
| 37 | + ); |
63 | 38 |
|
64 |
| - if (projects !== undefined && projects.length === 1) { |
65 |
| - const project = projects[0]; |
66 |
| - setProjectDataState(project); |
67 |
| - setPidState(project.id); |
68 |
| - // a project is editable if the user either belongs to the team or the admin |
69 |
| - if (auth.isLoggedIn()) { |
70 |
| - const { uid: currentUserId } = auth.getUserInfo(); |
71 |
| - if (currentUserId) { |
72 |
| - const editable = some(project.team, id => id === currentUserId) |
73 |
| - || some(project.admin, id => id === currentUserId) |
74 |
| - || project.owner === currentUserId; |
75 |
| - // set whether or not editable |
76 |
| - setEditableState(editable); |
| 39 | + if (projects !== undefined && projects.length === 1) { |
| 40 | + const project = projects[0]; |
| 41 | + setProjectDataState(project); |
| 42 | + setPidState(project.id); |
| 43 | + // a project is editable if the user either belongs to the team or the admin |
| 44 | + if (auth.isLoggedIn()) { |
| 45 | + const { uid: currentUserId } = auth.getUserInfo(); |
| 46 | + if (currentUserId) { |
| 47 | + const editable = some(project.team, id => id === currentUserId) |
| 48 | + || some(project.admin, id => id === currentUserId) |
| 49 | + || project.owner === currentUserId; |
| 50 | + // set whether or not editable |
| 51 | + setEditableState(editable); |
| 52 | + } |
77 | 53 | }
|
78 | 54 | }
|
79 |
| - } |
80 |
| - setLoadingState(false); |
81 |
| - }; |
| 55 | + setLoadingState(false); |
| 56 | + }; |
82 | 57 |
|
83 |
| - useEffect(() => { |
84 |
| - handleMount(); |
| 58 | + init(); |
85 | 59 | }, []);
|
86 | 60 |
|
87 |
| - const handleCloseDashboard = async () => { |
88 |
| - window.location.reload(); |
89 |
| - }; |
90 |
| - |
91 |
| - const handleShowDashboard = async () => { |
92 |
| - setShowDashboardState(true); |
93 |
| - }; |
94 |
| - |
95 |
| - /** |
96 |
| - * programmatic display of content |
97 |
| - */ |
98 |
| - const render = () => { |
99 |
| - if (loadingState) { |
100 |
| - return <LoadingView />; |
101 |
| - } |
102 |
| - if (pidState && showDashboardState) { |
103 |
| - return <Dashboard pid={pidState} handleCloseDashboard={handleCloseDashboard} />; |
104 |
| - } |
105 |
| - return ( |
106 |
| - <> |
107 |
| - {editableState ? <EditProjectBanner onEdit={handleShowDashboard} /> : <div />} |
108 |
| - <SizeMe> |
109 |
| - {({ size }) => ( |
110 |
| - <Row> |
111 |
| - <BannerContent |
112 |
| - width={size.width} |
113 |
| - name={projectDataState.name} |
114 |
| - images={projectDataState.images} |
115 |
| - /> |
116 |
| - <ProjectInfoContent project={projectDataState} /> |
117 |
| - </Row> |
118 |
| - )} |
119 |
| - </SizeMe> |
120 |
| - </> |
121 |
| - ); |
122 |
| - }; |
| 61 | + const toggleDashboard = toggle => () => setShowDashboardState(toggle); |
123 | 62 |
|
124 | 63 | if (loadingState) {
|
125 |
| - return <LoadingView />; |
| 64 | + return <LoadingView>Loading ...</LoadingView>; |
126 | 65 | }
|
127 | 66 | if (pidState && showDashboardState) {
|
128 | 67 | return <Dashboard pid={pidState} handleCloseDashboard={toggleDashboard(false)} />;
|
129 | 68 | }
|
130 | 69 | return (
|
131 | 70 | <>
|
132 |
| - <EditProjectButton isOwner={editableState} onEdit={toggleDashboard(true)} /> |
133 |
| - <SizeMe> |
134 |
| - {({ size }) => ( |
135 |
| - <Row> |
136 |
| - <BannerContent |
137 |
| - width={size.width} |
138 |
| - name={projectDataState.name} |
139 |
| - images={projectDataState.images} |
140 |
| - /> |
141 |
| - <ProjectInfoContent project={projectDataState} /> |
142 |
| - </Row> |
143 |
| - )} |
144 |
| - </SizeMe> |
| 71 | + {editableState ? <EditProjectBanner onEdit={toggleDashboard(true)} /> : <div />} |
| 72 | + <Row> |
| 73 | + <BannerContent name={projectDataState.name} images={projectDataState.images} /> |
| 74 | + <ProjectInfoContent project={projectDataState} /> |
| 75 | + </Row> |
145 | 76 | <SocialContentSection
|
146 | 77 | isOwner={editableState}
|
147 | 78 | projectId={pidState}
|
148 | 79 | ourstory={projectDataState.about ? projectDataState.about : ''}
|
149 |
| - selected={hash} |
| 80 | + selected={location.hash} |
150 | 81 | />
|
151 | 82 | </>
|
152 | 83 | );
|
|
0 commit comments