mirror of https://github.com/sgoudham/carbon.git
Use Unsplash API (#301)
* Add unsplash-js, isomorphic-fetch * /unsplash route/handling * RandomImage use unsplash api * Implement Photographer accredidation * Add referral link * Fetch photographer profile_url from unsplash api * Change credit copy * Add PhotoCredit componentmain
parent
eef096bc49
commit
cf1e92b8cb
@ -0,0 +1,27 @@
|
||||
import React from 'react'
|
||||
|
||||
export default ({ photographer }) => (
|
||||
<div className="photo-credit">
|
||||
Photo by{' '}
|
||||
<a href={`${photographer.profile_url}?utm_source=carbon&utm_medium=referral`}>
|
||||
{photographer.name}
|
||||
</a>
|
||||
<style jsx>
|
||||
{`
|
||||
.photo-credit {
|
||||
cursor: unset;
|
||||
user-select: none;
|
||||
text-align: left;
|
||||
font-size: 10px;
|
||||
color: #aaa;
|
||||
margin-bottom: -2px;
|
||||
}
|
||||
|
||||
.photo-credit a {
|
||||
cursor: pointer;
|
||||
text-decoration: underline;
|
||||
}
|
||||
`}
|
||||
</style>
|
||||
</div>
|
||||
)
|
@ -0,0 +1,40 @@
|
||||
require('isomorphic-fetch')
|
||||
const { default: Unsplash, toJson } = require('unsplash-js')
|
||||
|
||||
const WALLPAPER_COLLECTION_ID = 136026
|
||||
|
||||
const client = new Unsplash({
|
||||
applicationId: process.env.UNSPLASH_ACCESS_KEY,
|
||||
secret: process.env.UNSPLASH_SECRET_KEY,
|
||||
callbackUrl: process.env.UNSPLASH_CALLBACK_URL
|
||||
})
|
||||
|
||||
const parseImageResult = img => ({
|
||||
id: img.id,
|
||||
photographer: {
|
||||
name: img.user.name,
|
||||
profile_url: img.user.links.html
|
||||
},
|
||||
url: img.urls.small
|
||||
})
|
||||
|
||||
const getRandomImages = () =>
|
||||
client.photos
|
||||
.getRandomPhoto({
|
||||
collections: [WALLPAPER_COLLECTION_ID],
|
||||
count: 20
|
||||
})
|
||||
.then(toJson)
|
||||
.then(imgs => imgs.map(parseImageResult))
|
||||
|
||||
const downloadImage = imageId =>
|
||||
client.photos
|
||||
.getPhoto(imageId)
|
||||
.then(toJson)
|
||||
.then(client.photos.downloadPhoto)
|
||||
.then(toJson)
|
||||
|
||||
module.exports = {
|
||||
randomImages: (req, res) => getRandomImages().then(imgs => res.json(imgs)),
|
||||
downloadImage: (req, res) => downloadImage(req.params.imageId).then(url => res.json(url))
|
||||
}
|
Loading…
Reference in New Issue