Hi, I’m making a web app with ClojureScript, react, and re-frame. I’m trying to write…
- User
Hi, I’m making a web app with ClojureScript, react, and re-frame. I’m trying to write code to handle this dropdown menu:
(defn quote-actions [sales-contractor? _ id {{:keys [status sales-order-file-url editable] :as quote} :raw-data idx :idx}] (ra/with-let [approved-quote-exists? @(subscribe [:quote.job/approved-quotes-exist?]) branding-theme-set? @(subscribe [:organisation/branding-themes]) approve-disabled? @(subscribe [:quote.job/approve-button-disabled? idx]) reject-disabled? @(subscribe [:quote.job/reject-button-disabled? idx])] [new/dropdown-button-menu {:id “quote-actions” :label “Quote Actions” :dropdown-toggle {:color (dropdown-color-logic status approved-quote-exists?)} :items [{:label [:div.d-flex “Approve” (when (and (not (= status :approved)) approved-quote-exists?) [ui/with-popover [:i.fas.fa-question-circle.fa-md.ml-2 {:id (str “approved-existing-” id)}] “An approved quote already exists. If you wish to approve this quote, reject that one first.”])] :disabled approve-disabled? :on-click #(dispatch [:quote.job/open-comment-modal id :approved])} {:label “Reject” :disabled reject-disabled? :on-click #(dispatch [:quote.job/open-comment-modal id :rejected])} {:label [:div.d-flex (if sales-order-file-url “Recreate Sales Order” “Create Sales Order”) [ui/with-popover [:i.fas.fa-question-circle.fa-md.ml-2 {:id (str “sales-order-comment-” id)}] (sales-order-popover-logic status editable)]] :disabled (or (not (= status :approved)) ;; Only enable sales order button for approved quotes, and only if the branding theme is set up. (not (:sales-orders branding-theme-set?)) ;; Old quotes which don’t store quote data cannot make sales orders (not editable)) :on-click #(dispatch [:quote.job/api-create-sales-order id])} ;; Sales contractors cannot delete quotes (GOJ 2105) (when-not sales-contractor? [:hr]) ;; processed as separate line items (when-not sales-contractor? {:label “Delete” :on-click #(dispatch [::events/api-delete-quote id])})]}]))
I got comments that it shouldn’t be able to pass through raw Hiccup formatted like, but I need this feature to directly insert the horizontal rule and for other use cases as well.
Here’s the specifics of how that code is handled in the new/ namespace.
(defn dropdown-item-interpreter [config] (let [label (:label config)] (cond (not (nil? label)) [components/dropdown-item config label] ;; if it’s properly formatted, we expect it to be a dropdown item :else config)))
(def default-styles-dropdown-button {:label “Actions” :dropdown-button {:id (str “dropdown-button”) ;; If there are multiple dropdowns with the same ID this leads to problems. ;; You should set this manually. :direction “down”} :dropdown-toggle {:key “dropdown-toggle” :style {:width “”} :color “primary” :caret true} :dropdown-menu {:key “dropdown-menu” :class “dropdown-menu-right”} ;; These items are here to show examples of the structure. ;; :items [{:disabled false ;; :label “Clickable item”} ;; {:disabled true ;; :label [:div.d-flex ;; "Disabled item " ;; [:i.fas.fa-question-circle.fa-md.ml-2]]}] })
(defn dropdown-button-menu “Standard dropdown menu button for Gojee. Takes a config map.
- Assistant
- User
- Assistant
- User
What is the or % item-key for? Can't it just be item-key?
- Assistant
You're right in questioning the use of
or % item-keyin the context provided. In this specific scenario, especially given the way I've laid out the pseudo-code for handling keys, directly usingitem-keywithout theorwould make more sense and simplify the code. My earlier suggestion appears overly cautious and unnecessary for your described scenario.The original intent behind including was to suggest a pattern where, if the item configuration already contains a key, it would use that instead of overwriting it with the newly generated . However, in the context and the example provided, we can assume that every item does not already include a unique key, and thus, assigning directly is the correct and straightforward approach.
- User
- Assistant
- User
I want to make something a vector if it isn't already, but if it's already a vector, I want to leave it as-is.
- Assistant
To achieve this in Clojure, you can use a combination of the
vector?function to check if the input is already a vector and a conditional to either return the input as-is or wrap it in a vector. Here's how you can implement this:(defn ensure-vector [input] (if (vector? input) input [input]))This function checks if
inputis a vector usingvector?. Ifinputis already a vector, it returns it unchanged. If not, it wrapsinputin a vector and returns that.Example Usages:
(ensure-vector [1 2 3]) ;=> [1 2 3] (Already a vector, so it's returned as-is.) (ensure-vector "a string") ;=> ["a string"] (Not a vector, so it's wrapped in one.) (ensure-vector {:key "value"}) ;=> [{:key "value"}] (Same for maps, they're wrapped.) (ensure-vector nil) ;=> [nil] (Wraps even nil values in a vector.)This function is versatile and can handle any input type, making sure that the output is always in vector form while preserving vectors that are already provided.