Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(core): move app refresher, pager duty buttons to components (#…
- Loading branch information
1 parent
1c297b6
commit c128b91
Showing
3 changed files
with
130 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
app/scripts/modules/core/src/application/nav/ApplicationRefresher.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import * as React from 'react'; | ||
import { BindAll } from 'lodash-decorators'; | ||
import { Subscription } from 'rxjs'; | ||
|
||
import { Application } from 'core/application'; | ||
import { Refresher } from 'core/presentation/refresher/Refresher'; | ||
|
||
export interface IApplicationRefresherProps { | ||
app: Application; | ||
} | ||
|
||
export interface IApplicationRefresherState { | ||
refreshing: boolean; | ||
lastRefresh: number; | ||
} | ||
|
||
@BindAll() | ||
export class ApplicationRefresher extends React.Component<IApplicationRefresherProps, IApplicationRefresherState> { | ||
|
||
private activeStateRefreshUnsubscribe: () => void; | ||
private activeStateChangeSubscription: Subscription; | ||
private stopListeningToAppRefresh: Function; | ||
|
||
constructor(props: IApplicationRefresherProps) { | ||
super(props); | ||
this.configureApplicationEventListeners(props.app); | ||
this.state = Object.assign( | ||
this.parseRefreshState(props), | ||
); | ||
} | ||
|
||
private resetActiveStateRefreshStream(props: IApplicationRefresherProps): void { | ||
if (this.activeStateRefreshUnsubscribe) { this.activeStateRefreshUnsubscribe(); } | ||
const activeState = props.app.activeState || props.app; | ||
this.activeStateRefreshUnsubscribe = activeState.onRefresh(null, () => { | ||
this.setState(this.parseRefreshState(props)); | ||
}); | ||
} | ||
|
||
public componentWillReceiveProps(nextProps: IApplicationRefresherProps) { | ||
this.configureApplicationEventListeners(nextProps.app); | ||
} | ||
|
||
private configureApplicationEventListeners(app: Application): void { | ||
app.ready().then(() => this.setState(this.parseRefreshState(this.props))); | ||
this.clearApplicationListeners(); | ||
this.activeStateChangeSubscription = app.activeStateChangeStream.subscribe(() => { | ||
this.resetActiveStateRefreshStream(this.props); | ||
this.setState(this.parseRefreshState(this.props)); | ||
}); | ||
this.stopListeningToAppRefresh = app.onRefresh(null, () => { | ||
this.setState(this.parseRefreshState(this.props)); | ||
}); | ||
} | ||
|
||
private clearApplicationListeners(): void { | ||
if (this.activeStateChangeSubscription) { | ||
this.activeStateChangeSubscription.unsubscribe(); | ||
} | ||
if (this.stopListeningToAppRefresh) { | ||
this.stopListeningToAppRefresh(); | ||
} | ||
} | ||
|
||
private parseRefreshState(props: IApplicationRefresherProps): IApplicationRefresherState { | ||
const activeState = props.app.activeState || props.app; | ||
return { | ||
lastRefresh: activeState.lastRefresh, | ||
refreshing: activeState.loading, | ||
}; | ||
} | ||
|
||
public componentWillUnmount(): void { | ||
this.clearApplicationListeners(); | ||
} | ||
|
||
public handleRefresh(): void { | ||
// Force set refreshing to true since we are forcing the refresh | ||
this.setState({ refreshing: true }); | ||
this.props.app.refresh(true); | ||
} | ||
|
||
public render() { | ||
return ( | ||
<Refresher | ||
refreshing={this.state.refreshing} | ||
lastRefresh={this.state.lastRefresh} | ||
refresh={this.handleRefresh} | ||
/> | ||
); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
app/scripts/modules/core/src/application/nav/PagerDutyButton.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import * as React from 'react'; | ||
import { BindAll } from 'lodash-decorators'; | ||
|
||
import { Application } from 'core/application'; | ||
import { ReactInjector } from 'core/reactShims'; | ||
import { Tooltip } from 'core/presentation'; | ||
|
||
export interface IPagerDutyButtonProps { | ||
app: Application; | ||
} | ||
|
||
@BindAll() | ||
export class PagerDutyButton extends React.Component<IPagerDutyButtonProps> { | ||
|
||
private pageApplicationOwner(): void { | ||
ReactInjector.pagerDutyWriter.pageApplicationOwnerModal(this.props.app); | ||
} | ||
|
||
public render() { | ||
if (!this.props.app.attributes.pdApiKey) { | ||
return null; | ||
} | ||
return ( | ||
<Tooltip value="Page application owner"> | ||
<button | ||
className="btn btn-xs page-button btn-page-owner" | ||
onClick={this.pageApplicationOwner} | ||
> | ||
<i className="fa fa-phone"/> | ||
</button> | ||
</Tooltip> | ||
); | ||
} | ||
} |