Skip to content

Instantly share code, notes, and snippets.

@robinheinze
Created July 22, 2019 21:23
Show Gist options
  • Save robinheinze/699d5fec16dcc62cc859c15c726f7ae9 to your computer and use it in GitHub Desktop.
Save robinheinze/699d5fec16dcc62cc859c15c726f7ae9 to your computer and use it in GitHub Desktop.
Question Store Action
export const QuestionStoreModel = types
.model("QuestionStore")
.props({
questions: types.optional(types.array(QuestionModel), []),
})
.extend(withEnvironment)
.views(self => ({})) // eslint-disable-line @typescript-eslint/no-unused-vars
.actions(self => ({
saveQuestions: (questionSnapshots: QuestionSnapshot[]) => {
const questionModels: Question[] = questionSnapshots.map(c => QuestionModel.create(c)) // create model instances from the plain objects
self.questions.replace(questionModels) // Replace the existing data with the new data
},
}))
.actions(self => ({
getQuestions: flow(function*() {
const result: GetQuestionsResult = yield self.environment.api.getQuestions()
if (result.kind === "ok") {
self.saveQuestions(result.questions)
} else {
__DEV__ && console.tron.log(result.kind)
}
}),
})) // eslint-disable-line @typescript-eslint/no-unused-vars
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment