cloudhub-react-components
Version:
Various components to use in react projects
334 lines (274 loc) • 13.7 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
var _withStyles = _interopRequireDefault(require("@material-ui/core/styles/withStyles"));
var _async = _interopRequireDefault(require("react-select/async"));
var _debounce = _interopRequireDefault(require("lodash/debounce"));
var _isEqual = _interopRequireDefault(require("lodash/isEqual"));
var _axios = _interopRequireDefault(require("axios"));
var _hexToRgba = _interopRequireDefault(require("hex-to-rgba"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }
function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance"); }
function _iterableToArray(iter) { if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } }
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(source, true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(source).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
// Import React Scrit Libraray to load Google object
var PlacesAutoComplete =
/*#__PURE__*/
function (_Component) {
_inherits(PlacesAutoComplete, _Component);
function PlacesAutoComplete(props) {
var _this;
_classCallCheck(this, PlacesAutoComplete);
_this = _possibleConstructorReturn(this, _getPrototypeOf(PlacesAutoComplete).call(this, props)); // Declare State
_defineProperty(_assertThisInitialized(_this), "onChange", function (_ref) {
var value = _ref.value,
item = _ref.item;
var _this$props = _this.props,
onChange = _this$props.onChange,
input = _this$props.input,
axiosinstance = _this$props.axiosinstance,
mapskey = _this$props.mapskey;
var searchurl = "/mapsapi/maps/api/place/details/json?placeid=".concat(value, "&key=").concat(_this.props.API_KEY);
axiosinstance().get(searchurl).then(function (_ref2) {
var data = _ref2.data;
var result = data.result || {};
var address = result.geometry || {};
var location = address.location || {};
var origin = {
latitude: typeof location.lat === 'function' ? location.lat() : location.lat,
longitude: typeof location.lng === 'function' ? location.lng() : location.lng,
place: result
};
_this.props.onChange(_objectSpread({}, item, {}, origin));
_this.props.input.onChange(_objectSpread({}, item, {}, origin));
}).catch(function (e) {
console.log(e);
});
});
_defineProperty(_assertThisInitialized(_this), "nearBySearch", function (region) {
var options = _this.state.options;
if (!region) {
return;
}
if (!region.lat || !region.lng) {
return;
}
var params = {
location: "".concat(region.lat, ",").concat(region.lng),
type: ['road', 'building', 'hotel', 'store'],
key: _this.props.API_KEY,
rankby: 'distance'
};
var searchurl = '/mapsapi/maps/api/place/nearbysearch/json'; // eslint-disable-next-line consistent-return
return _axios.default.get(searchurl, {
params: params
}).then(function (_ref3) {
var data = _ref3.data;
var result = data.results[0] || {};
var address = result.geometry || {};
var location = address.location || {};
var item = {
label: result.name,
item: {
name: result.name,
id: result.id,
place_id: result.place_id
},
value: result.place_id
};
_this.setState({
options: [].concat(_toConsumableArray(options), [item])
});
var origin = {
latitude: typeof location.lat === 'function' ? location.lat() : location.lat,
longitude: typeof location.lng === 'function' ? location.lng() : location.lng,
place: result
};
_this.props.onChange(_objectSpread({}, item.item, {}, origin));
_this.props.input.onChange(_objectSpread({}, item, {}, origin));
});
});
_defineProperty(_assertThisInitialized(_this), "loadOptions", function (inputValue, callback) {
var _this$props2 = _this.props,
axiosinstance = _this$props2.axiosinstance,
latitude = _this$props2.latitude,
longitude = _this$props2.longitude,
mapskey = _this$props2.mapskey;
var searchurl = "/mapsapi/maps/api/place/autocomplete/json?input=".concat(inputValue, "&types=geocode&location=").concat(latitude, ",").concat(longitude, "&radius=1000&key=").concat(_this.props.API_KEY);
if (inputValue) {
// use axios to query places
axiosinstance().get(searchurl).then(function (_ref4) {
var data = _ref4.data;
var places = (data.predictions || []).map(function (p) {
return {
label: p.description,
item: {
name: p.description,
id: p.id,
place_id: p.place_id
},
value: p.place_id
};
});
_this.setState({
options: places
});
return callback(places);
}).catch(function (e) {
callback([]);
});
}
});
_this.state = {
inputValue: '',
value: null,
defaultValue: null,
options: []
};
_this.loadOptions = (0, _debounce.default)(_this.loadOptions, 500);
return _this;
}
_createClass(PlacesAutoComplete, [{
key: "componentDidUpdate",
value: function componentDidUpdate(prevProps) {
if (!(0, _isEqual.default)(prevProps.region, this.props.region)) {
this.nearBySearch(this.props.region);
}
}
}, {
key: "render",
value: function render() {
var _this$props3 = this.props,
height = _this$props3.height,
backgroundColor = _this$props3.backgroundColor,
borderColor = _this$props3.borderColor,
borderColorFocused = _this$props3.borderColorFocused;
var customStyles = {
menu: function menu(provided, state) {
return _objectSpread({}, provided, {
marginTop: -2,
borderTopLeftRadius: 0,
borderTopRightRadius: 0,
backgroundColor: backgroundColor || '#FFF',
border: "1.5px solid ".concat(borderColorFocused || '#2684FF'),
borderTopWidth: 0,
boxShadow: "0 1px 0 1px ".concat((0, _hexToRgba.default)(borderColor, 0.1))
});
},
option: function option(provided, state) {
return _objectSpread({}, provided, {
'&:hover': {
borderRightWidth: 1.5
}
});
},
control: function control(provided, state) {
var style = {};
if (state.isFocused) {
style = _objectSpread({}, style, {
boxShadow: "0 1px 0 1px ".concat((0, _hexToRgba.default)(borderColor, 0.1)),
borderColor: borderColorFocused || '#2684FF',
'&:hover': {
borderColor: borderColorFocused || '#2684FF'
}
});
}
if (state.menuIsOpen) {
style = _objectSpread({}, style, {
borderBottomLeftRadius: 0,
borderBottomRightRadius: 0,
boxShadow: '0 1px 0 1px hsla(0, 0%, 0%, 0.1)',
borderWidth: 1.5,
borderColor: borderColorFocused || '#2684FF'
});
}
return _objectSpread({}, provided, {
height: height,
borderColor: borderColor || provided.borderColor,
backgroundColor: backgroundColor || '#FFF',
borderWidth: 1,
'&:hover': {
borderColor: borderColorFocused || '#333'
}
}, style);
}
};
return _react.default.createElement(_async.default, {
styles: customStyles // cacheOptions
,
options: this.state.options,
loadOptions: this.loadOptions,
value: this.state.defaultValue,
onChange: this.onChange,
placeholder: this.props.placeholder
});
}
}], [{
key: "getDerivedStateFromProps",
value: function getDerivedStateFromProps(nextProps, prevState) {
var value = nextProps.input.value || nextProps.value;
if (!(0, _isEqual.default)(value, prevState.value) && Boolean(value)) {
var defaultValue = {
item: {
name: value.name,
id: value.id,
place_id: value.place_id
},
value: value.place_id,
label: value.name
};
if (prevState.options.length > 0) {
return _objectSpread({}, prevState, {
value: value,
defaultValue: defaultValue
});
}
return _objectSpread({}, prevState, {
value: value,
defaultValue: defaultValue,
options: [defaultValue]
});
}
return _objectSpread({}, prevState);
}
}]);
return PlacesAutoComplete;
}(_react.Component);
_defineProperty(PlacesAutoComplete, "defaultProps", {
axiosinstance: function axiosinstance() {
return _axios.default.create({});
},
latitude: '-1.292066',
longitude: '36.821946',
onChange: function onChange() {},
value: null,
input: {
value: null,
onChange: function onChange() {}
},
placeholder: 'Search location...',
height: 31,
region: null,
API_KEY: ''
});
var styles = {};
var _default = (0, _withStyles.default)(styles)(PlacesAutoComplete);
exports.default = _default;
//# sourceMappingURL=PlacesAutoComplete.js.map