diff --git a/.eslintrc.json b/.eslintrc.json index f20c3f5..02c846a 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -8,6 +8,7 @@ "components": ["Link"], "specialLink": ["to"] }], - "react/prop-types": [0] + "react/prop-types": [0], + "jsx-a11y/click-events-have-key-events": [0] } } diff --git a/src/actions/index.js b/src/actions/index.js index d1faee1..5a95e6a 100644 --- a/src/actions/index.js +++ b/src/actions/index.js @@ -30,3 +30,16 @@ export const fetchSyntaxes = dispatch => ( .then(response => response.json()) .then(json => dispatch(setSyntaxes(json))) ); + +export const postSnippet = snippet => dispatch => ( + fetch('http://api.xsnippet.org/snippets', { + method: 'POST', + headers: { + Accept: 'application/json', + 'Content-Type': 'application/json', + }, + body: JSON.stringify(snippet), + }) + .then(response => response.json()) + .then(json => dispatch(setSnippet(json))) +); diff --git a/src/components/NewSnippet.jsx b/src/components/NewSnippet.jsx index 0ea9b6d..3544f40 100644 --- a/src/components/NewSnippet.jsx +++ b/src/components/NewSnippet.jsx @@ -1,4 +1,5 @@ import React from 'react'; +import { connect } from 'react-redux'; import { Controlled as CodeMirror } from 'react-codemirror2'; import 'codemirror/lib/codemirror.css'; @@ -6,44 +7,83 @@ import 'codemirror/lib/codemirror.css'; import Title from './common/Title'; import Input from './common/Input'; import Syntaxes from './Syntaxes'; +import * as actions from '../actions'; import '../styles/NewSnippet.styl'; class NewSnippet extends React.Component { - constructor() { - super(); - this.state = { code: '' }; + constructor(props) { + super(props); + this.state = { + content: '', + title: '', + tags: [], + syntax: '', // eslint-disable-line react/no-unused-state + }; + this.postSnippet = this.postSnippet.bind(this); + this.onSyntaxClick = this.onSyntaxClick.bind(this); + this.onInputChange = this.onInputChange.bind(this); } + + onSyntaxClick(syntax) { + this.setState({ syntax }); // eslint-disable-line react/no-unused-state + } + + onInputChange(e) { + const { name } = e.target; + let { value } = e.target; + + if (name === 'tags') { + value = value.split(',').map(item => item.trim()); + } + + this.setState({ [name]: value }); + } + + postSnippet(e) { + e.preventDefault(); + const { dispatch } = this.props; + dispatch(actions.postSnippet(this.state)); + } + render() { return ( [