|
| 1 | +import autobind from "autobind-decorator"; |
| 2 | +import React from "react"; |
| 3 | +import { connect } from "react-redux"; |
| 4 | +import { Dispatch } from "redux"; |
| 5 | +import { createRouteNodeSelector } from "redux-router5"; |
| 6 | +import { State as IRouterState } from "router5"; |
| 7 | +import { ICreateFeatureRunRequest, IFeatureRun } from "../../Sdk/nodes/FeatureRuns"; |
| 8 | +import { EmptyState } from "../containers/EmptyState"; |
| 9 | +import { ErrorState } from "../containers/ErrorState"; |
| 10 | +import { LoadingState } from "../containers/LoadingState"; |
| 11 | +import { IStore } from "../redux/IStore"; |
| 12 | +import { |
| 13 | + createFeatureRunForFeature, |
| 14 | + fetchFeatureRunsForFeature |
| 15 | +} from "../redux/modules/featureRuns/fetchFeatureRunsForFeature"; |
| 16 | + |
| 17 | +interface IStateToProps { |
| 18 | + error: string; |
| 19 | + featureId: string; |
| 20 | + loaded: boolean; |
| 21 | + pending: boolean; |
| 22 | + runs: IFeatureRun[]; |
| 23 | +} |
| 24 | + |
| 25 | +interface IDispatchToProps { |
| 26 | + createRun: (run: ICreateFeatureRunRequest) => void; |
| 27 | + loadRuns: (featId: string) => void; |
| 28 | +} |
| 29 | + |
| 30 | +class Page extends React.PureComponent<IStateToProps & IDispatchToProps> { |
| 31 | + public componentDidMount(): void { |
| 32 | + if (!this.props.loaded) { |
| 33 | + this.props.loadRuns(this.props.featureId); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + public render(): JSX.Element { |
| 38 | + if (this.props.pending) { |
| 39 | + return ( |
| 40 | + <LoadingState>Pending</LoadingState> |
| 41 | + ); |
| 42 | + } |
| 43 | + if (this.props.error) { |
| 44 | + return ( |
| 45 | + <ErrorState>{this.props.error}</ErrorState> |
| 46 | + ); |
| 47 | + } |
| 48 | + if (this.props.runs.length === 0) { |
| 49 | + return ( |
| 50 | + <EmptyState buttonLabel={"Create a run"} onActionClick={this.handleCreateNewRunClick}> |
| 51 | + No runs currently exists, let's create a run so people can start to see the feature. |
| 52 | + </EmptyState> |
| 53 | + ); |
| 54 | + } |
| 55 | + // finally return actual list |
| 56 | + return ( |
| 57 | + <div> |
| 58 | + Runs Page |
| 59 | + </div> |
| 60 | + ); |
| 61 | + } |
| 62 | + |
| 63 | + // eslint-disable-next-line @typescript-eslint/member-ordering |
| 64 | + @autobind |
| 65 | + private handleCreateNewRunClick(): void { |
| 66 | + const allocation = parseInt(prompt("enter allocation", "100"), 10); |
| 67 | + if (isNaN(allocation)) { |
| 68 | + alert("allocation must be a number"); |
| 69 | + return; |
| 70 | + } |
| 71 | + const currentTime = new Date(); |
| 72 | + const startAt = prompt("start time: (default selected for you in UTC timezone)", currentTime.toISOString()); |
| 73 | + currentTime.setDate(currentTime.getDate() + 2); |
| 74 | + const endAt = prompt("end time: (default selected after 2 days for you in UTC timezone) (this is optional, feel free to remove it)", currentTime.toISOString()); |
| 75 | + const request: ICreateFeatureRunRequest = { |
| 76 | + allocation, |
| 77 | + featId: this.props.featureId, |
| 78 | + startAt |
| 79 | + }; |
| 80 | + if (endAt.trim() !== "") { |
| 81 | + request.endAt = endAt; |
| 82 | + } |
| 83 | + // todo: find a way to parse date correctly, make sure timezones are handled correctly on backend. |
| 84 | + this.props.createRun(request); |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +const mapStateToProps = (state: Pick<IStore, "featureRuns">): IStateToProps => { |
| 89 | + const route: IRouterState = createRouteNodeSelector("")(state).route; |
| 90 | + return { |
| 91 | + error: state.featureRuns.error, |
| 92 | + featureId: route.params.featId, |
| 93 | + loaded: state.featureRuns.loaded, |
| 94 | + pending: state.featureRuns.pending, |
| 95 | + runs: state.featureRuns.runs |
| 96 | + }; |
| 97 | +}; |
| 98 | + |
| 99 | +const mapDispatchToProps = (dispatch: Dispatch): IDispatchToProps => { |
| 100 | + return { |
| 101 | + createRun: (run: ICreateFeatureRunRequest) => dispatch(createFeatureRunForFeature.invoke(run)), |
| 102 | + loadRuns: (featId: string) => dispatch(fetchFeatureRunsForFeature.invoke(featId)) |
| 103 | + }; |
| 104 | +}; |
| 105 | + |
| 106 | +export const RunsPage = connect(mapStateToProps, mapDispatchToProps)(Page); |
0 commit comments