From cc1ccbab8c38a879fd8ebc6f2c07938d4fd20c4c Mon Sep 17 00:00:00 2001 From: Stefano Brilli Date: Tue, 27 Jul 2021 19:31:53 +0200 Subject: [PATCH] fix: group deletion and code style --- src/components/main-rows.tsx | 2 +- src/components/main.tsx | 8 ++++---- src/redux/actions.ts | 4 ++-- src/services/netmd-mock.ts | 16 +++++++++------- src/services/netmd.ts | 10 ++++++---- src/utils.ts | 8 ++++++-- 6 files changed, 28 insertions(+), 20 deletions(-) diff --git a/src/components/main-rows.tsx b/src/components/main-rows.tsx index 07c5258..9a9dce3 100644 --- a/src/components/main-rows.tsx +++ b/src/components/main-rows.tsx @@ -182,7 +182,7 @@ interface GroupRowProps { export function GroupRow({ group, onRename, onDelete }: GroupRowProps) { const classes = useStyles(); - const handleDelete = useCallback((event: React.MouseEvent) => onDelete(event, group.tracks[0].index), [onDelete, group]); + const handleDelete = useCallback((event: React.MouseEvent) => onDelete(event, group.index), [onDelete, group]); const handleRename = useCallback((event: React.MouseEvent) => onRename(event, group.index), [onRename, group]); return ( diff --git a/src/components/main.tsx b/src/components/main.tsx index 73a749f..6bf781c 100644 --- a/src/components/main.tsx +++ b/src/components/main.tsx @@ -304,9 +304,9 @@ export const Main = (props: {}) => { [dispatch, selected] ); - const handleGroupRemoval = useCallback( - (event: React.MouseEvent, groupBegin: number) => { - dispatch(deleteGroup(groupBegin)); + const handleDeleteGroup = useCallback( + (event: React.MouseEvent, index: number) => { + dispatch(deleteGroup(index)); }, [dispatch] ); @@ -480,7 +480,7 @@ export const Main = (props: {}) => { )} {group.title === null && group.tracks.length === 0 && ( diff --git a/src/redux/actions.ts b/src/redux/actions.ts index 24bafdd..ab6fae1 100644 --- a/src/redux/actions.ts +++ b/src/redux/actions.ts @@ -76,10 +76,10 @@ export function groupTracks(indexes: number[]) { }; } -export function deleteGroup(groupBegin: number) { +export function deleteGroup(index: number) { return async function(dispatch: AppDispatch) { const { netmdService } = serviceRegistry; - netmdService!.deleteGroup(groupBegin); + netmdService!.deleteGroup(index); listContent()(dispatch); }; } diff --git a/src/services/netmd-mock.ts b/src/services/netmd-mock.ts index 5e1e97e..1889eb2 100644 --- a/src/services/netmd-mock.ts +++ b/src/services/netmd-mock.ts @@ -73,7 +73,7 @@ class NetMDMockService implements NetMDService { { title: 'Test', fullWidthTitle: '', - index: 0, + index: 1, tracksIdx: [0, 1], }, ]; @@ -141,7 +141,7 @@ class NetMDMockService implements NetMDService { } async renameGroup(gropuIndex: number, newName: string, newFullWidth?: string) { - let group = this._groupsDef.slice(1).find(n => n.index === gropuIndex); + let group = this._groupsDef.find(n => n.index === gropuIndex); if (!group) { return; } @@ -171,16 +171,18 @@ class NetMDMockService implements NetMDService { }; this._groupsDef.push(newGroupDef); + this._groupsDef = this._groupsDef.filter(g => g.tracksIdx.length !== 0).sort((a, b) => a.tracksIdx[0] - b.tracksIdx[0]); + ungroupedDefs.tracksIdx = ungroupedDefs.tracksIdx.filter(idx => !newGroupTracks.includes(idx)); if (ungroupedLengthBeforeGroup - ungroupedDefs.tracksIdx.length !== groupLength) { throw new Error('A track cannot be in 2 groups!'); } } - async deleteGroup(groupBegin: number) { + async deleteGroup(index: number) { const groups = this._getGroups(); - const thisGroup = groups.slice(1).find(n => n.tracks[0].index === groupBegin); - if (!thisGroup) { + const group = groups.find(g => g.index === index); + if (!group) { return; } let ungroupedGroup = this._groupsDef.find(n => n.title === null); @@ -193,8 +195,8 @@ class NetMDMockService implements NetMDService { }; this._groupsDef.unshift(ungroupedGroup); } - ungroupedGroup.tracksIdx = ungroupedGroup.tracksIdx.concat(thisGroup.tracks.map(t => t.index)).sort(); - this._groupsDef.splice(groups.indexOf(thisGroup), 1); + ungroupedGroup.tracksIdx = ungroupedGroup.tracksIdx.concat(group.tracks.map(t => t.index)).sort(); + this._groupsDef.splice(groups.indexOf(group), 1); } async rewriteGroups(groups: Group[]) { diff --git a/src/services/netmd.ts b/src/services/netmd.ts index 9d94970..17aac9e 100644 --- a/src/services/netmd.ts +++ b/src/services/netmd.ts @@ -36,7 +36,7 @@ export interface NetMDService { finalize(): Promise; renameTrack(index: number, newTitle: string, newFullWidthTitle?: string): Promise; renameDisc(newName: string, newFullWidthName?: string): Promise; - renameGroup(groupBegin: number, newTitle: string, newFullWidthTitle?: string): Promise; + renameGroup(groupIndex: number, newTitle: string, newFullWidthTitle?: string): Promise; addGroup(groupBegin: number, groupLength: number, name: string): Promise; deleteGroup(groupIndex: number): Promise; rewriteGroups(groups: Group[]): Promise; @@ -218,11 +218,13 @@ export class NetMDUSBService implements NetMDService { } @asyncMutex - async deleteGroup(groupBegin: number) { + async deleteGroup(index: number) { const disc = await this.listContentUsingCache(); - let thisGroup = disc.groups.find(n => n.tracks[0].index === groupBegin); - if (thisGroup) disc.groups.splice(disc.groups.indexOf(thisGroup), 1); + let groupIndex = disc.groups.findIndex(g => g.index === index); + if (groupIndex >= 0) { + disc.groups.splice(groupIndex, 1); + } await this.writeRawTitles(compileDiscTitles(disc)); } diff --git a/src/utils.ts b/src/utils.ts index ad13869..d858944 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -171,14 +171,18 @@ export function getSortedTracks(disc: Disc | null) { } export function getGroupedTracks(disc: Disc | null) { - if (!disc) return []; + if (!disc) { + return []; + } let groupedList: Group[] = []; let ungroupedTracks = [...(disc.groups.find(n => n.title === null)?.tracks ?? [])]; let lastIndex = 0; for (let group of disc.groups) { - if (group.title === null) continue; // Ungrouped tracks + if (group.title === null) { + continue; // Ungrouped tracks + } let toCopy = group.tracks[0].index - lastIndex; groupedList.push({ index: -1,