// IMPORT MODULES
import React from "react";
import i18n from '../i18n';
import { withTranslation } from 'react-i18next';

// IMPORT COMPONENTS
import Breadcrumb from "../components/Global/Breadcrumb";

// IMPORT IMAGES
// import NoveltyIMG from "../img/noveltyImg.jpg";

// const Novelty = () => {
//   useEffect(() => {
//     window.scrollTo(0, 0);
//   }, []);

class Novelty extends React.Component {

  constructor(props) {
    super(props);

    this.state = {
      items: [],
      DataisLoaded: false,
      error: null
  };
  }

  componentDidMount() {
    this._mounted = true
    this.makeRemoteRequest();
  }

 

  componentWillUnmount() {
    this._mounted = false
  }

  makeRemoteRequest = () => {

    const url = `http://217.174.238.204:8888/api/v1/posts/`+this.props.match.params.slug;
  
    if (this._mounted) {
      this.setState({ DataisLoaded: true });
    }
    setTimeout(() => {
  
      fetch(url, {
        method: 'GET',
       
      })
  
        .then(res => res.json())
        .then(res => {
          if (this._mounted) {
            this.setState({
              items: res.data,
              error: res.message || null,
              DataisLoaded: true,
            })
            console.log(this.state.items[0][0]['content'])
          }
         
        })
        .catch(error => {
          if (this._mounted) {
            this.setState({ error, DataisLoaded: false});
          }
        });
    }, 1500);
  };

  render() {

  // const [pageIndex, setPageIndex] = useState(1);


  var {DataisLoaded, items} = this.state;
    if(!DataisLoaded){
      return <div>Loading ....</div>
    }else{

      

      return (
        <section className="novelty">
          <Breadcrumb
            path_1="Новости"
            link_1="/news"
            path_2={this.props.match.params.slug}
            currentUrl="../news/novelty"
          />
          <div className="container">


          
          {items.map(item=>(
            <div className="novelty-inner" key={item[0]['id']}>
              <div className="novelty-head">
                <div className="novelty-img">
                  <img src={item[0]['featured_images'][0]['path']} alt="Novelty" />
                </div>
                <h4 className="novelty-title">
                { i18n.language === 'en' &&
                    JSON.parse(item[0]['translations'][0]['attribute_data']).title 
                }
                { i18n.language === 'ru' && 
                    item[0]['title']
                } 
                { i18n.language === 'tm' && 
                    JSON.parse(item[0]['translations'][1]['attribute_data']).title 
                }
                </h4>
              </div>
              <div className="novelty-text">
                
            {/* {item[0]['content']} */}
            { i18n.language === 'en' &&
                JSON.parse(item[0]['translations'][0]['attribute_data']).content 
            }
            { i18n.language === 'ru' && 
                item[0]['content']
            } 
            { i18n.language === 'tm' && 
                JSON.parse(item[0]['translations'][1]['attribute_data']).content 
            }

              </div>
            </div>

          ))}
            
          </div>
        </section>
      );
    }
  }
};

// export default Novelty;
export default withTranslation()(Novelty);
