Skip to content

Commit

Permalink
feat: Distinguish no results from no orders
Browse files Browse the repository at this point in the history
  • Loading branch information
romshark committed Nov 21, 2024
1 parent 988cf21 commit 14bbc21
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 8 deletions.
21 changes: 18 additions & 3 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,13 +198,20 @@ func (s *Server) handleGetIndex(w http.ResponseWriter, r *http.Request) {
return
}

totalOrdersAvailable, err := s.store.TotalOrdersAvailable()
if err != nil {
s.errInternal(w, err)
return
}

if r.Header.Get("HX-Request") != "" {
var f template.Form
f.UnmarshalForm(r)
f.ResetErrorsForZero()
if err := template.RenderViewIndex(
r.Context(), w, f, searchQuery,
s.addressCountryOptions, s.shippingCompanyOptions, orders,
s.addressCountryOptions, s.shippingCompanyOptions,
totalOrdersAvailable, orders,
); err != nil {
s.errInternal(w, err)
return
Expand All @@ -216,7 +223,8 @@ func (s *Server) handleGetIndex(w http.ResponseWriter, r *http.Request) {
if err := template.RenderPageIndex(
r.Context(), w, theme == middleware.ThemeDark,
template.Form{}, searchQuery,
s.addressCountryOptions, s.shippingCompanyOptions, orders,
s.addressCountryOptions, s.shippingCompanyOptions,
totalOrdersAvailable, orders,
); err != nil {
s.errInternal(w, err)
return
Expand Down Expand Up @@ -314,10 +322,17 @@ func (s *Server) handlePostOrders(w http.ResponseWriter, r *http.Request) {
return
}

totalOrdersAvailable, err := s.store.TotalOrdersAvailable()
if err != nil {
s.errInternal(w, err)
return
}

if err := template.RenderViewIndex(
r.Context(), w,
f, searchQuery,
s.addressCountryOptions, s.shippingCompanyOptions, orders,
s.addressCountryOptions, s.shippingCompanyOptions,
totalOrdersAvailable, orders,
); err != nil {
s.errInternal(w, err)
return
Expand Down
8 changes: 8 additions & 0 deletions server/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ func NewStore() *Store {
}
}

// TotalOrdersAvailable returns the total number of orders stored.
func (s *Store) TotalOrdersAvailable() (int, error) {
s.lock.Lock()
defer s.lock.Unlock()

return len(s.orders), nil
}

// GetAllOrders returns all currently stored orders.
func (s *Store) GetAllOrders() ([]domain.ShippingDetails, error) {
s.lock.Lock()
Expand Down
9 changes: 7 additions & 2 deletions server/template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ func RenderPageIndex(
searchQuery string,
addressCountryOptions []NamedOption,
shippingCompanyOptions []NamedOption,
totalOrdersAvailable int,
orders []domain.ShippingDetails,
) error {
return pageIndex(
form, searchQuery, darkMode,
addressCountryOptions, shippingCompanyOptions, orders,
addressCountryOptions, shippingCompanyOptions,
totalOrdersAvailable, orders,
).Render(ctx, w)
}

Expand All @@ -29,10 +31,13 @@ func RenderViewIndex(
searchQuery string,
addressCountryOptions []NamedOption,
shippingCompanyOptions []NamedOption,
totalOrdersAvailable int,
orders []domain.ShippingDetails,
) error {
return viewIndex(
form, searchQuery, addressCountryOptions, shippingCompanyOptions, orders,
form, searchQuery,
addressCountryOptions, shippingCompanyOptions,
totalOrdersAvailable, orders,
).Render(ctx, w)
}

Expand Down
23 changes: 20 additions & 3 deletions server/template/template.templ
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ templ viewIndex(
searchQuery string,
addressCountryOptions []NamedOption,
shippingCompanyOptions []NamedOption,
totalOrdersAvailable int,
shippingOrders []domain.ShippingDetails,
) {
<div x-data="{}">
Expand Down Expand Up @@ -187,9 +188,23 @@ templ viewIndex(
@componentOrderForm(form, addressCountryOptions, shippingCompanyOptions)
</sl-drawer>
}
<h1 class="mt-2 mb-4 text-xl">Pending Orders:</h1>
if len(shippingOrders) < 1 {
<p>No currently pending orders.</p>
if totalOrdersAvailable == 0 {
// No orders available.
<div class="flex flex-col w-full h-32 items-center justify-center">
<p>There are no currently pending orders.</p>
<sl-button
class="mt-4"
@click="$refs.formDrawer.show()"
>
<sl-icon slot="prefix" name="file-earmark-plus"></sl-icon>
Add a new order
</sl-button>
</div>
} else if len(shippingOrders) < 1 {
// No search results.
<div class="flex flex-col w-full h-32 items-center justify-center">
<p>Couldn't find any orders.</p>
</div>
}
<div class="flex flex-row flex-wrap gap-4">
for i, o := range shippingOrders {
Expand Down Expand Up @@ -321,6 +336,7 @@ templ pageIndex(
darkMode bool,
addressCountryOptions []NamedOption,
shippingCompanyOptions []NamedOption,
totalOrdersAvailable int,
shippingOrders []domain.ShippingDetails,
) {
@htmlMain("Index", darkMode) {
Expand All @@ -329,6 +345,7 @@ templ pageIndex(
searchQuery,
addressCountryOptions,
shippingCompanyOptions,
totalOrdersAvailable,
shippingOrders,
)
}
Expand Down

0 comments on commit 14bbc21

Please sign in to comment.