import React from "react" import { StyleSheet, View, TouchableHighlight, TouchableOpacity, Image, FlatList, Text, } from "react-native" import { Icon, Header } from "../ui" import { MessageModal } from "../Modal" import { reactAutoBind } from "auto-bind2" import { SwipeListView } from "react-native-swipe-list-view" import { api } from "../API" import { workItemTypeEnum, workItemTypeText, formatLatLng, parseLatLng, pad, geoDistance, dotify, } from "../util" import "url-search-params-polyfill" const styles = StyleSheet.create({ container: { height: "100%", width: "100%", justifyContent: "flex-start", backgroundColor: "#FFFFFF", }, }) export class WorkItemList extends React.Component { constructor(props) { super(props) reactAutoBind(this) this.state = { messageModal: null, } const { search } = this.props.location if (search) { const params = new URLSearchParams(search) const latLng = params.get("latLng") if (latLng) { const [lat, lng] = latLng.split(",") if (lat && lng) { this.position = { latitude: parseFloat(lat), longitude: parseFloat(lng), } } } } api .listWorkItems() .then((list) => { this.setState({ listItems: list.items }) }) .catch(() => { this.setState({ messageModal: { icon: "hand", message: "Unable to get list of work items", detail: error.message, }, }) }) } handleItemSelect(item, ref) { this.props.history.push(`/workItem?id=${item._id}`) } handleItemDelete(item, ref) { api .deleteWorkItem(item._id) .then(() => { return api .listWorkItems() .then((list) => { this.setState({ listItems: list.items }) }) .catch(() => { this.setState({ messageModal: { icon: "hand", message: "Unable to get list of work items", detail: error.message, }, }) }) }) .catch(() => { this.setState({ messageModal: { icon: "hand", message: "Unable to get delete work item", detail: error.message, }, }) }) } handleMessageDismiss() { this.setState({ messageModal: null }) } handleDonePress() { this.props.history.push("/workItem") } handleBackPress() { const { history } = this.props if (history.length > 1) { history.goBack() } else { history.replace("/home") } } render() { const { listItems, messageModal } = this.state return (
item._id} renderItem={({ item }, rowMap) => ( this.handleItemSelect(item, rowMap[item._id])}> {pad(item.ticketNumber, 4)} {workItemTypeText[item.workItemType]} {`${dotify(item.address) || "..."} | ${ this.position ? geoDistance( this.position.latitude, this.position.longitude, item.location.coordinates[1], item.location.coordinates[0], "K" ).toFixed(2) : "?" } km`} )} renderHiddenItem={({ item }, rowMap) => ( this.handleItemDelete(item, rowMap[item._id])}> Delete )} rightOpenValue={-80} stopRightSwipe={-120} disableRightSwipe /> ) } }