1
1
package views
2
2
3
3
import (
4
- " fmt"
5
4
" go-github/views/components"
6
- " strings"
7
5
)
8
6
9
7
// GitHubUser represents a GitHub user with extended details
@@ -36,7 +34,7 @@ templ Index(user *GitHubUser) {
36
34
</nav >
37
35
<div class =" mt-6 w-full flex justify-center items-center flex-col" >
38
36
<!-- Search Form -->
39
- <form method =" get" class =" w-96" >
37
+ <form method =" get" id = " searchForm " class =" w-96" >
40
38
<input
41
39
type =" text"
42
40
name =" username"
@@ -54,85 +52,100 @@ templ Index(user *GitHubUser) {
54
52
<br />
55
53
56
54
<!-- Main Content Container -->
57
- <div class =" flex flex-col lg:flex-row w-full max-w-6xl mx-auto mt-6 gap-8" >
55
+ <div class =" flex flex-col lg:flex-row w-full max-w-6xl mx-auto mt-6 gap-8" style = " display: none; " id = " profileContent " >
58
56
<!-- Profile Information -->
59
- <div class =" w-full lg:w-1/2 flex justify-center items-center flex-col p-4 border border-gray-300 rounded-lg" >
60
- <img class =" w-24 h-24 rounded-full" src ={ user.AvatarURL } />
61
- <h1 class =" text-3xl font-bold mt-4" >{ user.Name }</h1 >
62
- <p class =" text-center mb-2" ><em >{ user.Bio }</em ></p >
63
-
64
- <!-- Additional Information -->
65
- <div class =" w-full px-4 items-center flex flex-col justify-center" >
66
- <p ><strong >Username:</strong > { user.Login }</p >
67
- <p ><strong >Company:</strong > { user.Company }</p >
68
- <p ><strong >Blog:</strong > <a href ={ templ.URL (user.Blog ) }>{ user.Blog }</a ></p >
69
- <p ><strong >Location:</strong > { user.Location }</p >
70
- <p ><strong >Email:</strong > { user.Email }</p >
71
- <p ><strong >Public Repos:</strong > { fmt.Sprintf (" %d " , user.PublicRepos ) }</p >
72
- <p ><strong >Followers:</strong > { fmt.Sprintf (" %d " , user.Followers ) }</p >
73
- <p ><strong >Following:</strong > { fmt.Sprintf (" %d " , user.Following ) }</p >
74
- <p ><strong >Joined:</strong > { user.CreatedAt }</p >
75
- </div >
57
+ <div id =" profileInfo" class =" w-full lg:w-1/2 flex justify-center items-center flex-col p-4 border border-gray-300 rounded-lg" >
58
+ <!-- Dynamic user info will be inserted here -->
76
59
</div >
77
60
78
61
<!-- Commit History Scatterplot -->
79
- <div class =" w-full lg:w-1/2 flex flex-col items-center p-4 border border-gray-300 rounded-lg" >
80
- <h2 class =" text-xl font-bold mb-4" >Commit History Scatterplot</h2 >
81
- <div class =" w-full" >
82
- <canvas id =" commitHistoryChart" data-chart ={ strings.Join (user.CommitHistory , " ," ) } class =" w-full h-64" ></canvas >
83
- </div >
62
+ <div id =" commitHistory" class =" w-full lg:w-1/2 flex flex-col items-center p-4 border border-gray-300 rounded-lg" >
63
+ <!-- Dynamic commit history chart will be inserted here -->
84
64
</div >
85
65
</div >
86
66
</div >
87
67
</main >
88
68
</body >
89
69
<script >
90
- window .onload = function () {
91
- // Retrieve commit history data from Go
92
- const commitData = document .getElementById (' commitHistoryChart' ).getAttribute (' data-chart' );
93
-
94
- const listToChartData = commitData => {
95
- const lines = commitData .trim ().split (' ,' );
96
- return lines .map (line => {
97
- const [date , commits ] = line .split (' :' );
98
- return {
99
- x: date,
100
- y: commits
101
- }
102
- });
103
- };
104
-
105
- console .log (listToChartData (commitData));
106
-
107
- // Configure Chart.js for a scatterplot
108
- const ctx = document .getElementById (' commitHistoryChart' ).getContext (' 2d' );
109
- const config = {
110
- type: ' line' ,
111
- data: {
112
- labels: [],
113
- datasets: [{
114
- data: listToChartData (commitData),
115
- label: " Commits" ,
116
- borderColor: " #3e95cd" ,
117
- fill: false
118
- }]
119
- },
120
- options: {
121
- scales: {
122
- xAxes: [{
123
- type: ' time' ,
124
- distribution: ' linear' ,
125
- }],
126
- title: {
127
- display: false ,
70
+ document .getElementById (' searchForm' ).addEventListener (' submit' , async function (event ) {
71
+ event .preventDefault (); // Prevent page reload
72
+
73
+ const username = document .getElementById (' username' ).value ;
74
+ document .getElementById (' profileContent' ).style .display = ' flex' ;
75
+
76
+ try {
77
+ // Show a loading state if needed
78
+ document .getElementById (' profileInfo' ).innerHTML = " <p>Loading...</p>" ;
79
+ document .getElementById (' commitHistory' ).innerHTML = " <p>Loading chart...</p>" ;
80
+
81
+ // Fetch user profile and commit history
82
+ const response = await fetch (` http://localhost:8080/?username=${ encodeURIComponent (username)} ` );
83
+ const user = await response .json ();
84
+
85
+ // Populate Profile Info
86
+ const profileHTML = `
87
+ <img class="w-24 h-24 rounded-full" src="${ user .AvatarURL } " />
88
+ <h1 class="text-3xl font-bold mt-4">${ user .Name } </h1>
89
+ <p class="text-center mb-2"><em>${ user .Bio } </em></p>
90
+ <div class="w-full px-4 items-center flex flex-col justify-center">
91
+ <p><strong>Username:</strong> ${ user .Login } </p>
92
+ <p><strong>Company:</strong> ${ user .Company } </p>
93
+ <p><strong>Blog:</strong> <a href="${ user .Blog } " target="_blank">${ user .Blog } </a></p>
94
+ <p><strong>Location:</strong> ${ user .Location } </p>
95
+ <p><strong>Email:</strong> ${ user .Email } </p>
96
+ <p><strong>Public Repos:</strong> ${ user .PublicRepos } </p>
97
+ <p><strong>Followers:</strong> ${ user .Followers } </p>
98
+ <p><strong>Following:</strong> ${ user .Following } </p>
99
+ <p><strong>Joined:</strong> ${ user .CreatedAt } </p>
100
+ </div>
101
+ ` ;
102
+ document .getElementById (' profileInfo' ).innerHTML = profileHTML;
103
+
104
+ // Inject the canvas for the commit history chart
105
+ document .getElementById (' commitHistory' ).innerHTML = `
106
+ <h2 class =" text-xl font-bold mb-4" >Commit History Scatterplot</h2 >
107
+ <canvas id =" commitHistoryChart" class =" w-full h-64" ></canvas >
108
+ ` ;
109
+
110
+ const listToChartData = commitData => {
111
+ return commitData .map (datapoint => {
112
+ const [date , commits ] = datapoint .split (' :' );
113
+ return {
114
+ x: date,
115
+ y: commits
116
+ }
117
+ });
118
+ };
119
+ const ctx = document .getElementById (' commitHistoryChart' ).getContext (' 2d' );
120
+ new Chart (ctx, {
121
+ type: ' line' ,
122
+ data: {
123
+ datasets: [{
124
+ label: " Commits" ,
125
+ data: listToChartData (user .CommitHistory || []),
126
+ backgroundColor: " #3e95cd" ,
127
+ borderColor: " #3e95cd" ,
128
+ fill: false
129
+ }]
130
+ },
131
+ options: {
132
+ scales: {
133
+ xAxes: [{
134
+ type: ' time' ,
135
+ time: {
136
+ unit: ' day'
137
+ }
138
+ }]
128
139
}
129
140
}
130
- }
131
- };
141
+ });
132
142
133
- new Chart (ctx, config);
134
-
135
- }
143
+ } catch (error) {
144
+ console .error (' Error fetching data:' , error);
145
+ document .getElementById (' profileInfo' ).innerHTML = " <p>Error loading profile information.</p>" ;
146
+ document .getElementById (' commitHistory' ).innerHTML = " <p>Error loading commit history.</p>" ;
147
+ }
148
+ });
136
149
</script >
137
150
@ components.Footer ()
138
151
</html >
0 commit comments