fix: group deletion and code style

This commit is contained in:
Stefano Brilli 2021-07-27 19:31:53 +02:00
parent 8f1b6c1c5e
commit cc1ccbab8c
6 changed files with 28 additions and 20 deletions

View File

@ -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 (
<TableRow hover className={classes.groupHeadRow} onDoubleClick={handleRename}>

View File

@ -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: {}) => {
<GroupRow
group={group}
onRename={handleRenameGroup}
onDelete={handleGroupRemoval}
onDelete={handleDeleteGroup}
/>
)}
{group.title === null && group.tracks.length === 0 && (

View File

@ -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);
};
}

View File

@ -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[]) {

View File

@ -36,7 +36,7 @@ export interface NetMDService {
finalize(): Promise<void>;
renameTrack(index: number, newTitle: string, newFullWidthTitle?: string): Promise<void>;
renameDisc(newName: string, newFullWidthName?: string): Promise<void>;
renameGroup(groupBegin: number, newTitle: string, newFullWidthTitle?: string): Promise<void>;
renameGroup(groupIndex: number, newTitle: string, newFullWidthTitle?: string): Promise<void>;
addGroup(groupBegin: number, groupLength: number, name: string): Promise<void>;
deleteGroup(groupIndex: number): Promise<void>;
rewriteGroups(groups: Group[]): Promise<void>;
@ -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));
}

View File

@ -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,