Guest User

Untitled

a guest
Aug 16th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  1. import { observable, action, toJS, values, decorate } from "mobx";
  2. import { getTransactions } from "../Services/transactionsRequests";
  3.  
  4. class Transactions {
  5. constructor() {
  6. this.data = observable.map({});
  7. this.fetchData();
  8. }
  9. set(key, transaction) {
  10. this.data.set(key, transaction);
  11. }
  12. remove(key) {
  13. this.data.delete(key);
  14. }
  15. getSorted(property = "date", order = "ascending") {
  16. return toJS([...this.data.values()]).sort((a, b) => {
  17. return order === "ascending"
  18. ? a[property] - b[property]
  19. : b[property] - a[property];
  20. });
  21. }
  22. async fetchData() {
  23. console.log("fetch data");
  24. const transactions = await getTransactions();
  25. transactions.forEach(transaction => {
  26. this.data.set(transaction._id, transaction);
  27. });
  28. }
  29. }
  30.  
  31. export default decorate(Transactions, {
  32. data: observable.shallow,
  33. set: action,
  34. remove: action,
  35. getSorted: action,
  36. fetchData: action
  37. });
Add Comment
Please, Sign In to add comment