OAuthEditPage.tsx 3.39 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
import * as React from 'react';
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
import { translate } from 'react-i18next';
import * as _ from 'lodash';
// Actions
import { deleteOAuthClient, updateOAuthClient, loadOAuthClientDetails, receiveOAuthClient } from 'user/actions/admin';
import navigateTo from 'actions/navigation';
// Models
import OAuthClient from 'model/oauth/OAuthClient';
import ClientDetails from 'model/oauth/ClientDetails';
// UI
import Loading from 'ui/common/Loading';
import Card, { CardContent } from 'ui/common/Card';
import ContentHeaderWithButton from 'ui/common/heading/ContentHeaderWithButton';
import { PageContents } from 'ui/layout/PageLayout';
import OAuthClientForm from './c/OAuthClientForm';
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
18
import ApiCall from 'model/ApiCall';
19 20 21 22 23 24 25 26

interface IEditPageProps extends React.ClassAttributes<any> {
  t: any;
  oAuthClient: ClientDetails;
  clientId: string;
  deleteClient: (clientId: string) => void;
  loadOAuthClientDetails: (uuid: string) => void;
  navigateTo: (path: string) => void;
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
27
  receiveOAuthClient: (oAuthClient: ApiCall<OAuthClient>) => void;
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
  updateOAuthClient: (oAuthClient: OAuthClient) => void;
}

class EditOAuthClientPage extends React.Component<IEditPageProps, any> {

  protected static needs = [
    ({ params: { clientId } }) => clientId && loadOAuthClientDetails(clientId),
  ];

  constructor(props: IEditPageProps, context: any) {
    super(props, context);
  }

  public componentWillMount() {
    const { clientId, oAuthClient, loadOAuthClientDetails, receiveOAuthClient } = this.props;
    if (clientId && (!oAuthClient || (oAuthClient.clientDetails && clientId !== oAuthClient.clientDetails.clientId))) {
      loadOAuthClientDetails(clientId);
    }
    if (!clientId) {
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
47
      receiveOAuthClient(ApiCall.success(new OAuthClient()));
48 49 50 51 52 53
    }
  }

  public render() {
    const { oAuthClient, t } = this.props;

Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
54
    const clientDetails = oAuthClient && oAuthClient.clientDetails ?  oAuthClient.clientDetails : new OAuthClient();
55 56 57

    return  clientDetails === null ? (<Loading/>) : (
      <div>
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
58
        <ContentHeaderWithButton title={ clientDetails && clientDetails.title || t('user.admin.p.oAuthEdit.createNew') }/>
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
        <PageContents className="pt-1rem">
          <Card>
            <CardContent>
              <OAuthClientForm initialValues={ clientDetails } onCancel={ this.handleCancel } onSubmit={ this.handleSubmit }/>
            </CardContent>
          </Card>
        </PageContents>
      </div>
    );
  }

  private handleCancel = () => {
    const { clientId, navigateTo } = this.props;
    navigateTo(`/admin/oauth/${ clientId || '' }`);
  }

  private handleSubmit = (updatedClient: OAuthClient) => {
    const { oAuthClient, clientId, navigateTo, updateOAuthClient } = this.props;
    if (!_.isEqual({ ...updatedClient }, { ...oAuthClient })) {
      updateOAuthClient({...updatedClient, authorities: null});
    } else {
      navigateTo(`/admin/oauth/${ clientId || '' }`);
    }
  }
}

const mapStateToProps = (state, ownProps) => ({
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
86
  oAuthClient: state.user.admin.oAuthClient ? state.user.admin.oAuthClient.data : undefined,
87 88 89 90 91 92 93 94 95 96 97 98 99
  clientId: ownProps.match.params.clientId,
});

const mapDispatchToProps = (dispatch) => bindActionCreators({
  receiveOAuthClient,
  loadOAuthClientDetails,
  deleteOAuthClient,
  navigateTo,
  updateOAuthClient,
}, dispatch);


export default connect(mapStateToProps, mapDispatchToProps)((translate()(EditOAuthClientPage)));