import { createContext, useEffect } from 'react'
import RouterSetup from './RouterSetup'
import { useDispatch, useSelector } from 'react-redux'
import type { StoreType } from './stores'
import Loading from './components/Loading'
import { Apis } from './apis'
import { userAction } from './stores/slices/user.slice'

export default function App() {
  const userStore = useSelector((store: StoreType) => store.user)
  const dipatch = useDispatch()

  useEffect(() => {
    if(userStore.data && !userStore.loading) {
      try {
        Apis.cart.getCartByUserId(userStore.data.id)
        .then(res => {
          dipatch(userAction.initCartData(res))
        })
      }catch(err) {

      }
    }
  }, [userStore.data, userStore.loading, userStore.reloadCart])
  return (
    <>
      {
        userStore.loading ? <Loading />
          : <RouterSetup />
      }
    </>
  )
}
