Skip to content

Commit

Permalink
Merge pull request #231 from crpalmer/macro-ordering
Browse files Browse the repository at this point in the history
macros: Properly order the saved macros according to user preferences
  • Loading branch information
walidkayhan authored Dec 7, 2022
2 parents c0c0c3c + b7ad85f commit f41be25
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/app/widgets/Macro/Macro.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ const Macro = ({ state, actions, workflow }) => {
column2: { items: [] },
});

const setRowIndices = macros => {
for (let i = 0; i < macros.length; i++) {
macros[i].rowIndex = i;
}
return macros;
};

useEffect(() => {
const computedColumns = [computeColumn('column1'), computeColumn('column2')];

Expand Down Expand Up @@ -94,10 +101,13 @@ const Macro = ({ state, actions, workflow }) => {

// Update the macro column and row index properties so they can be saved
// within the API call and can be sorted properly when the user closes and opens the program again
popped.rowIndex = destination.rowIndex;
popped.column = destination.droppableId;

destItems.splice(destination.index, 0, popped);

setRowIndices(sourceItems);
setRowIndices(destItems);

setColumns({
...columns,
[source.droppableId]: {
Expand All @@ -114,6 +124,7 @@ const Macro = ({ state, actions, workflow }) => {
const copiedItems = [...column.items];
const [removed] = copiedItems.splice(source.index, 1);
copiedItems.splice(destination.index, 0, removed);
setRowIndices(copiedItems);
setColumns({
...columns,
[source.droppableId]: {
Expand All @@ -124,7 +135,8 @@ const Macro = ({ state, actions, workflow }) => {
}
};

const computeColumn = (columnName) => macros.filter(macro => macro.column === columnName);
// Set rowIndices after sorting to correct any invalid saved data, normally it should be a no-op
const computeColumn = (columnName) => setRowIndices(macros.filter(macro => macro.column === columnName).sort((a, b) => a.rowIndex - b.rowIndex));

const disabled = !canRunMacro();
const { column1, column2 } = columns;
Expand Down

0 comments on commit f41be25

Please sign in to comment.