forked from slince-zero/IMaker
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9301f0b
commit c099444
Showing
5 changed files
with
120 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { | ||
createContext, | ||
useEffect, | ||
useState, | ||
KeyboardEvent, | ||
ReactNode, | ||
} from 'react' | ||
import ImageItem from '@/page/left/ImageItem' | ||
|
||
export const ImgContext = createContext({ | ||
imgList: [], | ||
setImgList: () => {}, | ||
} as any) | ||
|
||
export default function ImgContextProvider({ | ||
children, | ||
}: { | ||
children: ReactNode | ||
}) { | ||
const [imageList, setImageList] = useState<ImageItem[]>([]) | ||
const [searchValue, setSearchValue] = useState('') | ||
const [isLoading, setIsLoading] = useState(false) | ||
// 获取图片 | ||
async function getImage(searchText: string = '') { | ||
try { | ||
setIsLoading(true) | ||
const accessKey = 'cPSLU8ro2LKkaLQ71-RKKFFwb2g-_DIx9NCf27YnCHs' | ||
const endpoint = | ||
searchText.length > 0 | ||
? `https://api.unsplash.com/search/photos` | ||
: `https://api.unsplash.com/photos` | ||
const queryParam = searchText | ||
? `&query=${encodeURIComponent(searchText)}` | ||
: '' | ||
const res = await fetch( | ||
`${endpoint}?per_page=30${queryParam}&client_id=${accessKey}` | ||
) | ||
const data = await res.json() | ||
|
||
if (data && data.results) { | ||
// 如果是使用搜索API,结果会在data.results中 | ||
setImageList(data.results) | ||
} else { | ||
setImageList(data) // 直接访问 Unsplash collections/photos 数据在顶层 | ||
} | ||
// console.log(imageList,'33') | ||
} catch (e) { | ||
console.error(e) | ||
} finally { | ||
setIsLoading(false) | ||
setSearchValue('') | ||
} | ||
} | ||
|
||
useEffect(() => { | ||
getImage() | ||
}, []) | ||
|
||
// 按下回车事件 | ||
function onSearchKeyDown(e: KeyboardEvent<HTMLInputElement> | KeyboardEvent) { | ||
if (e.key === 'Enter') { | ||
getImage(searchValue) | ||
} | ||
} | ||
|
||
return ( | ||
<ImgContext.Provider | ||
value={{ | ||
imageList, | ||
setImageList, | ||
searchValue, | ||
setSearchValue, | ||
isLoading, | ||
setIsLoading, | ||
getImage, | ||
onSearchKeyDown, | ||
}}> | ||
{children} | ||
</ImgContext.Provider> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters