-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move the imports into a new page and create the sidenav (#8)
- Loading branch information
Showing
11 changed files
with
235 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
{{define "sidenav-btn"}} | ||
<button | ||
data-mdb-ripple-init | ||
data-mdb-toggle="sidenav" | ||
data-mdb-target="#sidenav" | ||
class="btn shadow-0 p-0 me-3 d-block d-xxl-none" | ||
aria-controls="#sidenav" | ||
aria-haspopup="true" | ||
> | ||
<i class="fas fa-bars fa-lg"></i> | ||
</button> | ||
{{end}} | ||
|
||
{{define "sidenav"}} | ||
<!-- Sidenav --> | ||
<nav | ||
data-mdb-sidenav-init | ||
id="sidenav" | ||
class="sidenav" | ||
data-mdb-hidden="true" | ||
> | ||
<ul class="sidenav-menu"> | ||
<li class="sidenav-item"> | ||
<a class="sidenav-link" href="/web/contacts" hx-boost="true"> | ||
<i class="fas fa-address-book me-2"></i>Contacts | ||
</a> | ||
</li> | ||
<li class="sidenav-item"> | ||
<a class="sidenav-link" href="/web/imports" hx-boost="true"> | ||
<i class="fas fa-arrow-up-from-bracket me-2"></i>Import | ||
</a> | ||
</li> | ||
</ul> | ||
</nav> | ||
<!-- Sidenav --> | ||
{{end}} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<!doctype html > | ||
{{ template "header"}} | ||
|
||
<body hx-ext="response-targets" hx-target-5*="this" hx-target-4*="this"> | ||
<main> | ||
<!-- Modal --> | ||
<div id="modal-target" class="modal modal-blur fade" tabindex="-1" aria-hidden="true"> | ||
<div class="modal-dialog modal-lg modal-dialog-centered" role="document"> | ||
<div class="modal-content"></div> | ||
</div> | ||
</div> | ||
|
||
|
||
<div id="content"> | ||
{{ yield }} | ||
</div> | ||
|
||
{{template "sidenav"}} | ||
</main> | ||
|
||
|
||
<script src="/assets/js/libs/htmx.min.js"></script> | ||
<script src="/assets/js/libs/response-targets.js"></script> | ||
</body> | ||
|
||
<script type="module"> | ||
|
||
import {SetupBootstrapElems, SetupSideNav} from "/assets/js/setup.mjs" | ||
import {Modal, Button, Ripple, Dropdown, initMDB} from "/assets/js/libs/mdb.es.min.js"; | ||
|
||
initMDB({Modal, Button, Ripple, Dropdown}); | ||
SetupBootstrapElems() | ||
SetupSideNav() | ||
|
||
</script> | ||
|
||
</html> |
24 changes: 24 additions & 0 deletions
24
internal/web/html/templates/pages/imports/page_imports.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<header> | ||
<nav class="navbar sticky-top bg-primary"> | ||
<div class="container-fluid justify-content-start"> | ||
<a class="navbar-nav" href="/web/contacts" hx-boost="true"><i class="fas fa-arrow-left fa-lg"></i></a> | ||
<a class="navbar-brand ps-4">Imports</a> | ||
</div> | ||
</nav> | ||
</header> | ||
|
||
<div class="container-fluid py-2"> | ||
<h3 class="my-3">Import a csv file</h3> | ||
|
||
<form target="_top" hx-post="/web/imports/vcs" | ||
hx-encoding='multipart/form-data' | ||
hx-swap="outerHTML" hx-target="body"> | ||
<label for="fileInput" class="visually-hidden">Import a CSV file</label> | ||
<input class="form-control" name="file" id="fileInput" type="file" /> | ||
<button type="submit" class="btn btn-primary my-3">Submit</button> | ||
</form> | ||
|
||
{{if .ErrorMsg}} | ||
<div id="validation-alert" class="alert alert-danger role=">{{.ErrorMsg}}</div> | ||
{{end}} | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package imports | ||
|
||
type ImportsPageTmpl struct { | ||
ErrorMsg string | ||
} | ||
|
||
func (t *ImportsPageTmpl) Template() string { | ||
return "pages/imports/page_imports" | ||
} |
52 changes: 52 additions & 0 deletions
52
internal/web/html/templates/pages/imports/templates_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package imports | ||
|
||
import ( | ||
"io" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/Peltoche/gnocchi/internal/web/html" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func Test_Templates(t *testing.T) { | ||
renderer := html.NewRenderer(html.Config{ | ||
PrettyRender: false, | ||
HotReload: false, | ||
}) | ||
|
||
tests := []struct { | ||
Template html.Templater | ||
Name string | ||
Layout bool | ||
}{ | ||
{ | ||
Name: "ImportsPageTmpl", | ||
Layout: true, | ||
Template: &ImportsPageTmpl{}, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.Name, func(t *testing.T) { | ||
w := httptest.NewRecorder() | ||
r := httptest.NewRequest(http.MethodGet, "/foo", nil) | ||
|
||
if !test.Layout { | ||
r.Header.Add("HX-Boosted", "true") | ||
} | ||
|
||
renderer.WriteHTMLTemplate(w, r, http.StatusOK, test.Template) | ||
|
||
if !assert.Equal(t, http.StatusOK, w.Code) { | ||
res := w.Result() | ||
res.Body.Close() | ||
body, err := io.ReadAll(res.Body) | ||
require.NoError(t, err) | ||
t.Log(string(body)) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package imports | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/Peltoche/gnocchi/internal/service/vcard" | ||
"github.com/Peltoche/gnocchi/internal/tools/router" | ||
"github.com/Peltoche/gnocchi/internal/web/html" | ||
importsmpl "github.com/Peltoche/gnocchi/internal/web/html/templates/pages/imports" | ||
"github.com/go-chi/chi/v5" | ||
) | ||
|
||
type ImportsPage struct { | ||
html html.Writer | ||
vcard vcard.Service | ||
} | ||
|
||
func NewImportsPage(html html.Writer, vcard vcard.Service) *ImportsPage { | ||
return &ImportsPage{ | ||
html: html, | ||
vcard: vcard, | ||
} | ||
} | ||
|
||
func (h *ImportsPage) Register(r chi.Router, mids *router.Middlewares) { | ||
if mids != nil { | ||
r = r.With(mids.Defaults()...) | ||
} | ||
|
||
r.Get("/web/imports", h.getImportsPage) | ||
r.Post("/web/imports/vcs", h.importFile) | ||
} | ||
|
||
func (h *ImportsPage) getImportsPage(w http.ResponseWriter, r *http.Request) { | ||
h.html.WriteHTMLTemplate(w, r, http.StatusOK, &importsmpl.ImportsPageTmpl{}) | ||
} | ||
|
||
func (h *ImportsPage) importFile(w http.ResponseWriter, r *http.Request) { | ||
file, _, err := r.FormFile("file") | ||
if err != nil { | ||
h.html.WriteHTMLTemplate(w, r, http.StatusUnprocessableEntity, &importsmpl.ImportsPageTmpl{ | ||
ErrorMsg: "missing file", | ||
}) | ||
return | ||
} | ||
defer file.Close() | ||
|
||
err = h.vcard.ImportVCardFile(r.Context(), file) | ||
switch { | ||
case err == nil: | ||
http.Redirect(w, r, "/web/contacts", http.StatusFound) | ||
return | ||
case errors.Is(err, vcard.ErrUnsupportedVCardVersion) || | ||
errors.Is(err, vcard.ErrInvalidVCard): | ||
h.html.WriteHTMLTemplate(w, r, http.StatusUnprocessableEntity, &importsmpl.ImportsPageTmpl{ | ||
ErrorMsg: err.Error(), | ||
}) | ||
default: | ||
h.html.WriteHTMLErrorPage(w, r, fmt.Errorf("failed to import the vcard file: %w", err)) | ||
} | ||
} |