All files / applications/go-by-transit/src/view/bookmarks/component/bookmarkSections BookmarkSectionComponent.tsx

66.66% Statements 14/21
35% Branches 7/20
71.42% Functions 5/7
66.66% Lines 14/21

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163                                                                            2x   2x       2x               9x 9x 2x   7x                     9x                                                                                         9x   9x       9x   9x                                       2x                                 1x          
import { Card, CardHeader, CardBody } from "@wintermuted/ui-react";
import { isEmpty, map } from "lodash";
import React, { Component } from "react";
import { StopLocation } from "../../../../api/trimet/interfaces/types";
import { RouteBookmark } from "../../../../api/localstorage/routeBookmarks";
import { RemoveBookmarkButton } from "../../../../component/buttons/RemoveBookmarkButton";
import StopContainer from "../../../nearbyStops/containers/StopContainer";
import "./BookmarkSectionComponent.css";
import { BookmarkSectionNav } from "./BookmarkSectionNav";
import BookmarkedRouteItem from "../BookmarkedRouteItem";
 
interface Props {
  name: string;
  id: number;
  bookmarksInSection: StopLocation[];
  routesInSection: RouteBookmark[];
  removeBookmarkSection: (bookmarkSectionId: number) => void;
  allBookmarks: StopLocation[];
  removeBookmarkFromSection: (
    bookmarkSectionId: number,
    stopId: number
  ) => void;
  removeRouteFromSection: (
    bookmarkSectionId: number,
    routeId: number
  ) => void;
  updateBookmarkSectionName: (
    bookmarkSectionId: number,
    bookmarkSectionName: string
  ) => void;
}
 
interface State {
  editMode: boolean;
}
 
export default class BookmarkSectionComponent extends Component<Props, State> {
  constructor(props) {
    super(props);
 
    this.state = {
      editMode: false
    };
 
    this.toggleEditMode = this.toggleEditMode.bind(this);
  }
 
  public getBookmarksInSection(
    bookmarkedStops: StopLocation[],
    id: number,
    hasRoutes: boolean
  ) {
    Eif (isEmpty(bookmarkedStops)) {
      if (hasRoutes) {
        return null;
      }
      return (
        <p className="bookmark-section-add-more">
          Add stops or routes using the edit controls.
        </p>
      );
    }
    const { removeBookmarkFromSection } = this.props;
    const onRemoveBookmarkFromSection = removeBookmarkFromSection
      ? removeBookmarkFromSection.bind(this, id)
      : undefined;
 
    return map(bookmarkedStops, stopLocation => {
      if (isEmpty(stopLocation)) {
        return null;
      }
 
      const locationId =
        stopLocation && stopLocation.locid
          ? stopLocation.locid
          : stopLocation.id;
 
      return (
        <li key={locationId}>
          <div className="bookmark-stop-wrapper">
            <div className="bookmark-flex-container">
              <div className="bookmark-stop-flex-item">
                <StopContainer
                  showArrivals={false}
                  locationId={locationId}
                  onRouteIndicatorClick={undefined}
                />
              </div>
              {this.state.editMode && (
                <div className="bookmark-remove-flex-item">
                  <RemoveBookmarkButton
                    stopId={locationId}
                    removeBookmarkFromSection={onRemoveBookmarkFromSection}
                  />
                </div>
              )}
            </div>
          </div>
        </li>
      );
    });
  }
 
  public render() {
    const {
      name,
      bookmarksInSection,
      routesInSection,
      id,
      updateBookmarkSectionName,
      removeBookmarkSection,
      removeRouteFromSection
    } = this.props;
 
    const onUpdateBookmarkSectionName = updateBookmarkSectionName
      ? updateBookmarkSectionName.bind(this, id)
      : undefined;
    const removeSection =
      removeBookmarkSection && removeBookmarkSection.bind(this, id);
 
    return (
      <Card key={id}>
        <CardHeader>
          <BookmarkSectionNav
            name={name}
            toggleEditMode={this.toggleEditMode}
            editMode={this.state.editMode}
            removeBookmarkSection={removeSection}
            updateBookmarkSectionName={onUpdateBookmarkSectionName}
            bookmarkSectionId={id}
          />
        </CardHeader>
        <CardBody>
          <ul className="bookmark-section-bookmarks">
            {this.getBookmarksInSection(
              bookmarksInSection,
              id,
              routesInSection.length > 0
            )}
            {routesInSection.map(route => (
              <BookmarkedRouteItem
                key={route.route}
                route={route}
                onRemove={
                  this.state.editMode
                    ? routeId => removeRouteFromSection(id, routeId)
                    : undefined
                }
              />
            ))}
          </ul>
        </CardBody>
      </Card>
    );
  }
 
  public toggleEditMode() {
    this.setState({
      editMode: !this.state.editMode
    });
  }
}