2019-12-07 16:58:52 +05:30
|
|
|
import React from 'react';
|
|
|
|
import { RouteComponentProps } from 'react-router';
|
2020-07-22 15:31:12 +05:30
|
|
|
|
|
|
|
import logger from 'app/services/logger';
|
|
|
|
import { connect } from 'app/functions';
|
2019-12-08 00:32:00 +05:30
|
|
|
import { FormModel } from 'app/components/ui/form';
|
|
|
|
import { browserHistory } from 'app/services/history';
|
|
|
|
import oauth from 'app/services/api/oauth';
|
2020-01-18 02:07:52 +05:30
|
|
|
import * as loader from 'app/services/loader';
|
2019-12-08 00:32:00 +05:30
|
|
|
import PageNotFound from 'app/pages/404/PageNotFound';
|
2020-05-24 04:38:24 +05:30
|
|
|
import { getApp, fetchApp as fetchAppAction } from 'app/components/dev/apps/actions';
|
2019-12-08 00:32:00 +05:30
|
|
|
import ApplicationForm from 'app/components/dev/apps/applicationForm/ApplicationForm';
|
|
|
|
import { OauthAppResponse } from 'app/services/api/oauth';
|
2018-03-26 00:46:45 +05:30
|
|
|
|
2019-12-07 16:58:52 +05:30
|
|
|
type OwnProps = RouteComponentProps<{
|
2020-05-24 04:38:24 +05:30
|
|
|
clientId: string;
|
2019-12-07 16:58:52 +05:30
|
|
|
}>;
|
2018-03-26 00:46:45 +05:30
|
|
|
|
2019-12-07 16:58:52 +05:30
|
|
|
interface Props extends OwnProps {
|
2020-05-24 04:38:24 +05:30
|
|
|
app: OauthAppResponse | null;
|
|
|
|
fetchApp: (app: string) => Promise<void>;
|
2019-12-07 16:58:52 +05:30
|
|
|
}
|
2019-11-27 14:33:32 +05:30
|
|
|
|
2019-12-07 16:58:52 +05:30
|
|
|
class UpdateApplicationPage extends React.Component<
|
2020-05-24 04:38:24 +05:30
|
|
|
Props,
|
|
|
|
{
|
|
|
|
isNotFound: boolean;
|
|
|
|
}
|
2019-11-27 14:33:32 +05:30
|
|
|
> {
|
2020-05-24 04:38:24 +05:30
|
|
|
form: FormModel = new FormModel();
|
2019-11-27 14:33:32 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
state = {
|
|
|
|
isNotFound: false,
|
|
|
|
};
|
2019-11-27 14:33:32 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
componentDidMount() {
|
|
|
|
this.props.app === null && this.fetchApp();
|
|
|
|
}
|
2019-11-27 14:33:32 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
render() {
|
|
|
|
const { app } = this.props;
|
2019-11-27 14:33:32 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
if (this.state.isNotFound) {
|
|
|
|
return <PageNotFound />;
|
|
|
|
}
|
2018-03-26 00:46:45 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
if (!app) {
|
|
|
|
// we are loading
|
|
|
|
return null;
|
|
|
|
}
|
2018-03-26 00:46:45 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
return <ApplicationForm form={this.form} onSubmit={this.onSubmit} app={app} type={app.type} />;
|
2018-03-26 00:46:45 +05:30
|
|
|
}
|
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
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();
|
2018-05-05 14:31:25 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (status === 404) {
|
|
|
|
this.setState({
|
|
|
|
isNotFound: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.unexpected('Error fetching app', resp);
|
|
|
|
} finally {
|
|
|
|
loader.hide();
|
|
|
|
}
|
2019-11-27 14:33:32 +05:30
|
|
|
}
|
2018-03-26 00:46:45 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
onSubmit = async () => {
|
|
|
|
const { form } = this;
|
|
|
|
const { app } = this.props;
|
|
|
|
|
|
|
|
if (!app || !app.clientId) {
|
|
|
|
throw new Error('Form has an invalid state');
|
|
|
|
}
|
|
|
|
|
|
|
|
form.beginLoading();
|
|
|
|
const result = await oauth.update(app.clientId, form.serialize());
|
|
|
|
form.endLoading();
|
2018-03-26 00:46:45 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
this.goToMainPage(result.data.clientId);
|
|
|
|
};
|
2018-03-26 00:46:45 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
goToMainPage = (hash?: string) => browserHistory.push(`/dev/applications${hash ? `#${hash}` : ''}`);
|
2018-03-26 00:46:45 +05:30
|
|
|
}
|
|
|
|
|
2019-12-07 16:58:52 +05:30
|
|
|
export default connect(
|
2020-07-22 15:31:12 +05:30
|
|
|
(state, props: OwnProps) => ({
|
2020-05-24 04:38:24 +05:30
|
|
|
app: getApp(state, props.match.params.clientId),
|
|
|
|
}),
|
|
|
|
{
|
|
|
|
fetchApp: fetchAppAction,
|
|
|
|
},
|
2019-11-27 14:33:32 +05:30
|
|
|
)(UpdateApplicationPage);
|