diff --git a/VERSION b/VERSION index a7c176a..7ecad14 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -6.20.0 +6.21.0 diff --git a/play-life-backend/main.go b/play-life-backend/main.go index 83f05f6..e1b7f0e 100644 --- a/play-life-backend/main.go +++ b/play-life-backend/main.go @@ -4841,6 +4841,7 @@ func main() { protected.HandleFunc("/api/shopping/boards/{boardId}/items", app.createShoppingItemHandler).Methods("POST", "OPTIONS") protected.HandleFunc("/api/shopping/items/{id}", app.getShoppingItemHandler).Methods("GET", "OPTIONS") protected.HandleFunc("/api/shopping/items/{id}", app.updateShoppingItemHandler).Methods("PUT", "OPTIONS") + protected.HandleFunc("/api/shopping/items/{id}/copy", app.copyShoppingItemHandler).Methods("POST", "OPTIONS") protected.HandleFunc("/api/shopping/items/{id}", app.deleteShoppingItemHandler).Methods("DELETE", "OPTIONS") protected.HandleFunc("/api/shopping/items/{id}/complete", app.completeShoppingItemHandler).Methods("POST", "OPTIONS") protected.HandleFunc("/api/shopping/items/{id}/postpone", app.postponeShoppingItemHandler).Methods("POST", "OPTIONS") @@ -20064,6 +20065,91 @@ func (a *App) deleteShoppingItemHandler(w http.ResponseWriter, r *http.Request) w.WriteHeader(http.StatusNoContent) } +// copyShoppingItemHandler копирует товар в ту же доску +func (a *App) copyShoppingItemHandler(w http.ResponseWriter, r *http.Request) { + if r.Method == "OPTIONS" { + setCORSHeaders(w) + w.WriteHeader(http.StatusOK) + return + } + setCORSHeaders(w) + + userID, ok := getUserIDFromContext(r) + if !ok { + sendErrorWithCORS(w, "Unauthorized", http.StatusUnauthorized) + return + } + + vars := mux.Vars(r) + itemID, err := strconv.Atoi(vars["id"]) + if err != nil { + sendErrorWithCORS(w, "Invalid item ID", http.StatusBadRequest) + return + } + + // Получаем оригинальный товар + var boardID int + var itemName string + var description sql.NullString + var groupName sql.NullString + var volumeBase float64 + var repetitionPeriod sql.NullString + var authorID int + + err = a.DB.QueryRow(` + SELECT board_id, name, description, group_name, volume_base, + repetition_period::text, user_id + FROM shopping_items WHERE id = $1 AND deleted = FALSE + `, itemID).Scan(&boardID, &itemName, &description, &groupName, &volumeBase, + &repetitionPeriod, &authorID) + + if err == sql.ErrNoRows { + sendErrorWithCORS(w, "Item not found", http.StatusNotFound) + return + } + if err != nil { + log.Printf("Error getting shopping item for copy: %v", err) + sendErrorWithCORS(w, "Error getting item", http.StatusInternalServerError) + return + } + + // Проверяем доступ к доске + var ownerID int + a.DB.QueryRow(`SELECT owner_id FROM shopping_boards WHERE id = $1`, boardID).Scan(&ownerID) + if ownerID != userID { + var isMember bool + a.DB.QueryRow(`SELECT EXISTS(SELECT 1 FROM shopping_board_members WHERE board_id = $1 AND user_id = $2)`, + boardID, userID).Scan(&isMember) + if !isMember { + sendErrorWithCORS(w, "Access denied", http.StatusForbidden) + return + } + } + + // Создаём копию товара + var newItemID int + err = a.DB.QueryRow(` + INSERT INTO shopping_items (user_id, board_id, author_id, name, description, group_name, volume_base, repetition_period, next_show_at) + VALUES ($1, $2, $3, $4, $5, $6, $7, $8::interval, CURRENT_DATE) + RETURNING id + `, ownerID, boardID, userID, itemName, description, groupName, volumeBase, repetitionPeriod).Scan(&newItemID) + + if err != nil { + log.Printf("Error copying shopping item: %v", err) + sendErrorWithCORS(w, "Error copying item", http.StatusInternalServerError) + return + } + + log.Printf("Shopping item %d copied to new item %d by user %d", itemID, newItemID, userID) + + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusCreated) + json.NewEncoder(w).Encode(map[string]interface{}{ + "id": newItemID, + "success": true, + }) +} + // completeShoppingItemHandler выполняет товар (покупку) func (a *App) completeShoppingItemHandler(w http.ResponseWriter, r *http.Request) { if r.Method == "OPTIONS" { diff --git a/play-life-web/package.json b/play-life-web/package.json index c7d82d6..f900406 100644 --- a/play-life-web/package.json +++ b/play-life-web/package.json @@ -1,6 +1,6 @@ { "name": "play-life-web", - "version": "6.20.0", + "version": "6.21.0", "type": "module", "scripts": { "dev": "vite", diff --git a/play-life-web/src/components/ShoppingItemForm.jsx b/play-life-web/src/components/ShoppingItemForm.jsx index e5b1ab0..423baff 100644 --- a/play-life-web/src/components/ShoppingItemForm.jsx +++ b/play-life-web/src/components/ShoppingItemForm.jsx @@ -1,9 +1,9 @@ -import React, { useState, useEffect } from 'react' +import React, { useState, useEffect, useRef } from 'react' import { createPortal } from 'react-dom' import { useAuth } from './auth/AuthContext' import Toast from './Toast' import SubmitButton from './SubmitButton' -import DeleteButton from './DeleteButton' +import './Wishlist.css' import './ShoppingItemForm.css' function ShoppingItemForm({ onNavigate, itemId, boardId, previousTab, onSaved, isActive }) { @@ -18,6 +18,9 @@ function ShoppingItemForm({ onNavigate, itemId, boardId, previousTab, onSaved, i const [loading, setLoading] = useState(false) const [loadingItem, setLoadingItem] = useState(false) const [isDeleting, setIsDeleting] = useState(false) + const [isCopying, setIsCopying] = useState(false) + const [showActionMenu, setShowActionMenu] = useState(false) + const actionMenuHistoryRef = useRef(false) const [toastMessage, setToastMessage] = useState(null) const isEdit = !!itemId @@ -145,22 +148,75 @@ function ShoppingItemForm({ onNavigate, itemId, boardId, previousTab, onSaved, i } } + const openActionMenu = () => { + setShowActionMenu(true) + window.history.pushState({ actionMenu: true }, '') + actionMenuHistoryRef.current = true + } + + const closeActionMenu = () => { + setShowActionMenu(false) + if (actionMenuHistoryRef.current) { + actionMenuHistoryRef.current = false + window.history.back() + } + } + + // Обработка popstate для закрытия action menu кнопкой назад + useEffect(() => { + const handlePopState = () => { + if (showActionMenu) { + actionMenuHistoryRef.current = false + setShowActionMenu(false) + } + } + window.addEventListener('popstate', handlePopState) + return () => window.removeEventListener('popstate', handlePopState) + }, [showActionMenu]) + const handleDelete = async () => { - if (!window.confirm('Удалить товар?')) return + if (!itemId) return + + setShowActionMenu(false) + if (actionMenuHistoryRef.current) { + actionMenuHistoryRef.current = false + window.history.go(-2) + } else { + window.history.back() + } setIsDeleting(true) try { const res = await authFetch(`/api/shopping/items/${itemId}`, { method: 'DELETE' }) if (res.ok) { onSaved?.() - window.history.back() - } else { - setToastMessage({ text: 'Ошибка удаления', type: 'error' }) - setIsDeleting(false) } } catch (err) { - setToastMessage({ text: 'Ошибка удаления', type: 'error' }) - setIsDeleting(false) + console.error('Error deleting item:', err) + } + } + + const handleCopy = async () => { + if (!itemId) return + + setShowActionMenu(false) + if (actionMenuHistoryRef.current) { + actionMenuHistoryRef.current = false + window.history.go(-2) + } else { + window.history.back() + } + + setIsCopying(true) + try { + const res = await authFetch(`/api/shopping/items/${itemId}/copy`, { method: 'POST' }) + if (!res.ok) { + const errorText = await res.text().catch(() => '') + throw new Error(errorText || 'Ошибка при копировании товара') + } + onSaved?.() + } catch (err) { + console.error('Error copying item:', err) } } @@ -305,7 +361,7 @@ function ShoppingItemForm({ onNavigate, itemId, boardId, previousTab, onSaved, i }}> )} , document.body ) : null} + {showActionMenu && createPortal( +
+
e.stopPropagation()}> +
+

{name}

+
+
+ + +
+
+
, + document.body + )} ) }