Created
June 16, 2017 16:41
-
-
Save DreySkee/f291a89b3fad14decada97fd41169af7 to your computer and use it in GitHub Desktop.
7 - Wordpress API + ReactJS (Updated)
This file contains hidden or 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
import axios from 'axios'; | |
import alt from 'flux/alt/alt.js'; | |
class DataActions { | |
constructor() { | |
const appUrl = 'http://wordpress-installation-example-url.com'; // Wordpress installation url | |
this.pagesEndPoint = `${appUrl}/wp-json/wp/v2/pages`; // Endpoint for getting Wordpress Pages | |
this.postsEndPoint = `${appUrl}/wp-json/wp/v2/posts`; // Endpoint for getting Wordpress Posts | |
} | |
// Method for getting data from the provided end point url | |
api(endPoint) { | |
return new Promise((resolve, reject) => { | |
axios.get(endPoint).then((response) => { | |
resolve(response.data); | |
}).catch((error) => { | |
reject(error); | |
}); | |
}); | |
} | |
// Method for getting Pages data | |
getPages(cb){ | |
this.api(this.pagesEndPoint).then((response)=>{ | |
this.getPosts(response, cb) | |
}); | |
return true; | |
} | |
// Method for getting Posts data | |
getPosts(pages, cb){ | |
this.api(this.postsEndPoint).then((response)=>{ | |
const posts = response | |
const payload = { pages, posts }; | |
this.getSuccess(payload); // Pass returned data to the store | |
cb(payload); // This callback will be used for dynamic rout building | |
}); | |
return true; | |
} | |
// This returnes an object with Pages and Posts data together | |
// The Alt Store will listen for this method to fire and will store the returned data | |
getSuccess(payload){ | |
return payload; | |
} | |
} | |
export default alt.createActions(DataActions); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment