Web channel editing
This commit is contained in:
parent
3a39b84528
commit
879238e61e
@ -1,12 +1,76 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/Jaculabilis/intake/core"
|
||||
"github.com/Jaculabilis/intake/web/html"
|
||||
)
|
||||
|
||||
func (env *Env) getChannels(writer http.ResponseWriter, req *http.Request) {
|
||||
allSources, err := core.GetSources(env.db)
|
||||
if err != nil {
|
||||
http.Error(writer, err.Error(), 400)
|
||||
return
|
||||
}
|
||||
|
||||
channelSources, err := core.GetSourcesInChannel(env.db)
|
||||
if err != nil {
|
||||
http.Error(writer, err.Error(), 400)
|
||||
return
|
||||
}
|
||||
|
||||
data := html.EditChannelsData{
|
||||
Sources: allSources,
|
||||
ChannelSources: channelSources,
|
||||
}
|
||||
html.EditChannels(writer, data)
|
||||
}
|
||||
|
||||
func (env *Env) editChannel(writer http.ResponseWriter, req *http.Request) {
|
||||
if err := req.ParseForm(); err != nil {
|
||||
http.Error(writer, err.Error(), 400)
|
||||
return
|
||||
}
|
||||
|
||||
channel := req.Form.Get("channel")
|
||||
source := req.Form.Get("source")
|
||||
|
||||
if channel == "" {
|
||||
http.Error(writer, "missing channel", 400)
|
||||
return
|
||||
}
|
||||
if source == "" {
|
||||
http.Error(writer, "missing source", 400)
|
||||
return
|
||||
}
|
||||
if exists, err := core.SourceExists(env.db, source); err != nil || !exists {
|
||||
http.Error(writer, fmt.Sprintf("could not find source %s: %v", source, err), 500)
|
||||
return
|
||||
}
|
||||
|
||||
if req.Method == http.MethodPost {
|
||||
if err := core.AddSourceToChannel(env.db, channel, source); err != nil {
|
||||
http.Error(writer, err.Error(), 500)
|
||||
return
|
||||
}
|
||||
writer.Header()["HX-Refresh"] = []string{"true"}
|
||||
writer.WriteHeader(http.StatusNoContent)
|
||||
return
|
||||
}
|
||||
|
||||
if req.Method == http.MethodDelete {
|
||||
if err := core.DeleteSourceFromChannel(env.db, channel, source); err != nil {
|
||||
http.Error(writer, err.Error(), 500)
|
||||
return
|
||||
}
|
||||
writer.Header()["HX-Refresh"] = []string{"true"}
|
||||
writer.WriteHeader(http.StatusNoContent)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (env *Env) getChannel(writer http.ResponseWriter, req *http.Request) {
|
||||
channel := req.PathValue("channel")
|
||||
|
||||
|
49
web/html/editChannels.html
Normal file
49
web/html/editChannels.html
Normal file
@ -0,0 +1,49 @@
|
||||
{{ define "title" }}Channels - Intake{{ end }}
|
||||
|
||||
{{ define "content" -}}
|
||||
<nav class="center">
|
||||
<span class="feed-controls">
|
||||
<a href="/">Home</a>
|
||||
</span>
|
||||
</nav>
|
||||
|
||||
<nav>
|
||||
<span class="feed-controls">Edit channels</span>
|
||||
|
||||
<p>
|
||||
<form>
|
||||
<label for="source">Add</label>
|
||||
<select name="source">{{ range .Sources }}
|
||||
<option value="{{ . }}">{{ . }}</option>{{ end }}
|
||||
</select>
|
||||
<label for="channel">to</label>
|
||||
<input type="text" name="channel" list="channel-options">
|
||||
<button
|
||||
hx-post="/channel/"
|
||||
>Submit</button>
|
||||
</form>
|
||||
</p>
|
||||
|
||||
<datalist id="channel-options">{{ range $channel, $_ := .ChannelSources }}
|
||||
<option value="{{ $channel }}"></option>{{ end }}
|
||||
</datalist>
|
||||
|
||||
{{ range $channel, $sources := .ChannelSources }}
|
||||
<p><b><a href="/channel/{{ $channel }}">{{ $channel }}</a></b></p>
|
||||
<table>
|
||||
{{- range $sources }}
|
||||
<tr>
|
||||
<td>
|
||||
<button
|
||||
hx-delete="/channel/?channel={{ $channel }}&source={{ . }}"
|
||||
>✕</button>
|
||||
</td>
|
||||
</form>
|
||||
<td><a href="/source/{{ . }}">{{ . }}</a></td>
|
||||
</tr>
|
||||
{{ end }}
|
||||
|
||||
</table>
|
||||
{{ end }}
|
||||
</nav>
|
||||
{{- end }}
|
@ -8,7 +8,7 @@
|
||||
{{ range .Channels }}
|
||||
<p><a href="/channel/{{ .Name }}">
|
||||
{{ if .Active }}
|
||||
{{ .Name }} ({{ .Active }})
|
||||
({{ .Active }}) {{ .Name }}
|
||||
{{ else }}
|
||||
{{ .Name }}
|
||||
{{ end }}
|
||||
@ -17,6 +17,8 @@
|
||||
{{ else }}
|
||||
<p>No channels found.</p>
|
||||
{{ end }}
|
||||
|
||||
<p><a href="/channel/">(Edit channels)</a></p>
|
||||
</details>
|
||||
</nav>
|
||||
|
||||
|
@ -169,3 +169,16 @@ func Fetch(writer io.Writer, data FetchData) {
|
||||
log.Printf("error: failed to render fetch: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
var editChannels = load("editChannels.html")
|
||||
|
||||
type EditChannelsData struct {
|
||||
Sources []string
|
||||
ChannelSources map[string][]string
|
||||
}
|
||||
|
||||
func EditChannels(writer io.Writer, data EditChannelsData) {
|
||||
if err := editChannels.Execute(writer, data); err != nil {
|
||||
log.Printf("error: failed to render edit channels: %v", err)
|
||||
}
|
||||
}
|
||||
|
@ -41,6 +41,9 @@ func RunServer(db core.DB, addr string, port string) {
|
||||
handleFunc("POST /login", env.login, logged)
|
||||
handleFunc("GET /source/{source}", env.getSource, env.authed, logged)
|
||||
handleFunc("POST /source/{source}/fetch", env.fetchSource, env.authed, logged)
|
||||
handleFunc("GET /channel/", env.getChannels, env.authed, logged)
|
||||
handleFunc("POST /channel/", env.editChannel, env.authed, logged)
|
||||
handleFunc("DELETE /channel/", env.editChannel, env.authed, logged)
|
||||
handleFunc("GET /channel/{channel}", env.getChannel, env.authed, logged)
|
||||
handleFunc("POST /item", env.addItem, env.authed, logged)
|
||||
handleFunc("GET /item/{source}/{id}", env.getItem, env.authed, logged)
|
||||
|
Loading…
Reference in New Issue
Block a user