Item sharing progress, fixed click on checkboxes and relative labels and colors

This commit is contained in:
roberto 2024-09-09 17:33:20 +02:00
parent 7b2cf94ad6
commit 6523c2aed6
8 changed files with 128 additions and 19 deletions

View File

@ -1,10 +1,11 @@
CREATE TABLE "bm_item_shares" (
"id" INTEGER,
"token" TEXT NOT NULL UNIQUE,
"item_id" INTEGER DEFAULT 0,
"read" INTEGER DEFAULT 0,
"update" INTEGER DEFAULT 0,
"start_datetime" TEXT DEFAULT 0,
"end_datetime" TEXT DEFAULT 0,
"item_id" INTEGER DEFAULT '',
"read" INTEGER DEFAULT '',
"update" INTEGER DEFAULT '',
"start_datetime" TEXT DEFAULT '',
"end_datetime" TEXT DEFAULT '',
"password" TEXT DEFAULT '',
PRIMARY KEY("id" AUTOINCREMENT)
);

View File

@ -517,7 +517,8 @@ a {
width: 18px;
height: 18px;
position: relative;
top: 6px
top: 6px;
accent-color: #2b5797;
}
.w3-sidebar {

View File

@ -1,4 +1,4 @@
const cacheVersion = "0.266"
const cacheVersion = "0.267"
const cacheName = "speedtech-brainminder"
const cacheFiles = [
'/static/bootstrap-icons/font/bootstrap-icons.min.css',

View File

@ -112,6 +112,7 @@
<p>
<label class="switch">
<input
id="on_dashboard"
class="w3-check"
name="On_dashboard"
type="checkbox"
@ -125,7 +126,7 @@
{{end}}
/>
<span class="slider round"></span>
</label><span class="label-checkbox">On dashboard</span>
</label><label for="on_dashboard" class="label-checkbox">On dashboard</label>
</p>
</div>

View File

@ -68,6 +68,7 @@
<p>
<label class="switch">
<input
id="show_summary"
class="w3-check"
name="Show_summary"
type="checkbox"
@ -77,12 +78,13 @@
{{end}}
/>
<span class="slider round"></span>
</label><span class="label-checkbox">Show summary</span>
</label><label for="show_summary" class="label-checkbox">Show summary</label>
</p>
<p>
<label class="switch">
<input
id="show_description"
class="w3-check"
name="Show_description"
type="checkbox"
@ -92,7 +94,7 @@
{{end}}
/>
<span class="slider round"></span>
</label><span class="label-checkbox">Show description</span>
</label><label for="show_description" class="label-checkbox">Show description</label>
</p>
</div>

View File

@ -34,6 +34,18 @@ type itemForm struct {
Validator validator.Validator `form:"-"`
}
type itemShareForm struct {
Id int64 `form:"Id"`
Item_id int64 `form:"Item_id"`
Token string `form:"Token"`
Start_datetime string `form:"start_datetime"`
End_datetime string `form:"end_datetime"`
Password string `form:"password"`
Permission_read int `form:"read"`
Permission_edit int `form:"edit"`
Validator validator.Validator `form:"-"`
}
func (form *itemForm) Validate(w http.ResponseWriter, r *http.Request, app *application, data map[string]any) bool {
var fullBuf = new(bytes.Buffer)
@ -60,6 +72,32 @@ func (form *itemForm) Validate(w http.ResponseWriter, r *http.Request, app *appl
return true
}
func (form *itemShareForm) Validate(w http.ResponseWriter, r *http.Request, app *application, data map[string]any) bool {
var fullBuf = new(bytes.Buffer)
//form.Validator.CheckField(form.Title != "", "Title", "Title is required")
//form.Validator.CheckField(form.Description != "", "Description", "Description is required")
if form.Validator.HasErrors() {
w.Header().Add("HX-Retarget", "#message")
data["messageType"] = "failure"
data["messageContent"] = "Impossible to save the item"
data["messageFieldErrors"] = form.Validator.FieldErrors
err := response.HXFragment(fullBuf, []string{"partials/message.tmpl"}, "message", data)
if err != nil {
app.serverError(w, r, err)
}
fullBuf.WriteTo(w)
w.WriteHeader(http.StatusUnprocessableEntity)
return false
}
return true
}
func (app *application) itemsRelationAdd(w http.ResponseWriter, r *http.Request) {
data := app.newTemplateData(r)
itemModel := models.NewItemModel(app.db)
@ -862,11 +900,15 @@ func (app *application) itemShare(w http.ResponseWriter, r *http.Request) {
itemModel := models.NewItemModel(app.db)
item_id, _ := strconv.ParseInt(flow.Param(r.Context(), "item_id"), 10, 64)
item, _, _ := itemModel.One(item_id)
var fullBuf = new(bytes.Buffer)
data := app.newTemplateData(r)
data["item"] = item
data["baseUrl"] = app.config.baseURL
switch r.Method {
case http.MethodGet:
var fullBuf = new(bytes.Buffer)
data["shareToken"] = app.generateSecureToken(18)
err := response.HXFragment(fullBuf, []string{"items/share.tmpl"}, "page:content", data)
@ -875,6 +917,30 @@ func (app *application) itemShare(w http.ResponseWriter, r *http.Request) {
}
fullBuf.WriteTo(w)
case http.MethodPost:
var itemShareFromForm itemShareForm
err := request.DecodePostForm(r, &itemShareFromForm)
if err != nil {
app.serverError(w, r, err)
}
if !itemShareFromForm.Validate(w, r, app, data) {
return
}
itemShare := &models.ItemShare{
Item_id: item_id,
Token: itemShareFromForm.Token,
}
itemshareModel := &models.ItemShareModel{DB: app.db}
_, err = itemshareModel.Create(itemShare)
if err != nil {
app.badRequest(w, err)
return
}
}
}
func (app *application) itemAddToDashboard(w http.ResponseWriter, r *http.Request) {

View File

@ -419,8 +419,8 @@ func widget_checkboxes(name string, label string, value any, options []WidgetOpt
}
id_str := strings.ReplaceAll(name+"-"+option.Key, " ", "-")
o = o + "<p>"
o = o + fmt.Sprintf(`<label for="%v" class="switch"><input id="%v" type="checkbox" name="%v" %v value="%v" %v /><span class="slider round"></span></label>`, id_str, id_str, name, checked, option.Key, attributes)
o = o + fmt.Sprintf(`<span class="label-checkbox" for="%v">%v</span>`, id_str, option.Value)
o = o + fmt.Sprintf(`<label class="switch"><input id="%v" type="checkbox" name="%v" %v value="%v" %v /><span class="slider round"></span></label>`, id_str, name, checked, option.Key, attributes)
o = o + fmt.Sprintf(`<label class="label-checkbox" for="%v">%v</label>`, id_str, option.Value)
o = o + "</p>"
}
o = o + "</fiedlset>"

38
models/itemshare.go Normal file
View File

@ -0,0 +1,38 @@
package models
import "brainminder.speedtech.it/internal/database"
type ItemShareModel struct {
DB *database.DB
}
type ItemShare struct {
Id int64 `db:"id"`
Token string `db:"token"`
Summary string `db:"summary"`
Item_id int64 `db:"item_id"`
Read int `db:"crypted"`
Update int `db:"hidden"`
Start_datetime string `db:"start_datetime"`
End_datetime string `db:"end_datetime"`
password string `db:"password"`
}
func (model *ItemShareModel) Create(ItemShare *ItemShare) (int64, error) {
ctx, cancel := database.GetContext()
defer cancel()
query := `INSERT INTO bm_items_share (title, description, icon, hidden) VALUES (:title, :description, :icon, :hidden)`
result, err := model.DB.NamedExecContext(ctx, query, ItemShare)
if err != nil {
return 0, err
}
id, err := result.LastInsertId()
if err != nil {
return 0, err
}
return id, err
}