Skip to content

Commit

Permalink
ft(article-edit-delete): enable users edit and delete their articles
Browse files Browse the repository at this point in the history
- add component for article editing
- add modal that appears before article deletion

[finishes #166107267]
  • Loading branch information
patrickf949 committed May 23, 2019
1 parent dc0e8eb commit 733bbfa
Show file tree
Hide file tree
Showing 39 changed files with 448 additions and 710 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"redux": "^4.0.1",
"redux-mock-store": "^1.5.3",
"redux-thunk": "^2.3.0",
"semantic-ui-react": "^0.87.1",
"url-loader": "^1.1.2"
},
"devDependencies": {
Expand Down
8 changes: 5 additions & 3 deletions src/actions/articleCreateEditAction.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,18 @@ export const articleCreateEditAction = (article, url, method, props) => {
return async dispatch => {
try {
const response = await axios({
method: method,
url: url,
method: method,
data: article,
headers: {
Authorization: `Bearer ${sessionStorage.getItem("token")}`
}
});

toast.dismiss();
console.log("this happens");
dispatch(successCreateArticle(response.data));
toast.dismiss();

console.log("tHese are the props", props);
props.history.push("/article/" + response.data.article.slug);
} catch (error) {
if (error.response) {
Expand Down
31 changes: 31 additions & 0 deletions src/actions/deleteArticleAction.js
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
};
};
3 changes: 3 additions & 0 deletions src/actions/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,6 @@ export const CREATE_COMMENTS = "CREATE_COMMENTS";
export const CREATE_COMMENTS_ERRORS = "CREATE_COMMENTS_ERRORS";
export const FETCH_COMMENTS = "FETCH_COMMENTS";
export const FETCH_COMMENTS_ERRORS = "FETCH_COMMENTS_ERRORS";
export const EDIT_ARTICLE_SUCCESS = "EDIT_ARTICLE_SUCCESS";
export const EDIT_ARTICLE_FAIL = "EDIT_ARTICLE_FAIL";
export const DELETE_ARTICLE_SUCCESS = "DELETE_ARTICLE_SUCCESS";
29 changes: 26 additions & 3 deletions src/components/articles/createArticleComponent.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import React from "react";
import "../../styles/createArticles.scss";
import "../../styles/singleArticle.scss";

const CreateArticleComponent = props => {
const { onChange, onSubmit, onUpload, image } = props;
const { onChange, onSubmit, onUpload, image, article } = props;

return (
<div className="container-fluid mt-5">
<div className="row">
Expand All @@ -17,6 +19,7 @@ const CreateArticleComponent = props => {
placeholder="title"
name="title"
id="title"
defaultValue={article ? article.title : ""}
/>
</div>
<div className="form-group">
Expand All @@ -26,6 +29,7 @@ const CreateArticleComponent = props => {
placeholder="description"
name="description"
id="description"
defaultValue={article ? article.description : ""}
/>
</div>
<div className="form-group">
Expand All @@ -35,7 +39,17 @@ const CreateArticleComponent = props => {
placeholder="Body"
name="body"
id="body"
value={article ? article.body : ""}
/>
{/* <div
className="form-control, "
id="body"
name="body"
contentEditable
suppressContentEditableWarning
>
{article ? article.body : ""}
</div> */}
</div>
<div className="form-group">
<input
Expand All @@ -44,14 +58,24 @@ const CreateArticleComponent = props => {
placeholder="tags"
name="tags"
id="tags"
defaultValue={
article
? article.tagList
? article.tagList.join(",")
: ""
: ""
}
/>
<div>
<i>Separate your tags with a comma</i>
</div>
</div>
<div className="image-div">
<div className="actual-image">
<img src={image} className="image-fluid" />
<img
src={image ? image : article ? article.image : " "}
className="image-fluid"
/>
</div>
<input
type="file"
Expand All @@ -61,7 +85,6 @@ const CreateArticleComponent = props => {
/>
</div>

<input name="image" />
<div className="form-group">
<button onClick={onSubmit} className="btn btn-primary">
Publish Story
Expand Down
4 changes: 2 additions & 2 deletions src/components/articles/createArticlePage.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ export class CreateArticlePage extends Component {

/* istanbul ignore next */
onUpload(files) {
const image = URL.createObjectURL(event.target.files[0]);
this.setState({ image });
const image1 = URL.createObjectURL(event.target.files[0]);
this.setState({ image1 });

const task = firebase
.storage()
Expand Down
117 changes: 117 additions & 0 deletions src/components/articles/editArticlePage.js
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);
Loading

0 comments on commit 733bbfa

Please sign in to comment.