| 
									
										
										
										
											2018-09-19 13:49:04 +02:00
										 |  |  | // @flow
 | 
					
						
							| 
									
										
										
										
											2018-09-18 16:51:31 +02:00
										 |  |  | import React from "react"; | 
					
						
							|  |  |  | import { connect } from "react-redux"; | 
					
						
							| 
									
										
										
										
											2018-09-19 13:49:04 +02:00
										 |  |  | import { | 
					
						
							|  |  |  |   ErrorNotification, | 
					
						
							|  |  |  |   Loading, | 
					
						
							| 
									
										
										
										
											2018-09-20 16:28:41 +02:00
										 |  |  |   Page, | 
					
						
							| 
									
										
										
										
											2018-09-19 13:49:04 +02:00
										 |  |  |   Paginator | 
					
						
							|  |  |  | } from "@scm-manager/ui-components"; | 
					
						
							| 
									
										
										
										
											2018-09-17 14:03:13 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | import { | 
					
						
							| 
									
										
										
										
											2018-09-18 16:51:31 +02:00
										 |  |  |   getFetchChangesetsFailure, | 
					
						
							| 
									
										
										
										
											2018-09-19 13:49:04 +02:00
										 |  |  |   isFetchChangesetsPending, | 
					
						
							| 
									
										
										
										
											2018-09-19 17:18:24 +02:00
										 |  |  |   selectListAsCollection, | 
					
						
							|  |  |  |   fetchChangesetsByLink, | 
					
						
							| 
									
										
										
										
											2018-09-20 16:28:41 +02:00
										 |  |  |   getChangesetsFromState, | 
					
						
							|  |  |  |   fetchChangesetsByPage, | 
					
						
							|  |  |  |   fetchChangesetsByBranchAndPage | 
					
						
							| 
									
										
										
										
											2018-09-17 14:03:13 +02:00
										 |  |  | } from "../modules/changesets"; | 
					
						
							| 
									
										
										
										
											2018-09-18 16:51:31 +02:00
										 |  |  | import type { History } from "history"; | 
					
						
							|  |  |  | import { | 
					
						
							|  |  |  |   fetchBranchesByNamespaceAndName, | 
					
						
							|  |  |  |   getBranchNames | 
					
						
							|  |  |  | } from "../../repos/modules/branches"; | 
					
						
							| 
									
										
										
										
											2018-09-19 13:49:04 +02:00
										 |  |  | import type { PagedCollection, Repository } from "@scm-manager/ui-types"; | 
					
						
							| 
									
										
										
										
											2018-09-19 16:19:59 +02:00
										 |  |  | import ChangesetList from "../components/ChangesetList"; | 
					
						
							| 
									
										
										
										
											2018-09-17 14:03:13 +02:00
										 |  |  | import DropDown from "../components/DropDown"; | 
					
						
							| 
									
										
										
										
											2018-09-18 16:51:31 +02:00
										 |  |  | import { withRouter } from "react-router-dom"; | 
					
						
							| 
									
										
										
										
											2018-09-17 14:03:13 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | type Props = { | 
					
						
							|  |  |  |   repository: Repository, | 
					
						
							|  |  |  |   branchName: string, | 
					
						
							|  |  |  |   history: History, | 
					
						
							| 
									
										
										
										
											2018-09-18 16:51:31 +02:00
										 |  |  |   fetchChangesetsByNamespaceNameAndBranch: ( | 
					
						
							|  |  |  |     namespace: string, | 
					
						
							|  |  |  |     name: string, | 
					
						
							|  |  |  |     branch: string | 
					
						
							| 
									
										
										
										
											2018-09-19 13:49:04 +02:00
										 |  |  |   ) => void, | 
					
						
							| 
									
										
										
										
											2018-09-19 17:18:24 +02:00
										 |  |  |   list: PagedCollection, | 
					
						
							|  |  |  |   fetchChangesetsByLink: string => void, | 
					
						
							|  |  |  |   page: number | 
					
						
							| 
									
										
										
										
											2018-09-18 16:51:31 +02:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2018-09-17 14:03:13 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-20 16:28:41 +02:00
										 |  |  | class Changesets extends React.PureComponent<State, Props> { | 
					
						
							| 
									
										
										
										
											2018-09-17 14:03:13 +02:00
										 |  |  |   constructor(props) { | 
					
						
							|  |  |  |     super(props); | 
					
						
							| 
									
										
										
										
											2018-09-18 09:22:08 +02:00
										 |  |  |     this.state = {}; | 
					
						
							| 
									
										
										
										
											2018-09-17 14:03:13 +02:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-19 14:46:01 +02:00
										 |  |  |   onPageChange = (link: string) => { | 
					
						
							| 
									
										
										
										
											2018-09-19 17:18:24 +02:00
										 |  |  |     const { namespace, name } = this.props.repository; | 
					
						
							|  |  |  |     const branch = this.props.match.params.branch; | 
					
						
							|  |  |  |     this.props.fetchChangesetsByLink(namespace, name, link, branch); | 
					
						
							| 
									
										
										
										
											2018-09-19 14:46:01 +02:00
										 |  |  |   }; | 
					
						
							| 
									
										
										
										
											2018-09-19 17:18:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-17 14:03:13 +02:00
										 |  |  |   componentDidMount() { | 
					
						
							| 
									
										
										
										
											2018-09-18 16:51:31 +02:00
										 |  |  |     const { namespace, name } = this.props.repository; | 
					
						
							| 
									
										
										
										
											2018-09-17 16:31:19 +02:00
										 |  |  |     const branchName = this.props.match.params.branch; | 
					
						
							| 
									
										
										
										
											2018-09-18 16:51:31 +02:00
										 |  |  |     const { | 
					
						
							| 
									
										
										
										
											2018-09-20 16:28:41 +02:00
										 |  |  |       fetchBranchesByNamespaceAndName, | 
					
						
							|  |  |  |       fetchChangesetsByPage, | 
					
						
							|  |  |  |       fetchChangesetsByBranchAndPage | 
					
						
							| 
									
										
										
										
											2018-09-18 16:51:31 +02:00
										 |  |  |     } = this.props; | 
					
						
							| 
									
										
										
										
											2018-09-17 16:31:19 +02:00
										 |  |  |     if (branchName) { | 
					
						
							| 
									
										
										
										
											2018-09-20 16:28:41 +02:00
										 |  |  |       fetchChangesetsByBranchAndPage( | 
					
						
							|  |  |  |         namespace, | 
					
						
							|  |  |  |         name, | 
					
						
							|  |  |  |         branchName, | 
					
						
							|  |  |  |         this.props.page | 
					
						
							|  |  |  |       ); | 
					
						
							| 
									
										
										
										
											2018-09-17 16:31:19 +02:00
										 |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2018-09-20 16:28:41 +02:00
										 |  |  |       fetchChangesetsByPage(namespace, name, this.props.page); | 
					
						
							| 
									
										
										
										
											2018-09-17 16:31:19 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-09-18 15:07:30 +02:00
										 |  |  |     fetchBranchesByNamespaceAndName(namespace, name); | 
					
						
							| 
									
										
										
										
											2018-09-17 14:03:13 +02:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-20 16:28:41 +02:00
										 |  |  |   componentDidUpdate() { | 
					
						
							|  |  |  |     const { page, list, repository, match } = this.props; | 
					
						
							|  |  |  |     const { namespace, name } = repository; | 
					
						
							|  |  |  |     const branch = match.params.branch; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (list && (list.page || list.page === 0)) { | 
					
						
							|  |  |  |       // backend starts paging at 0
 | 
					
						
							|  |  |  |       const statePage: number = list.page + 1; | 
					
						
							|  |  |  |       if (page !== statePage) { | 
					
						
							|  |  |  |         if (branch) { | 
					
						
							|  |  |  |           this.props.history.push( | 
					
						
							|  |  |  |             `/repo/${namespace}/${name}/${branch}/history/${statePage}` | 
					
						
							|  |  |  |           ); | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |           this.props.history.push( | 
					
						
							|  |  |  |             `/repo/${namespace}/${name}/history/${statePage}` | 
					
						
							|  |  |  |           ); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-17 14:03:13 +02:00
										 |  |  |   render() { | 
					
						
							| 
									
										
										
										
											2018-09-18 16:51:31 +02:00
										 |  |  |     const { changesets, loading, error } = this.props; | 
					
						
							| 
									
										
										
										
											2018-09-20 16:28:41 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-17 17:20:17 +02:00
										 |  |  |     if (loading || !changesets) { | 
					
						
							| 
									
										
										
										
											2018-09-18 16:51:31 +02:00
										 |  |  |       return <Loading />; | 
					
						
							| 
									
										
										
										
											2018-09-17 14:03:13 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-09-20 16:28:41 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (error) { | 
					
						
							|  |  |  |       return <ErrorNotification error={error} />; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-09-18 16:51:31 +02:00
										 |  |  |     return ( | 
					
						
							|  |  |  |       <div> | 
					
						
							| 
									
										
										
										
											2018-09-19 13:49:04 +02:00
										 |  |  |         {this.renderTable()} | 
					
						
							|  |  |  |         {this.renderPaginator()} | 
					
						
							| 
									
										
										
										
											2018-09-18 16:51:31 +02:00
										 |  |  |       </div> | 
					
						
							|  |  |  |     ); | 
					
						
							| 
									
										
										
										
											2018-09-18 09:22:08 +02:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-19 13:49:04 +02:00
										 |  |  |   renderTable = () => { | 
					
						
							| 
									
										
										
										
											2018-09-18 09:22:08 +02:00
										 |  |  |     const branch = this.props.match.params.branch; | 
					
						
							| 
									
										
										
										
											2018-09-19 16:05:35 +02:00
										 |  |  |     const { repository, changesets, branchNames } = this.props; | 
					
						
							| 
									
										
										
										
											2018-09-18 09:22:08 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-18 15:07:30 +02:00
										 |  |  |     if (branchNames && branchNames.length > 0) { | 
					
						
							| 
									
										
										
										
											2018-09-18 16:51:31 +02:00
										 |  |  |       return ( | 
					
						
							|  |  |  |         <div> | 
					
						
							|  |  |  |           <label className="label">Branch: </label> | 
					
						
							|  |  |  |           <DropDown | 
					
						
							|  |  |  |             options={branchNames} | 
					
						
							|  |  |  |             preselectedOption={branch} | 
					
						
							|  |  |  |             optionSelected={branch => this.branchChanged(branch)} | 
					
						
							|  |  |  |           /> | 
					
						
							| 
									
										
										
										
											2018-09-19 16:19:59 +02:00
										 |  |  |           <ChangesetList repository={repository} changesets={changesets} /> | 
					
						
							| 
									
										
										
										
											2018-09-18 16:51:31 +02:00
										 |  |  |         </div> | 
					
						
							|  |  |  |       ); | 
					
						
							| 
									
										
										
										
											2018-09-17 14:03:13 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-19 16:19:59 +02:00
										 |  |  |     return <ChangesetList changesets={changesets} />; | 
					
						
							| 
									
										
										
										
											2018-09-18 09:22:08 +02:00
										 |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-19 13:49:04 +02:00
										 |  |  |   renderPaginator() { | 
					
						
							|  |  |  |     const { list } = this.props; | 
					
						
							|  |  |  |     if (list) { | 
					
						
							|  |  |  |       return <Paginator collection={list} onPageChange={this.onPageChange} />; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return null; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   branchChanged = (branchName: string): void => { | 
					
						
							| 
									
										
										
										
											2018-09-18 16:51:31 +02:00
										 |  |  |     const { history, repository } = this.props; | 
					
						
							|  |  |  |     history.push( | 
					
						
							| 
									
										
										
										
											2018-09-20 16:28:41 +02:00
										 |  |  |       `/repo/${repository.namespace}/${repository.name}/${branchName}/history` | 
					
						
							| 
									
										
										
										
											2018-09-18 16:51:31 +02:00
										 |  |  |     ); | 
					
						
							| 
									
										
										
										
											2018-09-17 14:03:13 +02:00
										 |  |  |   }; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-19 17:18:24 +02:00
										 |  |  | const createKey = ( | 
					
						
							|  |  |  |   namespace: string, | 
					
						
							|  |  |  |   name: string, | 
					
						
							|  |  |  |   branch?: string | 
					
						
							|  |  |  | ): string => { | 
					
						
							|  |  |  |   let key = `${namespace}/${name}`; | 
					
						
							|  |  |  |   if (branch) { | 
					
						
							|  |  |  |     key = key + `/${branch}`; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   return key; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-20 16:28:41 +02:00
										 |  |  | const getPageFromProps = props => { | 
					
						
							|  |  |  |   let page = props.match.params.page; | 
					
						
							|  |  |  |   if (page) { | 
					
						
							|  |  |  |     page = parseInt(page, 10); | 
					
						
							|  |  |  |   } else { | 
					
						
							|  |  |  |     page = 1; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   return page; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-17 14:03:13 +02:00
										 |  |  | const mapStateToProps = (state, ownProps: Props) => { | 
					
						
							| 
									
										
										
										
											2018-09-18 16:51:31 +02:00
										 |  |  |   const { namespace, name } = ownProps.repository; | 
					
						
							| 
									
										
										
										
											2018-09-19 17:18:24 +02:00
										 |  |  |   const { branch } = ownProps.match.params; | 
					
						
							|  |  |  |   const key = createKey(namespace, name, branch); | 
					
						
							|  |  |  |   const loading = isFetchChangesetsPending(state, namespace, name, branch); | 
					
						
							|  |  |  |   const changesets = getChangesetsFromState(state, key); | 
					
						
							| 
									
										
										
										
											2018-09-19 13:49:04 +02:00
										 |  |  |   const branchNames = getBranchNames(namespace, name, state); | 
					
						
							| 
									
										
										
										
											2018-09-19 17:18:24 +02:00
										 |  |  |   const error = getFetchChangesetsFailure(state, namespace, name, branch); | 
					
						
							|  |  |  |   const list = selectListAsCollection(state, key); | 
					
						
							| 
									
										
										
										
											2018-09-20 16:28:41 +02:00
										 |  |  |   const page = getPageFromProps(ownProps); | 
					
						
							| 
									
										
										
										
											2018-09-19 13:49:04 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-17 14:03:13 +02:00
										 |  |  |   return { | 
					
						
							| 
									
										
										
										
											2018-09-19 13:49:04 +02:00
										 |  |  |     loading, | 
					
						
							|  |  |  |     changesets, | 
					
						
							|  |  |  |     branchNames, | 
					
						
							|  |  |  |     error, | 
					
						
							| 
									
										
										
										
											2018-09-20 16:28:41 +02:00
										 |  |  |     list, | 
					
						
							|  |  |  |     page | 
					
						
							| 
									
										
										
										
											2018-09-18 16:51:31 +02:00
										 |  |  |   }; | 
					
						
							| 
									
										
										
										
											2018-09-17 14:03:13 +02:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const mapDispatchToProps = dispatch => { | 
					
						
							|  |  |  |   return { | 
					
						
							| 
									
										
										
										
											2018-09-20 16:28:41 +02:00
										 |  |  |     fetchBranchesByNamespaceAndName: (namespace: string, name: string) => { | 
					
						
							|  |  |  |       dispatch(fetchBranchesByNamespaceAndName(namespace, name)); | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     fetchChangesetsByPage: (namespace: string, name: string, page: number) => { | 
					
						
							|  |  |  |       dispatch(fetchChangesetsByPage(namespace, name, page)); | 
					
						
							| 
									
										
										
										
											2018-09-17 14:03:13 +02:00
										 |  |  |     }, | 
					
						
							| 
									
										
										
										
											2018-09-20 16:28:41 +02:00
										 |  |  |     fetchChangesetsByBranchAndPage: ( | 
					
						
							| 
									
										
										
										
											2018-09-18 16:51:31 +02:00
										 |  |  |       namespace: string, | 
					
						
							|  |  |  |       name: string, | 
					
						
							| 
									
										
										
										
											2018-09-20 16:28:41 +02:00
										 |  |  |       branch: string, | 
					
						
							|  |  |  |       page: number | 
					
						
							| 
									
										
										
										
											2018-09-18 16:51:31 +02:00
										 |  |  |     ) => { | 
					
						
							| 
									
										
										
										
											2018-09-20 16:28:41 +02:00
										 |  |  |       dispatch(fetchChangesetsByBranchAndPage(namespace, name, branch, page)); | 
					
						
							| 
									
										
										
										
											2018-09-19 17:18:24 +02:00
										 |  |  |     }, | 
					
						
							|  |  |  |     fetchChangesetsByLink: ( | 
					
						
							|  |  |  |       namespace: string, | 
					
						
							|  |  |  |       name: string, | 
					
						
							|  |  |  |       link: string, | 
					
						
							|  |  |  |       branch?: string | 
					
						
							|  |  |  |     ) => { | 
					
						
							|  |  |  |       dispatch(fetchChangesetsByLink(namespace, name, link, branch)); | 
					
						
							| 
									
										
										
										
											2018-09-17 14:03:13 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-09-18 16:51:31 +02:00
										 |  |  |   }; | 
					
						
							| 
									
										
										
										
											2018-09-17 14:03:13 +02:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-18 16:51:31 +02:00
										 |  |  | export default withRouter( | 
					
						
							|  |  |  |   connect( | 
					
						
							|  |  |  |     mapStateToProps, | 
					
						
							|  |  |  |     mapDispatchToProps | 
					
						
							|  |  |  |   )(Changesets) | 
					
						
							|  |  |  | ); |