Guest User

Untitled

a guest
May 27th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.38 KB | None | 0 0
  1. import React from 'react'
  2. import Layout from '../Layout'
  3. import ProjectList from './ProjectList'
  4. import ProjectModal from './ProjectModal'
  5. import Button from 'material-ui/Button'
  6. import Haikunator from 'haikunator'
  7. import withData from './withData'
  8. import { compose, branch } from 'recompose'
  9. import { mapPropsStream, createEventHandler } from '../../rx'
  10. import { LoadingOverlay } from '../../lib/withAsync'
  11. import { startWith, combineLatest, map } from 'rxjs/operators'
  12.  
  13. const haikunator = new Haikunator()
  14.  
  15. export const findIndex = (arr, id) => {
  16. let index = 0
  17. while (index < arr.length) {
  18. if (arr[index].id === id) {
  19. break
  20. }
  21. index++
  22. }
  23. return index >= arr.length ? -1 : index
  24. }
  25.  
  26. class Projects extends React.Component {
  27. handleClick = (evt, id) => {
  28. this.props.history.replace('/projects/' + id)
  29. }
  30.  
  31. createProject = () => {
  32. this.props.createProject.mutate({
  33. name: haikunator.haikunate(),
  34. userId: this.props.auth.user.id
  35. })
  36. }
  37.  
  38. render () {
  39. const { createProject } = this.props
  40. return (
  41. <Layout {...this.props} loading={createProject.loading}>
  42. <ProjectModal
  43. {...this.props}
  44. />
  45. <div style={{
  46. width: '660px',
  47. margin: '2em auto'
  48. }}>
  49. <div style={{
  50. margin: '2em 0'
  51. }}>
  52. <ProjectList
  53. allProjects={this.props.data.allProjects}
  54. handleClick={this.props.handleClick}
  55. onMouseEnter={this.props.onMouseEnter}
  56. onMouseLeave={this.props.onMouseLeave}
  57. handleDelete={this.props.handleDelete}
  58. handleClose={this.props.handleClose}
  59. />
  60. </div>
  61. <Button
  62. variant='raised'
  63. color='primary'
  64. onClick={this.createProject}
  65. >
  66. New Project
  67. </Button>
  68. </div>
  69. </Layout>
  70. )
  71. }
  72. }
  73.  
  74. Projects.defaultProps = {
  75. data: {
  76. allProjects: []
  77. },
  78. activeProject: undefined,
  79. handleClick: () => {},
  80. handleClose: () => {}
  81. }
  82.  
  83. const withWrapper = compose(
  84. withData,
  85. branch(
  86. props => !props.data || props.data.loading,
  87. Component => props => <LoadingOverlay>
  88. <Component {...props} />
  89. </LoadingOverlay>
  90. ),
  91. mapPropsStream(props$ => {
  92. const { handler: handleProject, stream: projectStream$ } = createEventHandler()
  93.  
  94. const projectStream = projectStream$.pipe(startWith({
  95. activeProject: undefined,
  96. open: false
  97. }))
  98.  
  99. const handleClose = () => handleProject({
  100. activeProject: undefined,
  101. open: false
  102. })
  103.  
  104. const handleClick = activeProject => handleProject({
  105. activeProject,
  106. open: true
  107. })
  108.  
  109. const onMouseEnter = activeProject => {
  110. handleProject({
  111. activeProject,
  112. open: false
  113. })
  114. }
  115.  
  116. const onMouseLeave = activeProject => {}
  117.  
  118. const handleDelete = ({ deleteProject }) => activeProject =>
  119. Promise.resolve(handleClose()).then(
  120. deleteProject({ id: activeProject })
  121. )
  122.  
  123. return props$.pipe(
  124. map(props => ({
  125. ...props,
  126. handleClose,
  127. handleClick,
  128. handleDelete: handleDelete({
  129. deleteProject: props.deleteProject
  130. }),
  131. onMouseEnter,
  132. onMouseLeave
  133. })),
  134. combineLatest(
  135. projectStream,
  136. (props, state) => ({
  137. ...props,
  138. ...state
  139. })
  140. )
  141. )
  142. })
  143. )
  144. export default withWrapper(Projects)
Add Comment
Please, Sign In to add comment