Skip to content

Commit

Permalink
Front-end for reordering macro buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
smotired committed Feb 15, 2022
1 parent 36da7eb commit 830003b
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 1 deletion.
46 changes: 45 additions & 1 deletion src/containers/FlightDirector/MacroButtons/macroConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {capitalCase} from "change-case";
import {FaBan} from "react-icons/fa";
import {ActionConfig} from "../Macros/macroConfig";
import SortableActionList from "./sortableActionList";
import SortableButtonList from "./sortableMacroButtonList";

const colors = [
"primary",
Expand Down Expand Up @@ -201,6 +202,7 @@ const MacroConfig = ({macros}) => {
</ListGroupItem>
))}
</ListGroup>

<Mutation
mutation={gql`
mutation AddMacro($name: String!) {
Expand Down Expand Up @@ -294,6 +296,48 @@ const MacroConfig = ({macros}) => {
{macro && (
<>
<h3>Buttons</h3>
{/* SORTABLE BUTTON LIST */}
<Mutation
mutation={gql`
mutation ReorderButtons(
$configId: ID!
$oldIndex: Int!
$newIndex: Int!
) {
reorderMacroButton(
configId: $configId
oldIndex: $oldIndex
newIndex: $newIndex
)
}
`}
>
{action => (
<SortableButtonList
css={css`
flex: 1;
overflow-y: auto;
`}
distance={20}
buttons={macro.buttons}
selectedButton={selectedButton}
setSelectedButton={setSelectedButton}
onSortEnd={({oldIndex, newIndex}) => {
let configId = macro.id;
action({
variables: {
configId,
selectedButton,
oldIndex,
newIndex,
},
});
}}
/>
)}
</Mutation>

{/* OLD MACRO BUTTON LIST
<ListGroup style={{maxHeight: "60vh", overflowY: "auto"}}>
{macro.buttons.map(m => (
<ListGroupItem
Expand All @@ -309,7 +353,7 @@ const MacroConfig = ({macros}) => {
<small>{m.category}</small>
</ListGroupItem>
))}
</ListGroup>
</ListGroup> */}
<Mutation
mutation={gql`
mutation AddButton($configId: ID!, $name: String!) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import React from "react";
import {SortableContainer, SortableElement} from "react-sortable-hoc";
import {ListGroup, ListGroupItem} from "reactstrap";

const SortableButton = SortableElement(
({
button,
selectedButton,
setSelectedButton,
}: {
button: {id: string; name: string; category: string};
selectedButton?: string | null;
setSelectedButton: (id: string) => void;
}) => (
<ListGroupItem
key={`${button.id}`}
onClick={() => setSelectedButton(button.id)}
active={button.id === selectedButton}
>
{button.name}
<br />
<small>{button.category}</small>
</ListGroupItem>
),
);

const SortableButtonList = SortableContainer(
({
id,
selectedButton,
setSelectedButton,
buttons,
onSortEnd,
...props
}: {
id: string;
selectedButton?: string | null;
setSelectedButton: (id: string) => void;
buttons: {id: string; name: string; category: string}[];
onSortEnd: (newIndex: number) => void;
}) => {
return (
<ListGroup style={{maxHeight: "60vh", overflowY: "auto"}} {...props}>
{buttons.map((b, index) => (
<SortableButton
key={b.id}
index={index}
button={b}
selectedButton={selectedButton}
setSelectedButton={() => setSelectedButton(b.id)}
/>
))}
</ListGroup>
);
},
);

export default SortableButtonList;

0 comments on commit 830003b

Please sign in to comment.