Skip to content

Commit

Permalink
[add] Add batch mark leave function
Browse files Browse the repository at this point in the history
  • Loading branch information
kennhung committed Aug 27, 2019
1 parent 4722c84 commit 074cd67
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 1 deletion.
24 changes: 23 additions & 1 deletion templates/meetings_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ <h4>Participants</h4>
<div class="row mb-3">
{{if and .IsAdmin (not .MeetingFinished)}}
<div class="btn-bar ml-3">
<button class="btn btn-sm btn-outline-info">Mark leave</button>
<button class="btn btn-sm btn-outline-info" id="markLeaveBtn">Mark leave</button>
</div>
{{end}}
</div>
Expand Down Expand Up @@ -192,6 +192,28 @@ <h5 class="modal-title">Participant Detail</h5>
});
}
});

$("#markLeaveBtn").on('click', function () {
let checked = [];
$("input[name='participant[]']").each(function () {
if ($(this).prop("checked")) {
checked.push($(this).val());
}
});

let req = new XMLHttpRequest();
req.open("POST", "/meeting/participant/leave/batch/{{.Meeting.MeetId}}");
req.setRequestHeader("Content-Type", "application/json");
req.send(JSON.stringify(checked));

req.onload = function () {
if(req.status === 204){
window.location.reload();
} else {
console.log(req.response);
}
}
});
</script>
{{end}}

Expand Down
46 changes: 46 additions & 0 deletions web/meetings.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package web

import (
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"github.com/Team6083/OverHours/models"
Expand Down Expand Up @@ -350,6 +351,51 @@ func (web *Web) MeetingParticipantLeaveGET(w http.ResponseWriter, r *http.Reques
http.Redirect(w, r, fmt.Sprintf("/meeting/detail/%s", meetId), http.StatusSeeOther)
}

func (web *Web) MeetingParticipantLeaveBatchPOST(w http.ResponseWriter, r *http.Request) {
sessionUser := r.Context().Value("user").(*models.User)

if !sessionUser.CheckPermissionLevel(models.PermissionAdmin) {
web.handle403(w, r)
return
}

decoder := json.NewDecoder(r.Body)
var data []string
err := decoder.Decode(&data)
if err != nil {
handleWebErr(w, err)
return
}

vars := mux.Vars(r)
meetId := vars["meetId"]

meeting, err := web.database.GetMeetingByMeetId(meetId)
if err != nil {
handleWebErr(w, err)
return
}

participants := meeting.Participants

for _, v := range data {
if _, ok := participants[v]; ok {
participant := participants[v]
participant.Leave = true
participants[v] = participant
}
}

meeting.Participants = participants
_, err = web.database.SaveMeeting(meeting)
if err != nil {
handleWebErr(w, err)
return
}

w.WriteHeader(http.StatusNoContent)
}

func (web *Web) MeetingParticipantDeleteLogGET(w http.ResponseWriter, r *http.Request) {
sessionUser := r.Context().Value("user").(*models.User)

Expand Down
1 change: 1 addition & 0 deletions web/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func (web *Web) GetPageInfos() []PageInfo {
pages = append(pages, PageInfo{"/meeting/checkin/{meetId}", web.MeetingCheckinGET, "GET", PageLogin, true})
pages = append(pages, PageInfo{"/meeting/checkin/{meetId}/{userId}", web.MeetingCheckinGET, "GET", PageLogin, true})
pages = append(pages, PageInfo{"/meeting/participant/leave/{meetId}/{userId}", web.MeetingParticipantLeaveGET, "GET", PageLogin, true})
pages = append(pages, PageInfo{"/meeting/participant/leave/batch/{meetId}", web.MeetingParticipantLeaveBatchPOST, "POST", PageLeader, true})
pages = append(pages, PageInfo{"/meeting/participant/deleteLog/{meetId}/{userId}", web.MeetingParticipantDeleteLogGET, "GET", PageLogin, true})
pages = append(pages, PageInfo{"/meeting/participant/delete/{meetId}/{userId}", web.MeetingParticipantDeleteGET, "GET", PageLogin, true})
pages = append(pages, PageInfo{"/meeting/form", web.MeetingFormGET, "GET", PageLeader, true})
Expand Down

0 comments on commit 074cd67

Please sign in to comment.