6.21.0: Копирование товаров и меню действий
All checks were successful
Build and Push Docker Image / build-and-push (push) Successful in 1m20s
All checks were successful
Build and Push Docker Image / build-and-push (push) Successful in 1m20s
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -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" {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "play-life-web",
|
||||
"version": "6.20.0",
|
||||
"version": "6.21.0",
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"dev": "vite",
|
||||
|
||||
@@ -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
|
||||
}}>
|
||||
<button
|
||||
onClick={handleSave}
|
||||
disabled={loading || isDeleting || !name.trim() || !hasValidPeriod}
|
||||
disabled={loading || isDeleting || isCopying || !name.trim() || !hasValidPeriod}
|
||||
style={{
|
||||
flex: 1,
|
||||
maxWidth: '42rem',
|
||||
@@ -317,7 +373,7 @@ function ShoppingItemForm({ onNavigate, itemId, boardId, previousTab, onSaved, i
|
||||
borderRadius: '0.5rem',
|
||||
fontSize: '1rem',
|
||||
fontWeight: 600,
|
||||
cursor: (loading || isDeleting) ? 'not-allowed' : 'pointer',
|
||||
cursor: (loading || isDeleting || isCopying) ? 'not-allowed' : 'pointer',
|
||||
opacity: loading ? 0.6 : 1,
|
||||
transition: 'all 0.2s',
|
||||
}}
|
||||
@@ -325,16 +381,55 @@ function ShoppingItemForm({ onNavigate, itemId, boardId, previousTab, onSaved, i
|
||||
{loading ? 'Сохранение...' : 'Сохранить'}
|
||||
</button>
|
||||
{isEdit && (
|
||||
<DeleteButton
|
||||
onClick={handleDelete}
|
||||
loading={isDeleting}
|
||||
disabled={loading}
|
||||
title="Удалить товар"
|
||||
/>
|
||||
<button
|
||||
type="button"
|
||||
onClick={openActionMenu}
|
||||
disabled={loading || isDeleting || isCopying}
|
||||
style={{
|
||||
width: '52px',
|
||||
height: '52px',
|
||||
display: 'flex',
|
||||
alignItems: 'center',
|
||||
justifyContent: 'center',
|
||||
background: 'transparent',
|
||||
color: '#059669',
|
||||
border: '2px solid #059669',
|
||||
borderRadius: '0.5rem',
|
||||
fontSize: '1.25rem',
|
||||
fontWeight: 700,
|
||||
cursor: (loading || isDeleting || isCopying) ? 'not-allowed' : 'pointer',
|
||||
lineHeight: 1,
|
||||
flexShrink: 0,
|
||||
padding: 0,
|
||||
boxSizing: 'border-box',
|
||||
transition: 'all 0.2s',
|
||||
}}
|
||||
title="Действия"
|
||||
>
|
||||
⋮
|
||||
</button>
|
||||
)}
|
||||
</div>,
|
||||
document.body
|
||||
) : null}
|
||||
{showActionMenu && createPortal(
|
||||
<div className="wishlist-modal-overlay" style={{ zIndex: 2000 }} onClick={closeActionMenu}>
|
||||
<div className="wishlist-modal" onClick={(e) => e.stopPropagation()}>
|
||||
<div className="wishlist-modal-header">
|
||||
<h3>{name}</h3>
|
||||
</div>
|
||||
<div className="wishlist-modal-actions">
|
||||
<button className="wishlist-modal-copy" onClick={handleCopy}>
|
||||
Копировать
|
||||
</button>
|
||||
<button className="wishlist-modal-delete" onClick={handleDelete}>
|
||||
Удалить
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>,
|
||||
document.body
|
||||
)}
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user