Images not displaying in the react.js - reactjs

I am trying to render the images in React.js as a card content but it does not show the image.
The code to display image:
const Template = ({ template }) => {
console.log(template.preview_image) // <- It displays the image path
return (
<Card className="my-3 p-3 rounded">
<Link to={`/template/${template._id}`}>
<Card.Img src={template.preview_image} variant="top" />
</Link>
<Card.Body>
<Link to={`/template/${template._id}`}>
<Card.Title as="div">
<strong>{template.name}</strong>
</Card.Title>
</Link>
</Card.Body>
</Card>
);
};
The template prop coming from the following code:
{loading ? (
<Loader />
) : error ? (
<Message variant="danger">{error}</Message>
) : (
<Row>
{templates &&
templates.map((template) => (
<Col key={template._id} sm={12} md={6} lg={4} xl={3}>
<Template template={template} />
</Col>
))}
</Row>
)}
The useEffect function:
const templateList = useSelector((state) => state.templateList);
const { error, loading, templates } = templateList;
useEffect(() => {
dispatch(listTemplates());
}, [dispatch]);
The images are also saved at the server side in the uploads folder.
Am I missing something?

Related

React - Store data in localStorage after click on Icon

I'm trying to save data on localStorage if the user clicked on a like button and remove it if the user unclicked it. This is my code
const [menu, setMenu] = useState([]);
const [iconStates, setIconStates] = useState({});
...
...
...
{recipes.reduce(reduceRecipes, []).map((item, index) => (
<Carousel.Item key={item._id}>
<div className="d-flex justify-content-center">
<Row>
{item.map((item, index) => {
return (
<Col sm>
<Card key={item._id} style={{ width: "18rem" }}>
<Card.Img variant="top" src={item.photo_location} />
<Card.Body>
<div className="title-container">
<Card.Title>
{item.name}
<RiHeart3Fill
className={
iconStates[item._id] ? 'heart active' : 'heart'
}
onClick={() => {
if (menu.includes(item)) {
setMenu(menu.filter(menuItem => menuItem._id !== item._id));
} else {
setMenu(menu.concat(item));
}
localStorage.setItem('menu', JSON.stringify(menu));
setIconStates({...iconStates, [item._id]: !iconStates[item._id]})
}}
/>
I Codesandbox version is here.
It doesn't work as it supposed to do now, when I click the icon on one item it doesn't get added until I click it again, and if I unclicked any item everything in the LocalStorage is deleted. can anyone elaborate on how to fix this?

How to change from Table to Card in React?

I am rendering the products in a table but I want to display them as card. Every time I touch the code it stops working, I've tried mixing them together but nothing, any suggestions?
function Products(props) {
function Products(products) {
if (products === null) return;
return products.map((product) => (
<ListGroup.Item key={product.Id}>
<Link to={`/products/${product.name}`} key={product.id}> {product.name}
</Link>
</ListGroup.Item>
));
}
return (
<>
<h1>Products</h1>
<Stack direction="horizontal" gap={3}>
<ListGroup className="align-self-start w-75">
<ProductContext.Consumer>
{({ products }) => Products(products)}
</ProductContext.Consumer>
</ListGroup>
<Outlet />
</Stack>
</>
);
}
export default Products;

Showing categories using Wordpress wp-json in ReactJS

I wanted to know what I'm doing wrong, to extract the first category from the Wordpress api.
I have a list of posts, and I just need to show the category, no link or anything.
With the code below it is bringing me an empty value.
export default function Blog({ posts }) {
const { status } = useSession();
if (status === 'loading') return <Loading />;
const getCategories = async (id) => {
const response = await axios.get(`${process.env.API_CONTENT}/categories/${id}`);
const category = response.data;
return category;
};
return (
<BlogPosts>
<Container className="default">
{posts.map((post, i) => (
<Post key={i}>
<Row>
<Col className="post-image" md={6}>
<PostImage>
<Link href={`/blog/${post.id}`}>
<a title={post.title.rendered}>
<img className="image" src={post._embedded['wp:featuredmedia']['0'].source_url} />
</a>
</Link>
</PostImage>
</Col>
<Col className="post-container" md={6}>
<PostContent>
<Link href={`/blog/${post.id}`}>
<a title={post.title.rendered}>
<h2 className="title">{post.title.rendered}</h2>
</a>
</Link>
<PostDetails date={post.modified} category={getCategories(post.categories[0])} />
<div className="excerpt" dangerouslySetInnerHTML={{ __html: post.excerpt.rendered }} />
<Link href={`/blog/${post.id}`}>
<a className="link" title={post.title.rendered}>
LEIA MAIS +
</a>
</Link>
</PostContent>
</Col>
</Row>
</Post>
))}
</Container>
</BlogPosts>
);
}

Is there a way to resize an image once it is returned from an aws bucket

I am trying to use react-image-file-resizer and I have it working in some parts of my application but I am now trying to handle a response from the aws server of the image. The format is changed and the error I get is item is not type BLOB is there a way I can convert the url returned from the aws server to a type BLOB?
This is the component i am working with :
const Items = ({ orgList }) => {
const [logos, setLogos] = useState()
const resizeImage = (image) =>
new Promise((resolve) => {
Resizer.imageFileResizer(
image,
200,
200,
"JPEG",
100,
0,
(uri) => {
resolve(uri);
},
"base64"
);
});
const imageHelper = async (image) => {
const sizing = await resizeImage(image)
setLogos(sizing)
}
return (
<>
{orgList.items ? (
<div>
{orgList.items.map((item) => (
<Card key={item.orgID} className="w-100 m-2">
<CardHeader className="d-flex">
<Link to={`/monitoring-orgs/${item.orgID}`}>{item.name}</Link>
</CardHeader>
<CardBody>
<Row>
<Col md="5">
<dl>
<dt>GroupID</dt>
<dd>{item.GroupID || "-"}</dd>
<dt>Logo</dt>
<dd>
{imageHelper(item.logoURL)}
{!item.logoURL ? (
"No logo uploaded"
) : (
<img src={logos} alt={"company logo"} />
)}
</dd>
</dl>
</Col>
<Col md="5">
<dl>
<dt>OrgID</dt>
<dd>{item.orgID}</dd>
<dt>Enabled</dt>
<dd>{item.enabled ? "Yes" : "No"}</dd>
</dl>
</Col>
</Row>
</CardBody>
</Card>
))}
</div>
) : (
<div className="buttonSpinner">
<Spinner />
</div>
)}
</>
);
};
export default connect(mapStateToProps, mapDispatchToProps)(Items);

horizontal align card using map function ReactJS

I'm trying to align horizontal card using grid with react-bootstrap and map function
I'm using redux to bring the Data through the props so I create a JSON to traverse it with a function Map
this is a fraction of my code
render() {
//const { tablaCliente } = this.state
//const { isLoading } = this.state
const { entity } = this.props;
return (
<div>
<Header></Header>
<div className="col-xs-4">
<a className="btn btn-primary" href="/newclient">Nuevo Cliente</a>
</div>
<br />
<center>
<Spinner animation="border" role="status">
<span className="sr-only">Loading...</span>
</Spinner>
</center>
<Container>
{entity && entity.getAllEntities && entity.getAllEntities.length > 0 && entity.getAllEntities.map((item, index) => {
return (
<CardGroup value={item}key={index}>
<Card style={{ width: '1rem' }} >
<Card.Img variant="top" src="data:image/svg+xml;charset=UTF-8,%3Csvg%20width%3D%22286%22%20height%3D%22180%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%20286%20180%22%20preserveAspectRatio%3D%22none%22%3E%3Cdefs%3E%3Cstyle%20type%3D%22text%2Fcss%22%3E%23holder_16b0e6bd8ef%20text%20%7B%20fill%3A%23999%3Bfont-weight%3Anormal%3Bfont-family%3AHelvetica%2C%20monospace%3Bfont-size%3A14pt%20%7D%20%3C%2Fstyle%3E%3C%2Fdefs%3E%3Cg%20id%3D%22holder_16b0e6bd8ef%22%3E%3Crect%20width%3D%22286%22%20height%3D%22180%22%20fill%3D%22%23373940%22%3E%3C%2Frect%3E%3Cg%3E%3Ctext%20x%3D%22107.203125%22%20y%3D%2296.3%22%3E286x180%3C%2Ftext%3E%3C%2Fg%3E%3C%2Fg%3E%3C%2Fsvg%3E" />
<Card.Body>
<Card.Title> Cliente {item.name}</Card.Title>
<Card.Text>
{item.description}
</Card.Text>
</Card.Body>
<ListGroup className="list-group-flush">
<ListGroupItem>Estado:{''}{JSON.stringify(item.state)}</ListGroupItem>
<ListGroupItem>Creado por: {item.createby}</ListGroupItem>
</ListGroup>
<Card.Body>
<Card.Link href="/cliente">Card Link</Card.Link>
<Card.Link href="/cliente">Editar</Card.Link>
</Card.Body>
</Card>
</CardGroup>)
})}
</Container>
</div>
);
}
I dont use react-bootstrap but u can achieve it by doing
<Container>
<div class='row'>
{this.map(value => {
return (
<div class='col-md-6'>
<Card />
</div>
)
});}
</div>
</Container>
This should work
Wrap the map function in row and the returning jsx in col
And dont just write a whole logic in jsx use functions or components

Resources