Created
July 22, 2019 21:23
-
-
Save robinheinze/699d5fec16dcc62cc859c15c726f7ae9 to your computer and use it in GitHub Desktop.
Question Store Action
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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