Search Apps Documentation Source Content File Folder Download Copy Actions Download

render.gno

2.64 Kb · 95 lines
 1package basedao
 2
 3import (
 4	"strconv"
 5	"strings"
 6
 7	"gno.land/p/nt/mux/v0"
 8)
 9
10// WELL KNOWN PATHS
11const (
12	HOME_PATH             = ""
13	MEMBERS_PATH          = "members"
14	PROPOSALS_PATH        = "proposals"
15	HOME_NO_PROFILE_PATH  = "noprofile"
16	CONFIG_PATH           = "config"
17	PROPOSAL_HISTORY_PATH = "history"
18	MEMBER_DETAIL_PATH    = "member/{address}"
19	PROPOSAL_DETAIL_PATH  = "proposal/{id}"
20	ROLE_DETAIL_PATH      = "role/{name}"
21	FALLBACK_DISPLAY_NAME = "Anon"
22)
23
24func (d *DAOPrivate) initRenderingRouter() {
25	if d.RenderRouter == nil {
26		d.RenderRouter = mux.NewRouter()
27	}
28}
29
30func (d *DAOPrivate) InitDefaultRendering() {
31	d.RenderRouter.HandleFunc(HOME_PATH, d.MuxHomePage)
32	d.RenderRouter.HandleFunc(MEMBERS_PATH, d.MuxMembersPage)
33	d.RenderRouter.HandleFunc(PROPOSALS_PATH, d.MuxProposalsPage)
34	d.RenderRouter.HandleFunc(HOME_NO_PROFILE_PATH, d.MuxHomePage)
35	d.RenderRouter.HandleFunc(CONFIG_PATH, d.MuxConfigPage)
36	d.RenderRouter.HandleFunc(PROPOSAL_HISTORY_PATH, d.MuxProposalHistoryPage)
37	d.RenderRouter.HandleFunc(MEMBER_DETAIL_PATH, d.MuxMemberDetailPage)
38	d.RenderRouter.HandleFunc(PROPOSAL_DETAIL_PATH, d.MuxProposalDetailPage)
39	d.RenderRouter.HandleFunc(ROLE_DETAIL_PATH, d.MuxRoleDetailPage)
40}
41
42func (d *DAOPrivate) Render(path string) string {
43	if d.RenderFn != nil {
44		return d.RenderFn(path, d)
45	}
46	return d.RenderRouter.Render(path)
47}
48
49// DEFAULT_HANDLERS
50
51func (d *DAOPrivate) MuxHomePage(res *mux.ResponseWriter, req *mux.Request) {
52	res.Write(d.HomePageView(req.RawPath))
53}
54
55func (d *DAOPrivate) MuxMembersPage(res *mux.ResponseWriter, req *mux.Request) {
56	res.Write(d.MembersPageView(req.RawPath))
57}
58
59func (d *DAOPrivate) MuxProposalsPage(res *mux.ResponseWriter, req *mux.Request) {
60	res.Write(d.ProposalsPageView(req.RawPath))
61}
62
63func (d *DAOPrivate) MuxConfigPage(res *mux.ResponseWriter, req *mux.Request) {
64	res.Write(d.ConfigPageView())
65}
66
67func (d *DAOPrivate) MuxProposalHistoryPage(res *mux.ResponseWriter, req *mux.Request) {
68	res.Write(d.ProposalHistoryPageView(req.RawPath))
69}
70
71func (d *DAOPrivate) MuxMemberDetailPage(res *mux.ResponseWriter, req *mux.Request) {
72	res.Write(d.MemberDetailPageView(req.GetVar("address")))
73}
74
75func (d *DAOPrivate) MuxProposalDetailPage(res *mux.ResponseWriter, req *mux.Request) {
76	id, err := strconv.ParseUint(req.GetVar("id"), 10, 64)
77	if err != nil {
78		panic(err)
79	}
80	res.Write(d.ProposalDetailPageView(id))
81}
82
83func (d *DAOPrivate) MuxRoleDetailPage(res *mux.ResponseWriter, req *mux.Request) {
84	res.Write(d.RoleDetailPageView(req.RawPath, req.GetVar("name")))
85}
86
87// UTILITIES
88
89func getLinkPath(pkgPath string) string {
90	slashIdx := strings.IndexRune(pkgPath, '/')
91	if slashIdx != 1 {
92		return pkgPath[slashIdx:]
93	}
94	return ""
95}