1 | import * as React from 'react';
|
2 | import { from, Subject, Subscription } from 'rxjs';
|
3 | import { catchError, map, mapTo, startWith, switchMap, tap } from 'rxjs/operators';
|
4 | import { asError, isErrorLike } from '../errors';
|
5 | const LOADING = 'loading';
|
6 |
|
7 | export class ExtensionAddButton extends React.PureComponent {
|
8 | constructor() {
|
9 | super(...arguments);
|
10 | this.state = { operationResultOrError: null };
|
11 | this.clicks = new Subject();
|
12 | this.subscriptions = new Subscription();
|
13 | this.onClick = () => {
|
14 | if (!this.props.confirm || this.props.confirm()) {
|
15 | this.clicks.next();
|
16 | }
|
17 | };
|
18 | this.addExtensionForSubject = (extension, subject) => this.props.extensions.context.updateExtensionSettings(subject.subject.id, {
|
19 | extensionID: extension.id,
|
20 | enabled: true,
|
21 | });
|
22 | }
|
23 | componentDidMount() {
|
24 | this.subscriptions.add(this.clicks
|
25 | .pipe(switchMap(() => from(this.addExtensionForSubject(this.props.extension, this.props.subject)).pipe(mapTo(null), catchError(error => [asError(error)]), map(c => ({ operationResultOrError: c })), tap(() => this.props.onUpdate()), startWith({ operationResultOrError: LOADING }))))
|
26 | .subscribe(stateUpdate => this.setState(stateUpdate), error => console.error(error)));
|
27 | }
|
28 | componentWillUnmount() {
|
29 | this.subscriptions.unsubscribe();
|
30 | }
|
31 | render() {
|
32 | return (React.createElement("button", { className: `${this.props.className} d-flex align-items-center`, disabled: this.props.disabled || this.state.operationResultOrError === 'loading', onClick: this.onClick },
|
33 | this.props.children,
|
34 | isErrorLike(this.state.operationResultOrError) && (React.createElement("small", { className: "text-danger ml-2", title: this.state.operationResultOrError.message },
|
35 | React.createElement(this.props.extensions.context.icons.Warning, { className: "icon-inline" }),
|
36 | " Error"))));
|
37 | }
|
38 | }
|
39 |
|
\ | No newline at end of file |