Skip to content

Instantly share code, notes, and snippets.

@glortho
Last active July 21, 2017 21:26
Show Gist options
  • Save glortho/322ace3e8815ce0a01d5f090cce2b628 to your computer and use it in GitHub Desktop.
Save glortho/322ace3e8815ce0a01d5f090cce2b628 to your computer and use it in GitHub Desktop.
import React from 'react';
import { apiDecorator } from 'jetset';
const doAsyncStuff = data => new Promise( resolve =>
setTimeout(() =>
resolve( data.map( item => ({ ...item, foo: 'bar' }) ) )
)
);
const users = apiDecorator({
url: 'https://jsonplaceholder.typicode.com',
users: {
routes: {
default: '/users',
getUserAlbums: id => ({ method: 'get', route: `/users/${id}/albums`, usesCache: true }),
list: () => ({ onSuccess: doAsyncStuff })
}
}
});
@users
export default class ApiCustomExample extends React.Component {
render() {
const { getUserAlbums, list } = this.props.users;
return (
<div>
<h3>Albums for user 1</h3>
<div>
{ getUserAlbums( 1 ).map(({ data }) =>
<div key={ data.id }>{ data.title }</div>
)}
</div>
<h3>Users with extra attrs added</h3>
<div>
{ list().data.map(({ data }) =>
<div key={ data.id }>{ data.name } foo: { data.foo }</div>
)}
</div>
</div>
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment