2018-03-26 00:46:45 +05:30
|
|
|
// @flow
|
2018-05-05 14:31:25 +05:30
|
|
|
import type { OauthAppResponse } from 'services/api/oauth';
|
2018-03-26 00:46:45 +05:30
|
|
|
import React, { Component } from 'react';
|
2018-05-05 14:31:25 +05:30
|
|
|
import { connect } from 'react-redux';
|
|
|
|
import logger from 'services/logger';
|
2018-03-26 00:46:45 +05:30
|
|
|
import { FormModel } from 'components/ui/form';
|
|
|
|
import { browserHistory } from 'services/history';
|
|
|
|
import oauth from 'services/api/oauth';
|
|
|
|
import loader from 'services/loader';
|
|
|
|
import PageNotFound from 'pages/404/PageNotFound';
|
2018-05-05 14:31:25 +05:30
|
|
|
import { getApp, fetchApp } from 'components/dev/apps/actions';
|
|
|
|
import ApplicationForm from 'components/dev/apps/applicationForm/ApplicationForm';
|
2018-03-26 00:46:45 +05:30
|
|
|
|
2019-06-30 19:02:50 +05:30
|
|
|
type OwnProps = {|
|
2019-11-27 14:33:32 +05:30
|
|
|
match: {
|
|
|
|
params: {
|
|
|
|
clientId: string,
|
2018-03-26 00:46:45 +05:30
|
|
|
},
|
2019-11-27 14:33:32 +05:30
|
|
|
},
|
|
|
|
|};
|
2018-03-26 00:46:45 +05:30
|
|
|
|
2019-06-30 19:02:50 +05:30
|
|
|
type Props = {
|
2019-11-27 14:33:32 +05:30
|
|
|
...OwnProps,
|
|
|
|
app: ?OauthAppResponse,
|
|
|
|
fetchApp: string => Promise<void>,
|
|
|
|
};
|
|
|
|
|
|
|
|
class UpdateApplicationPage extends Component<
|
|
|
|
Props,
|
|
|
|
{
|
|
|
|
isNotFound: boolean,
|
|
|
|
},
|
|
|
|
> {
|
|
|
|
form: FormModel = new FormModel();
|
|
|
|
|
|
|
|
state = {
|
|
|
|
isNotFound: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
this.props.app === null && this.fetchApp();
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const { app } = this.props;
|
|
|
|
|
|
|
|
if (this.state.isNotFound) {
|
|
|
|
return <PageNotFound />;
|
2018-03-26 00:46:45 +05:30
|
|
|
}
|
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
if (!app) {
|
|
|
|
// we are loading
|
|
|
|
return null;
|
2018-03-26 00:46:45 +05:30
|
|
|
}
|
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
return (
|
|
|
|
<ApplicationForm
|
|
|
|
form={this.form}
|
|
|
|
onSubmit={this.onSubmit}
|
|
|
|
app={app}
|
|
|
|
type={app.type}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
async fetchApp() {
|
|
|
|
const { fetchApp, match } = this.props;
|
|
|
|
|
|
|
|
try {
|
|
|
|
loader.show();
|
|
|
|
await fetchApp(match.params.clientId);
|
|
|
|
} catch (resp) {
|
|
|
|
const { status } = resp.originalResponse;
|
|
|
|
|
|
|
|
if (status === 403) {
|
|
|
|
this.goToMainPage();
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (status === 404) {
|
|
|
|
this.setState({
|
|
|
|
isNotFound: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.unexpected('Error fetching app', resp);
|
|
|
|
} finally {
|
|
|
|
loader.hide();
|
2018-03-26 00:46:45 +05:30
|
|
|
}
|
2019-11-27 14:33:32 +05:30
|
|
|
}
|
2018-03-26 00:46:45 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
onSubmit = async () => {
|
|
|
|
const { form } = this;
|
|
|
|
const { app } = this.props;
|
2018-05-05 14:31:25 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
if (!app || !app.clientId) {
|
|
|
|
throw new Error('Form has an invalid state');
|
|
|
|
}
|
2018-03-26 00:46:45 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
form.beginLoading();
|
|
|
|
const result = await oauth.update(app.clientId, form.serialize());
|
|
|
|
form.endLoading();
|
2018-03-26 00:46:45 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
this.goToMainPage(result.data.clientId);
|
|
|
|
};
|
2018-03-26 00:46:45 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
goToMainPage = (hash?: string) =>
|
|
|
|
browserHistory.push(`/dev/applications${hash ? `#${hash}` : ''}`);
|
2018-03-26 00:46:45 +05:30
|
|
|
}
|
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
export default connect<Props, OwnProps, _, _, _, _>(
|
|
|
|
(state, props) => ({
|
2018-03-26 00:46:45 +05:30
|
|
|
app: getApp(state, props.match.params.clientId),
|
2019-11-27 14:33:32 +05:30
|
|
|
}),
|
|
|
|
{
|
2018-03-26 00:46:45 +05:30
|
|
|
fetchApp,
|
2019-11-27 14:33:32 +05:30
|
|
|
},
|
|
|
|
)(UpdateApplicationPage);
|