Skip to content

Commit 4e4b6ed

Browse files
committed
start to add data fetching
1 parent f7d2b17 commit 4e4b6ed

File tree

2 files changed

+30
-35
lines changed

2 files changed

+30
-35
lines changed

src/app/index.js

Lines changed: 3 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React, { useState, useEffect } from "react";
22
import Auth from '@aws-amplify/auth';
3+
import useTodos from '../hooks/useTodos';
34

45
function usePrivate() {
56
const [state, setState] = useState();
@@ -22,7 +23,8 @@ function usePrivate() {
2223

2324
function App() {
2425
const user = usePrivate();
25-
26+
const stuff = useTodos();
27+
console.log(stuff)
2628
if (!user) {
2729
return (<div>
2830
... Please Wait
@@ -50,40 +52,6 @@ function App() {
5052
</div>
5153
</header>
5254

53-
54-
<section class="text-gray-700 body-font">
55-
<div class="container px-5 py-24 mx-auto flex flex-wrap flex-col">
56-
<div class="flex mx-auto flex-wrap mb-20">
57-
<a class="sm:px-6 py-3 w-1/2 sm:w-auto justify-center sm:justify-start border-b-2 title-font font-medium bg-gray-100 inline-flex items-center leading-none border-teal-500 text-teal-500 tracking-wider rounded-t" href="#home">
58-
<svg fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" class="w-5 h-5 mr-3" viewBox="0 0 24 24">
59-
<path d="M12 22s8-4 8-10V5l-8-3-8 3v7c0 6 8 10 8 10z"></path>
60-
</svg>STEP 1
61-
</a>
62-
<a class="sm:px-6 py-3 w-1/2 sm:w-auto justify-center sm:justify-start border-b-2 title-font font-medium inline-flex items-center leading-none border-gray-200 hover:text-gray-900 tracking-wider" href="#home">
63-
<svg fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" class="w-5 h-5 mr-3" viewBox="0 0 24 24">
64-
<path d="M22 12h-4l-3 9L9 3l-3 9H2"></path>
65-
</svg>STEP 2
66-
</a>
67-
<a class="sm:px-6 py-3 w-1/2 sm:w-auto justify-center sm:justify-start border-b-2 title-font font-medium inline-flex items-center leading-none border-gray-200 hover:text-gray-900 tracking-wider" href="#home">
68-
<svg fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" class="w-5 h-5 mr-3" viewBox="0 0 24 24">
69-
<circle cx="12" cy="5" r="3"></circle>
70-
<path d="M12 22V8M5 12H2a10 10 0 0020 0h-3"></path>
71-
</svg>STEP 3
72-
</a>
73-
<a class="sm:px-6 py-3 w-1/2 sm:w-auto justify-center sm:justify-start border-b-2 title-font font-medium inline-flex items-center leading-none border-gray-200 hover:text-gray-900 tracking-wider" href="#home">
74-
<svg fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" class="w-5 h-5 mr-3" viewBox="0 0 24 24">
75-
<path d="M20 21v-2a4 4 0 00-4-4H8a4 4 0 00-4 4v2"></path>
76-
<circle cx="12" cy="7" r="4"></circle>
77-
</svg>STEP 4
78-
</a>
79-
</div>
80-
<img class="xl:w-1/4 lg:w-1/3 md:w-1/2 w-2/3 block mx-auto mb-10 object-cover object-center rounded" alt="hero" src="https://dummyimage.com/720x600" />
81-
<div class="flex flex-col text-center w-full">
82-
<h1 class="text-xl font-medium title-font mb-4 text-gray-900">Master Cleanse Reliac Heirloom</h1>
83-
<p class="lg:w-2/3 mx-auto leading-relaxed text-base">Whatever cardigan tote bag tumblr hexagon brooklyn asymmetrical gentrify, subway tile poke farm-to-table. Franzen you probably haven't heard of them man bun deep jianbing selfies heirloom prism food truck ugh squid celiac humblebrag.</p>
84-
</div>
85-
</div>
86-
</section>
8755
</div>
8856
)
8957
}

src/hooks/useTodos.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import useSWR from 'swr';
2+
import axios from 'axios';
3+
import Auth from '@aws-amplify/auth';
4+
5+
async function get(url) {
6+
return axios.get(url, {
7+
headers: {
8+
authorization: (await Auth.currentSession()).getIdToken().getJwtToken()
9+
}
10+
})
11+
}
12+
13+
async function post(url, body) {
14+
return axios.get(url, body, {
15+
headers: {
16+
authorization: (await Auth.currentSession()).getIdToken().getJwtToken()
17+
}
18+
})
19+
}
20+
21+
export default function useTodos() {
22+
const { data, loading } = useSWR('/api/todo', get);
23+
24+
25+
26+
return { loading, todos: data, create: (body) => post('api/todo', body) }
27+
}

0 commit comments

Comments
 (0)