import nanoid from '../nanoid.min.js'; import api from '../api.js'; const preventDblClickSelection = function(e){ // as per [https://stackoverflow.com/a/43321596] if(e.detail>1){ e.preventDefault(); } } const load_notes = function(state, dispatch){ api.post('/load-notes', {bin_id: state.bin_id}) .then(res=>{ if(res.success===true){ dispatch('notes-loaded', res.notes); } else if(res.authorized===false){ dispatch('notes-unauthorized'); } }); }; const runSearch = function(state, dispatch){ api.post('/search', {search_term: state.search_term, sorting: state.sorting, bin_id: state.bin_id}) .then(res=>{ dispatch('update-search-results', res.notes); }); }; const new_note_handler = function(state, dispatch){ dispatch('add-note', {id: nanoid(), date: Date.now()}); }; const new_note_by_dblclick_handler = function(state, dispatch, e){ e.preventDefault(); // for nothing gets highlighted dispatch('add-note', {id: nanoid(), date: Date.now()}); }; const search_term_change_handler = function(state, dispatch, e){ if(e.code === 'Enter'){ runSearch(state, dispatch); } else{ dispatch('update-search-term', e.target.value); } }; const sorting_change_handler = function(state, dispatch, e){ runSearch(state, dispatch); dispatch('update-sorting', e.target.value); }; const new_bin_handler = function(state, dispatch){ const id = nanoid(); // change browser location in address bar: window.history.pushState(null,'','#'+id); dispatch('new-bin', {id, name:id}); }; const username_change_handler = function(state, dispatch, e){ dispatch('update-username', e.target.value); }; const password_change_handler = function(state, dispatch, e){ dispatch('update-password', e.target.value); }; const login_request_handler = function(state, dispatch, e){ dispatch('login-requested'); api.post('/login', {username: state.login.username, password: state.login.password}) .then(res=>{ if(res.success===true){ dispatch('login-succeeded', {user:res.user, session_id:res.session_id}); dispatch('user-bin-list-loaded', res.bins); } else{ dispatch('login-failed'); } }, e=>{ dispatch('login-failed'); }); }; const logout_request_handler = function(state, dispatch, e){ api.post('/logout', {}); // we dispatch after the API call because the logout action clears the session_id, which is needed to logout dispatch('logout-requested'); // after logout, clear the bin: new_bin_handler(state, dispatch); }; const bin_name_editing_toggle_button_handler = function(state, dispatch){ dispatch('update-bin-name-editing', !state.is_editing_bin_name); }; const bin_name_change_handler = function(state, dispatch, e){ dispatch('update-bin-name', e.target.value); }; const bin_name_commit_handler = function(state, dispatch){ api.post('/bin-rename', {bin_id:state.bin_id, name:state.temp_bin_name}); dispatch('commit-bin-name'); }; const choose_bin_handler = function(state, dispatch, bin_id){ window.history.pushState(null,'','#'+bin_id); dispatch('bin-requested', bin_id); api.post('/load-bin', {bin_id}) .then(res=>{ if(res.success===true){ dispatch('bin-loaded', res.bin); api.post('/load-notes', {bin_id}) .then(res=>{ if(res.success===true && res.authorized===true){ dispatch('notes-loaded', res.notes); } else if(res.success===false && res.authorized===false){ dispatch('notes-unauthorized'); } }); } else if(res.success===false && res.authorized==false){ dispatch('bin-unauthorized'); } }); }; export {preventDblClickSelection, new_note_handler, new_note_by_dblclick_handler, search_term_change_handler, sorting_change_handler, load_notes, new_bin_handler, username_change_handler, password_change_handler, login_request_handler, logout_request_handler, bin_name_editing_toggle_button_handler, bin_name_change_handler, bin_name_commit_handler, choose_bin_handler };