// @flow
import type { OauthAppResponse } from 'services/api/oauth';
import React, { Component } from 'react';
import { FormattedMessage as Message } from 'react-intl';
import { Link } from 'react-router-dom';
import classNames from 'classnames';
import { SKIN_LIGHT, COLOR_BLACK, COLOR_RED } from 'components/ui';
import { Input, Button } from 'components/ui/form';
import Collapse from 'components/ui/collapse';
import styles from '../applicationsIndex.scss';
import messages from '../ApplicationsIndex.intl.json';
const ACTION_REVOKE_TOKENS = 'revoke-tokens';
const ACTION_RESET_SECRET = 'reset-secret';
const ACTION_DELETE = 'delete';
const actionButtons = [
{
type: ACTION_REVOKE_TOKENS,
label: messages.revokeAllTokens,
},
{
type: ACTION_RESET_SECRET,
label: messages.resetClientSecret,
},
{
type: ACTION_DELETE,
label: messages.delete,
},
];
export default class ApplicationItem extends Component<
{
application: OauthAppResponse,
expand: boolean,
onTileClick: string => void,
onResetSubmit: (string, boolean) => Promise<*>,
onDeleteSubmit: string => Promise<*>,
},
{
selectedAction: ?string,
isActionPerforming: boolean,
detailsHeight: number,
translateY: number,
},
> {
state = {
selectedAction: null,
isActionPerforming: false,
translateY: 0,
detailsHeight: 0,
};
actionContainer: ?HTMLDivElement;
render() {
const { application: app, expand } = this.props;
const { selectedAction, translateY } = this.state;
return (
{app.name}
Client ID: {app.clientId}
{typeof app.countUsers !== 'undefined' && (
{' | '}
)}
);
}
getActionContent() {
const { selectedAction, isActionPerforming } = this.state;
switch (selectedAction) {
case ACTION_REVOKE_TOKENS:
case ACTION_RESET_SECRET:
return (
{' '}
{isActionPerforming ? (
) : (
)}
);
case ACTION_DELETE:
return (
{' '}
{isActionPerforming ? (
) : (
)}
);
default:
return null;
}
}
setActiveAction = (type: ?string) => {
const { actionContainer } = this;
if (!actionContainer) {
return;
}
this.setState(
{
selectedAction: type,
},
() => {
const translateY = actionContainer.offsetHeight;
this.setState({ translateY });
},
);
};
onTileToggle = () => {
const { onTileClick, application } = this.props;
onTileClick(application.clientId);
};
onCollapseRest = () => {
if (!this.props.expand && this.state.selectedAction) {
this.setActiveAction(null);
}
};
onActionButtonClick = (type: ?string) => () => {
this.setActiveAction(type === this.state.selectedAction ? null : type);
};
onResetSubmit = (resetClientSecret: boolean) => async () => {
const { onResetSubmit, application } = this.props;
this.setState({
isActionPerforming: true,
});
await onResetSubmit(application.clientId, resetClientSecret);
this.setState({
isActionPerforming: false,
});
this.setActiveAction(null);
};
onSubmitDelete = () => {
const { onDeleteSubmit, application } = this.props;
this.setState({
isActionPerforming: true,
});
onDeleteSubmit(application.clientId);
};
}