PageHeader.jsx 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. /*
  2. Copyright (C) 2017 Cloudbase Solutions SRL
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Affero General Public License as
  5. published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Affero General Public License for more details.
  11. You should have received a copy of the GNU Affero General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. // @flow
  15. import React from 'react'
  16. import styled from 'styled-components'
  17. import { observer } from 'mobx-react'
  18. import type { User } from '../../../types/User'
  19. import type { Project } from '../../../types/Project'
  20. import type { Endpoint as EndpointType } from '../../../types/Endpoint'
  21. import Dropdown from '../../molecules/Dropdown'
  22. import NewItemDropdown from '../../molecules/NewItemDropdown'
  23. import NotificationDropdown from '../../molecules/NotificationDropdown'
  24. import UserDropdown from '../../molecules/UserDropdown'
  25. import Modal from '../../molecules/Modal'
  26. import ChooseProvider from '../../organisms/ChooseProvider'
  27. import Endpoint from '../../organisms/Endpoint'
  28. import UserModal from '../../organisms/UserModal'
  29. import ProjectModal from '../../organisms/ProjectModal'
  30. import AboutModal from '../../pages/AboutModal'
  31. import projectStore from '../../../stores/ProjectStore'
  32. import userStore from '../../../stores/UserStore'
  33. import endpointStore from '../../../stores/EndpointStore'
  34. import notificationStore from '../../../stores/NotificationStore'
  35. import providerStore from '../../../stores/ProviderStore'
  36. import Palette from '../../styleUtils/Palette'
  37. import StyleProps from '../../styleUtils/StyleProps'
  38. const Wrapper = styled.div`
  39. display: flex;
  40. margin: 32px 0 48px 0;
  41. align-items: center;
  42. flex-wrap: wrap;
  43. `
  44. const Title = styled.div`
  45. color: ${Palette.black};
  46. font-size: 32px;
  47. font-weight: ${StyleProps.fontWeights.light};
  48. flex-grow: 1;
  49. overflow: hidden;
  50. white-space: nowrap;
  51. text-overflow: ellipsis;
  52. margin-top: 16px;
  53. margin-right: 16px;
  54. `
  55. const Controls = styled.div`
  56. display: flex;
  57. margin-top: 16px;
  58. margin-left: -16px;
  59. & > div {
  60. margin-left: 16px;
  61. }
  62. `
  63. type Props = {
  64. title: string,
  65. onProjectChange?: (project: Project) => void,
  66. onModalOpen?: () => void,
  67. onModalClose?: () => void,
  68. componentRef?: (ref: any) => void
  69. }
  70. type State = {
  71. showChooseProviderModal: boolean,
  72. showEndpointModal: boolean,
  73. showUserModal: boolean,
  74. showProjectModal: boolean,
  75. showAbout: boolean,
  76. providerType: ?string,
  77. uploadedEndpoint: ?EndpointType,
  78. }
  79. @observer
  80. class PageHeader extends React.Component<Props, State> {
  81. state = {
  82. showChooseProviderModal: false,
  83. showEndpointModal: false,
  84. showUserModal: false,
  85. showProjectModal: false,
  86. providerType: null,
  87. uploadedEndpoint: null,
  88. showAbout: false,
  89. }
  90. pollTimeout: TimeoutID
  91. stopPolling: boolean
  92. componentWillMount() {
  93. this.stopPolling = false
  94. this.pollData(true)
  95. if (this.props.componentRef) {
  96. this.props.componentRef(this)
  97. }
  98. }
  99. componentWillUnmount() {
  100. clearTimeout(this.pollTimeout)
  101. this.stopPolling = true
  102. }
  103. getCurrentProject() {
  104. let project = userStore.loggedUser && userStore.loggedUser.project ? userStore.loggedUser.project : null
  105. if (project) {
  106. return projectStore.projects.find(p => p.id === project.id)
  107. }
  108. return null
  109. }
  110. handleUserItemClick(item: { value: string }) {
  111. switch (item.value) {
  112. case 'about':
  113. this.setState({ showAbout: true })
  114. if (this.props.onModalOpen) {
  115. this.props.onModalOpen()
  116. }
  117. return
  118. case 'signout':
  119. userStore.logout()
  120. break
  121. default:
  122. }
  123. }
  124. handleNewItem(item: ?string) {
  125. switch (item) {
  126. case 'endpoint':
  127. providerStore.loadProviders()
  128. if (this.props.onModalOpen) {
  129. this.props.onModalOpen()
  130. }
  131. this.setState({ showChooseProviderModal: true })
  132. break
  133. case 'user':
  134. projectStore.getProjects()
  135. if (this.props.onModalOpen) {
  136. this.props.onModalOpen()
  137. }
  138. this.setState({ showUserModal: true })
  139. break
  140. case 'project':
  141. if (this.props.onModalOpen) {
  142. this.props.onModalOpen()
  143. }
  144. this.setState({ showProjectModal: true })
  145. break
  146. default:
  147. }
  148. }
  149. handleNotificationsClose() {
  150. notificationStore.saveSeen()
  151. }
  152. handleCloseChooseProviderModal() {
  153. if (this.props.onModalClose) {
  154. this.props.onModalClose()
  155. }
  156. this.setState({ showChooseProviderModal: false })
  157. }
  158. handleProviderClick(providerType: string) {
  159. this.setState({
  160. showChooseProviderModal: false,
  161. showEndpointModal: true,
  162. uploadedEndpoint: null,
  163. providerType,
  164. })
  165. }
  166. handleUploadEndpoint(endpoint: EndpointType) {
  167. endpointStore.setConnectionInfo(endpoint.connection_info)
  168. this.setState({
  169. showChooseProviderModal: false,
  170. showEndpointModal: true,
  171. providerType: endpoint.type,
  172. uploadedEndpoint: endpoint,
  173. })
  174. }
  175. handleCloseEndpointModal() {
  176. if (this.props.onModalClose) {
  177. this.props.onModalClose()
  178. }
  179. this.setState({ showEndpointModal: false })
  180. }
  181. handleBackEndpointModal(options?: { autoClose?: boolean }) {
  182. this.setState({ showChooseProviderModal: !options || !options.autoClose, showEndpointModal: false })
  183. }
  184. async handleProjectChange(project: Project) {
  185. await userStore.switchProject(project.id)
  186. projectStore.getProjects()
  187. notificationStore.loadData()
  188. if (this.props.onProjectChange) {
  189. this.props.onProjectChange(project)
  190. }
  191. }
  192. handleUserModalClose() {
  193. if (this.props.onModalClose) {
  194. this.props.onModalClose()
  195. }
  196. this.setState({ showUserModal: false })
  197. }
  198. async handleUserUpdateClick(user: User) {
  199. await userStore.add(user)
  200. if (this.props.onModalClose) {
  201. this.props.onModalClose()
  202. }
  203. this.setState({ showUserModal: false })
  204. }
  205. handleProjectModalClose() {
  206. if (this.props.onModalClose) {
  207. this.props.onModalClose()
  208. }
  209. this.setState({ showProjectModal: false })
  210. }
  211. async handleProjectModalUpdateClick(project: Project) {
  212. await projectStore.add(project)
  213. if (this.props.onModalClose) {
  214. this.props.onModalClose()
  215. }
  216. this.setState({ showProjectModal: false })
  217. }
  218. async pollData(showLoading?: boolean) {
  219. if (
  220. this.stopPolling ||
  221. this.state.showChooseProviderModal ||
  222. this.state.showEndpointModal ||
  223. this.state.showProjectModal ||
  224. this.state.showUserModal ||
  225. this.state.showAbout
  226. ) {
  227. return
  228. }
  229. await notificationStore.loadData(showLoading)
  230. this.pollTimeout = setTimeout(() => { this.pollData() }, 5000)
  231. }
  232. render() {
  233. return (
  234. <Wrapper>
  235. <Title>{this.props.title}</Title>
  236. <Controls>
  237. <Dropdown
  238. selectedItem={this.getCurrentProject()}
  239. items={projectStore.projects}
  240. onChange={project => { this.handleProjectChange(project) }}
  241. noItemsMessage="Loading..."
  242. labelField="name"
  243. />
  244. <NewItemDropdown onChange={item => { this.handleNewItem(item.value) }} />
  245. <NotificationDropdown
  246. items={notificationStore.notificationItems}
  247. onClose={() => this.handleNotificationsClose()}
  248. />
  249. <UserDropdown user={userStore.loggedUser} onItemClick={item => { this.handleUserItemClick(item) }} />
  250. </Controls>
  251. <Modal
  252. isOpen={this.state.showChooseProviderModal}
  253. title="New Cloud Endpoint"
  254. onRequestClose={() => { this.handleCloseChooseProviderModal() }}
  255. >
  256. <ChooseProvider
  257. onCancelClick={() => { this.handleCloseChooseProviderModal() }}
  258. providers={providerStore.providerNames}
  259. loading={providerStore.providersLoading}
  260. onProviderClick={providerName => { this.handleProviderClick(providerName) }}
  261. onUploadEndpoint={endpoint => { this.handleUploadEndpoint(endpoint) }}
  262. />
  263. </Modal>
  264. <Modal
  265. isOpen={this.state.showEndpointModal}
  266. title="New Cloud Endpoint"
  267. onRequestClose={() => { this.handleCloseEndpointModal() }}
  268. >
  269. <Endpoint
  270. type={this.state.providerType}
  271. cancelButtonText="Back"
  272. onCancelClick={options => { this.handleBackEndpointModal(options) }}
  273. endpoint={this.state.uploadedEndpoint}
  274. isNewEndpoint={Boolean(this.state.uploadedEndpoint)}
  275. />
  276. </Modal>
  277. {this.state.showUserModal ? (
  278. <UserModal
  279. isNewUser
  280. loading={userStore.updating}
  281. projects={projectStore.projects}
  282. onRequestClose={() => { this.handleUserModalClose() }}
  283. onUpdateClick={user => { this.handleUserUpdateClick(user) }}
  284. />
  285. ) : null}
  286. {this.state.showProjectModal ? (
  287. <ProjectModal
  288. isNewProject
  289. loading={projectStore.updating}
  290. onRequestClose={() => { this.handleProjectModalClose() }}
  291. onUpdateClick={project => { this.handleProjectModalUpdateClick(project) }}
  292. />
  293. ) : null}
  294. {this.state.showAbout ? (
  295. <AboutModal onRequestClose={() => {
  296. this.setState({ showAbout: false })
  297. if (this.props.onModalClose) {
  298. this.props.onModalClose()
  299. }
  300. }}
  301. />
  302. ) : null}
  303. </Wrapper>
  304. )
  305. }
  306. }
  307. export default PageHeader