Skip to content

[Issue 143] Refactor /resources page to use React Query #147

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 9 additions & 17 deletions src/components/Resources/index.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,18 @@
import React, { useState, useEffect } from 'react';
import React from 'react';
import PropTypes from 'prop-types';
import axios from 'axios';
import { useQuery } from 'react-query';
import PersonalMenu from '../PersonalMenu';
import Search from '../Search';
import Grid from '@material-ui/core/Grid';
import { ResourceCard } from './ResourceCard';
import { getResources } from '../../utils/queries';

function Resources({ getResourcesUrl }) {
const [resources, setResources] = useState([]);
function Resources() {
const { isLoading, data } = useQuery('resources', getResources);

useEffect(() => {
axios
.get(getResourcesUrl)
.then(function(response) {
// handle success
setResources(response.data.results);
})
.catch(function(error) {
// handle error
console.log(error);
});
}, [getResourcesUrl]);
if (isLoading) {
return <p>Loading...</p>;
}

return (
<>
Expand All @@ -33,7 +25,7 @@ function Resources({ getResourcesUrl }) {
<Search label="Search resources" />
<br />
<Grid container spacing={1}>
{resources.map(resource => {
{data.results.map(resource => {
return (
<Grid item lg={3} key={resource.guid}>
<ResourceCard {...resource} />
Expand Down
1 change: 1 addition & 0 deletions src/components/Resources/submitResource.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ const SubmitResource = () => {
const classes = useStyles();
const inputLabel = useRef(null);
const [labelWidth, setLabelWidth] = useState(0);

useEffect(() => {
setLabelWidth(inputLabel.current.offsetWidth);
}, []);
Expand Down
7 changes: 6 additions & 1 deletion src/utils/queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,9 @@ const getResource = async (_key, id) => {
return data;
};

export { getResource };
const getResources = async () => {
const { data } = await axios.get(`${API_URL}/resources`);
return data;
};

export { getResource, getResources };