import React, { useState, useEffect, useContext } from 'react'; import unsplash from '../utils/unsplashConfig'; import { ImgContext } from '../utils/ImgContext'; const UnsplashSearch = ({ largeImgPreview }) => { const [imageList, setImageList] = useState([]); const [searchText, setSearchText] = useState('setup'); const { unsplashImage, setUnsplashImage } = useContext(ImgContext); const searchImages = () => { unsplash.search .getPhotos({ query: searchText, page: 1, per_page: 30, // orientation:'portrait' }) .then(response => { // console.log(response.response.results); setImageList(response.response.results) }); } const selectImage = (image) => { setUnsplashImage({ url: image.urls.regular, name: image.user.name, avatar: image.user.profile_image.small, profile: `${image.user.links.html}?utm_source=https://coverview.czl.net&utm_medium=referral`, downloadLink: image.links.download_location }) } const handleSearchSubmit = (e) => { e.preventDefault(); searchImages(searchText); } useEffect(() => { unsplash.search .getPhotos({ query: 'setup', page: 1, per_page: 30 }) .then(response => { // console.log(response.response.results); setImageList(response.response.results) }); }, []) return (
handleSearchSubmit(e)} className=" mx-auto w-full flex bg-gray-50 rounded-full border border-gray-50 mb-2"> setSearchText(e.target.value)} />
{ imageList.map(image => { return
Click to Select {image.alt_description} selectImage(image) } className="rounded-lg object-cover h-full w-full" />
}) }
); } export default UnsplashSearch;