Browse Source

Add custom handler for *sonic error message

master
jeffvli 3 years ago
parent
commit
4b53ae8fef
  1. 15
      src/components/playlist/PlaylistView.tsx
  2. 17
      src/components/shared/ContextMenu.tsx
  3. 32
      src/shared/utils.ts

15
src/components/playlist/PlaylistView.tsx

@ -1,5 +1,4 @@
import React, { useEffect, useState } from 'react';
import _ from 'lodash';
import fs from 'fs';
import path from 'path';
import settings from 'electron-settings';
@ -38,7 +37,9 @@ import {
} from '../../redux/multiSelectSlice';
import {
createRecoveryFile,
errorMessages,
getRecoveryPath,
isFailedResponse,
moveToIndex,
} from '../../shared/utils';
import useSearchQuery from '../../hooks/useSearchQuery';
@ -180,15 +181,15 @@ const PlaylistView = ({ ...rest }) => {
// Tested on Airsonic instances, /createPlaylist fails with around ~350+ songId params
res = await clearPlaylist(data.id);
if (res.status === 'failed') {
notifyToast('error', res.error.message);
if (isFailedResponse(res)) {
notifyToast('error', errorMessages(res)[0]);
} else {
res = await updatePlaylistSongsLg(data.id, playlistData);
if (_.map(res, 'status').includes('failed')) {
if (isFailedResponse(res)) {
res.forEach((response) => {
if (response.status === 'failed') {
return notifyToast('error', response.error);
if (isFailedResponse(response)) {
return notifyToast('error', errorMessages(response)[0]);
}
return false;
});
@ -220,7 +221,7 @@ const PlaylistView = ({ ...rest }) => {
try {
const res = await deletePlaylist(data.id);
if (res.status === 'failed') {
if (isFailedResponse(res)) {
notifyToast('error', res.error.message);
} else {
history.push('/playlist');

17
src/components/shared/ContextMenu.tsx

@ -4,7 +4,12 @@ import _ from 'lodash';
import { useQuery, useQueryClient } from 'react-query';
import { useHistory } from 'react-router';
import { Popover, Whisper } from 'rsuite';
import { getPlaylists, populatePlaylist, star, unstar } from '../../api/api';
import {
getPlaylists,
updatePlaylistSongsLg,
star,
unstar,
} from '../../api/api';
import { useAppDispatch, useAppSelector } from '../../redux/hooks';
import {
addProcessingPlaylist,
@ -25,7 +30,7 @@ import {
StyledButton,
} from './styled';
import { notifyToast } from './toast';
import { sleep } from '../../shared/utils';
import { errorMessages, isFailedResponse, sleep } from '../../shared/utils';
export const ContextMenuButton = ({ text, children, ...rest }: any) => {
return (
@ -107,13 +112,13 @@ export const GlobalContextMenu = () => {
);
try {
const res = await populatePlaylist(
const res = await updatePlaylistSongsLg(
localSelectedPlaylistId,
sortedEntries
);
if (res.status === 'failed') {
notifyToast('error', res.error.message);
if (isFailedResponse(res)) {
notifyToast('error', errorMessages(res)[0]);
} else {
notifyToast(
'success',
@ -140,7 +145,7 @@ export const GlobalContextMenu = () => {
);
}
} catch (err) {
console.log(err);
notifyToast('error', err);
}
dispatch(removeProcessingPlaylist(localSelectedPlaylistId));

32
src/shared/utils.ts

@ -1,4 +1,5 @@
import fs from 'fs';
import _ from 'lodash';
import path from 'path';
import moment from 'moment';
import settings from 'electron-settings';
@ -46,6 +47,37 @@ export const createRecoveryFile = (id: any, type: string, data: any) => {
fs.writeFileSync(filePath, JSON.stringify(data, null, 4), 'utf-8');
};
export const isFailedResponse = (res: any) => {
if (res.length >= 1) {
const statuses = _.map(res, 'status');
if (statuses.includes('failed')) {
return true;
}
} else if (res.status === 'failed') return true;
return false;
};
export const errorMessages = (res: any) => {
const errors: any[] = [];
if (res.length >= 1) {
const statuses = _.map(res, 'status');
if (statuses.includes('failed')) {
res.forEach((response: any) => {
if (response.status === 'failed') {
errors.push(response.error.message);
}
});
}
} else {
errors.push(res.error.message);
}
return errors;
};
export const shuffle = (array: any[]) => {
let currentIndex = array.length;
let randomIndex;

Loading…
Cancel
Save