/ nav.go
nav.go
  1  package main
  2  
  3  import (
  4  	"encoding/base64"
  5  	"log"
  6  
  7  	"github.com/maxence-charriere/go-app/v10/pkg/app"
  8  	shell "github.com/stateless-minds/go-ipfs-api"
  9  )
 10  
 11  type nav struct {
 12  	app.Compo
 13  	sh            *shell.Shell
 14  	loggedIn      bool
 15  	termsAccepted bool
 16  	isBusiness    bool
 17  	isGovernment  bool
 18  	businessName  string
 19  	businessID    string
 20  	entity        string
 21  	userID        string
 22  	plan          Plan
 23  }
 24  
 25  func newNav() *nav {
 26  	return &nav{}
 27  }
 28  
 29  func (n *nav) OnMount(ctx app.Context) {
 30  	ctx.GetState("loggedIn", &n.loggedIn)
 31  	if n.loggedIn {
 32  		ctx.GetState("userID", &n.userID)
 33  		ctx.GetState("isBusiness", &n.isBusiness)
 34  		ctx.GetState("isGovernment", &n.isGovernment)
 35  		sh := shell.NewShell("localhost:5001")
 36  		n.sh = sh
 37  	}
 38  
 39  	ctx.ObserveState("termsAccepted", &n.termsAccepted)
 40  	ctx.ObserveState("entity", &n.entity)
 41  	ctx.ObserveState("businessName", &n.businessName)
 42  	ctx.ObserveState("businessID", &n.businessID)
 43  	ctx.ObserveState("plan", &n.plan)
 44  }
 45  
 46  func (n *nav) doOverlay(ctx app.Context, e app.Event) {
 47  	app.Window().GetElementByID("content").Get("classList").Call("toggle", "overlay")
 48  }
 49  
 50  func (n *nav) acceptTermsIndividual(ctx app.Context, e app.Event) {
 51  	e.PreventDefault()
 52  	ctx.SetState("termsAccepted", true)
 53  	app.Window().GetElementByID("main-menu").Call("click")
 54  }
 55  
 56  func (n *nav) acceptTermsBusiness(ctx app.Context, e app.Event) {
 57  	e.PreventDefault()
 58  	ctx.SetState("termsAccepted", true)
 59  }
 60  
 61  func (n *nav) deleteAccount(ctx app.Context, e app.Event) {
 62  	e.PreventDefault()
 63  	n.deleteUser()
 64  	n.deleteBalance()
 65  	// delete subscriptions
 66  	if n.isBusiness {
 67  		n.deletePlan()
 68  		// delete clients
 69  		// delete suppliers
 70  	}
 71  	ctx.DelState("termsAccepted")
 72  	ctx.Reload()
 73  
 74  }
 75  
 76  func (n *nav) deletePlan() {
 77  	err := n.sh.OrbitDocsDelete(dbPlan, n.plan.ID)
 78  	if err != nil {
 79  		log.Fatal(err)
 80  	}
 81  }
 82  
 83  func (n *nav) registerIndividual(ctx app.Context, e app.Event) {
 84  	e.PreventDefault()
 85  	ctx.SetState("entity", "individual")
 86  }
 87  
 88  func (n *nav) registerBusiness(ctx app.Context, e app.Event) {
 89  	e.PreventDefault()
 90  	ctx.SetState("entity", "business")
 91  }
 92  
 93  func (n *nav) submitBusinessID(ctx app.Context, e app.Event) {
 94  	validBusinessID := app.Window().GetElementByID("business-id").Call("reportValidity").Bool()
 95  	validBusinessName := app.Window().GetElementByID("business-name").Call("reportValidity").Bool()
 96  	if validBusinessID && validBusinessName {
 97  		businessName := app.Window().GetElementByID("business-name").Get("value").String()
 98  		associateName := app.Window().GetElementByID("associate-name").Get("value").String()
 99  		businessID := app.Window().GetElementByID("business-id").Get("value").String()
100  		ctx.SetState("businessID", businessID)
101  		ctx.SetState("businessName", businessName)
102  		ctx.SetState("associateName", associateName).Persist()
103  		app.Window().GetElementByID("main-menu").Call("click")
104  	}
105  }
106  
107  func (n *nav) deleteUser() {
108  	userId := base64.StdEncoding.EncodeToString([]byte(n.userID))
109  	err := n.sh.OrbitDocsDelete(dbUser, string(userId))
110  	if err != nil {
111  		log.Fatal(err)
112  	}
113  }
114  
115  func (n *nav) deleteBalance() {
116  	err := n.sh.OrbitDocsDelete(dbWallet, n.userID)
117  	if err != nil {
118  		log.Fatal(err)
119  	}
120  }
121  
122  func (n *nav) Render() app.UI {
123  	return app.Nav().Body(
124  		app.Div().Class("navbar").Body(
125  			app.Div().Class("container nav-container").Body(
126  				app.If(n.loggedIn, func() app.UI {
127  					return app.Input().ID("main-menu").Class("checkbox").Type("checkbox").Name("Main Menu").OnClick(n.doOverlay)
128  				}).Else(func() app.UI {
129  					return app.Input().ID("main-menu").Class("checkbox").Type("checkbox").Name("Main Menu").OnClick(n.doOverlay).Style("pointer-events", "none")
130  				}),
131  				app.If(n.loggedIn, func() app.UI {
132  					return app.Div().Class("hamburger-lines").Body(
133  						app.Span().Class("line line1"),
134  						app.Span().Class("line line2"),
135  						app.Span().Class("line line3"),
136  					)
137  				}).Else(func() app.UI {
138  					return app.Div().Class("hamburger-lines").Body(
139  						app.Span().Class("line line1"),
140  						app.Span().Class("line line2"),
141  						app.Span().Class("line line3"),
142  					).Style("display", "none")
143  				}),
144  
145  				app.If(!n.loggedIn, func() app.UI {
146  					return app.If(!n.termsAccepted, func() app.UI {
147  						return app.If(n.entity == "individual", func() app.UI {
148  							return app.Div().Class("menu-items").Body(
149  								app.Div().Class("header-summary").Body(
150  									app.Span().Class("logo").Text("cyber-gubi"),
151  									app.Div().Class("summary-text").Body(
152  										app.Span().Text("Individual"),
153  									),
154  								),
155  								app.Li().Body(
156  									app.A().Href("/terms").Target("_blank").Text("Terms of Use"),
157  								),
158  								app.Li().Body(
159  									app.A().Href("/privacy").Target("_blank").Text("Privacy"),
160  								),
161  								app.Li().Body(
162  									app.A().Href("/cookie").Target("_blank").Text("Cookie"),
163  								),
164  								app.Div().Class("menu-btn").Body(
165  									app.Button().ID("accept-terms").Class("submit").Type("submit").Text("Accept Terms").OnClick(n.acceptTermsIndividual),
166  								),
167  							)
168  						}).ElseIf(n.entity == "business", func() app.UI {
169  							return app.Div().Class("menu-items").Body(
170  								app.Div().Class("header-summary").Body(
171  									app.Span().Class("logo").Text("cyber-gubi"),
172  									app.Div().Class("summary-text").Body(
173  										app.Span().Text("Business"),
174  									),
175  								),
176  								app.Li().Body(
177  									app.A().Href("/terms-business").Target("_blank").Text("Terms of Use"),
178  								),
179  								app.Li().Body(
180  									app.A().Href("/privacy").Target("_blank").Text("Privacy"),
181  								),
182  								app.Li().Body(
183  									app.A().Href("/cookie").Target("_blank").Text("Cookie"),
184  								),
185  								app.Div().Class("menu-btn").Body(
186  									app.Button().ID("accept-terms-business").Class("submit").Type("submit").Text("Accept Terms").OnClick(n.acceptTermsBusiness),
187  								),
188  							)
189  						}).Else(func() app.UI {
190  							return app.Div().Class("menu-items").Body(
191  								app.Div().Class("header-summary").Body(
192  									app.Span().Class("logo").Text("cyber-gubi"),
193  									app.Div().Class("summary-text").Body(
194  										app.Span().Text("Menu"),
195  									),
196  								),
197  								app.Li().Body(
198  									app.A().Text("For Individuals").OnClick(n.registerIndividual),
199  								),
200  								app.Li().Body(
201  									app.Div().Class("tooltip").DataSet("direction", "bottom").Body(
202  										app.Div().Class("tooltip__initiator").Body(
203  											app.A().Text("For Businesses").OnClick(n.registerBusiness),
204  										),
205  										app.Div().Class("tooltip__item").Text("Coming soon! Join the waitlist and check https://github.com/stateless-minds/cyber-gubi for opening announcement."),
206  									),
207  								),
208  							)
209  						})
210  					}).Else(func() app.UI {
211  						return app.If(n.entity == "business", func() app.UI {
212  							return app.Div().Class("menu-items").Body(
213  								app.Div().Class("header-summary").Body(
214  									app.Span().Class("logo").Text("cyber-gubi"),
215  									app.Div().Class("summary-text").Body(
216  										app.Span().Text("Business"),
217  									),
218  								),
219  								app.Label().Class("menu-label").For("business-name").Text("Business Name:"),
220  								app.Input().ID("business-name").Class("input-register").Type("text").Placeholder("Enter business name").MaxLength(22).Required(true),
221  								app.Label().Class("menu-label").For("associate-name").Text("Associate Name:"),
222  								app.Input().ID("associate-name").Class("input-register").Type("text").Placeholder("Enter associate name").MaxLength(22).Required(true),
223  								app.Label().Class("menu-label").For("business-id").Text("Business ID:"),
224  								app.Input().ID("business-id").Class("input-register").Type("text").Placeholder("Enter Business ID").Required(true),
225  								app.Div().Class("menu-btn").Body(
226  									app.Button().ID("submit-business-id").Class("submit").Text("Register Business").OnClick(n.submitBusinessID)),
227  							)
228  						})
229  					})
230  				}).Else(func() app.UI {
231  					return app.If(n.isBusiness, func() app.UI {
232  						return app.Div().Class("menu-items").Body(
233  							app.Div().Class("header-summary").Body(
234  								app.Span().Class("logo").Text("cyber-gubi"),
235  								app.Div().Class("summary-text").Body(
236  									app.Span().Text("Business"),
237  								),
238  							),
239  							app.Li().Body(
240  								app.A().Href("/associates").Text("Associates"),
241  							),
242  							app.Li().Body(
243  								app.A().Href("/wallet").Text("Wallet"),
244  							),
245  							app.Li().Body(
246  								app.A().Href("/payment").Text("Payment"),
247  							),
248  							app.Li().Body(
249  								app.A().Href("/transactions").Text("Transactions"),
250  							),
251  							app.If(n.plan == Plan{}, func() app.UI {
252  								return app.Li().Body(
253  									app.A().Href("/plan").Text("Create Plan"),
254  								)
255  							}).Else(func() app.UI {
256  								return app.Li().Body(
257  									app.A().Href("/plan").Text("Edit Plan"),
258  								)
259  							}),
260  							app.Li().Body(
261  								app.A().Href("/clients").Text("Clients"),
262  							),
263  							app.Li().Body(
264  								app.A().Href("/suppliers").Text("Suppliers"),
265  							),
266  							app.Li().Body(
267  								app.A().Href("/terms-business").Text("Terms of Use"),
268  							),
269  							app.Li().Body(
270  								app.A().Href("/privacy-business").Text("Privacy"),
271  							),
272  							app.Li().Body(
273  								app.A().Href("/cookie-business").Text("Cookie"),
274  							),
275  							app.Li().Body(
276  								app.A().Text("Delete Account").OnClick(n.deleteAccount),
277  							),
278  						)
279  					}).ElseIf(n.isGovernment, func() app.UI {
280  						return app.Div().Class("menu-items").Body(
281  							app.Div().Class("header-summary").Body(
282  								app.Span().Class("logo").Text("cyber-gubi"),
283  								app.Div().Class("summary-text").Body(
284  									app.Span().Text("Government"),
285  								),
286  							),
287  							app.Li().Body(
288  								app.A().Href("/associates").Text("Associates"),
289  							),
290  							app.Li().Body(
291  								app.A().Href("/wallet").Text("Treasury"),
292  							),
293  							app.Li().Body(
294  								app.A().Href("/payment").Text("Payment"),
295  							),
296  							app.Li().Body(
297  								app.A().Href("/transactions").Text("Transactions"),
298  							),
299  							app.Li().Body(
300  								app.A().Href("/wallets").Text("Collect Tax"),
301  							),
302  							app.Li().Body(
303  								app.A().Href("/terms-government").Text("Terms of Use"),
304  							),
305  							app.Li().Body(
306  								app.A().Href("/privacy").Text("Privacy"),
307  							),
308  							app.Li().Body(
309  								app.A().Href("/cookie").Text("Cookie"),
310  							),
311  							app.Li().Body(
312  								app.A().Text("Delete Account").OnClick(n.deleteAccount),
313  							),
314  						)
315  					}).Else(func() app.UI {
316  						return app.Div().Class("menu-items").Body(
317  							app.Div().Class("header-summary").Body(
318  								app.Span().Class("logo").Text("cyber-gubi"),
319  								app.Div().Class("summary-text").Body(
320  									app.Span().Text("Individual"),
321  								),
322  							),
323  							app.Li().Body(
324  								app.A().Href("/wallet").Text("Wallet"),
325  							),
326  							app.Li().Body(
327  								app.A().Href("/payment").Text("Payment"),
328  							),
329  							app.Li().Body(
330  								app.A().Href("/transactions").Text("Transactions"),
331  							),
332  							app.Li().Body(
333  								app.A().Href("/subscriptions").Text("Subscriptions"),
334  							),
335  							app.Li().Body(
336  								app.A().Href("/terms").Text("Terms of Use"),
337  							),
338  							app.Li().Body(
339  								app.A().Href("/privacy").Text("Privacy"),
340  							),
341  							app.Li().Body(
342  								app.A().Href("/cookie").Text("Cookie"),
343  							),
344  							app.Li().Body(
345  								app.A().Text("Delete Account").OnClick(n.deleteAccount),
346  							),
347  						)
348  					})
349  				}),
350  			),
351  		),
352  	)
353  }