refactor getCurrentPath
parent
0200fe2792
commit
de4942f4d4
|
@ -3,7 +3,7 @@ import { Everything } from "../../interfaces";
|
|||
import * as moment from "moment";
|
||||
import * as _ from "lodash";
|
||||
import { t } from "i18next";
|
||||
import { history } from "../../history";
|
||||
import { history, getPathArray } from "../../history";
|
||||
import {
|
||||
selectAllFarmEvents,
|
||||
indexRegimenById,
|
||||
|
@ -144,8 +144,7 @@ export function mapStateToPropsAddEdit(props: Everything): AddEditFarmEventProps
|
|||
const farmEvents = selectAllFarmEvents(props.resources.index);
|
||||
|
||||
const getFarmEvent = (): TaggedFarmEvent | undefined => {
|
||||
const url = history.getCurrentLocation().pathname;
|
||||
const id = parseInt(url.split("/")[4]);
|
||||
const id = parseInt(getPathArray()[4]);
|
||||
if (id && hasId(props.resources.index, "FarmEvent", id)) {
|
||||
return findFarmEventById(props.resources.index, id);
|
||||
} else {
|
||||
|
|
|
@ -1,8 +1,6 @@
|
|||
jest.mock("../../../history", () => ({
|
||||
history: {
|
||||
getCurrentLocation: () => {
|
||||
return { pathname: "/app/designer/plants/crop_search/aplant/add" };
|
||||
}
|
||||
getPathArray: () => {
|
||||
return "/app/designer/plants/crop_search/aplant/add".split("/");
|
||||
}
|
||||
}));
|
||||
|
||||
|
|
|
@ -1,18 +1,18 @@
|
|||
const mockHistory = jest.fn();
|
||||
jest.mock("../../../history", () => ({
|
||||
history: {
|
||||
push: mockHistory,
|
||||
getCurrentLocation: jest.fn()
|
||||
.mockImplementationOnce(() => {
|
||||
return { pathname: "/app/designer/plants" };
|
||||
})
|
||||
.mockImplementationOnce(() => {
|
||||
return { pathname: "/app/designer/plants/1/edit" };
|
||||
})
|
||||
.mockImplementationOnce(() => {
|
||||
return { pathname: "/app/designer/plants/1" };
|
||||
})
|
||||
}
|
||||
push: mockHistory
|
||||
},
|
||||
getPathArray: jest.fn()
|
||||
.mockImplementationOnce(() => {
|
||||
return "/app/designer/plants".split("/");
|
||||
})
|
||||
.mockImplementationOnce(() => {
|
||||
return "/app/designer/plants/1/edit".split("/");
|
||||
})
|
||||
.mockImplementationOnce(() => {
|
||||
return "/app/designer/plants/1".split("/");
|
||||
})
|
||||
}));
|
||||
|
||||
import * as React from "react";
|
||||
|
|
|
@ -5,7 +5,7 @@ import { Plant, DEFAULT_PLANT_RADIUS } from "../plant";
|
|||
import { movePlant } from "../actions";
|
||||
import * as moment from "moment";
|
||||
import { GardenMapProps, GardenMapState } from "../interfaces";
|
||||
import { history } from "../../history";
|
||||
import { getPathArray } from "../../history";
|
||||
import { initSave, save, edit } from "../../api/crud";
|
||||
import { TaggedPlantPointer, SpecialStatus } from "../../resources/tagged_resources";
|
||||
import {
|
||||
|
@ -108,20 +108,19 @@ export class GardenMap extends
|
|||
get isEditing(): boolean { return location.pathname.includes("edit"); }
|
||||
|
||||
getPlant = (): TaggedPlantPointer | undefined =>
|
||||
history.getCurrentLocation().pathname.split("/")[4] != "select"
|
||||
getPathArray()[4] != "select"
|
||||
? this.props.selectedPlant
|
||||
: undefined;
|
||||
|
||||
handleDragOver = (e: React.DragEvent<HTMLElement>) => {
|
||||
if (!this.isEditing &&
|
||||
history.getCurrentLocation().pathname.split("/")[4] == "crop_search") {
|
||||
if (!this.isEditing && getPathArray()[4] == "crop_search") {
|
||||
e.preventDefault();
|
||||
e.dataTransfer.dropEffect = "move";
|
||||
}
|
||||
}
|
||||
|
||||
handleDragEnter = (e: React.DragEvent<HTMLElement>) => {
|
||||
if (history.getCurrentLocation().pathname.split("/")[4] == "crop_search") {
|
||||
if (getPathArray()[4] == "crop_search") {
|
||||
e.preventDefault();
|
||||
}
|
||||
}
|
||||
|
@ -134,7 +133,7 @@ export class GardenMap extends
|
|||
e.preventDefault();
|
||||
const gardenCoords = this.getGardenCoordinates(e);
|
||||
if (gardenCoords) {
|
||||
const crop = history.getCurrentLocation().pathname.split("/")[5];
|
||||
const crop = getPathArray()[5];
|
||||
const OFEntry = this.findCrop(crop);
|
||||
const { x, y } = gardenCoords;
|
||||
if (x < 0 || y < 0 || x > this.props.gridSize.x || y > this.props.gridSize.y) {
|
||||
|
@ -164,7 +163,7 @@ export class GardenMap extends
|
|||
}
|
||||
|
||||
click = (e: React.MouseEvent<SVGElement>) => {
|
||||
if (history.getCurrentLocation().pathname.split("/")[6] == "add") {
|
||||
if (getPathArray()[6] == "add") {
|
||||
// In 'click-to-add' mode
|
||||
this.handleDrop(e);
|
||||
}
|
||||
|
|
|
@ -2,13 +2,12 @@ import * as React from "react";
|
|||
import { GridProps } from "./interfaces";
|
||||
import { getXYFromQuadrant } from "./util";
|
||||
import * as _ from "lodash";
|
||||
import { history } from "../../history";
|
||||
import { history, getPathArray } from "../../history";
|
||||
import { Color } from "../../ui/colors";
|
||||
|
||||
export function Grid(props: GridProps) {
|
||||
function closePlantInfo() {
|
||||
const currentPath = history.getCurrentLocation().pathname;
|
||||
if (!isNaN(parseInt(currentPath.split("/").slice(-1)[0]))) {
|
||||
if (!isNaN(parseInt(getPathArray().slice(-1)[0]))) {
|
||||
// A plant is selected and plant info is open. Unselect and close it.
|
||||
props.dispatch({ type: "SELECT_PLANT", payload: undefined });
|
||||
props.dispatch({
|
||||
|
|
|
@ -4,7 +4,7 @@ import * as _ from "lodash";
|
|||
import { GardenPlant } from "../garden_plant";
|
||||
import { PlantLayerProps, CropSpreadDict } from "../interfaces";
|
||||
import { defensiveClone } from "../../../util";
|
||||
import { history } from "../../../history";
|
||||
import { getPathArray } from "../../../history";
|
||||
|
||||
const cropSpreadDict: CropSpreadDict = {};
|
||||
|
||||
|
@ -24,9 +24,8 @@ export function PlantLayer(props: PlantLayerProps) {
|
|||
.filter(c => !!c.body.spread)
|
||||
.map(c => cropSpreadDict[c.body.slug] = c.body.spread);
|
||||
|
||||
const pathName = history.getCurrentLocation().pathname;
|
||||
const clickToAddMode = pathName.split("/")[6] == "add";
|
||||
const selectMode = pathName.split("/")[4] == "select";
|
||||
const clickToAddMode = getPathArray()[6] == "add";
|
||||
const selectMode = getPathArray()[4] == "select";
|
||||
const maybeNoPointer = (clickToAddMode || selectMode)
|
||||
? { "pointerEvents": "none" } : {};
|
||||
|
||||
|
|
|
@ -3,7 +3,7 @@ import { BackArrow } from "../../ui";
|
|||
import { Everything } from "../../interfaces";
|
||||
import { connect } from "react-redux";
|
||||
import { t } from "i18next";
|
||||
import { history } from "../../history";
|
||||
import { history, getPathArray } from "../../history";
|
||||
import { svgToUrl } from "../../open_farm/index";
|
||||
import { CropLiveSearchResult } from "../interfaces";
|
||||
import { findBySlug } from "../search_selectors";
|
||||
|
@ -27,7 +27,7 @@ export class AddPlant
|
|||
extends React.Component<AddPlantProps, {}> {
|
||||
|
||||
render() {
|
||||
const crop = history.getCurrentLocation().pathname.split("/")[5];
|
||||
const crop = getPathArray()[5];
|
||||
const result =
|
||||
findBySlug(this.props.cropSearchResults, crop || "PLANT_NOT_FOUND");
|
||||
|
||||
|
|
|
@ -3,7 +3,7 @@ import { t } from "i18next";
|
|||
import * as _ from "lodash";
|
||||
import { DATA_URI, DEFAULT_ICON, svgToUrl } from "../../open_farm/index";
|
||||
import { CropInfoProps, DraggableEvent } from "../interfaces";
|
||||
import { history } from "../../history";
|
||||
import { history, getPathArray } from "../../history";
|
||||
import { connect } from "react-redux";
|
||||
import { findBySlug } from "../search_selectors";
|
||||
import { Everything } from "../../interfaces";
|
||||
|
@ -26,7 +26,7 @@ export function mapStateToProps(props: Everything): CropInfoProps {
|
|||
export class CropInfo extends React.Component<CropInfoProps, {}> {
|
||||
|
||||
componentDidMount() {
|
||||
const crop = history.getCurrentLocation().pathname.split("/")[5];
|
||||
const crop = getPathArray()[5];
|
||||
OFSearch(crop)(this.props.dispatch);
|
||||
}
|
||||
|
||||
|
@ -44,7 +44,7 @@ export class CropInfo extends React.Component<CropInfoProps, {}> {
|
|||
}
|
||||
|
||||
render() {
|
||||
const crop = history.getCurrentLocation().pathname.split("/")[5];
|
||||
const crop = getPathArray()[5];
|
||||
const result =
|
||||
findBySlug(this.props.cropSearchResults, crop || "PLANT_NOT_FOUND");
|
||||
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import * as React from "react";
|
||||
import { t } from "i18next";
|
||||
import { EditPlantInfoProps } from "../interfaces";
|
||||
import { history } from "../../history";
|
||||
import { history, getPathArray } from "../../history";
|
||||
import { destroy } from "../../api/crud";
|
||||
import { error } from "farmbot-toastr";
|
||||
|
||||
|
@ -10,7 +10,7 @@ export abstract class PlantInfoBase extends
|
|||
|
||||
get stringyID() {
|
||||
// TODO: ("We should put this into a query object incase the URL changes")
|
||||
return history.getCurrentLocation().pathname.split("/")[4] || "";
|
||||
return getPathArray()[4] || "";
|
||||
}
|
||||
|
||||
get plant() { return this.props.findPlant(this.stringyID); }
|
||||
|
|
|
@ -2,3 +2,6 @@ import { browserHistory } from "react-router";
|
|||
export let history = browserHistory;
|
||||
export let push = (url: string) => history.push(url);
|
||||
export let pathname = history.getCurrentLocation().pathname;
|
||||
export function getPathArray() {
|
||||
return history.getCurrentLocation().pathname.split("/");
|
||||
}
|
||||
|
|
|
@ -3,7 +3,7 @@ import * as _ from "lodash";
|
|||
import { t } from "i18next";
|
||||
import { links } from "./nav/nav_links";
|
||||
import { sync } from "./devices/actions";
|
||||
import { history, push } from "./history";
|
||||
import { push, getPathArray } from "./history";
|
||||
import { Row, Col } from "./ui/index";
|
||||
import {
|
||||
Hotkey,
|
||||
|
@ -108,7 +108,7 @@ export class HotKeys extends React.Component<Props, Partial<State>> {
|
|||
}
|
||||
|
||||
renderHotkeys() {
|
||||
const slug = history.getCurrentLocation().pathname.split("/")[2];
|
||||
const slug = getPathArray()[2];
|
||||
return <Hotkeys>
|
||||
{
|
||||
this.hotkeys(this.props.dispatch, slug)
|
||||
|
|
|
@ -4,7 +4,7 @@ import { NavBarProps, NavBarState } from "./interfaces";
|
|||
import { EStopButton } from "../devices/components/e_stop_btn";
|
||||
import { Session } from "../session";
|
||||
import { Row, Col } from "../ui";
|
||||
import { history } from "../history";
|
||||
import { getPathArray } from "../history";
|
||||
import { updatePageInfo } from "../util";
|
||||
import { SyncButton } from "./sync_button";
|
||||
import { NavLinks } from "./nav_links";
|
||||
|
@ -52,7 +52,7 @@ export class NavBar extends React.Component<NavBarProps, Partial<NavBarState>> {
|
|||
];
|
||||
|
||||
/** The way our app is laid out, we'll pretty much always want this bit. */
|
||||
const pageName = history.getCurrentLocation().pathname.split("/")[2] || "";
|
||||
const pageName = getPathArray()[2] || "";
|
||||
|
||||
/** Change document meta title on every route change. */
|
||||
updatePageInfo(pageName);
|
||||
|
|
Loading…
Reference in New Issue