This repository has been archived on 2022-08-14. You can view files and clone it, but cannot push or open issues or pull requests.
outline/frontend/scenes/CollectionNew/CollectionNew.js

73 lines
1.9 KiB
JavaScript
Raw Normal View History

2017-07-09 17:27:29 +00:00
// @flow
import React, { Component } from 'react';
import { observer } from 'mobx-react';
import Button from 'components/Button';
import Input from 'components/Input';
2017-07-10 03:02:10 +00:00
import HelpText from 'components/HelpText';
2017-07-09 17:27:29 +00:00
import Collection from 'models/Collection';
2017-07-10 03:02:10 +00:00
import CollectionsStore from 'stores/CollectionsStore';
2017-07-09 17:27:29 +00:00
@observer class CollectionNew extends Component {
2017-07-10 03:02:10 +00:00
props: {
history: Object,
collection: Collection,
collections: CollectionsStore,
onCollectionCreated: () => void,
};
state: { name: string, isSaving: boolean };
state = { name: '', isSaving: false };
2017-07-09 17:27:29 +00:00
static defaultProps = {
collection: new Collection(),
};
handleSubmit = async (ev: SyntheticEvent) => {
ev.preventDefault();
2017-07-10 03:02:10 +00:00
this.setState({ isSaving: true });
const { collection, collections } = this.props;
2017-07-09 17:27:29 +00:00
2017-07-10 03:02:10 +00:00
collection.updateData(this.state);
await collection.save();
collections.add(collection);
this.setState({ isSaving: false });
this.props.onCollectionCreated();
this.props.history.push(collection.url);
2017-07-09 17:27:29 +00:00
};
2017-07-10 03:02:10 +00:00
handleNameChange = (ev: SyntheticInputEvent) => {
this.setState({ name: ev.target.value });
2017-07-09 17:27:29 +00:00
};
render() {
const { collection } = this.props;
return (
<form onSubmit={this.handleSubmit}>
{collection.errors.errors.map(error => <span>{error}</span>)}
2017-07-10 03:02:10 +00:00
<HelpText>
Collections are for grouping your Atlas. They work best when organized
around a topic or internal team Product or Engineering for example.
</HelpText>
2017-07-09 17:27:29 +00:00
<Input
type="text"
2017-07-10 03:02:10 +00:00
label="Name"
2017-07-09 17:27:29 +00:00
onChange={this.handleNameChange}
2017-07-10 03:02:10 +00:00
value={this.state.name}
required
2017-07-09 17:27:29 +00:00
autoFocus
/>
2017-07-10 03:02:10 +00:00
<Button
type="submit"
disabled={this.state.isSaving || !this.state.name}
>
{this.state.isSaving ? 'Creating…' : 'Create'}
2017-07-09 17:27:29 +00:00
</Button>
</form>
);
}
}
export default CollectionNew;