Add button to create a new List
This commit is contained in:
parent
e2952d0fda
commit
26a18592fa
@ -3,10 +3,12 @@ package webserver
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
|
"io"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
"encoding/json"
|
||||||
|
|
||||||
"gitlab.com/offline-twitter/twitter_offline_engine/pkg/persistence"
|
"gitlab.com/offline-twitter/twitter_offline_engine/pkg/persistence"
|
||||||
. "gitlab.com/offline-twitter/twitter_offline_engine/pkg/scraper"
|
. "gitlab.com/offline-twitter/twitter_offline_engine/pkg/scraper"
|
||||||
@ -140,6 +142,21 @@ func (app *Application) Lists(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// New list
|
||||||
|
if r.Method == "POST" {
|
||||||
|
var formdata struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
}
|
||||||
|
data, err := io.ReadAll(r.Body)
|
||||||
|
panic_if(err)
|
||||||
|
err = json.Unmarshal(data, &formdata)
|
||||||
|
panic_if(err)
|
||||||
|
new_list := List{Name: formdata.Name}
|
||||||
|
app.Profile.SaveList(&new_list)
|
||||||
|
http.Redirect(w, r, fmt.Sprintf("/lists/%d/users", new_list.ID), 302)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// List index
|
// List index
|
||||||
lists := app.Profile.GetAllLists()
|
lists := app.Profile.GetAllLists()
|
||||||
trove := NewTweetTrove()
|
trove := NewTweetTrove()
|
||||||
|
@ -666,6 +666,30 @@ func TestListAddAndDeleteUser(t *testing.T) {
|
|||||||
assert.Len(cascadia.QueryAll(root, selector(".users-list-container .author-info")), 2)
|
assert.Len(cascadia.QueryAll(root, selector(".users-list-container .author-info")), 2)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestCreateNewList(t *testing.T) {
|
||||||
|
require := require.New(t)
|
||||||
|
assert := assert.New(t)
|
||||||
|
|
||||||
|
// Initial list-of-lists
|
||||||
|
resp := do_request(httptest.NewRequest("GET", "/lists", nil))
|
||||||
|
require.Equal(resp.StatusCode, 200)
|
||||||
|
root, err := html.Parse(resp.Body)
|
||||||
|
require.NoError(err)
|
||||||
|
num_lists := len(cascadia.QueryAll(root, selector(".users-list-preview")))
|
||||||
|
|
||||||
|
// Create a new list
|
||||||
|
resp_add := do_request(httptest.NewRequest("POST", "/lists", strings.NewReader(`{"name": "My New List"}`)))
|
||||||
|
require.Equal(resp_add.StatusCode, 302)
|
||||||
|
require.Equal(fmt.Sprintf("/lists/%d/users", num_lists + 1), resp_add.Header.Get("Location"))
|
||||||
|
|
||||||
|
// Should be N+1 lists now
|
||||||
|
resp = do_request(httptest.NewRequest("GET", "/lists", nil))
|
||||||
|
require.Equal(resp.StatusCode, 200)
|
||||||
|
root, err = html.Parse(resp.Body)
|
||||||
|
require.NoError(err)
|
||||||
|
assert.Len(cascadia.QueryAll(root, selector(".users-list-preview")), num_lists + 1)
|
||||||
|
}
|
||||||
|
|
||||||
// Messages
|
// Messages
|
||||||
// --------
|
// --------
|
||||||
|
|
||||||
|
@ -2,6 +2,18 @@
|
|||||||
|
|
||||||
{{define "main"}}
|
{{define "main"}}
|
||||||
<h1>Lists</h1>
|
<h1>Lists</h1>
|
||||||
|
|
||||||
|
<button onclick="document.querySelector('#newListDialog').showModal()">New list</button>
|
||||||
|
<dialog id="newListDialog">
|
||||||
|
<h3>Create new list</h3>
|
||||||
|
<form hx-post="/lists" hx-ext="json-enc" hx-target="body" hx-push-url="true">
|
||||||
|
<label for="name">Name</label>
|
||||||
|
<input name="name" />
|
||||||
|
<input type="submit" value="Create" />
|
||||||
|
</form>
|
||||||
|
<button onclick="document.querySelector('#newListDialog').close()">Cancel</button>
|
||||||
|
</dialog>
|
||||||
|
|
||||||
<div class="users-list-previews">
|
<div class="users-list-previews">
|
||||||
{{range .}}
|
{{range .}}
|
||||||
{{$max_display_users := 10}}
|
{{$max_display_users := 10}}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user