Skip to content

Instantly share code, notes, and snippets.

@Gmousse
Last active April 18, 2017 13:34
Show Gist options
  • Save Gmousse/0943a47d69e962a09cc330b94574c011 to your computer and use it in GitHub Desktop.
Save Gmousse/0943a47d69e962a09cc330b94574c011 to your computer and use it in GitHub Desktop.
Mobx Application Example
/*
The MIT License (MIT)
Copyright (c) 2016 Guillaume Mousnier
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
const { observer } = mobxReact;
class Album extends React.Component {
componentWillMount() {
albumStore.fetchPhotos();
}
render() {
return (
<section style={{display: 'flex', alignItems: 'center', flexDirection: 'column'}}>
<h1>Mon Album Photos</h1>
{ albumStore.status === 'loading' ?
<h2>Chargement des photos...</h2> :
<h2>Nombre de photos dans l'album: {albumStore.photosCount}</h2>
}
{
albumStore.error ? (
<div style={{backgroundColor: 'red', color: 'white'}}>{albumStore.error}</div>
) : (
<div style={{display: 'flex', justifyContent: 'center', flexWrap: 'wrap'}}>
{
albumStore.photos ? albumStore.photos.map(
photo => (
<div style={{display: 'flex', flexDirection: 'column',
marginRight: '1em', marginBottom: '1em'}}>
<span>{photo.name.slice(0, 20)}...</span>
<img src={photo.url} width="200" height="200"/>
<button
style={{backgroundColor: 'red', color: 'white'}}
onClick={() => albumStore.deletePhoto(photo.id)}>
Supprimer
</button>
</div>
)
) : null
}
</div>
)
}
</section>
);
}
}
const AlbumObserver = observer(Album);
ReactDOM.render(<AlbumObserver />, document.getElementById('app'));
const { toJS, extendObservable, computed, action, autorun } = mobx;
class AlbumStore {
constructor() {
this.photos = undefined;
this.error = undefined;
this.status = computed(this._getStatus);
this.photosCount = computed(() => {
if (this.photos) return this.photos.length;
return 0;
});
this.deletePhoto = action(this._deletePhoto);
this.fetchPhotos = action(this._fetchPhotos);
}
_getStatus() {
if (this.photos) return 'ready';
return 'loading';
}
_fetchPhotos() {
this.photos = undefined;
this.error = undefined;
fetch('https://jsonplaceholder.typicode.com/photos', {method: 'GET'})
.then((response) => response.json())
.then((photos) => {
this.photos = photos.slice(0, 300).map(photo => ({
id: uuid.v4(),
name: photo.title,
url: photo.url
}));
}).catch((error) => {
console.error('Request failed:', String(error));
this.error = String(error);
});
}
_deletePhoto(id) {
this.photos = [...this.photos.filter(photo => photo.id !== id)];
}
}
const album = new AlbumStore();
const albumStore = extendObservable(album, album);
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.4.1/react.js" charset="utf-8"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.4.1/react-dom.js" charset="utf-8"></script>
<script src="https://unpkg.com/[email protected]/lib/mobx.umd.js" charset="utf-8"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/mobx-react/4.0.3/index.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/fetch/2.0.1/fetch.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/node-uuid/1.4.7/uuid.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.23/browser.js"></script>
<title></title>
</head>
<body>
<div id="app"></div>
<script src="./albumStore.js" charset="utf-8"></script>
<script src="./album.jsx" type="text/babel" charset="utf-8"></script>
</body>
</html>
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
from livereload import Server
server = Server()
server.watch('./index.html')
server.watch('./album.jsx')
server.watch('./albumStore.js')
server.serve(root='./')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment