Skip to content
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

Refactored out use of fetch in Unsplash block #2890

Merged
merged 3 commits into from
May 6, 2020
Merged
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
6 changes: 6 additions & 0 deletions .changeset/few-moles-talk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@keystonejs/app-admin-ui': minor
'@keystonejs/fields': minor
---

Refactored the Unsplash content block to use Apollo query hooks.
90 changes: 43 additions & 47 deletions packages/fields/src/types/Unsplash/views/blocks/unsplash-image.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import pluralize from 'pluralize';
import { BlockMenuItem } from '@keystonejs/field-content/block-components';
import { Input } from '@arch-ui/input';

import { useLazyQuery } from '@apollo/react-hooks';
import gql from 'graphql-tag';

export let type = 'unsplashImage';

// TODO: Receive this value from the server somehow. 'pluralize' is a fairly
Expand Down Expand Up @@ -110,71 +113,64 @@ const UnsplashImage = ({ width, height, unsplashId, publicUrl, alt, user, onClic
);
};

const UNSPLASH_SEARCH_QUERY = gql`
query searchImages($query: String!, $page: Int, $perPage: Int, $width: String) {
searchUnsplash(query: $query, perPage: $perPage, page: $page) {
total
totalPages
results {
id
unsplashId
publicUrl: publicUrlTransformed(transformation: { w: $width })
width
height
alt
user {
name
url
}
}
}
}
`;

const Search = ({ onSelect }) => {
const options = useContext(Context);
const [searchPage, setSearchPage] = useState(1);
const [searchTerm, setSearchTerm] = useState('');
const [searchResults, setSearchResults] = useState();

const [getUnsplashImages, { data: { searchUnsplash: searchResults } = {} }] = useLazyQuery(
UNSPLASH_SEARCH_QUERY,
{
variables: {
query: searchTerm,
page: searchPage,
perPage: RESULTS_PER_PAGE,
width: RESULTS_WIDTH,
},
}
);

const showPrevious = useCallback(() => {
const newPage = Math.max(1, searchPage - 1);
setSearchPage(newPage);
getUnsplashImages(searchTerm, newPage);
}, [searchPage, setSearchPage, searchTerm]);
setSearchPage(Math.max(1, searchPage - 1));
}, [searchPage]);

const showNext = useCallback(() => {
const newPage = Math.min(
(searchResults && searchResults.totalPages) || Infinity,
searchPage + 1
setSearchPage(
Math.min((searchResults && searchResults.totalPages) || Infinity, searchPage + 1)
);
setSearchPage(newPage);
getUnsplashImages(searchTerm, newPage);
}, [searchPage, setSearchPage, searchTerm]);

const getUnsplashImages = (query, page) => {
fetch(options.adminMeta.apiPath, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
variables: { query, page, perPage: RESULTS_PER_PAGE, width: RESULTS_WIDTH },
query: `query searchImages($query: String!, $page: Int, $perPage: Int, $width: String) {
searchUnsplash(query: $query, perPage: $perPage, page: $page) {
total
totalPages
results {
id
unsplashId
publicUrl: publicUrlTransformed(transformation: { w: $width})
width
height
alt
user {
name
url
}
}
}
}`,
}),
})
.then(x => x.json())
.then(results => {
setSearchResults(results.data.searchUnsplash);
});
};
}, [searchPage]);

const onChange = useCallback(
event => {
event.preventDefault();
event.stopPropagation();
setSearchTerm(event.target.value);
if (event.target.value.length > 3) {
getUnsplashImages(event.target.value, 1);
getUnsplashImages();
}
},
[searchTerm, setSearchTerm]
[getUnsplashImages]
);

const unsplashUrl = attributeUrl('https://unsplash.com', options.attribution);
Expand Down