// © XlXi 2021 // Graphictoria 5 import axios from 'axios'; import React, { useEffect, useState } from "react"; import { Link, useHistory, useParams } from "react-router-dom"; import Config from '../config.js'; import SetTitle from "../Helpers/Title.js"; import Loader from '../Components/Loader.js'; import { GenericErrorModal } from './Errors.js'; import { Card, CardTitle } from '../Components/Card.js'; import { paginate } from '../helpers/utils.js'; import { Modal } from 'react-bootstrap'; var url = Config.BaseUrl.replace('http://', ''); var protocol = Config.Protocol; const Item = (props) => { var id = useParams().id; const [validity, setValidity] = useState({error: false, message: ``, inputs: []}); const [state, setState] = useState({offline: false, loading: true}); const [item, setPost] = useState({item: [], sellingPrices: {sellingPrices: [], meta: [], currentPage: 1}}); const [show, setShow] = useState(false); const user = props.user; const history = useHistory(); const fetchItem = async () => { await axios.get(`${protocol}apis.${url}/fetch/item/${id}?page=${item.sellingPrices.currentPage}`, {headers: {"X-Requested-With":"XMLHttpRequest"}}).then(data=>{ if (!data.data) {history.push(`/catalog`);} const res = data.data; SetTitle(res.item.title); setPost({item: res.item, sellingPrices: {...item.sellingPrices, sellingPrices: res.sellingPrices.data, meta: res.sellingPrices}}); }).catch(error=>{console.log(error);}); } const removeSale = async (saleId) => { setState({...state, loading: true}); const body = new FormData(); await axios.post(`${protocol}apis.${url}/api/catalog/remove/sale/${saleId}`, body, {headers: {"X-Requested-With":"XMLHttpRequest"}}).then(data=>{ setState({...state, loading: false}); const res = data.data; if (res.badInputs.length >= 1) { setValidity({error: true, message:res.message, inputs: res.badInputs}); setTimeout(()=>{setValidity({...validity, error: false, inputs: res.badInputs});}, 4000); }else{ setPost({item: res.item, sellingPrices: {...item.sellingPrices, sellingPrices: res.sellingPrices.data, meta: res.sellingPrices}}); } }).catch(error=>{console.log(error);}); } const buyItem = async (decision, isSelling, sellingId) => { setState({...state, loading: true}); const body = new FormData(); body.append(`decision`, decision); if (isSelling) body.append('sellingId', sellingId); await axios.post(`${protocol}apis.${url}/api/catalog/buy/${id}?page=${item.sellingPrices.currentPage}`, body, {headers: {"X-Requested-With":"XMLHttpRequest"}}).then(data=>{ if (!data.data) {history.push(`/catalog`);} setState({...state, loading: false}); const res = data.data; if (res.badInputs.length >= 1) { setValidity({error: true, message:res.message, inputs: res.badInputs}); setTimeout(()=>{setValidity({...validity, error: false, inputs: res.badInputs});}, 4000); }else{ setPost({item: res.item, sellingPrices: {...item.sellingPrices, sellingPrices: res.sellingPrices.data, meta: res.sellingPrices}}); } }).catch(error=>{console.log(error);}); } const sellItem = async (form) => { setState({...state, loading: true}); const body = form; await axios.post(`${protocol}apis.${url}/api/catalog/sell/${id}?page=${item.sellingPrices.currentPage}`, body, {headers: {"X-Requested-With":"XMLHttpRequest"}}).then(data=>{ if (!data.data) {history.push(`/catalog`);} setState({...state, loading: false}); const res = data.data; if (res.badInputs.length >= 1) { setValidity({error: true, message:res.message, inputs: res.badInputs}); setTimeout(()=>{setValidity({...validity, error: false, inputs: res.badInputs});}, 4000); }else{ setPost({item: res.item, sellingPrices: {...item.sellingPrices, sellingPrices: res.sellingPrices.data, meta: res.sellingPrices}}); setShow(false); } }).catch(error=>{console.log(error);}); } const paginatePrices = async (decision) => { paginate(decision, item.sellingPrices.currentPage, item.sellingPrices.meta).then(res=>{ switch(res){ case "increase": setPost({...item, sellingPrices: {...item.sellingPrices, currentPage: item.sellingPrices.currentPage+1}}); break; case "decrease": setPost({...item, sellingPrices: {...item.sellingPrices, currentPage: item.sellingPrices.currentPage-1}}); break; default: break; } }).catch(error=>console.log(error)); } useEffect(async ()=>{ setState({...state, loading: false}); }, []); useEffect(async()=>{ setState({...state, loading: true}); await fetchItem(); setState({...state, loading: false}); }, [item.sellingPrices.currentPage]); return ( state.loading ? :
{setShow(false);}}> Sell {item.item.title}
{e.preventDefault();sellItem(new FormData(e.target));}}>

How much are you selling for?


{validity.error?

{validity.message}

: null}

Date Created:

{item.item.created_at}


Price: {item.item.current_price? `$${item.item.current_price}` : `N/A`}

Description: '{item.item.description}'

{item.item.ownsItem?

You own this item!

: null} {!item.item.isLimited? : item.item.stock >= 1? : !item.item.isSelling && item.item.ownsItem? : item.item.isSelling?

You're selling this item!

:

Sorry, you don't own this item!

}
Creator Info:
{item.item.creator.username}
{item.item.isLimited != 0 && item.item.stock <= 0?

: null} {item.sellingPrices.sellingPrices.length <= 0 && item.item.isLimited != 0 && item.item.stock <= 0?

No one is selling this item yet!

: null} {item.item.isLimited != 0 && item.item.stock <= 0?
{item.sellingPrices.sellingPrices.map(reply=>(

Date posted:

{reply.created_at}


{reply.seller_name}

Price: ${reply.price}

Item UID: {reply.uid}

{!reply.isMeta? : }
))} {item.sellingPrices.sellingPrices.length >= 10?
{item.sellingPrices.currentPage >= 2? : null} {item.sellingPrices.currentPage < item.sellingPrices.meta.last_page? : null}
: null}
: null}
); } export default Item;