Skip to content

Commit f8dec55

Browse files
committed
Make Language Bar on New Snippet page fetch actual languages
The very basics functionality were implement, now language bar recieves actual list of languages and represents them to user
1 parent c2bc0ae commit f8dec55

File tree

6 files changed

+96
-49
lines changed

6 files changed

+96
-49
lines changed

src/actions/index.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,14 @@ export const fetchSnippet = id => dispatch => (
1919
.then(response => response.json())
2020
.then(json => dispatch(setSnippet(json)))
2121
);
22+
23+
export const setSyntaxes = syntaxes => ({
24+
type: 'SET_SYNTAXES',
25+
syntaxes,
26+
});
27+
28+
export const fetchSyntaxes = dispatch => (
29+
fetch('http://api.xsnippet.org/syntaxes')
30+
.then(response => response.json())
31+
.then(json => dispatch(setSyntaxes(json)))
32+
);

src/components/NewSnippet.jsx

Lines changed: 36 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,49 @@
11
import React from 'react';
2-
import { Scrollbars } from 'react-custom-scrollbars';
32
import { Controlled as CodeMirror } from 'react-codemirror2';
43

54
import 'codemirror/lib/codemirror.css';
65

76
import Title from './common/Title';
87
import Input from './common/Input';
8+
import Syntaxes from './Syntaxes';
99

1010
import '../styles/NewSnippet.styl';
1111

12-
const NewSnippet = () => (
13-
[
14-
<Title title="New snippet" key="title" />,
15-
<div className="new-snippet" key="new-snippet">
16-
<div className="new-snippet-code-wrapper">
17-
<div className="new-snippet-code-header">
18-
<Input placeholder="Title" />
19-
<Input placeholder="Tags (separate tags by comma)" />
20-
</div>
21-
<div className="new-snippet-code">
22-
<CodeMirror
23-
value="console.log('All hail XSnippet')"
24-
options={{ lineNumbers: false }}
25-
/>
26-
<div className="new-snippet-code-bottom-bar">
27-
<button>POST</button>
12+
class NewSnippet extends React.Component {
13+
constructor() {
14+
super();
15+
this.state = { code: '' };
16+
}
17+
render() {
18+
return (
19+
[
20+
<Title title="New snippet" key="New Snippet Title" />,
21+
<div className="new-snippet" key="New Snippet">
22+
<div className="new-snippet-code-wrapper">
23+
<div className="new-snippet-code-header">
24+
<Input placeholder="Title" />
25+
<Input placeholder="Tags (separate tags by comma)" />
26+
</div>
27+
<div className="new-snippet-code">
28+
<CodeMirror
29+
value={this.state.code}
30+
options={{ lineNumbers: false }}
31+
onBeforeChange={(editor, data, code) => {
32+
this.setState({ code });
33+
}}
34+
/>
35+
<div className="new-snippet-code-bottom-bar">
36+
<button>POST</button>
37+
</div>
38+
</div>
2839
</div>
29-
</div>
30-
</div>
31-
<div className="new-snippet-lang-wrapper">
32-
<div className="new-snippet-lang-header">
33-
<Input placeholder="Type to search..." />
34-
</div>
35-
<div className="new-snippet-lang-list-wrapper">
36-
<Scrollbars>
37-
<ul className="new-snippet-lang-list">
38-
<li className="new-snippet-lang-item">Text1</li>
39-
<li className="new-snippet-lang-item">Text2</li>
40-
<li className="new-snippet-lang-item">Text3</li>
41-
<li className="new-snippet-lang-item">Text4</li>
42-
<li className="new-snippet-lang-item">Text5</li>
43-
<li className="new-snippet-lang-item">Text6</li>
44-
<li className="new-snippet-lang-item">Text7</li>
45-
<li className="new-snippet-lang-item">Text8</li>
46-
<li className="new-snippet-lang-item">Text9</li>
47-
<li className="new-snippet-lang-item">Text10</li>
48-
<li className="new-snippet-lang-item">Text11</li>
49-
<li className="new-snippet-lang-item">Text12</li>
50-
<li className="new-snippet-lang-item">Text13</li>
51-
<li className="new-snippet-lang-item">Text14</li>
52-
</ul>
53-
</Scrollbars>
54-
</div>
55-
</div>
56-
</div>,
57-
]
58-
);
40+
<div className="new-snippet-lang-wrapper">
41+
<Syntaxes />
42+
</div>
43+
</div>,
44+
]
45+
);
46+
}
47+
}
5948

6049
export default NewSnippet;

src/components/Syntaxes.jsx

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import React from 'react';
2+
import { Scrollbars } from 'react-custom-scrollbars';
3+
import { connect } from 'react-redux';
4+
5+
import Input from './common/Input';
6+
import * as actions from '../actions';
7+
8+
class Syntaxes extends React.Component {
9+
componentDidMount() {
10+
const { dispatch } = this.props;
11+
dispatch(actions.fetchSyntaxes);
12+
}
13+
14+
render() {
15+
const { syntaxes } = this.props;
16+
17+
return (
18+
[
19+
<div className="new-snippet-lang-header">
20+
<Input placeholder="Type to search..." />
21+
</div>,
22+
<div className="new-snippet-lang-list-wrapper">
23+
<Scrollbars>
24+
<ul className="new-snippet-lang-list">
25+
{syntaxes.map(syntax => <li className="new-snippet-lang-item">{syntax}</li>)}
26+
</ul>
27+
</Scrollbars>
28+
</div>,
29+
]
30+
);
31+
}
32+
}
33+
34+
export default connect(state => ({
35+
syntaxes: state.syntaxes,
36+
}))(Syntaxes);

src/reducers/index.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,18 @@ const recent = (state = List(), action) => {
2424
}
2525
};
2626

27+
const syntaxes = (state = List(), action) => {
28+
switch (action.type) {
29+
case 'SET_SYNTAXES':
30+
return List(action.syntaxes);
31+
32+
default:
33+
return state;
34+
}
35+
};
36+
2737
export default combineReducers({
2838
snippets,
2939
recent,
40+
syntaxes,
3041
});

src/styles/NewSnippet.styl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ lang-bar-width = 230px
55

66
.new-snippet
77
display: flex
8-
height: 75vh
8+
min-height: 75vh
99
background-color: snippet-content-light
1010
box-shadow: 0 2px 2px 0 border-dark
1111
&-code,

src/styles/common/overwrite.styl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,5 @@
1616
.CodeMirror-gutters
1717
border-right: none
1818

19-
.CodeMirror-line
19+
.snippet .CodeMirror-line
2020
padding-left: 20px !important

0 commit comments

Comments
 (0)