Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { Instance, SnapshotOut, types, flow, applySnapshot } from "mobx-state-tree"
- import { PlaceModel, PlaceSnapshot, Place } from "../place/place"
- import { withEnvironment } from "../extensions"
- import { GetPlacesResult } from "../../services/api"
- import { action, observable, runInAction } from 'mobx'
- /**
- * Model description here for TypeScript hints.
- */
- export const PlaceStoreModel = types
- .model("PlaceStore")
- .props({
- places: types.optional(types.array(PlaceModel), [])
- })
- .extend(withEnvironment)
- .views(self => ({
- getAll() {
- return self.places
- },
- getPaginationPlaces(pagination) {
- return self.places.slice(0, pagination)
- },
- getPlace(id) {
- return self.places.find((obj) => obj.id === id)
- },
- getContentByLanguage(langCode) {
- return self.places.filter((obj) => obj.language === langCode)
- },
- getFavoritedEvents() {
- const favoriteEvents = self.places.filter(function (e) {
- return e.favorite === true
- })
- return favoriteEvents
- },
- })) // eslint-disable-line @typescript-eslint/no-unused-vars
- .actions(self => ({
- savePlaces: (placesSnapshots) => {
- applySnapshot(self.places, placesSnapshots)
- },
- toggleFavorite(id) {
- const index = self.places.findIndex((e) => e.id === id)
- return self.places[index].toggleFavorite()
- },
- toggleVisited(id) {
- const index = self.places.findIndex((e) => e.id === id)
- return self.places[index].toggleVisited()
- },
- getPlaces: flow(function * getPlaces() {
- try {
- const result: GetPlacesResult = yield self.environment.api.getPlaces()
- if (result.kind === "ok") {
- self.savePlaces(result.places)
- } else {
- __DEV__ && console.tron.log(result.kind)
- }
- } catch (error) {
- console.log(error)
- }
- }),
- }))
- /**
- * Un-comment the following to omit model attributes from your snapshots (and from async storage).
- * Useful for sensitive data like passwords, or transitive state like whether a modal is open.
- * Note that you'll need to import `omit` from ramda, which is already included in the project!
- * .postProcessSnapshot(omit(["password", "socialSecurityNumber", "creditCardNumber"]))
- */
- type PlaceStoreType = Instance<typeof PlaceStoreModel>
- export interface PlaceStore extends PlaceStoreType { }
- type PlaceStoreSnapshotType = SnapshotOut<typeof PlaceStoreModel>
- export interface PlaceStoreSnapshot extends PlaceStoreSnapshotType { }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement