import { h, render } from 'preact'
import { Provider } from 'react-redux'
import App from './components/App.jsx'
import store from './store'
import client from './client'
import socket from './socket'

import { loadFolders, loadOpenFolders, loadFiles, loadTasks } from './actions/index.js'

client.folder.index().then( folders => {
  store.dispatch(loadFolders(folders))
  try {
    const openFolders = JSON.parse(localStorage['openFolders'])
    store.dispatch( loadOpenFolders(openFolders) )
    openFolders.forEach( folder_id => {
      client.file.index({ folder_id }).then( files => {
        store.dispatch( loadFiles(files) )
      })
    })
  }
  catch (e) {
    console.log('error opening folders')
  }
})
client.task.index().then( tasks => {
  store.dispatch(loadTasks(tasks))
})

const app = (
  <Provider store={store}>
    <App />
  </Provider>
)

render(app, document.getElementById('container'))
