langbung01

merchant store

Jul 29th, 2021 (edited)
495
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import {action, computed, makeObservable, observable, runInAction} from "mobx";
  2. import {merchantApi, serverApi} from "../../api";
  3.  
  4. export default class MerchantStore {
  5.     merchant = []
  6.     async constructor() {
  7.         makeObservable(this, {
  8.             merchant: observable,
  9.             getMerchant: action
  10.         });
  11.         await this.getMerchant()
  12.     }
  13.     async getMerchant() {
  14.         try {
  15.             let page_state = null
  16.             let firstFetch = true
  17.             let merchant = []
  18.             while (page_state || firstFetch) {
  19.                 const { data } = await merchantApi().get('/v1/merchants', {params:{firm:"SHS",page_count:100}});
  20.                 merchant = [...merchant, data.merchant]
  21.                 page_state = data.page_state
  22.                 firstFetch = false
  23.             }
  24.            
  25.             // after await, modifying state again, needs an actions:
  26.             runInAction(async () => {
  27.                 this.merchant = merchant
  28.             })
  29.         } catch (error) {
  30.             runInAction(() => {
  31.                 this.state = "error";
  32.                 this.message = error.response.data.message
  33.             })
  34.         }
  35.     }
  36.     filterMerchant = (filter) => {
  37.         return this.merchant.filter(merchant => {
  38.             return filter.data === merchant.service
  39.         })
  40.     }
  41. }
Add Comment
Please, Sign In to add comment