ref: e7a68f17cbe9bc49fb1b3d719f51e6d758030aae
parent: f16d4e23961408d0167dbf1818400bf554be56c3
author: halfwit <michaelmisch1985@gmail.com>
date: Fri Nov 8 12:16:12 PST 2019
Move more things to plugins
--- a/pages/doctor/application.go
+++ b/pages/doctor/application.go
@@ -12,7 +12,7 @@
CSS: "",
Path: "doctor/application",
Data: Application,
- Extra: router.ListCountries | plugins.ListServices | router.FormErrors | router.FormToken,
+ Extra: plugins.ListCountries | plugins.ListServices | router.FormErrors | router.FormToken,
}
router.AddPage(b)
}
--- a/plugins/countries.go
+++ b/plugins/countries.go
@@ -4,10 +4,14 @@
"sort"
"strings"
+ "github.com/olmaxmedical/olmax_go/router"
"github.com/pariz/gountries"
"golang.org/x/text/message"
)
+// ListCountries - Populate a localized spinner to select country
+const ListCountries router.IncludeExtra = 4
+
// Country - Mapping token to internationalized country code
type Country struct {
ID string
@@ -30,6 +34,12 @@
list: l,
}
sort.Sort(cache)
+ b := &router.Plugin{
+ Name: "countrylist",
+ Run: Countries,
+ Validate: CheckCountries,
+ }
+ router.AddPlugin(b, ListCountries)
}
// Len - For Sort implementation
@@ -54,16 +64,18 @@
c.list[j] = tmp
}
-// TODO: Filter out any countries we don't support
-func listcountries() []Country {
- var c []Country
+// Countries - return a localized list of countries
+func Countries(_ *message.Printer) map[string]interface{} {
+ c := make(map[string]interface{})
for _, item := range cache.list {
- c = append(c, Country{
- ID: item.Name.Common,
- Name: item.Name.Common,
- })
+ c[item.Name.Common] = item.Name.Common
}
return c
+}
+
+// CheckCountries - no-op
+func CheckCountries() error {
+ return nil
}
func validateCountries(p *message.Printer, countries []string) string {
--- a/plugins/doctor.go
+++ b/plugins/doctor.go
@@ -6,7 +6,7 @@
)
// ListDoctors - Bitmask to list doctors of in client country
-const ListDoctors router.IncludeExtra = 2
+const ListDoctors router.IncludeExtra = 3
type doctor struct {
Image string
--- a/plugins/specialties.go
+++ b/plugins/specialties.go
@@ -6,7 +6,7 @@
)
// ListServices - Bitmask to list services in native language
-const ListServices router.IncludeExtra = 1
+const ListServices router.IncludeExtra = 2
func init() {
b := &router.Plugin{
--- a/router/pages.go
+++ b/router/pages.go
@@ -34,8 +34,7 @@
type IncludeExtra uint32
const (
- ListCountries IncludeExtra = 1 << iota
- FormToken
+ FormToken IncludeExtra = 1 << iota
FormErrors
SessionToken
)
@@ -99,40 +98,36 @@
r["header"] = header(p.printer, p.status)
r["footer"] = footer(p.printer)
r["basedir"] = getBaseDir(cache.Path)
- var i IncludeExtra
- for i = 0; i < Nplugin; i++ {
- if (cache.Extra & i) != 0 {
- r[pluginCache[i].Name] = pluginCache[i].Run(p.printer)
+ // comparing an int against cache.Extra is not useful, we need an array of keys instead set at AddPlugin.
+ for _, key := range pluginKey {
+ if (cache.Extra & key) != 0 {
+ r[pluginCache[key].Name] = pluginCache[key].Run(p.printer)
}
}
- /*
- if p.session != nil && cache.Extra&FormErrors != 0 {
- i["errors"] = p.session.Get("errors")
- }
- if p.session != nil && cache.Extra&SessionToken != 0 {
- // TODO(halfwit) once database is live
- // session token is a temporary db entry
- // keyed by token which returns the current session data at this point
- // useful for things like offers while a patient fills out symptoms
- //i["sessiontoken"] = db.SetData("offer", p.sessionsomething)
- }
- //if cache.Extra&ClientName != 0 {
- // i["firstname"] = db.ClientName(p.session)
- //}
- //if cache.Extra&ClientSurname != 0 {
- // i["surname"] = db.ClientSurname(p.session)
- //}
- //if cache.Extra&ClientUsername != 0 {
- // i["username"] = db.ClientUsername(p.session)
- //}
- if cache.Extra&FormErrors != 0 && p.session != nil {
- i["errors"] = p.session.Get("errors")
- }
- if cache.Extra&FormToken != 0 {
- // generate token and put it in the form!
- i["token"] = newToken()
- }
- */
+
+ if p.session != nil && cache.Extra&FormErrors != 0 {
+ r["errors"] = p.session.Get("errors")
+ }
+ if p.session != nil && cache.Extra&SessionToken != 0 {
+ // TODO(halfwit) once database is live
+ // session token is a temporary db entry
+ // keyed by token which returns the current session data at this point
+ // useful for things like offers while a patient fills out symptoms
+ //i["sessiontoken"] = db.SetData("offer", p.sessionsomething)
+ }
+ //if cache.Extra&ClientName != 0 {
+ // i["firstname"] = db.ClientName(p.session)
+ //}
+ //if cache.Extra&ClientSurname != 0 {
+ // i["surname"] = db.ClientSurname(p.session)
+ //}
+ //if cache.Extra&ClientUsername != 0 {
+ // i["username"] = db.ClientUsername(p.session)
+ //}
+ if cache.Extra&FormErrors != 0 && p.session != nil {
+ r["errors"] = p.session.Get("errors")
+ }
+
if p.session != nil {
r["username"] = p.session.Get("username")
}
--- a/router/plugins.go
+++ b/router/plugins.go
@@ -6,14 +6,16 @@
"golang.org/x/text/message"
)
+// PluginMask - (Must be unique) ID for a plugin
+// Once everything is moved to plugins, remove IncludeExtra in favour of PluginMask
+type PluginMask uint32
+
// DEAD is a magic string to indicate a non-unique plugin key
const DEAD IncludeExtra = 0x0666000
var pluginCache map[IncludeExtra]*Plugin
+var pluginKey []IncludeExtra
-// Nplugin - Number of current plugins
-var Nplugin IncludeExtra
-
// Plugin - Provide extra data or functionality from GET/POST pages
type Plugin struct {
Name string
@@ -35,18 +37,19 @@
if err != nil {
errs = append(errs, err)
}
- if (DEAD & key) != 0 {
- errs = append(errs, fmt.Errorf("Error registering %s: Key requested already in use (%d)", item.Name, key^DEAD))
+ if (key & DEAD) != 0 {
+ errs = append(errs, fmt.Errorf("Error registering %s: Key requested already in use", item.Name))
}
}
- Nplugin = IncludeExtra(len(pluginCache))
return errs
}
// AddPlugin - Add Plugin to map by key
func AddPlugin(p *Plugin, key IncludeExtra) {
+
if pluginCache[key] != nil {
key |= DEAD
}
+ pluginKey = append(pluginKey, key)
pluginCache[key] = p
}
--- a/templates/doctor/application.tpl
+++ b/templates/doctor/application.tpl
@@ -7,15 +7,15 @@
<fieldset>
<label for="country">{{.doccountry}}</label>
<select name="country" id="country" multiple required>
- {{range .countrylist}}
- <option value="{{.ID}}">{{.Name}}</option>
+ {{range $id, $name := .countrylist}}
+ <option value="{{$id}}">{{$name}}</option>
{{end}}
</select>
<br><!-- clean up the formatting on some browsers -->
<label for="specialty">{{.docspecial}}</label>
<select name="specialty" id="specialty" multiple required>
- {{range .specialties}}
- <option value="{{.ID}}">{{.Name}}</option>
+ {{range $id, $name := .specialties}}
+ <option value="{{$id}}">{{$name}}</option>
{{end}}
</select>
</fieldset>
--- a/templates/help/provider.tpl
+++ b/templates/help/provider.tpl
@@ -5,8 +5,8 @@
<form method="post">
<p>{{.specialtyHeader}}</p>
<select name="specialty">
- {{range .specialties}}
- <option value="{{.ID}}">{{.Name}}</option>
+ {{range $id, $name := .specialties}}
+ <option value="{{$id}}">{{$name}}</option>
{{end}}
</select>
<button>{{.getStartedHeader}}</button>
--- a/templates/patient/offer.tpl
+++ b/templates/patient/offer.tpl
@@ -4,8 +4,8 @@
<form action="findspecialty">
<label for="Specialty">{{.specialty}}</label>
<select name="Specialty">
- {{range .specialties}}
- <option value="{{.ID}}">{{.Name}}</option>
+ {{range $id, $name := .specialties}}
+ <option value="{{$id}}">{{$name}}</option>
{{end}}
</select>
<br/>