import m from "mithril"

const QuestionAnswer = {
    list: [],
    current: {},
    loading: false,
    fetch: function(questionId) {
        QuestionAnswer.loading = true
        m.request({
            url: "http://api.questionnaire.dev/v1/questions/" + questionId + "/answers",
            method: "GET"
        })
        .then(function(res) {
            res.data = res.data[0]
            QuestionAnswer.current = res
            QuestionAnswer.loading = false
        })
    },
    upload: function(questionId) {
        QuestionAnswer.loading = true
        questionId = questionId || QuestionAnswer.current.questionId
        m.request({
            url: "http://api.questionnaire.dev/v1/questions/" + questionId + "/answers",
            method: "POST",
            data: QuestionAnswer.current
        })
        .then(function(res) {
            QuestionAnswer.loading = false
        })
    }
}

export default QuestionAnswer
