parcoursmob/renderer/journeys.go

151 lines
4.6 KiB
Go
Raw Normal View History

package renderer
2022-12-07 10:12:43 +00:00
import (
"encoding/json"
"html/template"
"net/http"
groupstorage "git.coopgo.io/coopgo-platform/groups-management/storage"
mobilityaccountsstorage "git.coopgo.io/coopgo-platform/mobility-accounts/storage"
)
const journeysMenu = "journeys"
2022-12-07 10:12:43 +00:00
type BeneficiariesCovoiturage struct {
Group string `json:"group"`
Count int `json:"count"`
CacheId string `json:"cache_id"`
Beneficiaries []any `json:"beneficiaries"`
}
type BeneficiariesCovoiturageA struct {
Count int `json:"count"`
CacheId string `json:"cache_id"`
Beneficiaries []any `json:"beneficiaries"`
}
func (s BeneficiariesCovoiturage) JSON() template.JS {
result, _ := json.Marshal(s)
return template.JS(result)
}
func (s BeneficiariesCovoiturage) JSONWithLimits(a int, b int) template.JS {
if b < len(s.Beneficiaries) {
s.Beneficiaries = s.Beneficiaries[a:b]
}
return s.JSON()
}
func (renderer *Renderer) JourneysSearch(w http.ResponseWriter, r *http.Request, carpools any, transitjourneys any, vehicles []any, searched bool, departure any, destination any, departuredate string, departuretime string) {
files := renderer.ThemeConfig.GetStringSlice("views.journeys.search.files")
state := NewState(r, renderer.ThemeConfig, journeysMenu)
state.ViewState = map[string]any{
"searched": searched,
"departuredate": departuredate,
"departuretime": departuretime,
"departure": departure,
"destination": destination,
"journeys": transitjourneys,
"carpools": carpools,
"vehicles": vehicles,
}
renderer.Render("journeys", w, r, files, state)
}
2022-12-07 10:12:43 +00:00
type BeneficiariesListstate struct {
Count int `json:"count"`
CacheId string `json:"cache_id"`
Beneficiaries []groupstorage.Group `json:"beneficiaries"`
}
func (s BeneficiariesListstate) JSON() template.JS {
result, _ := json.Marshal(s)
return template.JS(result)
}
func (s BeneficiariesListstate) JSONWithLimits(a int, b int) template.JS {
if b < len(s.Beneficiaries) {
s.Beneficiaries = s.Beneficiaries[a:b]
}
return s.JSON()
}
func (renderer *Renderer) GroupsGestion(w http.ResponseWriter, r *http.Request, groups []groupstorage.Group, cacheid string) {
files := renderer.ThemeConfig.GetStringSlice("views.journeys.list.files")
state := NewState(r, renderer.ThemeConfig, journeysMenu)
state.ViewState = BeneficiariesListstate{
Count: len(groups),
CacheId: cacheid,
Beneficiaries: groups,
}
renderer.Render("journeys", w, r, files, state)
}
func (renderer *Renderer) CreateGroup(w http.ResponseWriter, r *http.Request, depart any, arrive any, searched bool, beneficiary any, beneficiaries []mobilityaccountsstorage.Account, departure any, destination any) {
files := renderer.ThemeConfig.GetStringSlice("views.journeys.create.files")
state := NewState(r, renderer.ThemeConfig, journeysMenu)
viewstate := map[string]any{
"deeparture": depart,
"deestination": arrive,
"beneficiaries": beneficiaries,
"searched": searched,
"departure": departure,
"destination": destination,
}
if searched {
viewstate["search"] = map[string]any{
"beneficiary": beneficiary,
}
}
state.ViewState = viewstate
renderer.Render("journeys", w, r, files, state)
}
func (renderer *Renderer) DisplayGroupCovoiturage(w http.ResponseWriter, r *http.Request, number string, groupid string, depart any, arrive any, accounts []any, cacheid string, searched bool, beneficiary any, group any, beneficiaries []mobilityaccountsstorage.Account, groups map[string]any) {
2022-12-07 10:12:43 +00:00
files := renderer.ThemeConfig.GetStringSlice("views.journeys.display.files")
state := NewState(r, renderer.ThemeConfig, journeysMenu)
viewstate := map[string]any{
"beneficiaries": beneficiaries,
"searched": searched,
"group": group,
"deeparture": depart,
"deestination": arrive,
"groups": groups,
"ben": accounts,
"number": number,
2022-12-07 10:12:43 +00:00
"list": BeneficiariesCovoiturage{
Group: groupid,
Count: len(accounts),
CacheId: cacheid,
Beneficiaries: accounts,
},
}
if searched {
viewstate["search"] = map[string]any{
"beneficiary": beneficiary,
}
}
state.ViewState = viewstate
renderer.Render("journeys", w, r, files, state)
}
func (renderer *Renderer) UpdateGroupCovoiturage(w http.ResponseWriter, r *http.Request, groupid string, memberid string) {
files := renderer.ThemeConfig.GetStringSlice("views.journeys.update.files")
state := NewState(r, renderer.ThemeConfig, journeysMenu)
state.ViewState = map[string]any{
"groupid": groupid,
"memberid": memberid,
}
renderer.Render("journeys", w, r, files, state)
}