import axios from 'axios'
import { addSearchObjects } from './algolia'

export default {
  getSiteData: () => ({
    title: 'React Static',
  }),
  getRoutes: async () => {
    const { data: posts } = await axios.get('https://jsonplaceholder.typicode.com/posts')

    // Add posts data to Algolia with an index key of "posts"
    addSearchObjects('posts', posts)

    return [
      {
        path: '/blog',

        getData: () => ({
          posts,
        }),
        children: posts.map(post => ({
          path: `/post/${post.id}`,
          component: 'src/containers/Post',
          getData: () => ({
            post,
          }),
        })),
      },
    ]
  },
}
