sipp11
9 years ago
13 changed files with 63 additions and 158 deletions
@ -1,47 +0,0 @@
|
||||
import React from 'react'; |
||||
import AuthenticatedComponent from './AuthenticatedComponent'; |
||||
import QuoteStore from '../stores/QuoteStore.js'; |
||||
import QuoteService from '../services/QuoteService.js'; |
||||
|
||||
export default AuthenticatedComponent(class Quote extends React.Component { |
||||
constructor(props) { |
||||
super(props); |
||||
this.state = this.getQuoteState(); |
||||
this._onChange = this._onChange.bind(this); |
||||
} |
||||
|
||||
componentDidMount() { |
||||
if (!this.state.quote) { |
||||
this.requestNextQuote(); |
||||
} |
||||
|
||||
QuoteStore.addChangeListener(this._onChange); |
||||
} |
||||
|
||||
componentWillUnmount() { |
||||
QuoteStore.removeChangeListener(this._onChange); |
||||
} |
||||
|
||||
_onChange() { |
||||
this.setState(this.getQuoteState()); |
||||
} |
||||
|
||||
requestNextQuote() { |
||||
QuoteService.nextQuote(); |
||||
} |
||||
|
||||
getQuoteState() { |
||||
return { |
||||
quote: QuoteStore.quote |
||||
}; |
||||
} |
||||
|
||||
render() { |
||||
return ( |
||||
<div> |
||||
<h1>{this.state.quote}</h1> |
||||
<button className="btn btn-primary" type="button" onClick={this.requestNextQuote}>Next Quote</button> |
||||
</div> |
||||
); |
||||
} |
||||
}); |
@ -1,6 +0,0 @@
|
||||
var BASE_URL = 'http://rocket.dev/'; |
||||
export default { |
||||
BASE_URL: BASE_URL, |
||||
QUOTE_URL: BASE_URL + 'account/', |
||||
QUOTE_GET: 'QUOTE_GET' |
||||
} |
@ -1,27 +0,0 @@
|
||||
import request from 'reqwest'; |
||||
import when from 'when'; |
||||
import {QUOTE_URL} from '../constants/QuoteConstants'; |
||||
import QuoteActions from '../actions/QuoteActions'; |
||||
import LoginStore from '../stores/LoginStore.js'; |
||||
|
||||
class QuoteService { |
||||
|
||||
nextQuote() { |
||||
|
||||
request({ |
||||
url: QUOTE_URL, |
||||
method: 'GET', |
||||
crossOrigin: true, |
||||
headers: { |
||||
'Authorization': 'Bearer ' + LoginStore.jwt |
||||
} |
||||
}) |
||||
.then(function(response) { |
||||
console.log(response); |
||||
QuoteActions.gotQuote(response); |
||||
}); |
||||
} |
||||
|
||||
} |
||||
|
||||
export default new QuoteService() |
@ -1,33 +0,0 @@
|
||||
import {QUOTE_GET} from '../constants/QuoteConstants'; |
||||
import {LOGOUT_USER} from '../constants/LoginConstants'; |
||||
import BaseStore from './BaseStore'; |
||||
|
||||
class QuoteStore extends BaseStore { |
||||
|
||||
constructor() { |
||||
super(); |
||||
this.subscribe(() => this._registerToActions.bind(this)) |
||||
this._quote = ''; |
||||
} |
||||
|
||||
_registerToActions(action) { |
||||
switch(action.actionType) { |
||||
case QUOTE_GET: |
||||
this._quote = action.quote; |
||||
this.emitChange(); |
||||
break; |
||||
case LOGOUT_USER: |
||||
this._quote = null; |
||||
this.emitChange(); |
||||
break; |
||||
default: |
||||
break; |
||||
}; |
||||
} |
||||
|
||||
get quote() { |
||||
return this._quote; |
||||
} |
||||
} |
||||
|
||||
export default new QuoteStore(); |
Loading…
Reference in new issue