import m from "mithril"

var model = {
    current: {},
    error: {},
    fetch: src => {
        model.current = {};
        model.error = {};
        return m.request({
            method: 'get',
            url: '/api/access_log',
            data: { src },
        })
            .then(response => {
                model.current = response;
            })
            .catch(e => {
                if (e.code === 0) {
                    model.error = {
                        message: e.message,
                        errors: {
                            accessLog: ['Terjadi kesalahan saat menghubungkan ke server.']
                        },
                    };
                }
                else
                    model.error = JSON.parse(e.message);
            });
    },
    create: data => {
        model.error = {};
        return m.request({
            method: 'post',
            url: '/api/access_log',
            data,
        })
            .then(response => {
                model.current = response;
            });
    },
};

export default model;
