-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ft(article-edit-delete): enable users edit and delete their articles
- add component for article editing - add modal that appears before article deletion [finishes #166107267]
- Loading branch information
1 parent
dc0e8eb
commit 733bbfa
Showing
39 changed files
with
448 additions
and
710 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import axios from "axios"; | ||
import { DELETE_ARTICLE_SUCCESS } from "./types"; | ||
import { toast } from "react-toastify"; | ||
|
||
export const deleteArticleAction = (slug, props) => dispatch => { | ||
return axios({ | ||
url: | ||
"https://ah-backend-prime-staging.herokuapp.com/api/v1/articles/" + slug, | ||
method: "delete", | ||
headers: { | ||
Authorization: `Bearer ${sessionStorage.getItem("token")}` | ||
} | ||
}).then(response => { | ||
toast.success(response.data.message, { | ||
position: toast.POSITION.TOP_RIGHT, | ||
autoClose: 2000, | ||
hideProgressBar: false, | ||
onClose: () => props.history.go(0) | ||
}); | ||
dispatch(deleteArticleSuccess(response.data)); | ||
|
||
// setTimeout(props.history.go(0), 5000); | ||
}); | ||
}; | ||
|
||
const deleteArticleSuccess = payload => { | ||
return { | ||
type: DELETE_ARTICLE_SUCCESS, | ||
payload: payload | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
import React, { Component } from "react"; | ||
import { connect } from "react-redux"; | ||
import "../../styles/app.scss"; | ||
import { articleCreateEditAction } from "../../actions/articleCreateEditAction"; | ||
import { getArticleAction } from "../../actions/getArticle"; | ||
import CreateArticleComponent from "./createArticleComponent"; | ||
import firebase from "../../firebase/config"; | ||
import { TextArea } from "semantic-ui-react"; | ||
|
||
export class EditArticlePage extends Component { | ||
constructor(props) { | ||
super(props); | ||
this.state = { | ||
title: "", | ||
body: "", | ||
description: "", | ||
tags: "", | ||
image: "", | ||
isUploaded: false | ||
}; | ||
this.onChange = this.onChange.bind(this); | ||
this.onSubmit = this.onSubmit.bind(this); | ||
this.onUpload = this.onUpload.bind(this); | ||
} | ||
componentWillMount() { | ||
this.props.getArticleAction(this.props.match.params.slug); | ||
} | ||
|
||
componentWillReceiveProps(newP) { | ||
// const { article } = newP.props; | ||
if (newP) { | ||
this.setState({ | ||
title: newP.article.article.title, | ||
description: newP.article.article.description, | ||
body: newP.article.article.body, | ||
image: newP.article.article.image, | ||
tags: newP.article.tagList, | ||
article: newP.article.article | ||
}); | ||
} | ||
// console.log("article ", this.state.title); | ||
} | ||
|
||
/* istanbul ignore next */ | ||
onUpload(files) { | ||
const image1 = URL.createObjectURL(event.target.files[0]); | ||
this.setState({ image1 }); | ||
|
||
const task = firebase | ||
.storage() | ||
.ref(`images/${files[0].name}`) | ||
.put(files[0]); | ||
|
||
task.then(res => { | ||
firebase | ||
.storage() | ||
.ref(`images/${files[0].name}`) | ||
.getDownloadURL() | ||
.then(url => { | ||
this.setState({ isUploaded: true, image: url }); | ||
}); | ||
}); | ||
|
||
task.on("state_changed", snapshot => { | ||
const isUploaded = true; | ||
this.setState({ isUploaded: isUploaded }); | ||
}); | ||
} | ||
|
||
onChange(e) { | ||
this.setState({ [e.target.name]: e.target.value }); | ||
} | ||
|
||
onSubmit(e) { | ||
e.preventDefault(); | ||
const { title, body, description, tags, image } = this.state; | ||
const messageObject = { | ||
title: title, | ||
body: body, | ||
description: description, | ||
tags: tags ? tags.split(",") : "", | ||
image: image | ||
}; | ||
const url = | ||
"https://ah-backend-prime-staging.herokuapp.com/api/v1/articles/" + | ||
this.props.match.params.slug + | ||
"/"; | ||
console.log("this is the url", url); | ||
this.props.articleCreateEditAction(messageObject, url, "put", this.props); | ||
} | ||
|
||
render() { | ||
const { image, article } = this.state; | ||
console.log("the body", this.state.body); | ||
|
||
return ( | ||
<div> | ||
<CreateArticleComponent | ||
onChange={this.onChange} | ||
onUpload={this.onUpload} | ||
onSubmit={this.onSubmit} | ||
image={image} | ||
article={article ? article : null} | ||
/> | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
export const mapStateToProps = state => ({ | ||
article: state.getArticleReducer.article | ||
}); | ||
|
||
export default connect( | ||
mapStateToProps, | ||
{ getArticleAction, articleCreateEditAction } | ||
)(EditArticlePage); |
Oops, something went wrong.