Skip to content

Commit 3150c53

Browse files
author
Sinu John
authored
feat(api response transformer for load more stories base): Accept apiResponseTransformer as a prop (#77)
* feat(api response transformer for load more stories base) * update read me with usage
1 parent afe0483 commit 3150c53

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,8 @@ import { Link } from '@quintype/components';
252252

253253
This component starts with a set of stories, and then provides a load more button. This calls out to `/api/v1/stories` with the properties passed via the `params` prop. The stories are concatenated with the stories in `props.data.stories`, and the contents of `props.data` are passed to the rendered template.
254254

255+
It can accept an alternate `api` as a prop as well as `apiResponseTransformer` which can be used to tranformer the api response before being passed to the `template`.
256+
255257
```javascript
256258
import { LoadMoreStoriesBase } from '@quintype/components';
257259

@@ -263,7 +265,9 @@ export function SectionPage(props) {
263265
return <LoadMoreStoriesBase template={SectionPageWithStories}
264266
fields={"id,headline"}
265267
{...props}
266-
params={{"section-id": props.data.section.id}}/>
268+
params={{"section-id": props.data.section.id}}
269+
api="/api/v1/stories"
270+
apiResponseTransformer={(response) => response.stories} />
267271
}
268272
```
269273

src/components/load-more-stories-base.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,12 @@ export class LoadMoreStoriesBase extends React.Component {
5353
offset: this.props.numStoriesToLoad * (pageNumber - 1) + stories.length,
5454
limit: this.props.numStoriesToLoad || 10,
5555
fields: this.props.fields
56-
})).json(response => response.stories || get(response, ['results', 'stories'], []));
56+
})).json(response => {
57+
if (this.props.apiResponseTransformer) {
58+
return this.props.apiResponseTransformer(response)
59+
}
60+
return response.stories || get(response, ['results', 'stories'], []);
61+
});
5762
}
5863

5964
render() {

0 commit comments

Comments
 (0)