(defn spawn-words-button
"Starts the game with a preset set of words."
[]
(let [game-started (db-get :game-started)]
(when-not @game-started
[:button
{:on-click #(rf/dispatch [::events/set-words-and-letters words/word-collection])
:class "button-style"}
"Get Letters!"])))
(defn shuffle-order-button
"Shuffles the order of the letters displayed."
[display-letters]
[:button {:on-click #(rf/dispatch [::events/shuffle-letter-order display-letters])
:class "button-style"}
"Shuffle letters"])
(defn letter-buttons-panel [valid-letters common-letter]
(let [top-row (take 2 valid-letters)
middle-row (concat (take 1 (drop 2 valid-letters)) [common-letter] (take 1 (drop 3 valid-letters)))
bottom-row (take 2 (drop 4 valid-letters))]
[:div.letter-buttons-panel
[:div.hex-row.top-row (map hex-button top-row)]
;; if index is 1 (the middle one), it passes letter and a truthy value into the argument so it can be detected.
[:div.hex-row.middle-row (doall (map-indexed (fn [idx letter] (hex-button letter (and (= idx 1) (= letter common-letter)))) middle-row))]
[:div.hex-row.bottom-row (map hex-button bottom-row)]]))
(rf/reg-event-db ::submit-word
(fn [db [_ word]]
(let [letters (:letters db)
common-letter (:common-letter db)
words (:words db)
point-val (logic/calculate-points word letters)
submit (partial assoc db :current-input "" :message)]
(js/setTimeout #(rf/dispatch [::reset-shake-message]) 500) ; preemptively set a timeout to kill any shaking
(case (logic/validate-word word words letters common-letter)
:submit-ok (if (contains? (:found-words db) word)
(submit "You've already found that word!")
(-> db
(update :found-words conj word)
(update :score + point-val)
(assoc :current-input "" :message (str "Great job! You found " word ", worth a score of " point-val "!")))) ; add the valid word to found words
:too-short (submit "Only words with 4 letters or more count.")
:not-in-list (submit (str "Sorry, " word " isn't in the word list today."))
:no-common (submit "Nice try, but the word needs to contain the common letter." :shake-message true)
:invalid (submit "All letters in the word must be from the given letter set." :shake-message true :shake-angry true)
:other (submit "Try again.")))))
; use reg-event-fx to dispatch further event to clear input
(defn find-common-letter [word-set]
(reduce
set/intersection
(map set (seq word-set))))
(defn validate-word
"Checks the given word against the current word list and letter set to see if it is valid. Gives the following keywords as a result.
:submit-ok :too-short :invalid :no-common :not-in-list :other"
[word word-list letters common-letter]
(cond
(contains? word-list word) :submit-ok ; first check if the word is in the word collection
(> 4 (count (seq word))) :too-short ; check length, notify if less than 3 letters
(not (every? letters (set word))) :invalid ; check if every letter in the word is in letters set
(not (contains? (set word) (first common-letter))) :no-common ; if it does not contain the common letter
(contains? (set word) (first common-letter)) :not-in-list ; then check if the word at least contains common letter
:else :other)) ; generic if it somehow manages to not match one of the above
Currently, the pop-up message component doesn't appear.
It seems you have created a Reagent component for a pop-up message, message-component, but it isn't displaying as expected. I noticed a couple of potential issues related to managing the pop-up display state and animation that could be responsible for this behavior.
Please review the following suggestions to ensure your message component displays correctly:
In the message-component function, you use a showing-message subscription to determine if the message should be displayed. Ensure that this subscription is updated correctly when you want to show or hide the message.
The @showing-message subscription appears to be referred to using the wrong keyword in the subscription registration. You're using :showing-message inside message-component, but there's no corresponding ::showing-message subscription or even in defaul-db. Either define ::showing-message in default-db and set it correctly when you want to show or hide the message or refer to :message-show instead if that's the intended flag.
Inside the message-component, you are combining the CSS class strings in a way that might not resulting in the expected classes. Try revising it to ensure proper class application (concatenating with spaces):
You may want to create a function that will return the appropriate class string based on the state of @shake-message? and @shake-angry?:
(defn message-class [shake-message? shake-angry?]
(str "message-animation "
(when shake-message? "shake ")
(when shake-angry? "shake-angry")))
; Then in your component:
[:div {:class (message-class @shake-message? @shake-angry?)} @message]
The CSS animation for .message-animation has a comment typo. To ensure CSS is processed correctly, ensure all comments are closed properly:
.message-animation {
animation: slide-in 0.5s ease-out, fade-out 1s ease-in 2.5s forwards; /* hold the message for 2.5 seconds before fading out */
will-change: transform, opacity; /* Optimization for smoother animations */
}
Ensure that the message is set correctly and that the :showing-message state is toggled on when you dispatch ::set-message. You may want to trigger a fade-out effect or hide the message explicitly after a set duration.
Lastly, make sure the component is included in your render tree where you expect it to appear. In your main-panel function, it looks like you're already doing that:
[elements/message-component]
Review and adjust your code according to these suggestions, and monitor your application's state (possibly through developer tools or println debug statements) to ensure the expected values are being triggered when you want to display the pop-up message.