Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sorting the message box #292

Open
wants to merge 4 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 0 additions & 37 deletions controllers/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,43 +55,6 @@ func (ic *IndexController) Get(w http.ResponseWriter, r *http.Request) {
ic.render(w, data)
}

// Post handles the HTTP POST request r to the index page that sends a message. Writes to w.
func (ic *IndexController) Post(w http.ResponseWriter, r *http.Request) {
// Parse message from request
r.ParseForm()
msg := r.Form.Get("message")

// Get all the data for the webpage
model := models.NewIndexModel(ic.session)

currentAndNext, banners, teams, podcasts, showOnAir, err := model.Get()

if err != nil {
log.Println(err)
return
}

data := RenderData{
CurrentAndNext: currentAndNext,
Banners: banners,
Teams: teams,
Podcasts: podcasts,
ShowOnAir: showOnAir,
MsgBoxError: false,
}

// Create the message model and send the message
msgmodel := models.NewMessageModel(ic.session)
err = msgmodel.Put(msg)
if err != nil {
// Set prompt if send fails
data.MsgBoxError = true
}

ic.render(w, data)

}

func (ic *IndexController) render(w http.ResponseWriter, data RenderData) {
// Render page
err := utils.RenderTemplate(w, ic.config.PageContext, data, "index.tmpl", "elements/current_and_next.tmpl", "elements/banner.tmpl", "elements/message_box.tmpl", "elements/istorn2020_countdown.tmpl")
Expand Down
13 changes: 13 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module github.com/UniversityRadioYork/2016-site

go 1.16

require (
github.com/BurntSushi/toml v0.3.1 // indirect
github.com/UniversityRadioYork/myradio-go v0.0.0-20210215224935-d88a7c80bfff // indirect
github.com/codegangsta/negroni v1.0.0 // indirect
github.com/gedex/inflector v0.0.0-20170307190818-16278e9db813 // indirect
github.com/gorilla/mux v1.8.0 // indirect
github.com/stretchr/graceful v1.2.15 // indirect
golang.org/x/net v0.0.0-20210505214959-0714010a04ed // indirect
)
19 changes: 19 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/UniversityRadioYork/myradio-go v0.0.0-20210215224935-d88a7c80bfff h1:9a8Sid4yAYQwi7eNB294ZsuDY4j6CGPNbOkFuR33crA=
github.com/UniversityRadioYork/myradio-go v0.0.0-20210215224935-d88a7c80bfff/go.mod h1:iFH6u3KFaQ73MR9bfqTThGd7TFUYUe9cAajxxh9E0Z8=
github.com/codegangsta/negroni v1.0.0 h1:+aYywywx4bnKXWvoWtRfJ91vC59NbEhEY03sZjQhbVY=
github.com/codegangsta/negroni v1.0.0/go.mod h1:v0y3T5G7Y1UlFfyxFn/QLRU4a2EuNau2iZY63YTKWo0=
github.com/gedex/inflector v0.0.0-20170307190818-16278e9db813 h1:Uc+IZ7gYqAf/rSGFplbWBSHaGolEQlNLgMgSE3ccnIQ=
github.com/gedex/inflector v0.0.0-20170307190818-16278e9db813/go.mod h1:P+oSoE9yhSRvsmYyZsshflcR6ePWYLql6UU1amW13IM=
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/stretchr/graceful v1.2.15 h1:vmXbwPGfe8bI6KkgmHry/P1Pk63bM3TDcfi+5mh+VHg=
github.com/stretchr/graceful v1.2.15/go.mod h1:IxdGAOTZueMKoBr3oJIzdeg5CCCXbHXfV44sLhfAXXI=
golang.org/x/net v0.0.0-20210505214959-0714010a04ed h1:V9kAVxLvz1lkufatrpHuUVyJ/5tR3Ms7rk951P4mI98=
golang.org/x/net v0.0.0-20210505214959-0714010a04ed/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
24 changes: 0 additions & 24 deletions models/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,27 +55,3 @@ func (m *IndexModel) Get() (currentAndNext *myradio.CurrentAndNext, banners []my

return
}

// MessageModel is the model that takes the response from sending a message
type MessageModel struct {
Model
}

// NewMessageModel returns a new MessageModel
func NewMessageModel(s *myradio.Session) *MessageModel {
// @TODO: Pass in the config options
return &MessageModel{Model{session: s}}
}

// Put PUTs the given message to the current show
//
// On success, it returns nil
// Otherwise, it returns the error causing failure.
func (m *MessageModel) Put(msg string) (err error) {
currentTimeslot, err := m.session.GetCurrentTimeslot()
if err != nil {
return
}
err = m.session.PutMessage(currentTimeslot.TimeslotID, msg)
return
}
32 changes: 32 additions & 0 deletions public/js/messageBox.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const sendButton = document.getElementById("messagesend");
const messageBox = document.getElementById("comments");

sendButton.onclick = () => {
if (messageBox.value != "") {
fetch(
"https://ury.org.uk/api/v2/timeslot/currentandnext/?api_key=" + MyRadioAPIKey
).then(res => res.json()).then(data => {
var currentTimeslot = data.payload.current.id;
fetch(
`https://ury.org.uk/api/v2/timeslot/${currentTimeslot}/sendmessage?api_key=${MyRadioAPIKey}`, {
method: 'put',
body: `message=${messageBox.value}`
}
).then(() => {
messageBox.value = "";
updateMessageboxCharacterCount();
})

// Update the Button
sendButton.innerText = "Message Sent!";
sendButton.classList.remove("btn-primary");
sendButton.classList.add("btn-success");
setTimeout(() => {
sendButton.innerText = "Send Message";
sendButton.classList.remove("btn-success");
sendButton.classList.add("btn-primary");
}, 3000);
})

}
}
29 changes: 13 additions & 16 deletions views/elements/message_box.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,19 @@
{{with .}}
<h2>Send a Message</h2>
<hr>
<form id="studiomessage" action="/" method="post">
<textarea class="form-control" id="comments" name="message" rows="4" maxlength="1000" placeholder=
{{if .MsgBoxError}}
"Message cannot be sent at this time, please try again later."
{{else if not .ShowOnAir}}
"No shows are on air right now, so there's no-one to send a message to." disabled
{{else}}
"Why not send a message to the studio presenters? Have a bit of banter, share your opinions or simply request your favourite song?"
{{end}}></textarea>
<p id="charcount" class="text-muted my-1"></p>
<button id="messagesend" class="btn btn-primary btn-block" type="submit" value="SEND MESSAGE"
{{if not .ShowOnAir}}
disabled
{{end}}
>Send Message</button>
</form>
<textarea class="form-control" id="comments" name="message" rows="4" maxlength="1000" placeholder=
{{if not .ShowOnAir}}
"No shows are on air right now, so there's no-one to send a message to." disabled
{{else}}
"Why not send a message to the studio presenters? Have a bit of banter, share your opinions or simply request your favourite song?"
{{end}}></textarea>
<p id="charcount" class="text-muted my-1"></p>
<button id="messagesend" class="btn btn-primary btn-block" value="SEND MESSAGE"
{{if not .ShowOnAir}}
disabled
{{end}}
>Send Message</button>
<p class="pt-1 mb-0">You can also text on <a href="sms:+447851101313">07851 101 313</a></p>
{{end}}
<script src='{{url "/js/messageBox.js"}}'></script>
{{end}}