Skip to content

Instantly share code, notes, and snippets.

@gabeklein
Last active August 22, 2019 18:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save gabeklein/c1bb6f4d4d5ff4f5f50bf7ab0c8742b4 to your computer and use it in GitHub Desktop.
Save gabeklein/c1bb6f4d4d5ff4f5f50bf7ab0c8742b4 to your computer and use it in GitHub Desktop.

Controller

class StickySituation extends Control {
  remaining = 60;
  surname = "bond";

  didMount(){
    this.timer = 
      setInterval(() => {
        const remains = this.remaining -= 1
        if(remains == 0)
          cutTheDrama();
      }, 1000);
  }

  willUnmount(){
    this.cutTheDrama()
  }

  cutTheDrama(){
    clearInterval(this.timer);
  }

  getSomebodyElse(){
    fetch("https://randomuser.me/api/")
    .then(res => res.json())
    .then(data => data.results[0])
    .then(recruit => {
      this.surname = recruit.name.last
    })
  }
}

Component

const ActionSequence = () => {
  const { surname, remaining, getSomebodyElse } = StickySituation.use();

  if(agent.remaining == 0)
    return <h1>🙀💥</h1>

  return (
    <div>
      <div>Agent <b>{surname}</b> we need you to diffuse the bomb!</div>
      <div>
        If you can't diffuse it in {remaining} seconds, the cat may or may not die!
      </div>
      <div>
        But there is time! 
        <u onClick={getSomebodyElse}>
          Tap another agent
        </u> 
        if you think they can do it.
      </div>
    </div>
  )
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment