import React, { useState ,Fragment, useEffect } from 'react';
import PropTypes from 'prop-types';
import { withRouter } from 'react-router'
import { Link } from 'react-router-dom';
import { connect } from 'react-redux';
import Moment from 'react-moment';
import { addPost, clearPost } from '../../actions/post';
import { setAlert } from '../../actions/alert';
import { useForm } from "react-hook-form";
import { getPost } from '../../actions/post';
import axios from 'axios';
import $ from 'jquery';
import 'animate.css';
// To do list
// 1. Fix add featured image when not selected.
// 2. Reload on page load, so it actually loads the post.
// 3. Fix issue when shows old post, so reload post before hand..
const EditPost = ({ post: { post, loading }, getPost,setAlert, auth: {user, isAuthenticated}, match}) => {
let count = 0;
const [state, setState] = useState({
text: '',
title: '',
excerpt: '',
image: { preview: "", raw: "", filename: ""},
tags: '',
credit: '',
hyperlink: '',
edit: true,
});
useEffect(() => {
getPost(match.params.id);
// if (!loading) {
// const postData = { ...state };
// for (const key in post) {
// if (key in postData) postData[key] = post[key];
// }
// setState(postData, () => {console.log(this.state)});
// }
if(!loading) {
setState({
...state,
text: post.text,
title: post.title,
excerpt: '',
image: { preview: "", raw: "", filename: ""},
tags: '',
credit: '',
hyperlink: '',
}, () => {console.log(state)});
}
}, [loading, getPost]);
const {
text,
title,
excerpt,
tags,
image,
credit,
hyperlink
} = state;
This takes the old state or previous state and set it, not the actual one that has loaded now?
It only works, when I reload the page, or press on the post twice or enter on the URL where it contains the ID of the post.
Any idea how I could force reload the page? I have also used a reducer to clearPost before loading this one but unfortunately no luck.
If I access the data directly such as post.text, it works, it loads perfectly fine, but then when I need to make editable, that doesn't work and then I have to use setState, but no luck.