I'm building a store website with clojure, reagent, react, and bootstrap. Here is the…
- User
I'm building a store website with clojure, reagent, react, and bootstrap. Here is the code for a page.
(ns jimmystore.pages.photo-page (:require [reagent.core :as ra] [re-frame.core :as rf] [jimmystore.image-utils :as image-utils]))
;; FIXME make these be the events ones (rf/reg-sub ::img (fn [db _] (::img db)))
(rf/reg-sub ::items (fn [db _] (::items db)))
(def dummy-photo-data {:path "/img/atsui.jpg" ; doing it like this with the intention to pull from server later :title "test"})
(defn photo-card ([photo] [:div {:class "card col-md-2-5 col-5 m-md-2 m-3"} [:img {:src (:path photo) :class "card-img-top"}] [:div.card-body [:h5.card-title (:title photo)]]])
([photo title] [:div {:class "card col-md-2-5 col-5 m-md-2 m-3"} [:img {:src (image-utils/img-blob->img-src photo) :class "card-img-top"}] [:div.card-body [:h5.card-title title]]]))
(defn photos-element [items] [:div {:class "container d-flex flex-wrap align-items-center justify-content-center"} (repeat 16 (photo-card dummy-photo-data)) ; grid test ;; (get (first items) :id)
;; (photo-card (rf/dispatch [:retrieve-item-picture (get (first items) :id)]) "test") ; grid test; TODO: logic for inserting objects from backend ])
(defn page [] (ra/with-let [_ (rf/dispatch [:get-items]) items (rf/subscribe [:items])] [:div{:class "container"} [:h3 (str @items "Select a photo.")] [photos-element @items]]))
(def page-info {:page-id :photo :view #'page})
(ns jimmystore.events (:require [re-frame.core :as rf] [ajax.core :as ajax] [reitit.frontend.easy :as rfe] [reitit.frontend.controllers :as rfc]))
;;dispatchers
(rf/reg-event-db :common/navigate (fn [db [_ match]] (def foo match) (let [old-match (:common/route db) new-match (assoc match :controllers (rfc/apply-controllers (:controllers old-match) match))] (assoc db :common/route new-match))))
(rf/reg-fx :common/navigate-fx! (fn [[k & [params query]]] (rfe/push-state k params query)))
(rf/reg-event-fx :common/navigate! (fn [_ [_ url-key params query]] {:common/navigate-fx! [url-key params query]}))
(rf/reg-event-db :set-docs (fn [db [_ docs]] (assoc db :docs docs)))
(rf/reg-event-fx :fetch-docs (fn [_ _] {:http-xhrio {:method :get :uri "/docs" :response-format (ajax/raw-response-format) :on-success [:set-docs]}}))
(rf/reg-event-fx :test-get-api (fn [_ _] {:http-xhrio {:method :get :uri "/test-get" :response-format (ajax/json-response-format {:keywords? true}) :on-success [:set-docs]}}))
(rf/reg-event-fx :test-post-api (fn [_ _] {:http-xhrio {:method :post :uri "/test-post" :params {:test-post {:data 1 :foo :bar 2 "ASDASD"}} :format (ajax/json-request-format) :response-format (ajax/json-response-format {:keywords? true}) :on-success [:no-op]}}))
- Assistant
To access each item's ID within the collection of items that you've obtained in your Reagent component, you will need to iterate over the items and extract the id from each one. Since dereferences the subscription to a Clojure map that contains an array of item maps under the key, you will typically map over these items and work with each item individually.
- User
Here's the current page code.
(ns jimmystore.pages.photo-page (:require [reagent.core :as ra] [re-frame.core :as rf] [jimmystore.image-utils :as image-utils]))
(def dummy-photo-data {:path "/img/atsui.jpg" ; doing it like this with the intention to pull from server later :title "test"})
(defn photo-card ([photo] [:div {:class "card col-md-2-5 col-5 m-md-2 m-3"} [:img {:src (:path photo) :class "card-img-top"}] [:div.card-body [:h5.card-title (:title photo)]]])
([photo title] [:div {:class "card col-md-2-5 col-5 m-md-2 m-3"} [:img {:src (image-utils/img-blob->img-src photo) :class "card-img-top"}] [:div.card-body [:h5.card-title title]]]))
(defn photos-element [items] (let [item-coll (:items items) ] [:div {:class "container d-flex flex-wrap align-items-center justify-content-center"} [:p (str item-coll)] ;; (repeat 16 (photo-card dummy-photo-data)) ; grid test (get (first item-coll) :id)
- Assistant
- User
I have the images displaying now.
(ns jimmystore.pages.photo-page (:require [reagent.core :as ra] [re-frame.core :as rf] [jimmystore.image-utils :as image-utils]))
(defn photo-card [photo title] [:div {:class "card col-md-2-5 col-5 m-md-2 m-3"} [:img {:src (image-utils/img-blob->img-src photo) :class "card-img-top"}] [:div.card-body [:h5.card-title title]]])
(defn photos-element [items img] (let [item-coll (:items items)] [:div {:class "container d-flex flex-wrap align-items-center justify-content-center"} ;; [:p (str item-coll)] ;; (repeat 16 (photo-card dummy-photo-data)) ; grid test
(let [pic-id (get (first item-coll) :id) img-data (get img pic-id)] (rf/dispatch [:retrieve-item-picture pic-id [:retrieve-picture-success pic-id]]) (photo-card img-data (str pic-id))) (let [pic-id (get (second item-coll) :id) img-data (get img pic-id)] (rf/dispatch [:retrieve-item-picture pic-id [:retrieve-picture-success pic-id]]) (photo-card img-data (str pic-id))) - Assistant
- User
(defn photo-card [photo title] [:div {:class "card col-md-2-5 col-5 m-md-2 m-3"} [:img {:src (image-utils/img-blob->img-src photo) :class "card-img-top"}] [:div.card-body [:h5.card-title.text-align-center title]]])
The text isn't aligning to the center of the cards.
- Assistant
The issue where the text isn't aligning to the center of the cards likely stems from an incorrect or insufficient application of Bootstrap classes for centering text. To ensure the text aligns to the center in Bootstrap, the correct class to use is
text-center. This class appliestext-align: centerto the element it's applied to, and its children.Let's correct your
photo-cardfunction:(defn photo-card [photo title] [:div {:class "card col-md-2-5 col-5 m-md-2 m-3"} [:img {:src (image-utils/img-blob->img-src photo) :class "card-img-top"}] [:div.card-body ;; Apply the correct Bootstrap class here for text centering [:h5.card-title.text-center title]]]) ; Changed from text-align-center to text-centerIn the correction above,
.text-align-centerwas replaced with.text-center. This is assuming you're using a Bootstrap version that follows common naming conventions (e.g., Bootstrap 4 or 5). If you're using a different framework or a custom CSS setup, you might need to adjust the class name according to your specific configuration.