1- // Configuração da API
2- const API_BASE_URL = "http://localhost:8000" ;
3-
4- // Conjunto de carrinhos selecionados
5- let selectedCarts = new Set ( ) ;
1+ // Não definimos API_BASE_URL — usamos as rotas do Flask como proxy
2+ const API_BASE_URL = "" ;
63
74// Helper para obter token de autenticação
85function getAuthToken ( ) {
@@ -49,11 +46,8 @@ function toggleSelect(cartId, cartName) {
4946 }
5047}
5148
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- }
49+ // Conjunto de carrinhos selecionados
50+ let selectedCarts = new Set ( ) ;
5751
5852// Finalizar carrinho
5953function finalizeSelection ( cartId ) {
@@ -81,87 +75,16 @@ function showPopup(message) {
8175 } , 3000 ) ;
8276}
8377
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)
78+ // Abrir pergaminho com itens (AGORA CHAMA O FLASK, NÃO O FASTAPI DIRETAMENTE)
15579function abrirPergaminho ( cartId ) {
15680 const modal = document . getElementById ( 'pergaminho-modal' ) ;
15781 const overlay = document . getElementById ( 'pergaminho-overlay' ) ;
15882 const lista = document . getElementById ( 'lista-itens-modal' ) ;
15983 const som = document . getElementById ( 'som-pergaminho' ) ;
16084
161- // Limpa a lista e mostra loading
16285 lista . innerHTML = '<li style="text-align:center; color:#3e2f1c; font-style:italic;">Carregando...</li>' ;
16386
164- fetch ( `${ API_BASE_URL } /api/carrinhos/${ cartId } /itens` , {
87+ fetch ( `/api/carrinhos/${ cartId } /itens` , {
16588 method : 'GET' ,
16689 headers : {
16790 'Authorization' : `Bearer ${ getAuthToken ( ) } `
@@ -173,7 +96,6 @@ function abrirPergaminho(cartId) {
17396 } )
17497 . then ( data => {
17598 lista . innerHTML = '' ;
176-
17799 if ( data . length === 0 ) {
178100 lista . innerHTML = '<li style="text-align:center; color:#3e2f1c;">Nenhum item encontrado</li>' ;
179101 } else {
@@ -200,7 +122,7 @@ function abrirPergaminho(cartId) {
200122 } ) ;
201123}
202124
203- // Deletar item específico (COM AUTENTICAÇÃO E URL CORRETA )
125+ // Deletar item específico (CHAMA O FLASK )
204126function deletarItem ( itemId , liElement ) {
205127 if ( ! confirm ( 'Tem certeza que deseja deletar este item?' ) ) return ;
206128
@@ -211,7 +133,7 @@ function deletarItem(itemId, liElement) {
211133 return ;
212134 }
213135
214- fetch ( `${ API_BASE_URL } /api/itens/${ itemId } ` , {
136+ fetch ( `/api/itens/${ itemId } ` , {
215137 method : 'DELETE' ,
216138 headers : {
217139 'Authorization' : `Bearer ${ token } `
@@ -221,7 +143,6 @@ function deletarItem(itemId, liElement) {
221143 if ( res . ok ) {
222144 liElement . remove ( ) ;
223145 showPopup ( '✅ Item deletado!' ) ;
224-
225146 const lista = document . getElementById ( 'lista-itens-modal' ) ;
226147 if ( lista && lista . children . length === 0 ) {
227148 lista . innerHTML = '<li style="text-align:center; color:#3e2f1c;">Nenhum item encontrado</li>' ;
@@ -264,7 +185,5 @@ document.addEventListener("DOMContentLoaded", function () {
264185
265186// Expõe funções globais
266187window . toggleSelect = toggleSelect ;
267- window . toggleItemList = toggleItemList ;
268188window . finalizeSelection = finalizeSelection ;
269- window . handleNavAction = handleNavAction ;
270189window . abrirPergaminho = abrirPergaminho ;
0 commit comments