accounts-frontend/src/pages/dev/CreateNewApplicationPage.js

68 lines
1.7 KiB
JavaScript
Raw Normal View History

// @flow
2018-05-05 12:01:25 +03:00
import type {OauthAppResponse} from 'services/api/oauth';
import type { ApplicationType } from 'components/dev/apps';
import React, { Component } from 'react';
import { FormModel } from 'components/ui/form';
import ApplicationForm from 'components/dev/apps/applicationForm/ApplicationForm';
import oauth from 'services/api/oauth';
import { browserHistory } from 'services/history';
const app: OauthAppResponse = {
clientId: '',
clientSecret: '',
countUsers: 0,
createdAt: 0,
2018-05-05 12:01:25 +03:00
type: 'application',
name: '',
description: '',
websiteUrl: '',
redirectUri: '',
minecraftServerIp: '',
};
export default class CreateNewApplicationPage extends Component<{}, {
2018-05-05 12:01:25 +03:00
type: ?ApplicationType,
}> {
state = {
type: null,
};
2018-05-05 12:01:25 +03:00
form: FormModel = new FormModel();
render() {
return (
<ApplicationForm
form={this.form}
displayTypeSwitcher
onSubmit={this.onSubmit}
type={this.state.type}
setType={this.setType}
app={app}
/>
);
}
onSubmit = async () => {
const { form } = this;
const { type } = this.state;
2018-05-05 12:01:25 +03:00
if (!type) {
throw new Error('Form was submitted without specified type');
}
form.beginLoading();
const result = await oauth.create(type, form.serialize());
form.endLoading();
this.goToMainPage(result.data.clientId);
};
2018-05-05 12:01:25 +03:00
setType = (type: ApplicationType) => {
this.setState({
type,
});
};
goToMainPage = (hash?: string) => browserHistory.push(`/dev/applications${hash ? `#${hash}` : ''}`);
}