Created
September 1, 2014 13:19
-
-
Save mokevnin/ede01802e4385f51cd0e to your computer and use it in GitHub Desktop.
This file contains hidden or 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
(ns hexlet.codex.actions | |
(:require | |
[jayq.core :as jq] | |
[cljs.core.async :as a] | |
[hexlet.helpers :as h] | |
[hexlet.shared :refer [chan]]) | |
(:require-macros [jayq.macros :as jm])) | |
(enable-console-print!) | |
(defn set-result | |
[state {response :response}] | |
(assoc-in state [:current :result] (:passed response))) | |
(defn append-response | |
[state {:keys [response]}] | |
(let [source (.getValue (:codemirror state)) | |
response-keys (keys response) | |
codex (get-in state [:current :codex]) | |
lesson-slug (get-in state [:lesson :slug]) | |
responses (get-in state [:current :responses]) | |
url (.api_lesson_codex_results_path js/Routes lesson-slug (:slug codex) #js {:version (:version h/gon) :format "json"})] | |
(when (or (nil? response-keys) (= '(:stdout) response-keys)) | |
(jm/let-ajax [response {:url url :method :post :dataType :json | |
:data {:lesson_codex_result {:passed true :source source}}}] | |
(a/put! chan [set-result {:response (js->clj response :keywordize-keys true)}]))) | |
(assoc-in state [:current :responses] (conj responses response)))) | |
(defn submit-code | |
[state] | |
(letfn [(handler [response] (a/put! chan [append-response {:response response}]))] | |
(let [url "http://eval.hexlet.io/api/evaluate" ; TODO move to configus/gon | |
codemirror (:codemirror state)] | |
(jm/let-ajax [response {:url url :method :post :dataType :json | |
:data (h/to-json {:language (get-in state [:lesson :current_language]) :code (.getValue codemirror)}) | |
:error #(handler {:error "add reason"})}] ; TODO add reason | |
(handler (js->clj response :keywordize-keys true)))) | |
state)) | |
(defn set-result | |
[state {:keys [response]}] | |
(assoc-in state [:current :result] (:passed response))) | |
(defn attach-current-codex | |
[{:keys [current codemirror] :as state}] | |
(.setValue codemirror (-> current :codex :filtered_source)) | |
state) | |
(defn set-next-codex | |
[{:keys [lesson current unpassed-codexes codemirror] :as state} {target :target}] | |
(let [[current-codex & rest-codexes] unpassed-codexes | |
next-codex (first rest-codexes)] | |
(if next-codex | |
(let [new-state {:lesson lesson | |
:codemirror codemirror | |
:unpassed-codexes (if (:result current) rest-codexes (concat rest-codexes (list (:codex current)))) | |
:current {:codex next-codex :result nil}}] | |
(attach-current-codex new-state) | |
new-state) | |
{:finished true :lesson lesson}))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment