Can I use React infinite scroll with GraphQL and datoCMS without apollo / react-query? - pagination

I'd like to pagination news, or use infinite scroll, but as the site is built then served as static files, I'm not sure the best way to go about this?
Is it possible to do without using apollo or react-query?
I did try react-query but couldn't get it to see my datoCMS endpoint.
(Update: comment below about promise)
import React from 'react'
import { useQuery } from 'react-query'
import gql from 'graphql-tag'
import { GraphQLClient, request } from 'graphql-request'
export const useGQLQuery = (key, query, variables, config = {}) => {
const endpoint = `https://graphql.datocms.com/`
const client = new GraphQLClient(endpoint, {
headers: {
authorization: `Bearer MY_TOKEN_HERE`
}
});
// use await to wait until promise has been fulfilled
const fethData = async () => await request(endpoint, client, query, variables)
return useQuery(key, fethData, config); // useQuery from react-query
};
const GET_ALL_NEWS_CONTENT = gql`
query {
allNews {
slug
id
title
}
}
`;
const AllNews = () => {
// Fetch data from custom hook that uses React-Query
const { data, isLoading, error } = useGQLQuery('allNews', GET_ALL_NEWS_CONTENT)
console.log(data)
if (isLoading) return <div>Loading…</div>
if (error) return <div>Something went wrong…</div>
return (
<>
<div className="generic-page">
<h2>All News</h2>
<div>
{data.allNews.map(news => (
<div key={news.title}>{news.title}</div>
))}
</div>
</div>
</>
)
};
export default AllNews;

With a little help from datoCMS and the react-query community, here is the solution! …
import React, { useState } from 'react'
import { useQuery } from 'react-query'
import gql from 'graphql-tag'
import { GraphQLClient } from 'graphql-request'
export const useGQLQuery = (key, query, variables) => {
const endpoint = `https://graphql.datocms.com/`
const client = new GraphQLClient(endpoint, {
headers: {
authorization: `Bearer MY_TOKEN_HERE`,
}
});
return useQuery(
key, () => client.request(query, variables),
{ keepPreviousData: true }
); // useQuery from react-query
};
const GET_ALL_NEWS_CONTENT = gql`
query GetAllNewsContent ($first: IntType = 10, $skip: IntType = 0) {
allNews(first: $first, skip: $skip, orderBy: _createdAt_DESC) {
id
title
slug
}
}
`;
export default function AllNews({ newscount }) {
const first = 5
let [skip, skipNews] = useState(0)
// Fetch data from custom hook that uses React-Query
const { data, status } = useGQLQuery(
['allNews', skip],
GET_ALL_NEWS_CONTENT,
{ skip, first }
);
const itemCount = Math.ceil(newscount / first);
return (
<div className="generic-page">
<button className="example_b"
onClick={() => skipNews(old => Math.max(old - 5, 0))}
disabled={skip === 0}
>
<span>Previous Page</span>
</button>{' '}
<button className="example_f"
onClick={() => skipNews(old => Math.max(old + 5, 0))}
// Disable the Next Page button when the next skip would be higher than the meta count
disabled={skip + 5 > newscount}
>
<span>Next Page</span>
</button>
<h2>All News</h2>
<p>Page {skip / 5 + 1} of {itemCount}</p>
{status === 'loading' && (
<div>Loading…</div>
)}
{status === 'error' && (
<div>Something went wrong…</div>
)}
{status === 'success' && (
<div>
{data.allNews.map(news => (
<div key={news.title}>{news.title}</div>
))}
</div>
)}
</div>
)
};

Related

Not able render the images using an API

I am using multi avatar api to render random images on the UI, but I am getting the below-mentioned error. I also tried using promises to render the UI but failed to get the results.
Uncaught TypeError: The first argument must be one of type string,
Buffer, ArrayBuffer, Array, or Array-like Object. Received type
undefined
import React, { useEffect, useState } from "react";
import { useNavigate } from "react-router-dom";
import { ToastContainer, toast } from "react-toastify";
import "react-toastify/dist/ReactToastify.css";
import axios from "axios";
import { profilePicRoute } from "../utils/apiRoutes";
import { Buffer } from "buffer";
function ProfilePic() {
const api = "https://api.multiavatar.com";
const navigate = useNavigate();
const [profilePic, setProfilePic] = useState([]);
const [isLoading, setIsLoading] = useState(true);
const [selectedPofilePic, setSelectedPofilePic] = useState(undefined);
const toastStyles = {
position: "top-center",
};
const setProfilePicture = async () => {};
useEffect(() => {
const data = [];
for (let i = 0; i < 4; i++) {
const image = axios.get(`${api}/${Math.round(Math.random() * 1000)}`);
const buffer = Buffer(image.data);
data.push(buffer.toString("base64"));
console.log(data);
}
setProfilePic(data);
setIsLoading(false);
}, []);
return (
<div className="profilePage">
<h1>Pick your favorite profile picture</h1>
<div className="profilePics">
{profilePic.map((pic, index) => {
return (
<div
key={index}
className={`pic ${selectedPofilePic === index ? "selected" : ""}`}
>
<img
src={`data:image/svg+xml;base64,${pic}`}
alt="profile pic"
onClick={() => setSelectedPofilePic(index)}
/>
</div>
);
})}
</div>
<ToastContainer />
</div>
);
}
export default ProfilePic;
Since you were using the async you must have to use await keyword , otherwise it will return promises,and you should use the function inside the useEffect
import React, { useEffect, useState } from "react";
import { useNavigate } from "react-router-dom";
import { ToastContainer, toast } from "react-toastify";
import "react-toastify/dist/ReactToastify.css";
import axios from "axios";
import { profilePicRoute } from "../utils/apiRoutes";
import { Buffer } from "buffer";
function ProfilePic() {
const api = "https://api.multiavatar.com";
const navigate = useNavigate();
const [profilePic, setProfilePic] = useState([]);
const [isLoading, setIsLoading] = useState(true);
const [selectedPofilePic, setSelectedPofilePic] = useState(undefined);
const toastStyles = {
position: "top-center"
};
useEffect(() => {
const setProfilePicture = async () => {
const data = [];
for (let i = 0; i < 4; i++) {
const image = await axios.get(
`${api}/${Math.round(Math.random() * 1000)}`
);
console.log(image);
const buffer = Buffer(image.data);
data.push(buffer.toString("base64"));
}
setProfilePic(data);
setIsLoading(false);
};
setProfilePicture();
}, []);
return (
<div className="profilePage">
<h1>Pick your favorite profile picture</h1>
<div className="profilePics">
{profilePic.map((pic, index) => {
return (
<div
key={index}
className={`pic ${selectedPofilePic === index ? "selected" : ""}`}
>
<img
src={`data:image/svg+xml;base64,${pic}`}
alt="profile pic"
onClick={() => setSelectedPofilePic(index)}
/>
</div>
);
})}
</div>
<ToastContainer />
</div>
);
}
export default ProfilePic;
Hope this code will help you.
Happy Coding :)

Does axios need extra config to get data from REST API?

I am trying to convert the fetch API to axios with get method.
Prior to do this, I plan to keep using 'async, await'.
And when I replaced the code below:
// before
const fetchPlanets = async () => {
const res = await fetch("http://swapi.dev/api/planets/");
return res.json();
};
// after
const fetchPlanets = async () => {
const res = await axios
.get("http://swapi.dev/api/planets/")
.then((respond) => {
respond.data;
});
};
async can be used when to address the function.
and returned const res as res.json();
Also...axios does not require to res.json as it returned as json type.
That's how I understand this so far. And with fetch API, this work flawlessly.
How the code should be to let axios work as I expected?
// Planets.js
import React from "react";
import { useQuery } from "react-query";
import Planet from "./Planet";
// import axios from "axios";
const fetchPlanets = async () => {
const res = await fetch("http://swapi.dev/api/planets/");
return res.json();
};
const Planets = () => {
const { data, status } = useQuery("planets", fetchPlanets);
console.log(data);
return (
<div>
<h2>Planets</h2>
{status === "loading" && <div>Loading data...</div>}
{status === "error" && <div>Error fetching data!</div>}
{status === "success" && (
<div>
{data.results.map((planet) => (
<Planet key={planet.name} planet={planet} />
))}
</div>
)}
</div>
);
};
export default Planets;
And Planet.js; just in case.
import React from "react";
const Planet = ({ planet }) => {
return (
<div className="card">
<h3>{planet.name}</h3>
<p>Population - {planet.population}</p>
<p>Terrain - {planet.terrain}</p>
</div>
);
};
export default Planet;
There are 2 problems in your axios code.
You should return respond.data.
You should return the whole axios response.
So this would work:
const fetchPlanets = async () => {
return await axios
.get("http://swapi.dev/api/planets/")
.then((respond) => {
return respond.data;
});
};

Why data fetched from backend shows in console but not in the webpage?

