src/view/components/root.lisp

DOWNLOAD
(in-package :murja.view.components.root)

(defun sidebar-tree ()
  (let* ((sidebar-titles (->>
			   (get-titles-by-year)
			   (lisp-fixup:group-by "Year"))))
    (with-html 
      (:ul :id "grouper"
	   (loop for year being the hash-keys of sidebar-titles
		 collecting (:li
			     (:details
			      (:summary (format nil "~a (~d)" year (length (gethash year sidebar-titles))))
			      (:ul 
			       (let ((by-month (lisp-fixup:group-by "Month" (gethash year sidebar-titles))))
				 (loop for month being the hash-keys of by-month
				       collecting (:li
						   (:details
						    (:summary (format nil "~a (~d)" (lisp-fixup:month->string month) (length (gethash month by-month))))
						    (:ul 
						     (map 'list
							  (lambda (title)
							    (let ((Id (gethash "Id" title))
								  (Title (gethash "Title" title)))
							      (:li
							       (:a :href (murja.genurl:route->url
									  'murja.view.single-post:blog/post/id
									  :id  Id)
								       Title))))
							  (gethash month by-month)))))))))))))))

(defun sidebar/new-post ()
  (let ((disabled-routes (list "/new-post"
			       "/post/editor")))
    (with-html
      (if (and *user*
	       (not (member (cl-ppcre:regex-replace "/\\d+$" (hunchentoot:request-uri*) "")
			    disabled-routes
			    :test 'equal)))
	  (:form :action (murja.genurl:route->url 'murja.view.admin.new-post:blog/new-post) :method "post"
		 (:input :type :submit :value "NEW POST"))))))
	

(defun loginform/user-widget ()
  (with-html 
    (if *user*
	(:p :data-testid "welcome-user-label"
	    ;; FIXME usersettings doesn't exist 
	    "Welcome, " (:a :href (murja.genurl:route->url 'murja.view.user-editor:blog/usersettings) (user-nickname *user*)))
	(:form :method "post"
	       :action  (murja.genurl:route->url 'murja.routes.login-routes:forms-post-login)
	       (:label "Username "
		       (:input :id "username" :name "username" :data-testid "username-input-field"))
	       (:label "Password "
		       (:input :id "password" :name "password" :type "password" :data-testid "password-input-field"))
	       (:input :type :submit :value "Log in")))))
  


(defvar *inject-to-head* nil)
(defvar *inject-to-sidebar* nil)
(defmacro root-component (inner-component)
  (assert inner-component)
  "Returns the root html element of murja with `inner-component` embedded inside it"
  `(format nil "<!DOCTYPE html>~%~a"
	   ;; let's make *inject-to-sidebar* request-scoped 
	   (let ((*inject-to-sidebar* nil))
	     (with-html-string 
	       (:html
		(:head
		 ;; TODO move css into lisp 
		 (:link :href (murja.genurl:route->url 'murja.routes.root-routes:resources :file "murja.css") :rel "stylesheet" :type "text/css")
		 (:meta :charset "UTF-8")
		 (:meta :name "viewport" :content "width=device-width,initial-scale=1.0")
		 (dolist (head-element *inject-to-head*)
		   (funcall head-element)))
		
		(:body
		 (:header
		  (:a :href "/" (blog-title)))

		 (:div :class "sidebar-flex"
		       ,inner-component
		       (:div :id "sidebar"
			     (sidebar/new-post)
			     (loginform/user-widget)
			     (sidebar-tree)
			     (when *inject-to-sidebar*
			       (funcall *inject-to-sidebar*))))))))))