Skip to content

Commit dc77d5d

Browse files
committed
ajustes pra voltar a abrir a url
1 parent 96c7673 commit dc77d5d

File tree

4 files changed

+95
-30
lines changed

4 files changed

+95
-30
lines changed

docker-compose.yml

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
version: '3.8'
2-
31
services:
42
db:
53
image: postgres:15
@@ -29,7 +27,6 @@ services:
2927
- ./backend:/app
3028
environment:
3129
DATABASE_URL: postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@db:5432/${POSTGRES_DB}
32-
SECRET_KEY: ${SECRET_KEY} # ← adicione aqui, se usar no backend
3330
healthcheck:
3431
test: ["CMD-SHELL", "curl -f http://localhost:8000/ || exit 1"]
3532
interval: 5s
@@ -51,9 +48,8 @@ services:
5148
volumes:
5249
- ./frontend:/app
5350
environment:
54-
BACKEND_URL: http://backend:8000
5551
FASTAPI_BASE_URL: http://backend:8000
56-
SECRET_KEY: ${SECRET_KEY}
52+
SECRET_KEY: ${SECRET_KEY} # ESSA LINHA FALTAVA — ERA O PROBLEMA
5753

5854
volumes:
59-
db_data:
55+
db_data:

frontend/app.py

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
login_manager.init_app(app)
1414
login_manager.login_view = 'login'
1515

16-
BACKEND_URL = os.getenv("BACKEND_URL")
16+
BACKEND_URL = os.getenv("BACKEND_URL", "http://localhost:8000")
1717

1818
class User(UserMixin):
1919
def __init__(self, user_id):
@@ -150,20 +150,6 @@ def add_bulk(cart_id):
150150
headers=get_auth_headers())
151151
return redirect(url_for("index"))
152152

153-
@app.route("/api/itens/<int:item_id>", methods=["DELETE"])
154-
@login_required
155-
def delete_item_api(item_id):
156-
response = requests.delete(
157-
f"{BACKEND_URL}/api/itens/{item_id}",
158-
headers=get_auth_headers()
159-
)
160-
if response.status_code == 200:
161-
return jsonify(response.json())
162-
elif response.status_code == 404:
163-
return jsonify({"error": "Item não encontrado"}), 404
164-
else:
165-
return jsonify({"error": "Erro ao deletar item"}), 500
166-
167153
if __name__ == "__main__":
168154
port = int(os.environ.get("PORT", 5000))
169155
app.run(debug=False, host="0.0.0.0", port=port) #produção

frontend/static/js/script.js

Lines changed: 89 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
// Não definimos API_BASE_URL — usamos as rotas do Flask como proxy
2-
const API_BASE_URL = "";
1+
// Configuração da API
2+
const API_BASE_URL = "http://localhost:8000";
3+
4+
// Conjunto de carrinhos selecionados
5+
let selectedCarts = new Set();
36

47
// Helper para obter token de autenticação
58
function getAuthToken() {
@@ -46,8 +49,11 @@ function toggleSelect(cartId, cartName) {
4649
}
4750
}
4851

49-
// Conjunto de carrinhos selecionados
50-
let selectedCarts = new Set();
52+
// Mostrar/esconder itens simples (não usado, mas mantido)
53+
function toggleItemList(cartId) {
54+
const itemsDiv = document.getElementById(`items-${cartId}`);
55+
itemsDiv.style.display = itemsDiv.style.display === 'none' ? 'block' : 'none';
56+
}
5157

5258
// Finalizar carrinho
5359
function finalizeSelection(cartId) {
@@ -75,16 +81,87 @@ function showPopup(message) {
7581
}, 3000);
7682
}
7783

