Guest User

Untitled

a guest
Nov 14th, 2018
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.52 KB | None | 0 0
  1. import React, { Component } from 'react';
  2. import App from "./App";
  3. import firebase from "./firebase.js";
  4. import {Redirect} from "react-router-dom";
  5. import 'antd/dist/antd.css';
  6. import { Layout, Form, Input, Icon, Card, Button, Col, Row, Menu, Avatar, Modal, DatePicker} from 'antd';
  7. import './Offers.css';
  8. import moment from 'moment';
  9.  
  10. const {RangePicker } = DatePicker;
  11. const { Meta } = Card;
  12. const { SubMenu } = Menu;
  13. const { Header, Content, Footer, Sider } = Layout;
  14. const Search = Input.Search;
  15. const InputGroup = Input.Group;
  16. const { TextArea } = Input;
  17. const FormItem = Form.Item;
  18.  
  19.  
  20. export default class Offers extends Component {
  21. constructor(props) {
  22. super(props);
  23. this.state = {
  24. name: "",
  25. userid: "",
  26. password: "",
  27. redirect: false,
  28. redirectTarget: "",
  29. currentVendorItems: [],
  30. user: [],
  31. currentUserName: '',
  32. currentUserDescription: '',
  33. visible: false,
  34. description: '',
  35. currUserBuyerArray: [],
  36. currUserVendorArray: [],
  37. requestedPrice: 'None',
  38. rentalTime: [],
  39. visible: false,
  40. };
  41. }
  42.  
  43. //Router
  44. redirect = e => {
  45. this.setState({
  46. redirect: true,
  47. redirectTarget: e
  48. });
  49. }
  50.  
  51. //Handles canceling an offer, and therefore deleting it
  52. handleDelete = (e) =>{
  53. firebase.database().ref('offers/' + e.id).remove();
  54.  
  55. // **** Might need to just remove it from the current state array to save time ****
  56. this.getOfferInfo();
  57. this.handleRented(e.itemID, false);
  58. }
  59.  
  60. //Handles vendor rejecting, so buyer can cancel or make a counter offer
  61. handleReject = (e) =>{
  62. firebase.database().ref('offers/' + e.id).update({
  63. status: "Rejected"
  64. });
  65. this.getOfferInfo();
  66. }
  67.  
  68. //Handles acceptance of an offer, buyer now will have option to pay
  69. handleAccept = (e) =>{
  70. firebase.database().ref('offers/' + e.id).update({
  71. status: "Accepted"
  72. });
  73. this.getOfferInfo();
  74. this.handleRented(e.itemID, true);
  75. }
  76.  
  77. handleRented = (e, boolean) =>{
  78. firebase.database().ref('items/' + e).update({
  79. itemRented: boolean
  80. });
  81. }
  82.  
  83. //Modal Functions
  84. showModal = () => {
  85. this.setState({
  86. visible: true,
  87. });
  88. }
  89.  
  90. handleOk = (e) => {
  91. this.setState({
  92. visible: false,
  93. });
  94. }
  95.  
  96. handleCancel = (e) => {
  97. this.setState({
  98. visible: false,
  99. });
  100. }
  101.  
  102. //Disables days before the current day
  103. disabledDate = (current) => {
  104. // Can not select days before today and today
  105. return current && current < moment().endOf('day');
  106. }
  107.  
  108. //Handles time string entered
  109. handleTime = (value, dateString) => {
  110. this.setState({
  111. rentalTime: dateString
  112. })
  113. }
  114.  
  115. //Handles additonal info inputted
  116. handlePrice = (e) => {
  117. e.preventDefault();
  118. this.setState({
  119. requestedPrice: e.target.value
  120. });
  121. }
  122.  
  123. //Handles submission of a counter offer on a rejected item
  124. handleSubmit = (date, reqPrice, offerID) => {
  125. firebase.database().ref('offers/' + offerID).update({
  126. status: "Pending",
  127. date: date,
  128. requestedPrice: reqPrice
  129. });
  130. this.getOfferInfo();
  131. this.handleCancel();
  132. }
  133.  
  134. getUserInfo = () => {
  135. firebase.auth().onAuthStateChanged( authUser => {
  136. if (authUser) {
  137. // User is signed in.
  138. //Auth database reference
  139. var user = firebase.auth().currentUser
  140. this.setState({
  141. user: firebase.auth().currentUser
  142. });
  143.  
  144. } else {
  145. // No user is signed in.
  146. console.log('we messed up somewhere!')
  147. }
  148. });
  149. }
  150.  
  151. getOfferInfo = () =>{
  152. const currUserOffersRef = firebase.database().ref('offers/');
  153. currUserOffersRef.on('value', (snapshot) => {
  154. let offersSnapshot = snapshot.val();
  155.  
  156. let buyerOffersArray = []
  157. let vendorOffersArray = []
  158.  
  159. for (let offer in offersSnapshot) {
  160. //If the user id == the user id on the offer, they made it
  161. if (this.state.user.uid === offersSnapshot[offer].userUID){
  162. buyerOffersArray.push({
  163. id: offer,
  164. name: offersSnapshot[offer].name,
  165. date: offersSnapshot[offer].date,
  166. info: offersSnapshot[offer].info,
  167. itemID: offersSnapshot[offer].itemID,
  168. userUID: offersSnapshot[offer].userUID,
  169. vendorUID: offersSnapshot[offer].vendorUID,
  170. price: offersSnapshot[offer].price,
  171. requestedPrice: offersSnapshot[offer].requestedPrice,
  172. status: offersSnapshot[offer].status,
  173. });
  174. }
  175. //if the user id == the vendor id on the offer, they created the listing
  176. if (this.state.user.uid === offersSnapshot[offer].vendorUID){
  177. vendorOffersArray.push({
  178. id: offer,
  179. name: offersSnapshot[offer].name,
  180. date: offersSnapshot[offer].date,
  181. info: offersSnapshot[offer].info,
  182. itemID: offersSnapshot[offer].itemID,
  183. userUID: offersSnapshot[offer].userUID,
  184. vendorUID: offersSnapshot[offer].vendorUID,
  185. price: offersSnapshot[offer].price,
  186. requestedPrice: offersSnapshot[offer].requestedPrice,
  187. status: offersSnapshot[offer].status,
  188. });
  189. }
  190. }
  191. this.setState({
  192. currUserBuyerArray: buyerOffersArray,
  193. currUserVendorArray: vendorOffersArray
  194. });
  195. })
  196. }
  197.  
  198. // On component mount, get info from DB and auth as well as the offer info
  199. componentDidMount() {
  200. this.getUserInfo();
  201. this.getOfferInfo();
  202. }
  203.  
  204. render() {
  205. //Router
  206. if(this.state.redirect === true){
  207. return <Redirect to= {this.state.redirectTarget} />;
  208. }
  209. console.log(this.state)
  210. return (
  211. <Layout>
  212. <Layout className="layout-header">
  213. <Header>
  214. <div className="logo" />
  215. <Menu
  216. theme="dark"
  217. mode="horizontal"
  218. defaultSelectedKeys={['2']}
  219. style={{ lineHeight: '64px' }}
  220. >
  221. <Menu.Item key="1" onClick={e => this.redirect("/Marketplace")}>Marketplace</Menu.Item>
  222. <Menu.Item key="2" onClick={e => this.redirect("/Profile")}>Profile</Menu.Item>
  223. <Menu.Item key="3" onClick={e => this.redirect("/VendorPage")}>Create Listing</Menu.Item>
  224. </Menu>
  225. </Header>
  226.  
  227. <Layout className="layout-main">
  228. <Sider width={300} style={{ background: '#fff' }}>
  229. <Menu
  230. mode="inline"
  231. defaultSelectedKeys={['3']}
  232. style={{ height: '100%', borderRight: 0 }}
  233. >
  234. <Menu.Item key="1" onClick={e => this.redirect("/Profile")}>Profile</Menu.Item>
  235. <Menu.Item key="2" onClick={e => this.redirect("/MyItems")}>My Items</Menu.Item>
  236. <Menu.Item key="3">Offers</Menu.Item>
  237. </Menu>
  238. </Sider>
  239. <Layout className="layout-profile">
  240. <div className= "offers-other-items" style={{margin: 40}}>
  241. <Row>
  242. <Col span = {22}>
  243. <Card title="My Offers">
  244. { //Checks if the array is empty, and lets the user know if there are no offers
  245. (this.state.currUserBuyerArray.length == 0)
  246. ? <div>You have no current offers on items</div>
  247. :
  248. <div>
  249. {this.state.currUserBuyerArray.map(offer => {
  250. return (
  251. <div>
  252. <Row>
  253. <Col span={20} offset={2}>
  254. <div className="card" style={{background: "#c4c4c4", width: "100%", margin: "0 auto"}}>
  255. <Card className="card-content" title={offer.name + " - " + offer.status}>
  256. Intial Price: ${offer.price} <br/>
  257. Requested Price: ${offer.requestedPrice} <br/>
  258. Requested Dates: {offer.date[0]} to {offer.date[1]} <br/>
  259. <div>
  260. {
  261. (offer.status === "Pending")
  262. ? <div className="offersButton">
  263. <Button type="primary" icon="delete" onClick={e => this.handleDelete(offer)}>Cancel Offer</Button>
  264. </div>
  265. : <div></div>
  266. }
  267. {
  268. (offer.status === "Accepted")
  269. ? <div className="offersButton">
  270. <Button type="primary" icon="delete" onClick={e => this.handleDelete(offer)}>Cancel Offer</Button>
  271. <Button type="primary" icon="check" onClick={e => this.handlePayment(offer.id)}>Payment</Button>
  272. </div>
  273. : <div></div>
  274. }
  275. {
  276. (offer.status === "Rejected")
  277. ?
  278. <div className="offersButton">
  279. <Button type="primary" icon="delete" onClick={e => this.handleDelete(offer)}>Cancel Offer</Button>
  280. <Button type="primary" icon="check" onClick={e => this.showModal()}>Make another Offer</Button>
  281. <Modal
  282. visible={this.state.visible}
  283. onOk={this.handleOk}
  284. onCancel={this.handleCancel}
  285. footer={[
  286. <Button key="back" onClick={this.handleCancel}>Cancel</Button>,
  287. <Button type="primary" onClick={e => this.handleSubmit(this.state.rentalTime, this.state.requestedPrice, offer.id)}>
  288. Request
  289. </Button>
  290. ]}
  291. >
  292. <RangePicker
  293. disabledDate={this.disabledDate}
  294. format="MM-DD-YYYY"
  295. placeholder={['Start Time', 'End Time']}
  296. onChange={this.handleTime}
  297. />
  298. <p style = {{color: "red"}}> {this.state.errorMessage}</p>
  299. <p>Request a different price (Optional)</p>
  300. <Input
  301. placeholder="Requested Price"
  302. onChange={this.handlePrice}
  303. />
  304. </Modal>
  305. </div>
  306. : <div></div>
  307. }
  308. </div>
  309. </Card>
  310. </div>
  311. </Col>
  312. </Row>
  313. <br />
  314. </div>
  315. )
  316. })
  317. }
  318. </div>
  319. }
  320. </Card>
  321. </Col>
  322. </Row>
  323. </div>
  324. <div className="offers-my-items" style={{margin: 40}}>
  325. <Row>
  326. <Col span = {22}>
  327. <Card title="Offers on my Items">
  328.  
  329. { //Check if message failed
  330. (this.state.currUserVendorArray.length == 0)
  331. ? <div>There are no offers on your items</div>
  332. :
  333. <div>
  334. {this.state.currUserVendorArray.map(offer => {
  335. return (
  336. <div>
  337. <Row>
  338. <Col span={20} offset={2}>
  339. <div className="card" style={{background: "#c4c4c4", width: "100%", margin: "0 auto"}}>
  340. <Card className="card-content" title={offer.name + " - " + offer.status}>
  341. Intial Price: ${offer.price} <br/>
  342. Requested Price: ${offer.requestedPrice} <br/>
  343. Requested Dates: {offer.date[0]} to {offer.date[1]} <br/>
  344. <div>
  345. {
  346. (offer.status === "Pending")
  347. ? <div className="offersButton">
  348. <Button type="primary" icon="delete" onClick= {e => this.handleReject(offer.id)}>Reject</Button>
  349. <Button type="primary" icon="check" onClick= {e => this.handleAccept(offer)}>Accept</Button>
  350. </div>
  351. : <div></div>
  352. }
  353. {
  354. (offer.status === "Rejected")
  355. ? <div className="offersButton">
  356. <p>Awaiting counter offer from buyer, or you can delete the offer </p>
  357. <Button type="primary" icon="delete" onClick= {e => this.handleDelete(offer)}>Cancel Offer</Button>
  358. </div>
  359. : <div></div>
  360. }
  361. </div>
  362. </Card>
  363. </div>
  364. </Col>
  365. </Row>
  366. <br/>
  367. </div>
  368. )
  369. })
  370. }
  371. </div>
  372. }
  373. </Card>
  374. </Col>
  375. </Row>
  376. </div>
  377. </Layout>
  378. </Layout>
  379. </Layout>
  380. </Layout>
  381. );
  382. }
  383. }
Add Comment
Please, Sign In to add comment