Skip to content

Commit

Permalink
Merge 59afae3 into 51cadd7
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickf949 committed May 23, 2019
2 parents 51cadd7 + 59afae3 commit 40ea02b
Show file tree
Hide file tree
Showing 13 changed files with 395 additions and 47 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
22 changes: 22 additions & 0 deletions src/actions/deleteArticle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import axios from "axios";
import { DELETE_ARTICLE_SUCCESS } from "./types";

export const getArticleAction = slug => 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 => {
dispatch(deleteArticleSuccess(response.data));
});
};

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 @@ -42,3 +42,6 @@ export const LIKE_ARTICLE_SUCCESS = "LIKE_ARTICLE_SUCCESS";
export const LIKE_ARTICLE_FAILURE = "LIKE_ARTICLE_FAILURE";
export const DISLIKE_ARTICLE_FAILURE = "DISLIKE_ARTICLE_FAILURE";
export const DISLIKE_ARTICLE_SUCCESS = "DISLIKE_ARTICLE_SUCCESS";
export const EDIT_ARTICLE_SUCCESS = "EDIT_ARTICLE_SUCCESS";
export const EDIT_ARTICLE_FAIL = "EDIT_ARTICLE_FAIL";
export const DELETE_ARTICLE_SUCCESS = "DELETE_ARTICLE_SUCCESS";
38 changes: 34 additions & 4 deletions src/components/articles/createArticleComponent.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
import React from "react";
import React, { useRef } 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;
// const inputRef = useRef();
// if (inputRef.current) {
// inputRef.current.readOnly = false;
// // inputRef.current.readOnly = article ? article.description : "";
// console.log(inputRef);
// }
// document.getElementById("body").innerHTML= article ? article.body : "";
return (
<div className="container-fluid mt-5">
<div className="row">
Expand All @@ -17,6 +25,7 @@ const CreateArticleComponent = props => {
placeholder="title"
name="title"
id="title"
defaultValue={article ? article.title : ""}
/>
</div>
<div className="form-group">
Expand All @@ -26,6 +35,7 @@ const CreateArticleComponent = props => {
placeholder="description"
name="description"
id="description"
defaultValue={article ? article.description : ""}
/>
</div>
<div className="form-group">
Expand All @@ -35,7 +45,18 @@ const CreateArticleComponent = props => {
placeholder="Body"
name="body"
id="body"
// ref={inputRef}
// 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 +65,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 +92,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
235 changes: 235 additions & 0 deletions src/components/articles/editArticlePage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,235 @@
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 className="container-fluid mt-5">
<div className="row">
<div className="col col-lg-3" />
<div className="col col-lg-6">
<h1>Tell A story</h1>
<form>
<div className="form-group">
<input
onChange={this.onChange}
className="form-control"
placeholder="title"
name="title"
id="title"
defaultValue={article ? article.title : ""}
/>
</div>
<div className="form-group">
<input
onChange={e => this.onChange(e)}
className="form-control"
placeholder="description"
name="description"
id="description"
defaultValue={article ? article.description : ""}
/>
</div>
<div className="form-group">
{/* <div
className="fixedTextArea"
contentEditable
id="body"
suppressContentEditableWarning
onChange={e => this.onChange(e)}
>
{article ? article.body : ""}
</div> */}

<textarea
onChange={e => this.onChange(e)}
className="form-control"
placeholder="Body"
name="body"
id="body"
// readOnly="true"
value={article ? article.body : ""}
/>
{/* <input
onChange={e => this.onChange(e)}
className="form-control text-area"
placeholder="Body"
name="body"
id="body"
// readOnly="true"
defaultValue={article ? article.body : ""}
/> */}

{/* <div
className="form-control, "
id="body"
name="body"
contentEditable
suppressContentEditableWarning
>
{article ? article.body : ""}
</div> */}
</div>
<div className="form-group">
<input
onChange={e => this.onChange(e)}
className="form-control"
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 ? image : article ? article.image : " "}
className="image-fluid"
/>
</div>
<input
type="file"
name="imageUpload"
onChange={e => this.onUpload(event.target.files)}
id="fitz"
/>
</div>

<div className="form-group">
<button
onClick={e => this.onSubmit(e)}
className="btn btn-primary"
>
Publish Story
</button>
</div>
</form>
</div>
<div className="col col-lg-3" />
</div>
</div>
</div>
);
}
}

export const mapStateToProps = state => ({
article: state.getArticleReducer.article
});

export default connect(
mapStateToProps,
{ getArticleAction, articleCreateEditAction }
)(EditArticlePage);

// export default connect(
// null,
// { getArticleAction, articleCreateEditAction }
// )();
Loading

0 comments on commit 40ea02b

Please sign in to comment.