Skip to content

Commit

Permalink
Added user submitted and comments
Browse files Browse the repository at this point in the history
  • Loading branch information
joeychilson committed Dec 21, 2023
1 parent e17f8a3 commit eb768a2
Show file tree
Hide file tree
Showing 26 changed files with 633 additions and 67 deletions.
8 changes: 2 additions & 6 deletions assets/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -685,10 +685,6 @@ video {
border-right-width: 1px;
}

.border-t {
border-top-width: 1px;
}

.border-gray-200 {
--tw-border-opacity: 1;
border-color: rgb(229 231 235 / var(--tw-border-opacity));
Expand Down Expand Up @@ -850,9 +846,9 @@ video {
background-color: rgb(249 250 251 / var(--tw-bg-opacity));
}

.hover\:bg-orange-500:hover {
.hover\:bg-orange-400:hover {
--tw-bg-opacity: 1;
background-color: rgb(249 115 22 / var(--tw-bg-opacity));
background-color: rgb(251 146 60 / var(--tw-bg-opacity));
}

.hover\:bg-orange-600:hover {
Expand Down
15 changes: 13 additions & 2 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,28 @@ func (c *Client) TopStories(ctx context.Context) ([]int, error) {
return stories, err
}

func (c *Client) GetItem(ctx context.Context, id int) (types.Item, error) {
func (c *Client) Item(ctx context.Context, id int) (types.Item, error) {
path := fmt.Sprintf("item/%d.json", id)
b, err := c.get(ctx, path)
if err != nil {
return types.Item{}, fmt.Errorf("while getting item: %w", err)
return types.Item{}, fmt.Errorf("failed to get item: %w", err)
}
item := types.Item{}
err = json.Unmarshal(b, &item)
return item, err
}

func (c *Client) User(ctx context.Context, id string) (types.User, error) {
path := fmt.Sprintf("/user/%s.json", id)
b, err := c.get(ctx, path)
if err != nil {
return types.User{}, fmt.Errorf("failed to get user: %w", err)
}
user := types.User{}
err = json.Unmarshal(b, &user)
return user, err
}

func (c *Client) get(ctx context.Context, path string) ([]byte, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, c.baseURL+path, nil)
if err != nil {
Expand Down
5 changes: 4 additions & 1 deletion components/comments.templ
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ templ Comments(comments []types.Item, level int) {
}
>
<div class="mb-2">
<span class="text-xs text-gray-600">by { comment.By } | { time.Unix(comment.Time, 0).Format("2006-01-02 15:04:05") }</span>
<span class="text-xs text-gray-600">
by <a href={ templ.URL(fmt.Sprintf("/user?id=%v", comment.By)) } class="hover:text-blue-600">{ comment.By }</a>
| { time.Unix(comment.Time, 0).Format("2006-01-02 15:04:05") }
</span>
</div>
<p class="text-sm">
{ comment.Text }
Expand Down
39 changes: 26 additions & 13 deletions components/comments_templ.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions components/pagination.templ
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

templ Pagination(total int, perPage int, currentPage int, startPage int, totalPages int, pageNumbers []int) {
<div class="flex justify-between items-center px-4 py-3 border-t border-gray-200 sm:px-6">
<div class="flex justify-between items-center px-4 py-3 sm:px-6">
<div class="sm:flex-1 sm:flex sm:items-center sm:justify-between">
<div>
<p class="text-sm text-gray-700">
Expand Down Expand Up @@ -34,7 +34,7 @@ templ Pagination(total int, perPage int, currentPage int, startPage int, totalPa
for _, num := range pageNumbers {
<a
href={ templ.URL(fmt.Sprintf("?p=%v", num)) }
class={ "px-4 py-2 border border-gray-300 text-gray-700 hover:bg-orange-500 hover:text-white", templ.KV("bg-orange-500 text-white", num == currentPage) }
class={ "px-4 py-2 border border-gray-300 text-gray-700 hover:bg-orange-400 hover:text-white", templ.KV("bg-orange-500 text-white", num == currentPage) }
>
{ strconv.Itoa(num) }
</a>
Expand Down
4 changes: 2 additions & 2 deletions components/pagination_templ.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion components/story_item.templ
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ templ StoryItem(story types.Item) {
{ story.Title }
</a>
<div class="text-sm text-gray-500">
by { story.By } | { time.Unix(story.Time, 0).Format("2006-01-02 15:04:05") } | <a href={ templ.URL(fmt.Sprintf("/item?id=%v", story.ID)) } class="hover:text-blue-600">{ strconv.Itoa(story.Descendants) } comments</a>
by <a href={ templ.URL(fmt.Sprintf("/user?id=%v", story.By)) } class="hover:text-blue-600">{ story.By }</a>
| { time.Unix(story.Time, 0).Format("2006-01-02 15:04:05") }
| <a href={ templ.URL(fmt.Sprintf("/item?id=%v", story.ID)) } class="hover:text-blue-600">{ strconv.Itoa(story.Descendants) } comments</a>
</div>
</div>
</div>
Expand Down
43 changes: 28 additions & 15 deletions components/story_item_templ.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion handlers/ask.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func HandleAsk(client *client.Client) http.HandlerFunc {

stories := make([]types.Item, 0, pageSize)
for _, id := range storyIDs[start:end] {
story, err := client.GetItem(r.Context(), id)
story, err := client.Item(r.Context(), id)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
Expand Down
4 changes: 2 additions & 2 deletions handlers/item.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func HandleItem(client *client.Client) http.HandlerFunc {
return
}

item, err := client.GetItem(r.Context(), idInt)
item, err := client.Item(r.Context(), idInt)
if err != nil {
pages.NotFound().Render(r.Context(), w)
return
Expand All @@ -44,7 +44,7 @@ func HandleItem(client *client.Client) http.HandlerFunc {
func getComments(ctx context.Context, client *client.Client, kids []int) ([]types.Item, error) {
var comments []types.Item
for _, kid := range kids {
comment, err := client.GetItem(ctx, kid)
comment, err := client.Item(ctx, kid)
if err != nil {
continue
}
Expand Down
2 changes: 1 addition & 1 deletion handlers/jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func HandleJobs(client *client.Client) http.HandlerFunc {

stories := make([]types.Item, 0, pageSize)
for _, id := range storyIDs[start:end] {
story, err := client.GetItem(r.Context(), id)
story, err := client.Item(r.Context(), id)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
Expand Down
2 changes: 1 addition & 1 deletion handlers/newest.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func HandleNewest(client *client.Client) http.HandlerFunc {

stories := make([]types.Item, 0, pageSize)
for _, id := range storyIDs[start:end] {
story, err := client.GetItem(r.Context(), id)
story, err := client.Item(r.Context(), id)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
Expand Down
2 changes: 1 addition & 1 deletion handlers/news.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func HandleNews(client *client.Client) http.HandlerFunc {

stories := make([]types.Item, 0, pageSize)
for _, id := range storyIDs[start:end] {
story, err := client.GetItem(r.Context(), id)
story, err := client.Item(r.Context(), id)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
Expand Down
2 changes: 1 addition & 1 deletion handlers/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func HandleShow(client *client.Client) http.HandlerFunc {

stories := make([]types.Item, 0, pageSize)
for _, id := range storyIDs[start:end] {
story, err := client.GetItem(r.Context(), id)
story, err := client.Item(r.Context(), id)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
Expand Down
Loading

0 comments on commit eb768a2

Please sign in to comment.