78-
// Abrir pergaminho com itens (AGORA CHAMA O FLASK, NÃO O FASTAPI DIRETAMENTE)
84+
// Ações da navbar (não usada atualmente, mas mantida)
85+
function handleNavAction(action) {
86+
if (selectedCarts.size === 0) {
87+
showPopup("⚠️ Selecione um carrinho antes de realizar esta ação!");
88+
return;
89+
}
90+
91+
const cartId = Array.from(selectedCarts)[0];
92+
93+
if (action === "view") {
94+
window.location.href = "/cart/" + cartId;
95+
} else if (action === "edit") {
96+
window.location.href = "/edit/" + cartId;
97+
} else if (action === "delete") {
98+
const skipConfirm = localStorage.getItem("skipDeleteConfirm");
99+
if (skipConfirm === "true") {
100+
confirmDelete(cartId);
101+
return;
102+
}
103+
104+
const confirmBox = document.createElement("div");
105+
confirmBox.className = "popup-confirm";
106+
confirmBox.innerHTML = `
107+
<p>Tem certeza que deseja deletar este carrinho?</p>
108+
<label><input type="checkbox" id="skipConfirm"> Não mostrar este aviso novamente</label>
109+
<div class="actions">
110+
<button onclick="confirmDelete(${cartId})">Sim</button>
111+
<button onclick="cancelDelete(this)">Cancelar</button>
112+
</div>
113+
`;
114+
document.body.appendChild(confirmBox);
115+
}
116+
}
117+
118+
// Confirmar exclusão de carrinho
119+
function confirmDelete(cartId) {
120+
const skip = document.getElementById("skipConfirm")?.checked;
121+
if (skip) localStorage.setItem("skipDeleteConfirm", "true");
122+
123+
fetch(`${API_BASE_URL}/api/carrinhos/${cartId}`, {
124+
method: "DELETE",
125+
headers: {
126+
'Authorization': `Bearer ${getAuthToken()}`
127+
}
128+
})
129+
.then(response => {
130+
if (response.ok) {
131+
showPopup(`🗑️ Carrinho deletado com sucesso!`);
132+
finalizeSelection(cartId);
133+
setTimeout(() => location.reload(), 1000);
134+
} else {
135+
if (response.status === 401) {
136+
showPopup('🔒 Sessão expirada. Faça login novamente.');
137+
setTimeout(() => window.location.href = '/login', 2000);
138+
} else {
139+
showPopup("❌ Erro ao deletar carrinho.");
140+
}
141+
}
142+
})
143+
.catch(() => showPopup("❌ Erro de conexão com o servidor."));
144+
145+
cancelDelete(document.querySelector(".popup-confirm .actions button:last-child"));
146+
}
147+
148+
// Cancelar exclusão
149+
function cancelDelete(el) {
150+
el.closest(".popup-confirm").remove();
151+
}
152+
153+
// Abrir pergaminho com itens (SEM DUPLICAÇÃO)
154+
// Abrir pergaminho com itens (CORRIGIDA)
79155
function abrirPergaminho(cartId) {
80156
const modal = document.getElementById('pergaminho-modal');
81157
const overlay = document.getElementById('pergaminho-overlay');
82158
const lista = document.getElementById('lista-itens-modal');
83159
const som = document.getElementById('som-pergaminho');
84160

161+
// Limpa a lista e mostra loading
85162
lista.innerHTML = '<li style="text-align:center; color:#3e2f1c; font-style:italic;">Carregando...</li>';
86163

87-
fetch(`/api/carrinhos/${cartId}/itens`, {
164+
fetch(`${API_BASE_URL}/api/carrinhos/${cartId}/itens`, {
88165
method: 'GET',
89166
headers: {
90167
'Authorization': `Bearer ${getAuthToken()}`
@@ -96,6 +173,7 @@ function abrirPergaminho(cartId) {
96173
})
97174
.then(data => {
98175
lista.innerHTML = '';
176+
99177
if (data.length === 0) {
100178
lista.innerHTML = '<li style="text-align:center; color:#3e2f1c;">Nenhum item encontrado</li>';
101179
} else {
@@ -122,7 +200,7 @@ function abrirPergaminho(cartId) {
122200
});
123201
}
124202

125-
// Deletar item específico (CHAMA O FLASK)
203+
// Deletar item específico (COM AUTENTICAÇÃO E URL CORRETA)
126204
function deletarItem(itemId, liElement) {
127205
if (!confirm('Tem certeza que deseja deletar este item?')) return;
128206

@@ -133,7 +211,7 @@ function deletarItem(itemId, liElement) {
133211
return;
134212
}
135213

136-
fetch(`/api/itens/${itemId}`, {
214+
fetch(`${API_BASE_URL}/api/itens/${itemId}`, {
137215
method: 'DELETE',
138216
headers: {
139217
'Authorization': `Bearer ${token}`
@@ -143,6 +221,7 @@ function deletarItem(itemId, liElement) {
143221
if (res.ok) {
144222
liElement.remove();
145223
showPopup('✅ Item deletado!');
224+
146225
const lista = document.getElementById('lista-itens-modal');
147226
if (lista && lista.children.length === 0) {
148227
lista.innerHTML = '<li style="text-align:center; color:#3e2f1c;">Nenhum item encontrado</li>';
@@ -185,5 +264,7 @@ document.addEventListener("DOMContentLoaded", function () {
185264

186265
// Expõe funções globais
187266
window.toggleSelect = toggleSelect;
267+
window.toggleItemList = toggleItemList;
188268
window.finalizeSelection = finalizeSelection;
269+
window.handleNavAction = handleNavAction;
189270
window.abrirPergaminho = abrirPergaminho;

textos/aws.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,6 @@ docker-compose up -d --build
5555
nano docker-compose.yml
5656
-------------
5757
rota
58-
http://13.59.205.141:5000
58+
http://13.59.205.141:5000
59+
60+
docker-compose logs frontend (pra ver as rotas do frontend ou erros se houver)

0 commit comments

Comments
 (0)