Farmbot-Web-App/frontend/draggable/actions.ts

49 lines
1.6 KiB
TypeScript
Raw Normal View History

2017-06-29 12:54:02 -06:00
import { DataXfer, DataXferIntent, DataXferBase } from "./interfaces";
import { SequenceBodyItem as Step, uuid as id } from "farmbot";
2017-06-29 12:54:02 -06:00
import { Everything } from "../interfaces";
import { ReduxAction } from "../redux/interfaces";
import * as React from "react";
import { Actions } from "../constants";
2019-12-23 15:38:48 -07:00
import { UUID } from "../resources/interfaces";
2017-06-29 12:54:02 -06:00
export const STEP_DATATRANSFER_IDENTIFER = "farmbot/sequence-step";
/** SIDE EFFECT-Y!! Stores a step into store.draggable.dataTransfer and
2017-06-29 12:54:02 -06:00
* attaches its lookup key to the event object. This allows you to retrieve
* the step when the "drop" event occurs elsewhere */
export function stepPut(value: Step,
ev: React.DragEvent<HTMLElement>,
intent: DataXferIntent,
2019-12-23 15:38:48 -07:00
draggerId: number,
resourceUuid?: UUID):
2017-06-29 12:54:02 -06:00
ReduxAction<DataXferBase> {
2017-08-28 05:49:13 -06:00
const uuid = id();
2017-06-29 12:54:02 -06:00
ev.dataTransfer.setData(STEP_DATATRANSFER_IDENTIFER, uuid);
return {
type: Actions.PUT_DATA_XFER,
2017-06-29 12:54:02 -06:00
payload: {
intent,
uuid,
value,
2019-12-23 15:38:48 -07:00
draggerId,
resourceUuid,
2017-06-29 12:54:02 -06:00
}
};
}
2017-06-29 12:54:02 -06:00
/** Used by a React component reacting to a "drop" event. Takes a UUID and looks
* for a step stored in store.draggable.data_transfer. Removes it from the store
* and returns it to the component. */
export function stepGet(uuid: string) {
return function (dispatch: Function,
getState: () => Everything):
DataXfer {
2017-08-28 05:49:13 -06:00
const obj = getState().draggable.dataTransfer[uuid];
2017-06-29 12:54:02 -06:00
if (obj && obj.intent) {
dispatch({ type: Actions.DROP_DATA_XFER, payload: uuid });
2017-06-29 12:54:02 -06:00
return obj;
} else {
throw new Error(`Can't find StepXferObject with UUID ${uuid}`);
}
};
}