11import NextImage from 'next/image' ;
2- import { NextPage } from 'next' ;
32import { Translate } from 'i18n' ;
43import { PageTemplate } from 'components/template' ;
54import styled from 'styled-components' ;
@@ -11,6 +10,11 @@ const Image = styled(NextImage)`
1110 box-shadow: 4px 4px 5px 0px rgba(0, 0, 0, 0.5);
1211` ;
1312
13+ const PostSection = styled . section `
14+ margin-top: 4rem;
15+ margin-bottom: 2rem;
16+ ` ;
17+
1418const ImageContainer = styled . section `
1519 margin-top: 1rem;
1620
@@ -60,36 +64,79 @@ const handleSocialMediaClick = (socialMedia: SocialMediaKey) => () => {
6064 } ) ;
6165} ;
6266
67+ const handlePostClock = ( slug : string ) => ( ) => {
68+ gtag . event ( {
69+ action : 'post_click' ,
70+ category : 'Post click' ,
71+ label : slug ,
72+ } ) ;
73+ } ;
74+
75+ type Post = {
76+ url : string ;
77+ slug : string ;
78+ title : string ;
79+ description : string ;
80+ } ;
6381type HomeProps = {
6482 t : Translate ;
83+ posts : Post [ ] ;
6584} ;
66- const Home : NextPage < HomeProps > = ( { t } ) => (
67- < PageTemplate t = { t } title = { t ( 'author' ) } >
68- < h1 > { t ( 'welcome' ) } </ h1 >
69- < p > { t ( 'bio' ) } </ p >
70- < Image
71- src = "/takah.jpg"
72- alt = "Picture of the author"
73- width = { 100 }
74- height = { 100 }
75- style = { { margin : 'auto' } }
76- />
77- < ImageContainer >
78- { Object . entries ( socialMedias ) . map (
79- ( [ socialMedia , { alt, src, link } ] : [ SocialMediaKey , SocialMedia ] ) => (
85+ function Home ( { t, posts } : HomeProps ) {
86+ return (
87+ < PageTemplate t = { t } title = { t ( 'author' ) } >
88+ < h1 > { t ( 'welcome' ) } </ h1 >
89+ < p > { t ( 'bio' ) } </ p >
90+ < Image
91+ src = "/takah.jpg"
92+ alt = "Picture of the author"
93+ width = { 100 }
94+ height = { 100 }
95+ style = { { margin : 'auto' } }
96+ />
97+ < ImageContainer >
98+ { Object . entries ( socialMedias ) . map (
99+ ( [ socialMedia , { alt, src, link } ] : [
100+ SocialMediaKey ,
101+ SocialMedia
102+ ] ) => (
103+ < a
104+ key = { socialMedia }
105+ href = { link }
106+ target = "_blank"
107+ rel = "noreferrer noreferrer"
108+ onClick = { handleSocialMediaClick ( socialMedia ) }
109+ >
110+ < img src = { src } alt = { alt } width = { 20 } height = { 20 } />
111+ </ a >
112+ )
113+ ) }
114+ </ ImageContainer >
115+ < PostSection >
116+ < h3 > Blog posts</ h3 >
117+ { posts . map ( ( { slug, title, description, url } ) => (
80118 < a
81- key = { socialMedia }
82- href = { link }
119+ key = { slug }
120+ href = { url }
83121 target = "_blank"
84122 rel = "noreferrer noreferrer"
85- onClick = { handleSocialMediaClick ( socialMedia ) }
123+ onClick = { handlePostClock ( slug ) }
86124 >
87- < img src = { src } alt = { alt } width = { 20 } height = { 20 } / >
125+ < p > { title } </ p >
88126 </ a >
89- )
90- ) }
91- </ ImageContainer >
92- </ PageTemplate >
93- ) ;
127+ ) ) }
128+ </ PostSection >
129+ </ PageTemplate >
130+ ) ;
131+ }
132+
133+ Home . getInitialProps = async function getStaticPaths ( ) {
134+ const result = await fetch ( 'https://dev.to/api/articles?username=luistak' ) ;
135+ const posts = await result . json ( ) ;
136+
137+ return {
138+ posts,
139+ } ;
140+ } ;
94141
95142export default Home ;
0 commit comments