Here I've fetched workouts from backend through api. It shows output in the console but unable to map through workouts state in the webpage.
import React, { useEffect, useState } from "react";
const Home = () => {
const [workouts, setWorkouts] = useState([]);
useEffect(() => {
const fetchWorkouts = async () => {
const response = await fetch("http://localhost:4000/api/workouts");
const json = await response.json();
if (response.ok) {
console.log('success');
console.log(json);
setWorkouts(json);
}
};
fetchWorkouts();
}, []);
return (
<div className="home">
<div className="workouts">
{workouts &&
workouts.map((workout) => {
<p key={workout._id}>{workout.title}</p>;
})}
</div>
</div>
);
};
export default Home;
You forgot to return it. Do this:
return <p key={workout._id}>{workout.title}</p>;
or you can also do this:
{workouts?.map((workout) => (
<p key={workout._id}>{workout.title}</p>
))}
You can remove the bracket on the map:
workouts.map((workout) =>
<p key={workout._id}>{workout.title}</p>;
)}
You're not returning anything. Either explicitly use the return keyword to return the element or You can do this in a more appropriate way like this.
{
workouts &&
workouts.map((workout) => (
<p key={workout._id}>{workout.title}</p>
))
}

Have a Handler Change Value of a Variable in Mongodb

I am working on a product where I have pre orders and normal orders. For my pre order items, I have the variable pre_orderQty, and for my normal items, I have the variable qty. In my OrderScreen.js, I have a handler that deals with if an order has been shipped (shipHandler). The issue that I am having is that I am trying to get this handler to also update any items in my order that have a qty value of 0 to the value of the pre_orderQty when clicked.
I am using node in my backend and react in my frontend. The orders are stored in mongodb. I'm not exactly sure how to do this, and I would really appreciate any help or advice on how to accomplish this.
So far, I have attempted to do this by:
In my backend orderRouter.js
orderRouter.put(
'/:id/updated',
isAuth,
isAdmin,
expressAsyncHandler(async (req, res) => {
const order = await Order.findById(req.params.id);
if (order && order.orderItems.qty === 0) {
order.orderItems.qty = order.orderItems.pre_orderQty;
order.orderItems.pre_orderQty = 0;
const updatedOrder = await order.save();
res.send({ message: 'Order Updated', order: updatedOrder });
} else {
res.status(404).send({ message: 'Order Not Found' });
}
}),
);
Frontend:
OrderActions.js
export const updateOrder = (orderId) => async (dispatch, getState) => {
dispatch({ type: ORDER_UPDATE_REQUEST, payload: orderId });
const {
userSignin: { userInfo },
} = getState();
try {
const { data } = Axios.put(
`/api/orders/${orderId}/updated`,
{},
{
headers: { Authorization: `Bearer ${userInfo.token}` },
},
);
dispatch({ type: ORDER_UPDATE_SUCCESS, payload: data });
} catch (error) {
const message = error.response && error.response.data.message ? error.response.data.message : error.message;
dispatch({ type: ORDER_UPDATE_FAIL, payload: message });
}
};
OrderScreen
import Axios from 'axios';
import { PayPalButton } from 'react-paypal-button-v2';
import React, { useEffect, useState } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { Link } from 'react-router-dom';
import {shipOrder, deliverOrder, detailsOrder, payOrder, updateOrder } from '../actions/orderActions';
import LoadingBox from '../components/LoadingBox';
import MessageBox from '../components/MessageBox';
import {
ORDER_DELIVER_RESET,
ORDER_SHIPPED_RESET,
ORDER_RETURN_RESET,
ORDER_PAY_RESET,
} from '../constants/orderConstants';
export default function OrderScreen(props) {
const orderId = props.match.params.id;
const [sdkReady, setSdkReady] = useState(false);
const orderDetails = useSelector((state) => state.orderDetails);
const { order, loading, error } = orderDetails;
const userSignin = useSelector((state) => state.userSignin);
const { userInfo } = userSignin;
const orderShip = useSelector((state) => state.orderShip);
const {
loading: loadingShip,
error: errorShip,
success: successShip,
} = orderShip;
const orderUpdate = useSelector((state) => state.orderUpdate);
const {
loading: loadingUpdate,
error: errorUpdate,
success: successUpdate,
} = orderUpdate;
const dispatch = useDispatch();
useEffect(() => {
if (
!order ||
successShip ||
successUpdate ||
(order && order._id !== orderId)
) {
dispatch({ type: ORDER_SHIPPED_RESET });
dispatch({ type: ORDER_UPDATE_RESET });
dispatch(detailsOrder(orderId));
} else {
if (!order.isPaid) {
if (!window.paypal) {
addPayPalScript();
} else {
setSdkReady(true);
}
}
}
}, [dispatch, order, orderId, sdkReady, successShip, successUpdate, order]);
const shipHandler = () => {
dispatch(shipOrder(order._id));
dispatch(updateOrder(order._id));
};
return loading ? (
<LoadingBox></LoadingBox>
) : error ? (
<MessageBox variant="danger">{error}</MessageBox>
) : (
<div>
<h1>Order {order._id}</h1>
<div className="row top">
<div className="col-1">
<div className="card card-body">
<ul>
{userInfo.isAdmin && (
<li>
{loadingShip && <LoadingBox></LoadingBox>}
{errorShip && (
<MessageBox variant="danger">{errorShip}</MessageBox>
)}
<button
type="button"
className="primary block"
onClick={shipHandler}
>
Order Shipped
</button>
</li>
)}
</ul>
</div>
</div>
</div>
</div>
);
}

