Giter Club home page Giter Club logo

Comments (8)

frankcalise avatar frankcalise commented on June 4, 2024

Ignite 7.15 is quite old, is this a new project or one that's been around for some time?

If the latter, if you could provide a snippet of what you have that would be helpful.

If it's a new project and you're just starting out, I'd try using the latest version and see if you run into the same issue.

from ignite.

Zilleabbas10 avatar Zilleabbas10 commented on June 4, 2024

@frankcalise It's a new project and Sorry ignite version is 9.2.0

So Let me add some details.
Let's suppose I am using the same code of boilerplate

This is my AuthenticationStore file

import { Instance, SnapshotOut, types } from "mobx-state-tree"

export const AuthenticationStoreModel = types
  .model("AuthenticationStore")
  .props({
    authToken: types.maybe(types.string),
    authEmail: "",
  })
  .views((store) => ({
    get isAuthenticated() {
      return !!store.authToken
    },
  }))
  .actions((store) => ({
    setAuthToken(value?: string) {
      store.authToken = value
    }
  }))

export interface AuthenticationStore extends Instance<typeof AuthenticationStoreModel> {}
export interface AuthenticationStoreSnapshot extends SnapshotOut<typeof AuthenticationStoreModel> {}

And This is my EpisodeStore file

import { Instance, SnapshotOut, types } from "mobx-state-tree"
import { api } from "../services/api"
import { Episode, EpisodeModel } from "./Episode"
import { withSetPropAction } from "./helpers/withSetPropAction"

export const EpisodeStoreModel = types
  .model("EpisodeStore")
  .props({
    episodes: types.array(EpisodeModel),
    favorites: types.array(types.reference(EpisodeModel)),
    favoritesOnly: false,
  })
  .actions(withSetPropAction)
  .actions((store) => ({
    async fetchEpisodes() {


// here I want to use action function `setAuthToken` from AuthenticationStore, How can I use here??


      const response = await api.getEpisodes()
      if (response.kind === "ok") {
        store.setProp("episodes", response.episodes)
      } else {
        console.error(`Error fetching episodes: ${JSON.stringify(response)}`)
      }
    },
  }))

export interface EpisodeStore extends Instance<typeof EpisodeStoreModel> {}
export interface EpisodeStoreSnapshot extends SnapshotOut<typeof EpisodeStoreModel> {}

In EpisodeStore file, I put a comment in action fetchEpisodes, Here I want to use setAuthToken from AuthenticationStore, How can I access this method??

Do I make my point clear??

from ignite.

frankcalise avatar frankcalise commented on June 4, 2024

@Zilleabbas10 hey, I think this Ignite Cookbook Recipe can help you out: https://ignitecookbook.com/docs/recipes/DistributingAuthTokenToAPI

from ignite.

Zilleabbas10 avatar Zilleabbas10 commented on June 4, 2024

@frankcalise In this example we are calling distributeAuthToken function from AuthenticationStore in a component. But in my case I need to call this distributeAuthToken function in my another store directly.

from ignite.

frankcalise avatar frankcalise commented on June 4, 2024

@Zilleabbas10 So what that recipe does is, upon setting the API token, your api requests made in other stores will already have that info filled in for you. So you don't need to obtain it and pass it in every call in other stores

from ignite.

Zilleabbas10 avatar Zilleabbas10 commented on June 4, 2024

@Zilleabbas10 Let suppose I have a LoadingStore file like the following

import { Instance, SnapshotOut, types } from "mobx-state-tree"

export const LoadingStoreModel = types
  .model("LoadingStore")
  .props({
    isLoading: types.maybe(types.boolean),
  })
  .views((store) => ({
  }))
  .actions((store) => ({
    setLoading(value?: boolean) {
      store.isLoading = value
    }
  }))

export interface LoadingStore extends Instance<typeof LoadingStoreModel> {}
export interface LoadingStoreSnapshot extends SnapshotOut<typeof LoadingStoreModel> {}

I need to call the action of this loadingStore.setLoading(true) before I call any API in some other store. How can I do this??

I think I am pretty more clear now??

from ignite.

frankcalise avatar frankcalise commented on June 4, 2024

You can get to other stores by first getting the root store using the included helper:

#2503 (comment)

from ignite.

Zilleabbas10 avatar Zilleabbas10 commented on June 4, 2024

@frankcalise thanks man. This is what I was looking for. Really appreciated your help 👍

from ignite.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.