Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2020
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { Instance, SnapshotOut, types, flow, applySnapshot } from "mobx-state-tree"
  2. import { PlaceModel, PlaceSnapshot, Place } from "../place/place"
  3. import { withEnvironment } from "../extensions"
  4. import { GetPlacesResult } from "../../services/api"
  5. import { action, observable, runInAction } from 'mobx'
  6.  
  7. /**
  8.  * Model description here for TypeScript hints.
  9.  */
  10. export const PlaceStoreModel = types
  11.   .model("PlaceStore")
  12.   .props({
  13.     places: types.optional(types.array(PlaceModel), [])
  14.   })
  15.   .extend(withEnvironment)
  16.   .views(self => ({
  17.     getAll() {
  18.       return self.places
  19.     },
  20.     getPaginationPlaces(pagination) {
  21.       return self.places.slice(0, pagination)
  22.     },
  23.     getPlace(id) {
  24.       return self.places.find((obj) => obj.id === id)
  25.     },
  26.     getContentByLanguage(langCode) {
  27.       return self.places.filter((obj) => obj.language === langCode)
  28.     },
  29.     getFavoritedEvents() {
  30.       const favoriteEvents = self.places.filter(function (e) {
  31.         return e.favorite === true
  32.       })
  33.  
  34.       return favoriteEvents
  35.     },
  36.   })) // eslint-disable-line @typescript-eslint/no-unused-vars
  37.   .actions(self => ({
  38.     savePlaces: (placesSnapshots) => {
  39.       applySnapshot(self.places, placesSnapshots)
  40.     },
  41.     toggleFavorite(id) {
  42.       const index = self.places.findIndex((e) => e.id === id)
  43.       return self.places[index].toggleFavorite()
  44.     },
  45.     toggleVisited(id) {
  46.       const index = self.places.findIndex((e) => e.id === id)
  47.       return self.places[index].toggleVisited()
  48.     },
  49.     getPlaces: flow(function * getPlaces() {
  50.       try {
  51.         const result: GetPlacesResult = yield self.environment.api.getPlaces()
  52.         if (result.kind === "ok") {
  53.           self.savePlaces(result.places)
  54.         } else {
  55.           __DEV__ && console.tron.log(result.kind)
  56.         }
  57.       } catch (error) {
  58.         console.log(error)
  59.       }
  60.     }),
  61.   }))
  62.  
  63. /**
  64. * Un-comment the following to omit model attributes from your snapshots (and from async storage).
  65. * Useful for sensitive data like passwords, or transitive state like whether a modal is open.
  66.  
  67. * Note that you'll need to import `omit` from ramda, which is already included in the project!
  68. *  .postProcessSnapshot(omit(["password", "socialSecurityNumber", "creditCardNumber"]))
  69. */
  70.  
  71. type PlaceStoreType = Instance<typeof PlaceStoreModel>
  72. export interface PlaceStore extends PlaceStoreType { }
  73. type PlaceStoreSnapshotType = SnapshotOut<typeof PlaceStoreModel>
  74. export interface PlaceStoreSnapshot extends PlaceStoreSnapshotType { }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement