Moving event bindings to separate components (ideally functional components)

This commit is contained in:
James Barnsley
2019-04-18 09:03:38 +12:00
51 changed files with 15974 additions and 15456 deletions

View File

@ -53,6 +53,12 @@ export function startSearch(search_type, query, only_mopidy = false){
}
export function handleException(message, data = {}, description = null, show_notification = true){
if (!message && data.message){
message = data.message;
}
if (!description && data.description){
description = data.description;
}
return {
type: 'HANDLE_EXCEPTION',
message: message,

View File

@ -216,6 +216,13 @@ export function setCommand(command){
}
}
export function setCommands(commands){
return {
type: 'PUSHER_SET_COMMANDS',
commands: commands
}
}
export function removeCommand(id){
return {
type: 'PUSHER_REMOVE_COMMAND',
@ -223,9 +230,9 @@ export function removeCommand(id){
}
}
export function sendCommand(id, notify = false){
export function runCommand(id, notify = false){
return {
type: 'PUSHER_SEND_COMMAND',
type: 'PUSHER_RUN_COMMAND',
id: id,
notify: notify
}

View File

@ -459,19 +459,23 @@ const PusherMiddleware = (function(){
break
case 'PUSHER_SET_COMMAND':
var commands_index = Object.assign({}, pusher.commands);
var commands = Object.assign({}, pusher.commands);
if (commands_index[action.command.id]){
var command = Object.assign({}, commands_index[action.command.id], action.command);
if (commands[action.command.id]){
var command = Object.assign({}, commands[action.command.id], action.command);
} else {
var command = action.command;
}
commands_index[action.command.id] = command;
commands[action.command.id] = command;
request(store, 'set_commands', {commands: commands_index})
store.dispatch(pusherActions.setCommands(commands));
break
case 'PUSHER_SET_COMMANDS':
request(store, 'set_commands', {commands: action.commands})
.then(
response => {
// No action required, the change will be broadcast
// No action required, the change will be broadcast
},
error => {
store.dispatch(coreActions.handleException(
@ -504,38 +508,28 @@ const PusherMiddleware = (function(){
next(action);
break
case 'PUSHER_SEND_COMMAND':
case 'PUSHER_RUN_COMMAND':
var command = Object.assign({}, pusher.commands[action.id]);
var notification_key = 'command_'+action.id;
if (action.notify){
store.dispatch(uiActions.startProcess(notification_key, 'Sending command'));
store.dispatch(uiActions.startProcess(notification_key, 'Running command'));
}
try {
var ajax_settings = JSON.parse(command.command);
} catch(error){
store.dispatch(uiActions.createNotification({key: notification_key, type: 'bad', content: 'Command failed', description: error}));
break;
}
// Handle success and failure
ajax_settings.success = function(response){
console.log("Command sent, response was:",response);
if (action.notify){
request(store, 'run_command', {id: action.id})
.then(response => {
store.dispatch(uiActions.processFinished(notification_key));
store.dispatch(uiActions.createNotification({key: notification_key, type: 'info', content: 'Command sent'}));
}
}
ajax_settings.error = function(xhr, status, error){
console.error("Command failed, response was:",xhr,error);
store.dispatch(uiActions.processFinished(notification_key));
store.dispatch(uiActions.createNotification({key: notification_key, type: 'bad', content: 'Command failed', description: xhr.status+": "+error}));
}
// Actually send the request
$.ajax(ajax_settings);
if (action.notify){
store.dispatch(uiActions.createNotification({key: notification_key, type: 'info', content: 'Command sent'}));
}
},
error => {
store.dispatch(uiActions.processFinished(notification_key));
store.dispatch(coreActions.handleException(
'Could not run command',
error
));
})
break
@ -553,7 +547,7 @@ const PusherMiddleware = (function(){
radio: response.radio
});
},
error => {
error => {
store.dispatch(coreActions.handleException(
'Could not load radio',
error

View File

@ -172,7 +172,12 @@ export function createNotification(data){
// Allow 'message' as an alias of our content
if (data.message){
data.content = data.message;
}
}
// Shortcut notifications are short and sweet
if (data.type == 'shortcut'){
data.duration = 0.4;
}
return {
type: 'CREATE_NOTIFICATION',