mirror of
https://github.com/LaCasemate/fab-manager.git
synced 2025-01-23 12:52:20 +01:00
30 lines
796 B
TypeScript
30 lines
796 B
TypeScript
|
import { useState, useEffect } from 'react';
|
||
|
import { Order } from '../models/order';
|
||
|
import CartAPI from '../api/cart';
|
||
|
import { getCartToken, setCartToken } from '../lib/cart-token';
|
||
|
|
||
|
export default function useCart () {
|
||
|
const [cart, setCart] = useState<Order>(null);
|
||
|
const [loading, setLoading] = useState<boolean>(true);
|
||
|
const [error, setError] = useState(null);
|
||
|
|
||
|
useEffect(() => {
|
||
|
async function createCart () {
|
||
|
const currentCartToken = getCartToken();
|
||
|
const data = await CartAPI.create(currentCartToken);
|
||
|
setCart(data);
|
||
|
setLoading(false);
|
||
|
setCartToken(data.token);
|
||
|
}
|
||
|
setLoading(true);
|
||
|
try {
|
||
|
createCart();
|
||
|
} catch (e) {
|
||
|
setLoading(false);
|
||
|
setError(e);
|
||
|
}
|
||
|
}, []);
|
||
|
|
||
|
return { loading, cart, error, setCart };
|
||
|
}
|