Last active
August 29, 2015 14:07
-
-
Save homam/bac91aa32fbc77871e27 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# *-state-maker :: (model) -> {render, cleanup} | |
# state-maker s are modules and can be defined in different modules | |
# render :: (all-state-makers, callback) -> void | |
# all-state-makers is an object, it's useful for explicitly specifying which states are accessible from this render function. | |
second = (_, b) -> b | |
init-state-maker = (_) -> | |
<- second -> | |
{ | |
render: ({sub-method-state-maker}, callback) -> | |
{visit-id, sub-method} <- $.get '/visits' | |
callback null, (sub-method-state-maker {visit-id, sub-method}) | |
cleanup: (callback) -> callback null | |
} | |
sub-method-state-maker = (model:{visit-id, sub-method, error}) -> | |
is-pin = sub-method.type == 'pin' | |
is-mo = sub-method.type == 'mo' | |
$parent = \.state.sub-method | |
<- second -> | |
{ | |
render: ({pin-method-state-maker, sub-method-state-maker}, callback) -> | |
$ \body .add-class \state-sub-method | |
if is-mo | |
$parent .find \.sub-method.mo .show! | |
else if is-pin | |
$parent .find \.sub-method.pin .show! | |
else | |
$parent .find \.sub-method.normal .show! | |
if error is not null | |
switch error | |
| 'msisdin-submission' => $ \.msisdin-submission.error .show! | |
| otherwise => console.error 'Unknown Error' | |
else | |
$ \.error .hide! | |
subscribe = -> | |
switch | |
| is-pin => | |
{succeed, submission-id} <- $.get '/submit' | |
callback <| if succeed then (pin-state-maker {visit-id, submission-id, retries: 0}) else sub-method-state-maker model <<< error: 'msisdin-submission' | |
| otherwise => | |
window.location.href = sub-method.url | |
callback <| congrats-state-maker {visit-id, sub-method} | |
$parent .find \.subscribe .on \click, subscribe | |
cleanup: (callback) -> | |
$parent | |
..find \.error .hide! | |
..find \.sub-method .hide! | |
..hide! | |
$ \body .remove-class \state-sub-method | |
callback null | |
} | |
pin-method-state-maker = (model:{visit-id, submission-id, retries, error}) -> | |
$parent = $ \.state-pin | |
<- second -> | |
{ | |
render: ({pin-method-state-maker, sub-method-state-maker, congrats-state-maker}, callback) -> | |
$ \body .add-class \state-pin | |
if error is not null | |
$ \.error .show! | |
submit-pin = -> | |
{succeed, mo:{short-code, keyword}?, subscriber-id, service-url} <- $.get '/submit-pin' | |
callback null, do -> | |
if succeed | |
congrats-state-maker {visit-id, subscriber-id, service-url} | |
else | |
if retries > 2 and mo is not null | |
sub-method-state-maker {visit-id, sub-method: {type: 'mo', short-code, keyword}} | |
else | |
pin-method-state-maker model <<< retries: retries + 1, error: 'invalid-pin' | |
$parent.find \.submit .on \click, -> submit-pin | |
cleanup: (next-state, callback) -> | |
$parent | |
..find \.error .hide | |
..hide! | |
$ \body .remove-class \state-pin | |
callback null | |
} | |
all-state-makers = { | |
init-state-maker | |
sub-method-state-maker | |
pin-method-state-maker | |
} | |
next = (prev-state:{cleanup}?, current-state:{render}) -> | |
_ <- cleanup current-state | |
_, next-state <- render all-state-makers | |
next current-state, next-state | |
next null, (init-state-maker {cleanup: -> it null}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment