Adding actions to handle pin events

This commit is contained in:
James Barnsley
2020-08-19 08:35:43 +12:00
parent c2e55cefae
commit 6b2b0850e3
10 changed files with 256 additions and 43 deletions

View File

@ -1,6 +1,6 @@
import React from 'react';
import { useSelector, useDispatch } from 'react-redux'
import * as coreActions from '../../services/core/actions';
import * as pusherActions from '../../services/pusher/actions';
import Icon from '../Icon';
import Button from '../Button';
@ -8,15 +8,17 @@ const PinButton = ({ item }) => {
if (!item || !item.name) return null;
const {
removePinned,
addPinned,
} = coreActions;
removePinned,
} = pusherActions;
const dispatch = useDispatch();
const remove = () => dispatch(removePinned(item.uri));
const add = () => dispatch(addPinned(item));
const isPinned = useSelector((state) => (
state.core.pinned ? state.core.pinned.find((pinnedItem) => pinnedItem.uri === item.uri) : false
state.pusher.pinned
? state.pusher.pinned.find((pinnedItem) => pinnedItem.uri === item.uri)
: false
));
if (isPinned) {

View File

@ -18,7 +18,7 @@ const PinListItem = ({ item }) => {
};
const PinList = () => {
let items = useSelector((state) => state.core.pinned || []);
let items = useSelector((state) => state.pusher.pinned || []);
if (items.length <= 0) return null;
items = sortItems(items, 'name');

View File

@ -202,6 +202,45 @@ export function addQueueMetadata(tlids = [], from_uri = null) {
}
/**
* Pinned URIs
**/
export function getPinned() {
return {
type: 'PUSHER_GET_PINNED',
};
}
export function addPinned(item) {
return {
type: 'PUSHER_ADD_PINNED',
item,
};
}
export function removePinned(uri) {
return {
type: 'PUSHER_REMOVE_PINNED',
uri,
};
}
export function setPinned(pinned) {
return {
type: 'PUSHER_SET_PINNED',
pinned,
};
}
export function pinnedUpdated(pinned) {
return {
type: 'PUSHER_PINNED_UPDATED',
pinned,
};
}
/**
* Commands (buttons)
* */
@ -246,4 +285,4 @@ export function commandsUpdated(commands) {
type: 'PUSHER_COMMANDS_UPDATED',
commands,
};
}
}

View File

@ -109,6 +109,9 @@ const PusherMiddleware = (function () {
case 'commands_changed':
store.dispatch(pusherActions.commandsUpdated(params.commands));
break;
case 'pinned_changed':
store.dispatch(pusherActions.pinnedUpdated(params.pinned));
break;
case 'reload':
window.location.reload(true);
break;
@ -299,6 +302,7 @@ const PusherMiddleware = (function () {
store.dispatch(pusherActions.getConfig());
store.dispatch(pusherActions.getRadio());
store.dispatch(pusherActions.getCommands());
store.dispatch(pusherActions.getPinned());
store.dispatch(pusherActions.getQueueMetadata());
// Give things a few moments to setup before we check for version.
@ -462,13 +466,73 @@ const PusherMiddleware = (function () {
));
},
);
return next(action);
next(action);
break;
/**
* Pinned uris
**/
case 'PUSHER_GET_PINNED':
request(store, 'get_pinned')
.then(
(response) => {
store.dispatch(pusherActions.pinnedUpdated(response.pinned));
},
(error) => {
// We're not too worried about capturing errors here
// It's also likely to fail where UI has been updated but
// server hasn't been restarted yet.
// TODO: Wrap Pusher in 5second wait, like Mopidy
},
);
next(action);
break;
case 'PUSHER_ADD_PINNED':
var pinned = [...pusher.pinned, action.item];
store.dispatch(pusherActions.setPinned(pinned));
break;
case 'PUSHER_SET_PINNED':
request(store, 'set_pinned', { pinned: action.pinned })
.then(
(response) => {
// No action required, the change will be broadcast
},
(error) => {
store.dispatch(coreActions.handleException(
'Could not pin URI',
error,
));
},
);
next(action);
break;
case 'PUSHER_REMOVE_PINNED':
var pinned = pusher.pinned.filter((item) => item.uri !== action.uri);
request(store, 'set_pinned', { pinned })
.then(
(response) => {
// No action required, the change will be broadcast
},
(error) => {
store.dispatch(coreActions.handleException(
'Could not unpin URI',
error,
));
},
);
next(action);
break;
/**
* Commands
* */
* Commands
**/
case 'PUSHER_GET_COMMANDS':
request(store, 'get_commands')
@ -480,6 +544,7 @@ const PusherMiddleware = (function () {
// We're not too worried about capturing errors here
// It's also likely to fail where UI has been updated but
// server hasn't been restarted yet.
// TODO: Wrap Pusher in 5second wait, like Mopidy
},
);
next(action);

View File

@ -1,18 +1,18 @@
export default function reducer(pusher = {}, action) {
switch (action.type) {
switch (action.type) {
case 'PUSHER_CONNECT':
case 'PUSHER_CONNECTING':
return { ...pusher, connected: false, connecting: true };
case 'PUSHER_CONNECTED':
return {
...pusher,
return {
...pusher,
connected: true,
connecting: false,
connection_id: action.connection_id,
client_id: action.client_id,
username: action.username,
username: action.username,
};
case 'PUSHER_DISCONNECTED':
@ -38,11 +38,11 @@ export default function reducer(pusher = {}, action) {
return { ...pusher, connections };
case 'PUSHER_CONNECTION_UPDATED':
return {
...pusher,
return {
...pusher,
username: action.connection.username,
client_id: action.connection.client_id,
connection_id: action.connection.connection_id,
connection_id: action.connection.connection_id,
};
case 'PUSHER_CONNECTION_REMOVED':
@ -51,10 +51,10 @@ export default function reducer(pusher = {}, action) {
return { ...pusher, connections };
case 'PUSHER_VERSION':
return {
...pusher,
return {
...pusher,
version: action.version,
upgrading: false,
upgrading: false,
};
case 'PUSHER_START_UPGRADE':
@ -63,6 +63,9 @@ export default function reducer(pusher = {}, action) {
case 'PUSHER_CONFIG':
return { ...pusher, config: action.config };
case 'PUSHER_PINNED_UPDATED':
return { ...pusher, pinned: action.pinned };
case 'PUSHER_COMMANDS_UPDATED':
return { ...pusher, commands: action.commands };