Skip to content

Commit 658a1f2

Browse files
committed
Add redirect to newly added snippet
Since we don't have notification about successfully added snippet it was a best choise to redirect to snippet details after submitting. To redirect we need to have id of new snippet which is generates on backend side, thus we need to call redirect after new snippet is created. We reach this just to pass callback to action.
1 parent 4c2bd61 commit 658a1f2

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

src/actions/index.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export const fetchSyntaxes = dispatch => (
3131
.then(json => dispatch(setSyntaxes(json)))
3232
);
3333

34-
export const postSnippet = snippet => dispatch => (
34+
export const postSnippet = (snippet, onSuccess) => dispatch => (
3535
fetch('http://api.xsnippet.org/snippets', {
3636
method: 'POST',
3737
headers: {
@@ -41,5 +41,8 @@ export const postSnippet = snippet => dispatch => (
4141
body: JSON.stringify(snippet),
4242
})
4343
.then(response => response.json())
44-
.then(json => dispatch(setSnippet(json)))
44+
.then((json) => {
45+
dispatch(setSnippet(json));
46+
onSuccess(json);
47+
})
4548
);

src/components/NewSnippet.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ class NewSnippet extends React.Component {
4242

4343
postSnippet(e) {
4444
e.preventDefault();
45-
const { dispatch } = this.props;
46-
dispatch(actions.postSnippet(this.state));
45+
const { dispatch, history } = this.props;
46+
dispatch(actions.postSnippet(this.state, json => history.push(`/${json.id}`)));
4747
}
4848

4949
render() {

0 commit comments

Comments
 (0)