diff options
-rw-r--r-- | elisp/grocy.el | 76 |
1 files changed, 49 insertions, 27 deletions
diff --git a/elisp/grocy.el b/elisp/grocy.el index 49638fb..923d36d 100644 --- a/elisp/grocy.el +++ b/elisp/grocy.el @@ -82,8 +82,24 @@ Set METHOD to other request types and include the already encoded DATA." (json-parse-buffer :object-type 'plist)) (setq grocy-locations result))))) +(defun grocy-update-product-groups () + "Update localdb with product groups." + (grocy--request + "objects/product_groups" + (lambda (_status) + (goto-char url-http-end-of-headers) + (let ((result (make-hash-table))) + (mapc + (lambda (groups) + (-let (((&plist :id :name) groups)) + (puthash id name result))) + (json-parse-buffer :object-type 'plist)) + (setq grocy-product-groups result))))) + + (grocy-update-units) (grocy-update-locations) +(grocy-update-product-groups) (defun grocy-stock--row (row) "Compose ROW into tabulated entry for stock view." @@ -124,39 +140,45 @@ Set METHOD to other request types and include the already encoded DATA." (tabulated-list-print) (display-buffer (current-buffer))))))) -(defun grocy-products () - "Render tabulated list view of products." - (interactive) +(defun grocy-products--row (product) + (-let* (((&hash "name" "location_id" "qu_id_stock" + "min_stock_amount" "product_group_id") product) + (prod-group (gethash product_group_id grocy-product-groups))) + (list product + (vector name + (car (gethash location_id grocy-locations)) + (car (gethash qu_id_stock grocy-units)) + (number-to-string min_stock_amount) + (if prod-group prod-group "NULL" ))))) + +(defun grocy-products--refresh () (grocy--request "objects/products" (lambda (_status) (goto-char url-http-end-of-headers) (let ((result (json-parse-buffer))) (with-current-buffer (get-buffer-create "Grocy products") - (tabulated-list-mode) - (setq tabulated-list-format [("Name" 40 t) - ("Location" 15 t) - ("Unit" 8 t) - ("min stock" 5 t)] - tabulated-list-sort-key '("Name")) + (setq tabulated-list-entries + (cl-map 'list #'grocy-products--row result)) + (tabulated-list-print 'remember t)))))) - (thread-last - (cl-map 'list - (lambda (product) - (-let (((&hash "name" "location_id" "qu_id_stock" - "min_stock_amount") product)) - (list product - (vector name - (car (gethash location_id grocy-locations)) - (car (gethash qu_id_stock grocy-units)) - (number-to-string min_stock_amount))))) - result) - (setq tabulated-list-entries)) - (local-set-key "e" #'grocy-update-product) - (local-set-key "a" #'grocy-add-product-shopping-list) - (tabulated-list-init-header) - (tabulated-list-print) - (display-buffer (current-buffer))))))) +(defun grocy-products () + "Render tabulated list view of products." + (interactive) + (with-current-buffer (get-buffer-create "Grocy products") + (tabulated-list-mode) + (setq tabulated-list-format [("Name" 40 t) + ("Location" 25 t) + ("Unit" 8 t) + ("min stock" 5 t) + ("Group" 10 t)] + tabulated-list-sort-key '("Name")) + (local-set-key "e" #'grocy-update-product) + (local-set-key "a" #'grocy-add-product-shopping-list) + (add-hook 'tabulated-list-revert-hook #'grocy-products--refresh nil t) + (run-hooks 'tabulated-list-revert-hook) + (tabulated-list-init-header) + (display-buffer (current-buffer)))) (defun grocy--message-error-reply (status) "Display `url-retrieve' response buffer and error STATUS." @@ -169,8 +191,8 @@ Set METHOD to other request types and include the already encoded DATA." (defun grocy-push-buffer-and-kill-fn (endpoint method) "Function to assign to keymap that pushed to ENDPOINT by METHOD." (lambda () - (interactive) "Push current buffer to defined endpoint." + (interactive) (grocy--request endpoint #'grocy--message-error-reply |