import API from "./api.js";

export default {
	'submit': {
		click: async (e)=>{
			const input = document.getElementById('input');
			const display = document.getElementById('userRes');
			const jsonArea = document.getElementById('jsonRes');

			const api = new API({ requestURI: '/api/demo-api' });
			const res = await api.send('save', { 
				timestamp: new Date().toISOString(),
				val: input.value 
			});

			if(res.success){
				display.innerHTML = 'Success! Server received: "' + res.req.val + '"';
				jsonArea.value = JSON.stringify(res, null, 4);
			}
			else{
				display.innerHTML = 'Error: ' + res.error.msg;
				jsonArea.value = JSON.stringify(res, null, 4);
			}
		},
	},
	'input': {
		focus: (e)=>{
			e.target.style.borderColor = '#E3C02B';
		},
        blur: (e)=>{
            e.target.style.borderColor = '#ccc';
        }
	}
}
