Skip to content

Instantly share code, notes, and snippets.

@Alex-Bakic
Last active November 18, 2019 13:53
Show Gist options
  • Save Alex-Bakic/03abd353755b7a005dc56e2b096d62c0 to your computer and use it in GitHub Desktop.
Save Alex-Bakic/03abd353755b7a005dc56e2b096d62c0 to your computer and use it in GitHub Desktop.
Feeling-lucky Feature

I'm feeling lucky button : development and walkthrough

The aim of this button is to allow users who are logged in , and have a somewhat completed profile , to simply click on the button and begin applying for random jobs. For just how random depends on how much we want the success of the button to work, which will impact it's randomness. For example, we could pull out of recommended and then give the candidate a chance of actually landing a job. But the recommended section isn't very large as it is pulled in on a page-by-page basis, and unless we call the entirety of the recommended jobs (which would be overkill) it wouldn't be very random, although 20 or so options is good. We wouldn't really want to make a graphql request though for a completely random job, say that uses Scala when the candidate uses Clojure, and if by some miracle he got the job he probably wouldn't want to take it. For now though , in this prototype , the button is in the recommended section.

So when the user hits the button, before we launch the chatbot to get things like the CV, let's pull a random job out of the sub-db:

;; wrapper for wh.logged-in.apply.common-events, pull out random job, just how random though may be tweaked.
;; Right now it is a completely random index, but maybe we want a real chance of this button to land you a job,
;; so we may pull only from :wh.logged-in.personalised-jobs.db/jobs those with a score of over 80% for example.
(reg-event-fx
  ::start-feeling-lucky-application
  personalised-jobs-interceptors
  (fn [{db :db} _]
    ;; initially there isn't an algorithm to shuffle or identify a particular job, just pluck one randomly
    (let [rand-job (rand-nth (::personalised-jobs/jobs db))]
      ;; name should probably be changed to show it is part of this button's application process...
      {:dispatch [::apply/try-random-application rand-job]})))

I've separated the events out, as one needs to be in recommended , to have access to the db , whilst another needs to be in apply and keep track of the whole thing. I'll be making use of the ::apply/feeling-lucky? key to see whether or not , when the application process is finished , to reload the recommended jobs page or not. We would only reload that page for this type of application.

(reg-event-fx ::try-random-application apply-interceptors (fn [{db :db} [job]] ;; we add this kw to denote that we want the recommended page to re-render after a job is picked out. {:db (assoc db ::apply/feeling-lucky? true) :dispatch [:apply/try-apply job :feeling-lucky]}))

The very last event that shows the end of the application process is ::check-application-success , which is where we should reset ::apply/feeling-lucky to false, and dispatch the reloading. Initially I tried this within ::handle-apply, but this is too soon , and things like the visa check would come after the reloading had been called ...

(reg-event-fx
  ::check-application-success
  apply-interceptors
  (fn [{:db db} [{:keys [data]}]]
    (let [result (gql-check-application->check-application (:check_application data))]
      (if (= :rejected (:check-status result))
         (cond-> (assoc db ::apply/current-step :rejection)
                 (:reason result) (assoc-in [::apply/rejection :reason] (:reason result)))
         (merge {:db (-> db
                         (assoc ::apply/updating? true)
                         (assoc ::apply/current-step :thanks)
                         (assoc ::apply/feeling-lucky? false))}
            (when (::apply/feeling-lucky? db)
              {:db (assoc db ::apply/feeling-lucky? false)
               :dispatch [:personalised-jobs/fetch-jobs-by-type :recommended 1]}))))))

And now to include the button itself into page

;; found within personalised_jobs/view.cljs

(defn page
  [type-of-jobs]
  ...
  (when (= type-of-jobs :recommended)
    [:button.button--feeling-lucky {:on-click #(dispatch [::events/start-feeling-lucky-application])} "Feeling Lucky"]))

The button should have the same style as the "improve recommendations" link , but with the ability to dispatch events. Whilst the button class itself is mainly for conversing with the candidate, for the moment I found just extending that bit of CSS, and wrapping the background in a different colour seems to look quite nice:

;; within _buttons.sass

.button
  ...
&--feeling-lucky
  @extend .button
  background: $metal-blue
  &:hover
    background: lighten($metal-blue, 10%) !important
    color: white !important

With the end result being:

pre-hover

post-hover

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment