Last active
May 12, 2017 16:20
-
-
Save alexvcasillas/5c312088a76dfa9023cb749c34cc1ce7 to your computer and use it in GitHub Desktop.
Migrating mobx to mobx-state-tree
This file contains 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
/** | |
* mobx store migration to mobx-state-tree system | |
**/ | |
// Original MobX Store | |
import { observable, action, runInAction } from 'mobx'; | |
export default class GithubStore { | |
@observable searchName; | |
@observable user; // Object with all the user data that comes from the Github API Fetch | |
@observable repos; // Array of Repositories that comes from the Github API Fetch | |
@observable fetchingData; | |
constructor() { | |
this.searchName = ''; | |
this.user = ''; | |
this.repos = []; | |
this.fetchingData = false; | |
} | |
async fetchFromGithub(endpoint) { | |
const url = `https://api.github.com${endpoint}?client_id=my_client_id&client_secret=my_secret_id`; | |
const response = await fetch(url); | |
return await response.json(); | |
} | |
@action('Change User to search for') | |
changeUserToSearchFor(username) { | |
this.searchName = username; | |
} | |
@action('Search for user on Github') | |
searchForUser = async () => { | |
if (!this.searchName || (this.user && this.searchName === this.user.login)) | |
return; | |
this.fetchingData = true; | |
const [user, repos] = await Promise.all([ | |
this.fetchFromGithub(`/users/${this.searchName}`), | |
this.fetchFromGithub(`/users/${this.searchName}/repos`) | |
]); | |
runInAction("Update State after fetching Github's Data", () => { | |
this.user = user; | |
this.repos = repos; | |
this.fetchingData = false; | |
}); | |
}; | |
} | |
// ------------------------------------------------------------------ | |
// mobx-state-tree Store | |
import { types } from 'mobx-state-tree'; | |
const GithubStore = types.model( | |
'GithubStore', | |
{ | |
searchName: types.string, | |
user: type.frozen, // Object with all the user data that comes from the Github API Fetch | |
repos: type.array, // Array of Repositories that comes from the Github API Fetch | |
fetchingData: type.bool | |
}, | |
{ | |
changeUserToSearchFor(username) { | |
this.searchName = username; | |
}, | |
async fetchFromGithub(endpoint) { | |
const url = `https://api.github.com${endpoint}?client_id=my_client_id&client_secret=my_secret_id`; | |
const response = await fetch(url); | |
return await response.json(); | |
}, | |
async searchForUser() { | |
if ( | |
!this.searchName || (this.user && this.searchName === this.user.login) | |
) | |
return; | |
this.fetchingData = true; | |
const [user, repos] = await Promise.all([ | |
this.fetchFromGithub(`/users/${this.searchName}`), | |
this.fetchFromGithub(`/users/${this.searchName}/repos`) | |
]); | |
runInAction("Update State after fetching Github's Data", () => { | |
this.user = user; | |
this.repos = repos; | |
this.fetchingData = false; | |
}); | |
} | |
} | |
); | |
export default GithubStore; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment