2015-06-05 11:06:36 +02:00
|
|
|
'use strict';
|
|
|
|
|
2015-10-06 16:47:59 +02:00
|
|
|
import { alt } from '../alt';
|
2016-01-14 13:20:45 +01:00
|
|
|
|
2015-06-08 18:14:25 +02:00
|
|
|
import PieceActions from '../actions/piece_actions';
|
2015-05-26 10:41:41 +02:00
|
|
|
|
2016-01-14 13:20:45 +01:00
|
|
|
import PieceSource from '../sources/piece_source';
|
|
|
|
|
2015-05-26 10:41:41 +02:00
|
|
|
|
|
|
|
class PieceStore {
|
|
|
|
constructor() {
|
2016-01-14 13:20:45 +01:00
|
|
|
this.getInitialState();
|
|
|
|
|
2015-06-08 18:14:25 +02:00
|
|
|
this.bindActions(PieceActions);
|
2016-01-14 13:20:45 +01:00
|
|
|
this.registerAsync(PieceSource);
|
|
|
|
this.exportPublicMethods({
|
|
|
|
getInitialState: this.getInitialState.bind(this)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
getInitialState() {
|
|
|
|
this.piece = {};
|
|
|
|
this.pieceMeta = {
|
2016-01-15 14:22:28 +01:00
|
|
|
err: null
|
2016-01-14 13:20:45 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
|
|
|
piece: this.piece,
|
|
|
|
pieceMeta: this.pieceMeta
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-15 14:22:28 +01:00
|
|
|
onFetchPiece(pieceId) {
|
|
|
|
this.getInstance().lookupPiece(pieceId);
|
2016-01-14 13:20:45 +01:00
|
|
|
|
2016-01-15 14:22:28 +01:00
|
|
|
// Prevent alt from sending an empty change event when a request is sent
|
|
|
|
// off to the source
|
|
|
|
this.preventDefault();
|
2016-01-14 13:20:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
onSuccessFetchPiece({ piece }) {
|
|
|
|
if (piece) {
|
|
|
|
this.onUpdatePiece(piece);
|
|
|
|
} else {
|
|
|
|
this.pieceMeta.err = new Error('Problem fetching the piece');
|
2016-01-15 14:22:28 +01:00
|
|
|
console.logGlobal(this.pieceMeta.err);
|
2016-01-14 13:20:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
onErrorPiece(err) {
|
2016-01-15 14:22:28 +01:00
|
|
|
console.logGlobal(err);
|
2016-01-14 13:20:45 +01:00
|
|
|
this.pieceMeta.err = err;
|
|
|
|
}
|
|
|
|
|
|
|
|
onFlushPiece() {
|
|
|
|
this.getInitialState();
|
2015-05-26 10:41:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
onUpdatePiece(piece) {
|
|
|
|
this.piece = piece;
|
2016-01-14 13:20:45 +01:00
|
|
|
this.pieceMeta.err = null;
|
2015-05-26 10:41:41 +02:00
|
|
|
}
|
2015-07-13 18:13:16 +02:00
|
|
|
|
2016-01-14 13:20:45 +01:00
|
|
|
onUpdateProperty({ key, value }) {
|
2016-01-15 14:22:28 +01:00
|
|
|
if (this.piece && key in this.piece) {
|
2015-07-13 18:13:16 +02:00
|
|
|
this.piece[key] = value;
|
|
|
|
} else {
|
|
|
|
throw new Error('There is no piece defined in PieceStore or the piece object does not have the property you\'re looking for.');
|
|
|
|
}
|
|
|
|
}
|
2015-05-26 13:33:35 +02:00
|
|
|
}
|
2015-05-26 10:41:41 +02:00
|
|
|
|
2015-06-15 08:44:44 +02:00
|
|
|
export default alt.createStore(PieceStore, 'PieceStore');
|