Skip to content

Commit

Permalink
Add mock client for Contentful
Browse files Browse the repository at this point in the history
Contentful provide a client via the contentful npm package and they
provide some boilerplate code. This commit aims to mirror the client's
interface (only one method at the moment, for Pride events) and return
data from the mock server.

https://github.com/contentful/contentful.js

Add `browser: true` to eslintrc file, otherwise it complains that the `fetch`
method in MockClient has not been defined.
See eslint/eslint#4015
  • Loading branch information
davidbasalla committed Jan 12, 2018
1 parent 2f3e4d1 commit 305f7db
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
3 changes: 2 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"env": {
"jest": true,
"node": true
"node": true,
"browser": true,
},
"extends": ["airbnb", "prettier"],
"parserOptions": {
Expand Down
13 changes: 8 additions & 5 deletions App.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import React, { Component } from "react";
import { ActivityIndicator, FlatList, Text, View } from "react-native";
import MockClient from "./MockClient";

const API_URL = "http://192.168.1.4:3000/events";
const client = MockClient; // Swap this out for the Contentful client in prod
const EVENTS_CONTENT_TYPE_SYS_ID = 123; // This will eventually be dynamic, based on the id in Contentful

export default class App extends Component {
constructor(props) {
Expand All @@ -12,16 +14,17 @@ export default class App extends Component {
}

componentDidMount() {
return fetch(API_URL)
.then(response => response.json())
return client
.getEntries({
content_type: EVENTS_CONTENT_TYPE_SYS_ID
})
.then(responseJson => {
console.log(responseJson);
this.setState(
{
isLoading: false,
dataSource: responseJson
},
function() {
() => {
// do something with new state
}
);
Expand Down
12 changes: 12 additions & 0 deletions MockClient.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const API_URL = "http://192.168.1.4:3000";
const SYS_ID_TO_URL_MAPPINGS = {
123: "/events"
};

export default class MockClient {
static getEntries(contentTypes) {
const contentTypeUrl = SYS_ID_TO_URL_MAPPINGS[contentTypes.content_type];

return fetch(API_URL + contentTypeUrl).then(response => response.json());
}
}

0 comments on commit 305f7db

Please sign in to comment.