[์ฅ๋ฐ๊ตฌ๋] ๋ด์ญ ๋ค๊ณ ์ค๊ธฐ
useEffect(() => {
if (!token) {
setIsLoaded(true);
}
fetch(API.cart, {
headers: {
Authorization: token,
},
})
.then(res => res.json())
.then(res => {
if (!!res.result) {
setItems(res.result);
}
})
.catch(e => {
console.error(e);
})
.finally(setIsLoaded(true));
}, [token]);
[์ฅ๋ฐ๊ตฌ๋] ์๋ ๋ณ๊ฒฝ
const changeItemQuantity = (cart_id, changedQuantity) => {
if (!changedQuantity.toString()) {
return;
}
fetch(API.cart, {
method: 'PATCH',
headers: {
Authorization: token,
},
body: JSON.stringify({
cart_id: cart_id,
quantity: changedQuantity,
}),
})
.then(res =>
res.json().then(res => {
switch (res.message) {
case 'Token not Exist':
alert('๋ก๊ทธ์ธ์ ํด์ฃผ์ธ์');
break;
case 'SUCCESS':
setItems(
items.map(item =>
item.cart_id !== cart_id
? item
: { ...item, quantity: changedQuantity }
)
);
break;
case 'KEY_ERROR':
alert('์๋ฌ์
๋๋ค!');
break;
default:
break;
}
})
)
.catch(e => {
console.error(e);
});
};
[์ฅ๋ฐ๊ตฌ๋] ์ํ ์ญ์
const deleteItem = cart_id => {
fetch(API.cart, {
method: 'DELETE',
headers: { Authorization: token },
body: JSON.stringify({
cart_id: [cart_id],
}),
})
.then(res => res.json())
.then(res => {
switch (res.message) {
case 'Token not Exist':
alert('๋ก๊ทธ์ธ์ ํด์ฃผ์ธ์');
break;
case 'SUCCESS':
setItems(items.filter(item => item.cart_id !== cart_id));
break;
case 'INVALID_CART':
alert('์ญ์ ํ ์ํ์ด ์์ต๋๋ค');
break;
case 'KEY_ERROR':
alert('์๋ฌ ์
๋๋ค');
break;
default:
break;
}
})
.catch(e => {
console.error(e);
});
};
[์ฅ๋ฐ๊ตฌ๋] ์ฌ๋ฌ๊ฐ ์ญ์
const deleteAllCheckedItem = () => {
const deleteItemsCartIdArray = items
.filter(item => !item.notChecked)
.map(item => item.cart_id);
fetch(API.cart, {
method: 'DELETE',
headers: { Authorization: token },
body: JSON.stringify({
cart_id: deleteItemsCartIdArray,
}),
})
.then(res => res.json())
.then(res => {
switch (res.message) {
case 'SUCCESS':
setItems(items.filter(item => item.notChecked));
break;
case 'Token not Exist':
alert('๋ก๊ทธ์ธ์ ํด์ฃผ์ธ์');
break;
case 'INVALID_CART':
alert('์ญ์ ํ ์ํ์ด ์์ต๋๋ค');
break;
case 'KEY_ERROR':
alert('์๋ฌ ์
๋๋ค');
break;
default:
break;
}
})
.catch(e => {
console.error(e);
});
};
[์ฅ๋ฐ๊ตฌ๋] ์ฃผ๋ฌธํ๊ธฐ
const orderItems = () => {
if (checkedItemsLength < 1) {
alert('์ฃผ๋ฌธํ์ค ์ํ์ ์ ํํด์ฃผ์ธ์');
return;
}
const orderItemsCartId = items
.filter(item => !item.notChecked)
.map(item => item.cart_id);
fetch(API.orders, {
method: 'POST',
headers: {
Authorization: token,
},
body: JSON.stringify({ cart_ids: orderItemsCartId }),
})
.then(res => res.json())
.then(res => {
switch (res.message) {
case 'Token not Exist':
alert('๋ก๊ทธ์ธ์ ํด์ฃผ์ธ์');
break;
case 'INVALID_ORDER_STATUS':
case 'INVALID_ORDER_ITEMS_STATUS':
case 'DATA_ERROR':
case 'TRANSACTION_ERROR':
case 'KEY_ERROR':
case 'INVALID_CART':
alert('์๋ฌ ์
๋๋ค');
break;
case 'CREATE':
setItems(items.filter(item => item.notChecked));
alert('์ฃผ๋ฌธ์ด ์๋ฃ๋์์ต๋๋ค.');
break;
default:
break;
}
})
.catch(e => {
console.error(e);
});
};
[์ฃผ๋ฌธ๋ด์ญ] ๋ด์ญ ๋ถ๋ฌ์ค๊ธฐ
useEffect(() => {
fetch(API.orders, {
headers: {
Authorization: token,
},
})
.then(res => res.json())
.then(res => {
setOrders(res.result);
})
.catch(e => {
console.error(e);
})
.finally(() => {
setLoaded(true);
});
}, [token]);
[์ฃผ๋ฌธ๋ด์ญ] ์ฃผ๋ฌธ ์ทจ์
const cancelOrder = order_id => {
fetch(API.orders, {
method: 'PATCH',
headers: {
Authorization: token,
},
body: JSON.stringify({
order_id: order_id,
}),
})
.then(res => res.json())
.then(res => {
switch (res.message) {
case 'INVALID_ORDER_STATUS':
case 'INVALID_ORDER_ITEMS_STATUS':
case 'KEY_ERROR':
console.error('ERROR', res.message);
alert('์๋ฌ์
๋๋ค!');
break;
case 'SUCCESS':
changeOrderState(order_id);
alert('์ฃผ๋ฌธ์ด ์ทจ์๋์์ต๋๋ค.');
break;
default:
break;
}
})
.catch(e => {
console.error('catch', e);
});
};
[์ํ ๋ฆฌ์คํธ] ์ํ ๋ฆฌ์คํธ ๋ถ๋ฌ์ค๊ธฐ
useEffect(() => {
fetch(
`${API.product}?menu=${menu}${
!category.length ? '' : `&category=${category}`
}&sort=${sort}`
)
.then(res => res.json())
.then(res => {
if (!!res.result) {
setProducts(res.result);
}
switch (res.message) {
case 'AttributeError':
case 'KeyError':
case 'TypeError':
case 'DoesNotExits':
alert('์๋ฌ์
๋๋ค.');
break;
default:
break;
}
})
.catch(e => {
console.error(e);
})
.finally(setLoaded(true));
}, [category, menu, searchParams, sort]);
[์ํ ๋ฆฌ์คํธ] ์ฅ๋ฐ๊ตฌ๋์ ์ํ ๋ด๊ธฐ
const addProductToCart = () => {
if (!!token) {
fetch(API.cart, {
method: 'POST',
headers: {
Authorization: token,
},
body: JSON.stringify({
product_id: id,
quantity: quantity,
}),
})
.then(res => res.json())
.then(res => {
switch (res.message) {
case 'SUCCESS':
case 'update':
alert('์ฅ๋ฐ๊ตฌ๋์ ์ํ์ด ์ถ๊ฐ ๋์์ต๋๋ค.');
closeModal();
break;
case 'DoesNotExist':
alert('๋ก๊ทธ์ธ ํด์ฃผ์ธ์');
break;
default:
break;
}
})
.catch(e => {
console.error(e);
});
} else {
alert('๋ก๊ทธ์ธํด์ฃผ์ธ์^^');
}
};