2016-01-28 19:55:17 +00:00
context = window
rest = context . JK . Rest ( )
TeacherSearchStore = @ TeacherSearchStore
TeacherSearchActions = @ TeacherSearchActions
TeacherSearchResultsActions = @ TeacherSearchResultsActions
LocationActions = @ LocationActions
InstrumentStore = @ InstrumentStore
SubjectStore = @ SubjectStore
GenreStore = @ GenreStore
LanguageStore = @ LanguageStore
ProfileActions = @ ProfileActions
@TeacherSearchScreen = React . createClass ( {
# Reflux.listenTo(@TeacherSearchStore, "onTeacherSearchChanged"),
mixins: [ Reflux . listenTo ( @ AppStore , " onAppInit " ) , Reflux . listenTo ( @ UserStore , " onUserChanged " ) ,
2016-04-06 02:23:15 +00:00
Reflux . listenTo ( @ TeacherSearchResultsStore , " onTeacherSearchResultsStore " ) ,
Reflux . listenTo ( @ TeacherSearchStore , " onTeacherSearchStore " ) ]
2016-01-28 19:55:17 +00:00
LIMIT: 20
visible: false
2016-04-06 02:23:15 +00:00
needToSearch: true
root: null
2016-05-18 01:29:56 +00:00
screen: null
2016-04-06 02:23:15 +00:00
endOfList: null
contentBodyScroller: null
refreshing: false
2016-01-28 19:55:17 +00:00
getInitialState: () ->
2016-06-01 00:20:03 +00:00
{ searchOptions: { } , results: [ ] , user: null , searching: false }
2016-01-28 19:55:17 +00:00
onAppInit: (@app) ->
@ app . bindScreen ( ' teachers/search ' , { beforeShow: @ beforeShow , afterShow: @ afterShow , afterHide: @ afterHide } )
beforeShow: (e) ->
@visible = true
afterShow: (e) ->
2016-05-26 21:25:51 +00:00
UserActions . refresh ( )
2016-05-26 23:10:05 +00:00
2016-01-28 19:55:17 +00:00
#@setState(TeacherSearchStore.getState())
2016-04-06 02:23:15 +00:00
#if @state.results.length == 0
# don't issue a new search every time someone comes to the screen, to preserve location from previous browsing
if @ needToSearch
@ contentBodyScroller . off ( ' scroll ' )
@ endOfList . hide ( )
TeacherSearchResultsActions . reset ( )
@needToSearch = false
2016-01-28 19:55:17 +00:00
afterHide: (e) ->
2016-05-18 01:29:56 +00:00
@visible = false
2016-05-16 16:39:20 +00:00
@ contentBodyScroller . off ( ' scroll ' )
2016-05-18 01:29:56 +00:00
@ hideSideBubble ( )
2016-01-28 19:55:17 +00:00
2016-04-06 02:23:15 +00:00
onTeacherSearchStore: (storeChanged) ->
@needToSearch = true
onTeacherSearchResultsStore: (results) ->
2016-06-01 00:20:03 +00:00
#results.searching = false
2016-04-06 02:23:15 +00:00
@refreshing = false
@ contentBodyScroller . find ( ' .infinite-scroll-loader-2 ' ) . remove ( )
@ setState ( results )
2016-01-28 19:55:17 +00:00
onUserChanged: (@user) ->
2016-05-05 02:20:38 +00:00
@ setState ( { user: @ user ? . user } )
2016-01-28 19:55:17 +00:00
#onTeacherSearchChanged: (options) ->
# if @visible
# @setState(options)
componentDidMount: () ->
@root = $ ( @ getDOMNode ( ) )
2016-05-18 01:29:56 +00:00
@screen = $ ( ' # teacherSearch ' )
2016-01-28 19:55:17 +00:00
@resultsNode = @ root . find ( ' .results ' )
2016-04-06 02:23:15 +00:00
@endOfList = @ root . find ( ' .end-of-teacher-list ' )
@contentBodyScroller = @ root
2016-05-18 01:29:56 +00:00
2016-04-06 02:23:15 +00:00
registerInfiniteScroll : () ->
$scroller = @ contentBodyScroller
logger . debug ( " registering infinite scroll " )
$scroller . off ( ' scroll ' )
$scroller . on ( ' scroll ' , () =>
# be sure to not fire off many refreshes when user hits the bottom
return if @ refreshing
if $scroller . scrollTop ( ) + $scroller . innerHeight ( ) + 100 >= $scroller [ 0 ] . scrollHeight
$scroller . append ( ' <div class= " infinite-scroll-loader-2 " >... Loading more Teachers ...</div> ' )
@refreshing = true
2016-06-01 00:20:03 +00:00
#@setState({searching: true})
2016-04-06 02:23:15 +00:00
logger . debug ( " refreshing more teachers for infinite scroll " )
TeacherSearchResultsActions . nextPage ( )
)
2016-05-18 01:29:56 +00:00
showSideBubble: () ->
setTimeout (
( () => (
if @ visible
context . JK . HelpBubbleHelper . didntFindTeacher ( @ screen , null , @ state . user || { } , ( (note, email, phone) => @ postHelp ( note , email , phone ) ) )
) ) , 3000 )
hideSideBubble: () ->
if @ screen . btOff
@ screen . btOff ( )
postHelp: (value, email, phone) ->
if ! value ? || value == ' '
@ app . layout . notify ( { title: ' note required ' , text: ' Please enter something about what you are looking for. ' } )
return
if ! context . JK . currentUserId ? && ( ! email ? || email == ' ' )
@ app . layout . notify ( { title: ' email required ' , text: ' Please enter your email. ' } )
return
rest . askSearchHelp ( { note: value , email: email , phone: phone } ) . done ( (response) => @ postHelpDone ( ) ) . fail ( @ app . ajaxError )
postHelpDone: () ->
console . log ( " show notice " )
context . JK . Banner . showNotice ( " request received " , " We got your note. We will reach back shortly! " )
@ hideSideBubble ( )
2016-01-28 19:55:17 +00:00
componentDidUpdate: () ->
@ resultsNode . find ( ' .teacher-bio ' ) . each ( (index, element) => (
$this = $ ( element )
if ! $this . data ( ' dotdotdot ' )
$this . dotdotdot ( {
after: " a.readmore "
} )
) )
2016-04-06 02:23:15 +00:00
2016-05-16 16:39:20 +00:00
if @ visible
2016-05-18 01:29:56 +00:00
if @ state . currentPage > 1
@ showSideBubble ( )
2016-05-16 16:39:20 +00:00
if @ state . next == null
@ contentBodyScroller . off ( ' scroll ' )
2017-07-10 02:21:29 +00:00
if @ state . currentPage == 1 and @ state . results . length == 0 && ! @ state . searching
2016-05-16 16:39:20 +00:00
@ endOfList . text ( ' No Teachers found matching your search ' ) . show ( )
logger . debug ( " TeacherSearch: empty search " )
2017-07-10 02:21:29 +00:00
else if @ state . currentPage > 0 && ! @ state . searching
2016-05-16 16:39:20 +00:00
logger . debug ( " end of search " )
@ endOfList . text ( ' No more Teachers ' ) . show ( )
else
@ registerInfiniteScroll ( @ contentBodyScroller )
2016-01-28 19:55:17 +00:00
moreAboutTeacher: (user, e) ->
e . preventDefault ( )
2016-06-01 00:20:03 +00:00
context.location = " /client # /profile/teacher/ #{ user . id } "
#ProfileActions.viewTeacherProfile(user, '/client#/teachers/search', 'BACK TO TEACHER SEARCH')
2016-01-28 19:55:17 +00:00
2016-04-06 02:23:15 +00:00
bookTestDrive: (user, e) ->
2016-01-28 19:55:17 +00:00
e . preventDefault ( )
2016-04-06 02:23:15 +00:00
rest . getTestDriveStatus ( { id: context . JK . currentUserId , teacher_id: user . id } )
. done ( (response) =>
2016-10-03 02:51:34 +00:00
if response . jamclass_credits > 0
logger . debug ( ' TeacherSearchScreen: user has jamclass credits available ' )
window . location.href = ' /client # /jamclass/book-lesson/test-drive_ ' + user . id
else if response . remaining_test_drives == 0 && response [ ' can_buy_test_drive? ' ]
2016-04-06 02:23:15 +00:00
logger . debug ( " TeacherSearchScreen: user offered test drive " )
2016-05-05 02:20:38 +00:00
#@app.layout.showDialog('try-test-drive', {d1: user.teacher.id})
window . location.href = ' /client # /jamclass/test-drive-selection/ ' + user . id
2016-04-06 02:23:15 +00:00
else if response . remaining_test_drives > 0
if response . booked_with_teacher && ! context . JK . currentUserAdmin
logger . debug ( " TeacherSearchScreen: teacher already test-drived " )
2016-05-20 02:42:27 +00:00
context . JK . Banner . showAlert ( ' TestDrive ' , " You have already taken a TestDrive lesson from this teacher. With TestDrive, you need to use your lessons on 4 different teachers to find one who is best for you. We ' re sorry, but you cannot take multiple TestDrive lessons from a single teacher. " )
2016-04-06 02:23:15 +00:00
else
# send on to booking screen for this teacher
logger . debug ( " TeacherSearchScreen: user being sent to book a lesson " )
window . location.href = ' /client # /jamclass/book-lesson/test-drive_ ' + user . id
2016-05-05 02:20:38 +00:00
#window.location.href = '/client#/jamclass/test-drive-selection/' + user.id
2016-04-06 02:23:15 +00:00
else
# user has no remaining test drives and can't buy any
logger . debug ( " TeacherSearchScreen: test drive all done " )
context . JK . Banner . showAlert ( ' TestDrive ' , " You have already taken advantage of the TestDrive program within the past year, so we are sorry, but you are not eligible to use TestDrive again now. You may book a normal lesson with this teacher by closing this message, and clicking the BOOK LESSON button for this teacher. " )
)
. fail ( (jqXHR, textStatus, errorMessage) =>
@ app . ajaxError ( jqXHR , textStatus , errorMessage )
)
2016-04-21 14:23:29 +00:00
bookNormalLesson: (user, e) ->
2016-01-28 19:55:17 +00:00
e . preventDefault ( )
2016-04-21 14:23:29 +00:00
rest . getTestDriveStatus ( { id: context . JK . currentUserId , teacher_id: user . id } )
. done ( (response) =>
if response . remaining_test_drives > 0
buttons = [ ]
buttons . push ( { name: ' CANCEL ' , buttonStyle: ' button-grey ' } )
2016-04-25 15:15:23 +00:00
buttons . push ( { name: ' PAY NORMALLY ' , buttonStyle: ' button-orange ' , click : ( () => ( window . location.href = ' /client # /jamclass/book-lesson/normal_ ' + user . id ) ) } )
buttons . push ( { name: ' USE TEST DRIVE ' , buttonStyle: ' button-orange ' , click : ( () => ( window . location.href = ' /client # /jamclass/book-lesson/test-drive_ ' + user . id ) ) } )
2016-04-21 14:23:29 +00:00
context . JK . Banner . show ( { title: ' You still have TestDrive Credits! ' , html: " You have #{ response . remaining_test_drives } TestDrive credits remaining. Would you rather use your TestDrive credit for a lesson with this teacher? " , buttons: buttons } )
else
window . location.href = ' /client # /jamclass/book-lesson/normal_ ' + user . id
)
. fail ( (jqXHR, textStatus, errorMessage) =>
@ app . ajaxError ( jqXHR , textStatus , errorMessage )
)
2016-01-28 19:55:17 +00:00
bookFreeLesson: (e) ->
e . preventDefault ( )
readMore: (e) ->
e . preventDefault ( )
target = $ ( e . target )
teacherBio = target . closest ( ' .teacher-bio ' )
teacherBio . css ( ' height ' , ' auto ' )
target . trigger ( ' destroy.dot ' ) ;
teacherBio . css ( ' height ' , ' auto ' )
2016-05-16 16:39:20 +00:00
2016-01-28 19:55:17 +00:00
render: () ->
resultsJsx = [ ]
2016-06-01 00:20:03 +00:00
if @ state . currentPage == 1 && @ state . searching
resultsJsx = ` < div className = " spinner spinner-large " > < / div > `
else
for user in @ state . results
2016-01-28 19:55:17 +00:00
2016-06-01 00:20:03 +00:00
photo_url = user . photo_url
if ! photo_url ?
photo_url = ' /assets/shared/avatar_generic.png '
2016-01-28 19:55:17 +00:00
2016-06-01 00:20:03 +00:00
bio = user . teacher . biography
if ! bio ?
bio = ' No bio '
2016-01-28 19:55:17 +00:00
2017-03-22 12:39:06 +00:00
#school_on_school = user.teacher.school_id? && @state.user?.school_id? && user.teacher.school_id == @state.user.school_id
school_on_school = false
2016-05-16 16:39:20 +00:00
2016-06-01 00:20:03 +00:00
bookSingleBtn = null
bookTestDriveBtn = null
2017-07-10 02:21:29 +00:00
backgroundCheck = null
if user . teacher . background_check_at
backgroundCheck = ` < img className = " background-check " src = " /assets/content/background-check.png " / > `
2016-10-03 02:51:34 +00:00
if ! school_on_school && ( ! @ state . user ? || @ state . user . jamclass_credits > 0 || @ state . user . remaining_test_drives > 0 || @ state . user [ ' can_buy_test_drive? ' ] )
2016-06-01 00:20:03 +00:00
bookTestDriveBtn = ` < a className = " button-orange try-test-drive " onClick = { this . bookTestDrive . bind ( this , user ) } > BOOK TESTDRIVE LESSON < / a > `
else
bookSingleBtn = ` < a className = " button-orange try-normal " onClick = { this . bookNormalLesson . bind ( this , user ) } > BOOK LESSON < / a > `
resultsJsx . push ( ` < div key = { user . id } className = " teacher-search-result " data - teacher - id = { user . id } >
< div className = " user-avatar " >
2017-07-10 02:21:29 +00:00
{ backgroundCheck }
2016-06-01 00:20:03 +00:00
< div className = " avatar small " >
< img src = { photo_url } / >
< / div >
< div className = " user-name " >
{ user . name }
< / div >
2016-01-28 19:55:17 +00:00
< / div >
2016-06-01 00:20:03 +00:00
< div className = " user-info " >
< div className = " teacher-bio " >
{ bio }
< a className = " readmore " onClick = { this . readMore } > more < / a >
< / div >
< div className = " teacher-actions " >
< a className = " button-orange more-about-teacher " onClick = { this . moreAboutTeacher . bind ( this , user ) } > MORE ABOUT THIS TEACHER < / a >
{ bookTestDriveBtn }
{ bookSingleBtn }
< / div >
2016-01-28 19:55:17 +00:00
< / div >
2016-06-01 00:20:03 +00:00
< br className = " clearall " / >
< / div > ` )
2016-01-28 19:55:17 +00:00
` < div className = " content-body-scroller " >
< div className = " screen-content " >
< div className = " header " >
2016-06-01 00:20:03 +00:00
< JamClassSearchHeader / >
2016-01-28 19:55:17 +00:00
< / div >
< div className = " results " >
{ resultsJsx }
2016-04-06 02:23:15 +00:00
< div className = " end-of-teacher-list end-of-list " > No more Teachers < / div >
2016-01-28 19:55:17 +00:00
< / div >
< / div >
< / div > `
} )