Skip to content

Commit

Permalink
Rewrite IntegrationButton as functional component
Browse files Browse the repository at this point in the history
Part of #72
  • Loading branch information
s0 committed Feb 11, 2023
1 parent 849d29b commit f4d41c3
Showing 1 changed file with 39 additions and 52 deletions.
91 changes: 39 additions & 52 deletions composer/src/scripts/ts/components/integration-button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,68 +13,55 @@ interface Props {
integration: IntegrationSource;
}

interface State {
state: ConnectionState;
}

class IntegrationButton extends React.Component<Props, State> {
public constructor(props: Props) {
super(props);
this.state = {
state: 'not_connected',
const IntegrationButton: React.FunctionComponent<Props> = ({
className,
integration,
settings,
}) => {
const [state, setState] = React.useState<ConnectionState>('not_connected');

React.useEffect(() => {
integration.addListener('state', setState);

return () => {
integration.removeListener('state', setState);
};
}

public componentDidMount() {
this.props.integration.addListener('state', (state) => {
this.setState({ state });
});
}
}, []);

private onClick = () => {
switch (this.state.state) {
const onClick = () => {
switch (state) {
case 'not_connected':
case 'error':
this.connect();
integration.connect();
break;
case 'connected':
this.disconnect();
integration.disconnect();
break;
}
};

private connect() {
this.props.integration.connect();
}

private disconnect() {
this.props.integration.disconnect();
}

public render() {
const buttonText = (() => {
switch (this.state.state) {
case 'not_connected':
return `Connect to ${this.props.settings.name}`;
case 'connecting':
return 'Connecting...';
case 'connected':
return `Connected to ${this.props.settings.name}`;
case 'error':
return 'An error ocurred';
}
})();

return (
<div className={this.props.className}>
<button className="connection-button" onClick={this.onClick}>
{buttonText}
<span className={`indicator ${this.state.state}`} />
</button>
</div>
);
}
}
const buttonText = (() => {
switch (state) {
case 'not_connected':
return `Connect to ${settings.name}`;
case 'connecting':
return 'Connecting...';
case 'connected':
return `Connected to ${settings.name}`;
case 'error':
return 'An error ocurred';
}
})();

return (
<div className={className}>
<button className="connection-button" onClick={onClick}>
{buttonText}
<span className={`indicator ${state}`} />
</button>
</div>
);
};

const StyledIntegrationButton = styled(IntegrationButton)`
display: flex;
Expand Down

0 comments on commit f4d41c3

Please sign in to comment.