set votes on homepage

This commit is contained in:
Matthieu Lamalle 2019-11-27 14:42:08 +01:00
parent 1af50c36ae
commit 62a91653c3
3 changed files with 28 additions and 13 deletions

BIN
server

Binary file not shown.

View File

@ -47,7 +47,6 @@ func main() {
if err := http.ListenAndServe(conf.HTTP.Address, r); err != nil { if err := http.ListenAndServe(conf.HTTP.Address, r); err != nil {
panic(errors.Wrapf(err, "error while listening on '%s'", conf.HTTP.Address)) panic(errors.Wrapf(err, "error while listening on '%s'", conf.HTTP.Address))
} }
} }
// ServerHTTP is the entry point to all requests // ServerHTTP is the entry point to all requests
@ -104,12 +103,18 @@ func LogInPage(w http.ResponseWriter, r *http.Request) {
// HomePage is the homepage of the app // HomePage is the homepage of the app
func HomePage(w http.ResponseWriter, r *http.Request) { func HomePage(w http.ResponseWriter, r *http.Request) {
foods := foodlist.GetFoodOfTheDay() type HomeData struct {
Foods foodlist.FoodOfTheDay
Votes vote.VotesOfTheDay
}
datas := HomeData{foodlist.GetFoodOfTheDay(), vote.GetVotesOfTheDay()}
paths := []string{ paths := []string{
"./templates/index.tmpl", "./templates/index.tmpl",
} }
t := template.Must(template.New("index.tmpl").ParseFiles(paths...)) t := template.Must(template.New("index.tmpl").ParseFiles(paths...))
err := t.Execute(w, foods) err := t.Execute(w, datas)
if err != nil { if err != nil {
log.Printf("\nExecute error: %v", err) log.Printf("\nExecute error: %v", err)
return return

View File

@ -7,27 +7,37 @@
<script src="https://code.jquery.com/jquery-3.1.1.min.js" integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8=" crossorigin="anonymous"></script> <script src="https://code.jquery.com/jquery-3.1.1.min.js" integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8=" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.2.13/semantic.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.2.13/semantic.min.js"></script>
<style type="text/css"> h2 { margin: 2em 0em; } .ui.container { padding-top: 5em; padding-bottom: 5em; } </style> <style type="text/css"> h2 { margin: 2em 0em; } .ui.container { padding-top: 5em; padding-bottom: 5em; } </style>
<script type="text/javascript">function vote(option){$.post( "/", {option}, ( data ) => { $('.mini.modal').modal('show');});}</script> <script type="text/javascript">
function vote(option){$.post( "/", {option}, ( data ) => { $('.modal').modal('show');});}
function reload(){document.location.reload(true);}
</script>
</head> </head>
<body> <body>
<div class="ui container"> <div class="ui container">
<h1 class="ui header">Foodoles - {{.Date}}</h1><br/> <h1 class="ui header">Foodoles - {{.Foods.Date}}</h1><br/>
<h2 class="ui header">Pick Your Preferred Lunch's Food For Today</h2><br/> <h2 class="ui header">Pick Your Preferred Lunch's Food For Today</h2><br/>
<div class="ui four column grid link cards"> <div class="ui compact menu">
{{ range .Foods }} {{ range .Foods.Foods }}
<div onclick="vote('{{ .Title }}')"> <a onclick="vote('{{ .Title }}')" class="ui item label massive">
<div class="ui massive label"> {{ .Icon }}</div> {{$cur := .Title}}
</div> {{ .Icon }}
{{ range $.Votes.Votes }}
{{ if eq .Food $cur }}
<div class="floating ui blue left label">{{ .Voices }}</div>
{{end}} {{end}}
{{end}}
</a>
{{ end }}
</div> </div>
</div> </div>
<div class="ui modal mini"> <div class="ui modal">
<div class="header">Foodoles</div> <div class="header">Foodoles</div>
<div class="content"> <div class="content">
<p>Vote sent <strong>successfully</strong>, <a href="/results"> check results !</a></p> <p>Vote sent <strong>successfully</strong></p>
</div> </div>
<div class="actions"> <div class="actions">
<div class="ui cancel button blue">Close</div> <div class="ui cancel button blue" onclick="reload()">Close</div>
</div> </div>
</div> </div>
</body> </body>