File tree Expand file tree Collapse file tree 8 files changed +129
-2
lines changed Expand file tree Collapse file tree 8 files changed +129
-2
lines changed Original file line number Diff line number Diff line change 1+ import AppDispatcher from '../dispatchers/AppDispatcher.js' ;
2+ import { QUOTE_GET } from '../constants/QuoteConstants.js' ;
3+
4+ export default {
5+ gotQuote : ( quote ) => {
6+ AppDispatcher . dispatch ( {
7+ actionType : QUOTE_GET ,
8+ quote : quote
9+ } )
10+ }
11+ }
Original file line number Diff line number Diff line change @@ -4,6 +4,7 @@ import AuthenticatedApp from './components/AuthenticatedApp'
44import Login from './components/Login' ;
55import Signup from './components/Signup' ;
66import Home from './components/Home' ;
7+ import Quote from './components/Quote' ;
78import RouterContainer from './services/RouterContainer' ;
89import LoginActions from './actions/LoginActions' ;
910
@@ -12,6 +13,7 @@ var routes = (
1213 < Route name = "login" handler = { Login } />
1314 < Route name = "signup" handler = { Signup } />
1415 < Route name = "home" path = "/" handler = { Home } />
16+ < Route name = "quote" handler = { Quote } />
1517 </ Route >
1618) ;
1719
Original file line number Diff line number Diff line change @@ -66,6 +66,9 @@ export default class AuthenticatedApp extends React.Component {
6666 < li >
6767 < Link to = "home" > Home</ Link >
6868 </ li >
69+ < li >
70+ < Link to = "quote" > Quote</ Link >
71+ </ li >
6972 < li >
7073 < a href = "" onClick = { this . logout } > Logout</ a >
7174 </ li >
Original file line number Diff line number Diff line change 1+ import React from 'react' ;
2+ import AuthenticatedComponent from './AuthenticatedComponent' ;
3+ import QuoteStore from '../stores/QuoteStore.js' ;
4+ import QuoteService from '../services/QuoteService.js' ;
5+
6+ export default AuthenticatedComponent ( class Quote extends React . Component {
7+ constructor ( props ) {
8+ super ( props ) ;
9+ this . state = this . getQuoteState ( ) ;
10+ this . _onChange = this . _onChange . bind ( this ) ;
11+ }
12+
13+ componentDidMount ( ) {
14+ if ( ! this . state . quote ) {
15+ this . requestNextQuote ( ) ;
16+ }
17+
18+ QuoteStore . addChangeListener ( this . _onChange ) ;
19+ }
20+
21+ componentWillUnmount ( ) {
22+ QuoteStore . removeChangeListener ( this . _onChange ) ;
23+ }
24+
25+ _onChange ( ) {
26+ this . setState ( this . getQuoteState ( ) ) ;
27+ }
28+
29+ requestNextQuote ( ) {
30+ QuoteService . nextQuote ( ) ;
31+ }
32+
33+ getQuoteState ( ) {
34+ return {
35+ quote : QuoteStore . quote
36+ } ;
37+ }
38+
39+ render ( ) {
40+ return (
41+ < div >
42+ < h1 > { this . state . quote } </ h1 >
43+ < button className = "btn btn-primary" type = "button" onClick = { this . requestNextQuote } > Next Quote</ button >
44+ </ div >
45+ ) ;
46+ }
47+ } ) ;
Original file line number Diff line number Diff line change 1+ var BASE_URL = 'http://localhost:3001/' ;
2+ export default {
3+ BASE_URL : BASE_URL ,
4+ QUOTE_URL : BASE_URL + 'api/protected/random-quote' ,
5+ QUOTE_GET : 'QUOTE_GET'
6+ }
Original file line number Diff line number Diff line change 1+ import request from 'reqwest' ;
2+ import when from 'when' ;
3+ import { QUOTE_URL } from '../constants/QuoteConstants' ;
4+ import QuoteActions from '../actions/QuoteActions' ;
5+ import LoginStore from '../stores/LoginStore.js' ;
6+
7+ class QuoteService {
8+
9+ nextQuote ( ) {
10+ request ( {
11+ url : QUOTE_URL ,
12+ method : 'GET' ,
13+ crossOrigin : true ,
14+ headers : {
15+ 'Authorization' : 'Bearer ' + LoginStore . jwt
16+ }
17+ } )
18+ . then ( function ( response ) {
19+ QuoteActions . gotQuote ( response ) ;
20+ } ) ;
21+ }
22+
23+ }
24+
25+ export default new QuoteService ( )
Original file line number Diff line number Diff line change 1+ import { QUOTE_GET } from '../constants/QuoteConstants' ;
2+ import { LOGOUT_USER } from '../constants/LoginConstants' ;
3+ import BaseStore from './BaseStore' ;
4+
5+ class QuoteStore extends BaseStore {
6+
7+ constructor ( ) {
8+ super ( ) ;
9+ this . subscribe ( ( ) => this . _registerToActions . bind ( this ) )
10+ this . _quote = '' ;
11+ }
12+
13+ _registerToActions ( action ) {
14+ switch ( action . actionType ) {
15+ case QUOTE_GET :
16+ this . _quote = action . quote ;
17+ this . emitChange ( ) ;
18+ break ;
19+ case LOGOUT_USER :
20+ this . _quote = null ;
21+ this . emitChange ( ) ;
22+ break ;
23+ default :
24+ break ;
25+ } ;
26+ }
27+
28+ get quote ( ) {
29+ return this . _quote ;
30+ }
31+ }
32+
33+ export default new QuoteStore ( ) ;
You can’t perform that action at this time.
0 commit comments