import m from "mithril"

const Respondent = {
    list: [],
    current: {},
    loading: false,
    fetch: function(id) {
        Respondent.loading = true
        m.request({
            url: "http://api.questionnaire.dev/v1/respondents/" + id,
            method: "GET"
        })
        .then(function(res) {
            Respondent.current = res
            Respondent.loading = false
        })
    },
    fetchByEmail: function(email) {
        Respondent.loading = true
        m.request({
            url: "http://api.questionnaire.dev/v1/respondents/search/"+email,
            method: "GET"
        })
        .then(function(res) {
            Respondent.current = res
            Respondent.loading = false
            console.log(Respondent.current, res);
        })
    },
    save: function(data) {
        m.request({
            url: "http://api.questionnaire.dev/v1/respondents",
            method: "POST",
            data
        })
        .then(function(res) {
            Respondent.current = res
            m.route.set('/questionnaires')
        })
    }
}

export default Respondent
