Skip to content

Commit

Permalink
feat: create method update product amount
Browse files Browse the repository at this point in the history
  • Loading branch information
robsonnatanael committed Apr 14, 2022
1 parent 088be61 commit db05ca7
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions src/hooks/useCart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,31 @@ export function CartProvider({ children }: CartProviderProps): JSX.Element {
amount,
}: UpdateProductAmount) => {
try {
// TODO
if (amount <= 0) {
return;
}

const stock = await api.get(`/stock/${productId}`);

const stockAmount = stock.data.amount;

if (amount > stockAmount) {
toast.error('Quantidade solicitada fora de estoque');
return;
}

const updatedCart = [...cart];
const productExists = updatedCart.find(product => product.id === productId);

if (productExists) {
productExists.amount = amount;
setCart(updatedCart);
localStorage.setItem('@RocketShoes:cart', JSON.stringify(updatedCart));
} else {
throw Error();
}
} catch {
// TODO
toast.error('Erro na alteração de quantidade do produto');
}
};

Expand Down

0 comments on commit db05ca7

Please sign in to comment.