Skip to content
TheChatArchiveThe Chat Archive
gpt-4-1106-preview· openaiHugging Face Suzhen/CodeChat

Here's the current code: (ns ^:figwheel-hooks simple-server.core (:require…

  1. User

    Here's the current code:

    (ns ^:figwheel-hooks simple-server.core (:require [clojure.string :as str] [ajax.core :refer [GET]] [goog.dom :as gdom] [goog.events :as events] [goog.history.EventType :as HistoryEventType] [reagent.core :as r :refer [atom]] [reagent.dom :as rdom] [reitit.core :as reitit]) (:import goog.History))

    (defonce session (r/atom {:page :home}))

    (defn nav-link [uri title page] [:a.navbar-item {:href uri :class (when (= page (:page @session)) "is-active")} title])

    (defn navbar [] (r/with-let [expanded? (r/atom false)] [:nav.navbar.is-info>div.container [:div.navbar-brand [:a.navbar-item {:href "/" :style {:font-weight :bold}} "Guessing Game"] [:span.navbar-burger.burger {:data-target :nav-menu :on-click #(swap! expanded? not) :class (when @expanded? :is-active)} [:span] [:span] [:span]]] [:div#nav-menu.navbar-menu {:class (when @expanded? :is-active)} [:div.navbar-start [nav-link "#/" "Home" :home] [nav-link "#/login" "Log In" :login]]]]))

    (def guess-val (r/atom 5)) (defn slider-on-change-handler [js-event] (reset! guess-val (-> js-event .-target .-value)))

    (defn login-page [] [:section.section>div.container>div.content [:img {:src "/img/warning_clojure.png"}]])

    (defn multiply [a b] (* a b))

    ;; define your app data so that it doesn't get over-written on reload (defonce app-state (atom {:text "Hello world!"}))

    (defn get-app-element [] (gdom/getElement "app"))

    (defn base-page [] [:div [navbar] [:h1 (:text @app-state)] [:h3 "Guess the number I'm thinking of!"] [:div {:class "slidecontainer"} [:input {:type "range" :id "MyRange1" :min 1 :max 10 :value 5 :on-change slider-on-change-handler}]] [:h3 @guess-val]

    ])

    (defn home-page [] [:section.section>div.container>div.content [:h1 {:style {:display "flex" :justify-content "center" :align-items "center"}} "ApexCharts.js"] [:h4 "ApexCharts Basics"] [:p {:style {:display "flex" :justify-content "center" :align-items "center"}} "ApexCharts is a modern charting library that works with the React framework. It provides a wide range of chart types and features such as animations, zooming, panning, and updating series dynamically. It is also responsive, making it suitable for creating interactive data visualizations for both desktop and mobile devices. It is open-source and provided under the MIT licence, so it is entirely viable for commercial projects."]

    ])

    (def pages {:home #'home-page :login #'login-page})

    (defn page [] [(pages (:page @session))])

    ;; ------------------------- ;; Routes

    (def router (reitit/router [["/" :home] ["/login" :login]]))

    (defn match-route [uri] (->> (or (not-empty (str/replace uri #"^.*#" "")) "/") (reitit/match-by-path router) :data :name))

    ;; ------------------------- ;; History ;; must be called after routes have been defined (defn hook-browser-navigation! [] (doto (History.) (events/listen HistoryEventType/NAVIGATE (fn [^js/Event.token event] (swap! session assoc :page (match-route (.-token event))))) (.setEnabled true)))

    ;; ------------------------- ;; Initialize app (defn fetch-docs! [] (GET "/docs" {:handler #(swap! session assoc :docs %)}))

    (defn ^:dev/after-load mount-components [] (rdom/render [navbar] (.getElementById js/document "navbar")) (rdom/render [page] (.getElementById js/document "app")))

    (defn init! []

    (fetch-docs!) (hook-browser-navigation!) (mount-components))

    (defn mount [el] (rdom/render [base-page] el) (rdom/render [#'navbar] (.getElementById js/document "navbar")) (rdom/render [#'page] (.getElementById js/document "app")))

    (defn mount-app-element [] (when-let [el (get-app-element)] (mount el)))

    ;; conditionally start your application based on the presence of an "app" element ;; this is particularly helpful for testing this ns without launching the app ;(mount-app-element)

    ;; specify reload hook with ^:after-load metadata (defn ^:after-load on-reload [] (mount-app-element) ;; optionally touch your app-state to force rerendering depending on ;; your application ;; (swap! app-state update-in [:__figwheel_counter] inc) )

    The issues it has are:

    1. When loading or reloading the page in browser it doesn't initialize, but it initializes when saving the file and triggering the update.
    2. The routes don't seem to be functioning.
  2. Assistant

    Looking at your code, there are a few things that stand out.

    1. The function init! is defined but it is not being called when the application loads for the first time. You need to call init! when the application loads to ensure that your app gets mounted and the browser navigation hook gets set.

    2. Instead of multiple render calls in the mount function, you should render a single root component that encompasses all your other views (like home-page and login-page). This root component is usually where you maintain consistent layout elements (like a navbar) and swap out the page content based on the route.

    Here's a simplified approach to how you might adjust your initialization and rendering strategy:

    First, make sure to call init! after its definition. Likely right in the script or by using a (defn main! [] ...) that calls init! and then call (main!) at the bottom of your script.

    Second, adjust your route rendering logic. Rather than calling rdom/render multiple times with different parts of the interface, have one unified root component that is responsible for rendering the correct page based on the current session state.

    Change your mount-components function to look more like this:

    (defn root-component []
      [:div
       [navbar]
       [page]]) 
    
    (defn mount-components []
      (rdom/render [root-component] (get-app-element)))
    

    With root-component being the main entry point, inserting your navbar and the page content based on current session state.

    Third, about the routing. In your hook-browser-navigation! function, you have:

    (fn [^js/Event.token event]
      (swap! session assoc :page (match-route (.-token event)))))
    

    Here, there might be a mistake because typically, event is the object that should match to the destructuring, but you've added ^js/Event.token event, which seems incorrect. You might want to change it to simply:

    (fn [event]
      (swap! session assoc :page (match-route (.-token event))))
    

    Finally, about testing if the app element is present before mounting:

    (defn mount-app-element []
      (when-let [el (get-app-element)]
        (mount el)))
    

    This function should be correct, but you need to make sure the template includes an element with id="app" for this to work, and it should be called properly at the application startup.

    Make these fixes, and then try loading your application again. If the problems persist, check if any errors are coming up in the browser's console that could give you more clues about what's going wrong.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.