accounts-frontend/src/pages/dev/UpdateApplicationPage.tsx

121 lines
2.5 KiB
TypeScript
Raw Normal View History

2019-12-07 16:58:52 +05:30
import React from 'react';
2018-05-05 14:31:25 +05:30
import { connect } from 'react-redux';
import logger from 'services/logger';
2019-12-07 16:58:52 +05:30
import { RouteComponentProps } from 'react-router';
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';
2019-12-07 16:58:52 +05:30
import {
getApp,
fetchApp as fetchAppAction,
} from 'components/dev/apps/actions';
2018-05-05 14:31:25 +05:30
import ApplicationForm from 'components/dev/apps/applicationForm/ApplicationForm';
2019-12-07 16:58:52 +05:30
import { OauthAppResponse } from 'services/api/oauth';
import { RootState } from 'reducers';
2019-12-07 16:58:52 +05:30
type OwnProps = RouteComponentProps<{
clientId: string;
}>;
2019-12-07 16:58:52 +05:30
interface Props extends OwnProps {
app: OauthAppResponse | null;
fetchApp: (app: string) => Promise<void>;
}
2019-12-07 16:58:52 +05:30
class UpdateApplicationPage extends React.Component<
Props,
{
2019-12-07 16:58:52 +05:30
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 />;
}
if (!app) {
// we are loading
return null;
}
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();
}
}
onSubmit = async () => {
const { form } = this;
const { app } = this.props;
2018-05-05 14:31:25 +05:30
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();
this.goToMainPage(result.data.clientId);
};
goToMainPage = (hash?: string) =>
browserHistory.push(`/dev/applications${hash ? `#${hash}` : ''}`);
}
2019-12-07 16:58:52 +05:30
export default connect(
(state: RootState, props: OwnProps) => ({
app: getApp(state, props.match.params.clientId),
}),
{
2019-12-07 16:58:52 +05:30
fetchApp: fetchAppAction,
},
)(UpdateApplicationPage);