3D model renderer component not loading file

I'm working on a feature for a web app I'm developing that consists in a .obj model viewer. The thing is that I don't really know how to work with the three.js library so I decided to go with a dependency to do the trick, I'm using this one. Basically what I'm doing is to get the content of the file in the redux state which is initially obtained by a backend request, so, I ultimately use the state variable and pass it a component prop to the <OBJModel /> component, but I get this error when trying to load:
https://pasteboard.co/Jtdkigq.png
https://pasteboard.co/JtdkPoRk.png
This is the front-end code:
import React from 'react';
import { OBJModel } from 'react-3d-viewer';
import { connect } from 'react-redux';
import { getModel } from './../../actions';
import golem from './../../Stone.obj';
class ModelViewer extends React.Component {
componentDidMount() {
document.querySelector('#upload-modal').style.display = 'flex';
let id = '';
let slashCounter = 0;
for (let i = 0; i < window.location.pathname.length; i++) {
if (slashCounter === 2) {
id = id + window.location.pathname[i];
}
if (window.location.pathname[i] === '/') {
slashCounter++;
}
}
this.props.getModel(id);
}
render() {
if (this.props.model.previewModel)
console.log(this.props.model.previewModel);
return (
<div style={{ display: 'flex', justifyContent: 'center' }}>
{!this.props.model.previewModel ? null : (
<OBJModel
height="500"
width="500"
position={{ x: undefined, y: -5, z: undefined }}
src={this.props.model.previewModel}
/>
)}
<div id="upload-modal" className="upload-modal">
<div className="upload-modal-content">
<p className="upload-modal-header">
<i
className="exclamation circle icon"
style={{ color: 'gray' }}
/>
Loading model...
</p>
<div
className="ui indicating progress active progress-indicator"
id="upload-bar-indicator"
>
<div className="bar" id="upload-bar">
<div className="progress" id="modal-upload-progress"></div>
</div>
</div>
</div>
</div>
</div>
);
}
}
const mapStateToProps = (state) => {
return { model: state.model };
};
export default connect(mapStateToProps, { getModel })(ModelViewer);
This is the action function that makes the request to the backend:
export const getModel = (id) => {
return async (dispatch) => {
const config = {
onDownloadProgress: function (progressEvent) {
let percentCompleted = Math.round(
(progressEvent.loaded * 100) / progressEvent.total
);
document.querySelector(
'#modal-upload-progress'
).innerHTML = `${percentCompleted}%`;
document.querySelector(
'#upload-bar'
).style.width = `${percentCompleted}%`;
document
.querySelector('#upload-bar-indicator')
.setAttribute('data-percent', `${percentCompleted}`);
},
};
const response = await axios.get(
`https://hushpuppys-3d-hub-api.herokuapp.com/api/v1/modelfiles/${id}.obj`,
config
);
document.querySelector('#upload-modal').style.display = 'none';
dispatch({ type: 'GET_MODEL', payload: response.data.data });
};
};
This is the controller that downloads the file from the Cloud Storage where im storing the files:
exports.fileDownloaderController = async (req, res) => {
try {
const fileName = req.params.id;
const config = {
headers: { Authorization: credentials.authorizationToken },
};
await axios
.get(
`${credentials.downloadUrl}/file/hushpuppys-3d-hub-storage/${fileName}`,
config
)
.then(function (response) {
console.log(response.data);
res.status(200).json({
status: 'success',
data: response.data,
});
})
.catch(function (err) {
console.log(err); // an error occurred
});
} catch (err) {
console.log(err);
res.status(404).json({
status: 'fail',
message: err.message,
});
}
};
And this is how the downloaded files look like when they are downloaded from the server, and logged in the console:
https://pasteboard.co/JtdpMCi.png

Resources