-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
index.go
178 lines (143 loc) · 4.07 KB
/
index.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package main
import (
"errors"
"net/http"
"github.com/maxence-charriere/go-app/v10/pkg/app"
"github.com/Skarlso/crd-to-sample-yaml/pkg/fetcher"
)
const maximumBytes = 200 * 1000 // 200KB
// index is the main page that contains the textarea and the submit button.
// It will also deal with navigation and user submits.
type index struct {
app.Compo
content []byte
isMounted bool
err error
comments bool
}
func (i *index) buildError() app.UI {
return app.Div().Class("alert alert-danger").Role("alert").Body(
app.Span().Class("closebtn").OnClick(i.dismissError).Body(app.Text("×")),
app.H4().Class("alert-heading").Text("Oops!"),
app.Text(i.err.Error()),
)
}
func (i *index) dismissError(_ app.Context, _ app.Event) {
i.err = nil
}
// header is the site header.
type header struct {
app.Compo
}
func (h *header) Render() app.UI {
return app.Header().Body(app.Nav().Body(
app.Div().Class("title").Text("CRD Parser"),
app.Ul().Body(
app.Li().Body(
app.A().Href("https://github.com/Skarlso/crd-to-sample-yaml").Target("_blank").Body(
app.I().Class("fa fa-github fa-2x")),
)),
))
}
// textarea is the textarea component that is used to supply the CRD content.
type textarea struct {
app.Compo
}
func (t *textarea) Render() app.UI {
return app.Div().Class("input-group mb-3").Body(
app.Span().Class("input-group-text").Body(app.Text("CRD")),
app.Textarea().
Class("form-control").
ID("crd_data").
Name("crd_data").
Placeholder("Place CRD here...").
AutoFocus(true),
)
}
// input is the input button.
type input struct {
app.Compo
}
func (i *input) Render() app.UI {
return app.Div().Class("input-group mb-3").Body(
app.Span().Class("input-group-text").Body(app.Text("URL")),
app.Input().
Class("url_to_crd").Class("form-control").Placeholder("Paste URL to CRD here...").
ID("url_to_crd").
Name("url_to_crd"),
)
}
// form is the form in which the user will submit their input.
type form struct {
app.Compo
formHandler app.EventHandler
checkHandler app.EventHandler
}
func (f *form) Render() app.UI {
return app.Div().Body(
app.Div().Class("row mb-3").Body(
&textarea{},
&input{},
&checkBox{checkHandler: f.checkHandler},
),
app.Div().Class("text-end").Body(app.Button().Class("btn btn-primary").Type("submit").Style("margin-top", "15px").Text("Submit").OnClick(f.formHandler)),
)
}
func (i *index) OnClick(_ app.Context, _ app.Event) {
ta := app.Window().GetElementByID("crd_data").Get("value")
if v := ta.String(); v != "" {
if len(v) > maximumBytes {
i.err = errors.New("content exceeds maximum length of 200KB")
return
}
i.content = []byte(v)
return
}
inp := app.Window().GetElementByID("url_to_crd").Get("value")
if inp.String() == "" {
return
}
f := fetcher.NewFetcher(http.DefaultClient)
content, err := f.Fetch(inp.String())
if err != nil {
i.err = err
return
}
if len(content) > maximumBytes {
i.err = errors.New("content exceeds maximum length of 200KB")
return
}
i.content = content
}
// checkBox defines if comments should be generated for the sample YAML output.
type checkBox struct {
app.Compo
checkHandler app.EventHandler
}
func (c *checkBox) Render() app.UI {
return app.Div().Class("form-check").Body(
app.Label().Class("form-check-label").For("enable-comments").Body(app.Text("Enable comments on YAML output")),
app.Input().Class("form-check-input").Type("checkbox").ID("enable-comments").OnClick(c.checkHandler),
)
}
func (i *index) OnCheck(_ app.Context, _ app.Event) {
i.comments = !i.comments
}
func (i *index) OnMount(_ app.Context) {
i.isMounted = true
}
func (i *index) Render() app.UI {
// Prevent double rendering components.
if i.isMounted {
return app.Main().Body(app.Div().Class("container").Body(func() app.UI {
if i.err != nil {
return app.Div().Class("container").Body(&header{}, i.buildError())
}
if i.content != nil {
return &crdView{content: i.content, comment: i.comments}
}
return app.Div().Class("container").Body(&header{}, &form{formHandler: i.OnClick, checkHandler: i.OnCheck})
}()))
}
return app.Main()
}