From 52d3bfada632e4c3bcb7140ae14c78207945ba4b Mon Sep 17 00:00:00 2001 From: DmitryZagorulko Date: Mon, 24 Sep 2018 17:31:31 +0300 Subject: [PATCH 1/7] improve translate, add markdowm formatting --- src/routing.go | 16 +++++++++------- translate/translate.en.yml | 2 +- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/routing.go b/src/routing.go index d2f5621..d48e4bb 100644 --- a/src/routing.go +++ b/src/routing.go @@ -587,7 +587,7 @@ func mgWebhookHandler(c *gin.Context) { var mb string switch msg.Data.Type { case v1.MsgTypeProduct: - mb = fmt.Sprintf("%s\n", msg.Data.Product.Name) + mb = fmt.Sprintf("*%s*\n", msg.Data.Product.Name) if msg.Data.Product.Cost != nil && msg.Data.Product.Cost.Value != 0 { mb += fmt.Sprintf( @@ -624,6 +624,8 @@ func mgWebhookHandler(c *gin.Context) { m.ReplyToMessageID = qid } + m.ParseMode = "Markdown" + msgSend, err := bot.Send(m) if err != nil { logger.Error(err) @@ -669,7 +671,7 @@ func mgWebhookHandler(c *gin.Context) { } func getOrderMessage(dataOrder *v1.MessageDataOrder) string { - mb := getLocalizedMessage("order") + mb := "*" + getLocalizedMessage("order") if dataOrder.Number != "" { mb += " " + dataOrder.Number @@ -678,7 +680,7 @@ func getOrderMessage(dataOrder *v1.MessageDataOrder) string { if dataOrder.Date != "" { mb += fmt.Sprintf(" (%s)", dataOrder.Date) } - mb += "\n" + mb += "*\n" if len(dataOrder.Items) > 0 { mb += "\n" for k, v := range dataOrder.Items { @@ -691,7 +693,7 @@ func getOrderMessage(dataOrder *v1.MessageDataOrder) string { if v.Quantity != nil { if v.Quantity.Value != 0 { mb += fmt.Sprintf( - " %v", + " _%v_", v.Quantity.Value, ) } @@ -700,7 +702,7 @@ func getOrderMessage(dataOrder *v1.MessageDataOrder) string { if v.Price != nil { if val, ok := currency[strings.ToLower(v.Price.Currency)]; ok { mb += fmt.Sprintf( - " x %s\n", + " _x %s_\n", getLocalizedTemplateMessage( "cost_currency", map[string]interface{}{ @@ -719,7 +721,7 @@ func getOrderMessage(dataOrder *v1.MessageDataOrder) string { if dataOrder.Delivery != nil { if dataOrder.Delivery.Name != "" { mb += fmt.Sprintf( - "\n%s:\n%s", + "\n*%s:*\n%s", getLocalizedMessage("delivery"), dataOrder.Delivery.Name, ) @@ -749,7 +751,7 @@ func getOrderMessage(dataOrder *v1.MessageDataOrder) string { if len(dataOrder.Payments) > 0 { mb += fmt.Sprintf( - "\n%s:\n", + "\n*%s:*\n", getLocalizedMessage("payment"), ) for _, v := range dataOrder.Payments { diff --git a/translate/translate.en.yml b/translate/translate.en.yml index b160a5c..e40bdb4 100644 --- a/translate/translate.en.yml +++ b/translate/translate.en.yml @@ -48,4 +48,4 @@ order: "Order" delivery: "Delivery" payment: "Payment" order_total: "Order total" -cost_currency: "{{.Amount}} {{.Currency}}" +cost_currency: "{{.Currency}}{{.Amount}}" From eff4b1e0ebebe482f6ca05b7428d9e22104c81c3 Mon Sep 17 00:00:00 2001 From: DmitryZagorulko Date: Mon, 24 Sep 2018 17:47:44 +0300 Subject: [PATCH 2/7] add bot name in channel settings request --- go.sum | 2 -- src/routing.go | 16 +++++++++++++--- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/go.sum b/go.sum index bc464c0..ae3bcb5 100644 --- a/go.sum +++ b/go.sum @@ -95,8 +95,6 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/retailcrm/api-client-go v1.0.6 h1:4Q3e4ve8GOOHIQdq3/wTGqgWuWa1cKMKqmgrTv4FoDU= github.com/retailcrm/api-client-go v1.0.6/go.mod h1:QRoPE2SM6ST7i2g0yEdqm7Iw98y7cYuq3q14Ot+6N8c= -github.com/retailcrm/mg-transport-api-client-go v1.1.9 h1:ogh5ThoqZJM5v4ZY6CqctUj01pVVHfBLXkrmX+BFjHE= -github.com/retailcrm/mg-transport-api-client-go v1.1.9/go.mod h1:AWV6BueE28/6SCoyfKURTo4lF0oXYoOKmHTzehd5vAI= github.com/retailcrm/mg-transport-api-client-go v1.1.10 h1:RR8S5NA6FPVrF6UVXaLwu/gJyKUg5aUObQ97S98M3Yc= github.com/retailcrm/mg-transport-api-client-go v1.1.10/go.mod h1:AWV6BueE28/6SCoyfKURTo4lF0oXYoOKmHTzehd5vAI= github.com/smartystreets/assertions v0.0.0-20180820201707-7c9eb446e3cf h1:6V1qxN6Usn4jy8unvggSJz/NC790tefw8Zdy6OZS5co= diff --git a/src/routing.go b/src/routing.go index d48e4bb..6d5dca3 100644 --- a/src/routing.go +++ b/src/routing.go @@ -57,11 +57,16 @@ func addBotHandler(c *gin.Context) { return } - b.Name = bot.Self.FirstName + b.Name = bot.Self.UserName conn := getConnectionById(b.ConnectionID) client := v1.New(conn.MGURL, conn.MGToken) - data, status, err := client.ActivateTransportChannel(getChannelSettings()) + channelSettings := getChannelSettings() + if b.Name != "" { + channelSettings.Name = "@" + b.Name + } + + data, status, err := client.ActivateTransportChannel(channelSettings) if status != http.StatusCreated { c.AbortWithStatusJSON(BadRequest("error_activating_channel")) logger.Error(conn.APIURL, status, err.Error(), data) @@ -367,7 +372,12 @@ func updateBots(conn *Connection, hashSettings string) { continue } - data, status, err := client.UpdateTransportChannel(getChannelSettings(bot.Channel)) + channelSettings := getChannelSettings(bot.Channel) + if bot.Name != "" { + channelSettings.Name = "@" + bot.Name + } + + data, status, err := client.UpdateTransportChannel(channelSettings) if config.Debug { logger.Infof( "updateChannelsSettings apiURL: %s, ChannelID: %d, Data: %v, Status: %d, err: %v", From a9c04bcfcff7d9bc716f269efc977954b534bf51 Mon Sep 17 00:00:00 2001 From: DmitryZagorulko Date: Mon, 24 Sep 2018 18:05:43 +0300 Subject: [PATCH 3/7] improve test --- src/routing_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/src/routing_test.go b/src/routing_test.go index e86ac04..f4b9445 100644 --- a/src/routing_test.go +++ b/src/routing_test.go @@ -58,6 +58,7 @@ func TestRouting_addBotHandler(t *testing.T) { ch := v1.Channel{ Type: "telegram", + Name: "@TestBot", Settings: v1.ChannelSettings{ SpamAllowed: false, Status: v1.Status{ From fd546a060c4312d8b77293ebc6b7fc886edf28b8 Mon Sep 17 00:00:00 2001 From: DmitryZagorulko Date: Tue, 25 Sep 2018 10:48:25 +0300 Subject: [PATCH 4/7] fix problem with empty lines --- src/routing.go | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/routing.go b/src/routing.go index 6d5dca3..ccefcb4 100644 --- a/src/routing.go +++ b/src/routing.go @@ -728,14 +728,12 @@ func getOrderMessage(dataOrder *v1.MessageDataOrder) string { } } - if dataOrder.Delivery != nil { - if dataOrder.Delivery.Name != "" { - mb += fmt.Sprintf( - "\n*%s:*\n%s", - getLocalizedMessage("delivery"), - dataOrder.Delivery.Name, - ) - } + if dataOrder.Delivery.Name != "" { + mb += fmt.Sprintf( + "\n*%s:*\n%s", + getLocalizedMessage("delivery"), + dataOrder.Delivery.Name, + ) if dataOrder.Delivery.Amount != nil { if val, ok := currency[strings.ToLower(dataOrder.Delivery.Amount.Currency)]; ok && dataOrder.Delivery.Amount.Value != 0 { From 06279fb4b4e6aa75e2e9bc07131e6fd1a1c2d826 Mon Sep 17 00:00:00 2001 From: DmitryZagorulko Date: Wed, 26 Sep 2018 17:12:35 +0300 Subject: [PATCH 5/7] update bot-api-client, replaced amount by price --- go.mod | 3 +-- go.sum | 6 ++---- src/routing.go | 22 ++++++++++++---------- 3 files changed, 15 insertions(+), 16 deletions(-) diff --git a/go.mod b/go.mod index d2d168d..b06da41 100644 --- a/go.mod +++ b/go.mod @@ -31,7 +31,6 @@ require ( github.com/jinzhu/now v0.0.0-20180511015916-ed742868f2ae // indirect github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af // indirect github.com/json-iterator/go v1.1.5 // indirect - github.com/jstemmer/go-junit-report v0.0.0-20180614143834-385fac0ced9a // indirect github.com/jtolds/gls v4.2.1+incompatible // indirect github.com/kr/pretty v0.1.0 // indirect github.com/lib/pq v1.0.0 // indirect @@ -45,7 +44,7 @@ require ( github.com/pkg/errors v0.8.0 github.com/pmezard/go-difflib v1.0.0 // indirect github.com/retailcrm/api-client-go v1.0.6 - github.com/retailcrm/mg-transport-api-client-go v1.1.10 + github.com/retailcrm/mg-transport-api-client-go v1.1.11 github.com/smartystreets/assertions v0.0.0-20180820201707-7c9eb446e3cf // indirect github.com/smartystreets/goconvey v0.0.0-20180222194500-ef6db91d284a // indirect github.com/stevvooe/resumable v0.0.0-20180830230917-22b14a53ba50 // indirect diff --git a/go.sum b/go.sum index ae3bcb5..dd1af3e 100644 --- a/go.sum +++ b/go.sum @@ -64,8 +64,6 @@ github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af h1:pmfjZENx5i github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k= github.com/json-iterator/go v1.1.5 h1:gL2yXlmiIo4+t+y32d4WGwOjKGYcGOuyrg46vadswDE= github.com/json-iterator/go v1.1.5/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= -github.com/jstemmer/go-junit-report v0.0.0-20180614143834-385fac0ced9a h1:2qq552JOlVHGYvqPc9ynBnGPDHeA7p0/QRn2NkrO8vk= -github.com/jstemmer/go-junit-report v0.0.0-20180614143834-385fac0ced9a/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU= github.com/jtolds/gls v4.2.1+incompatible h1:fSuqC+Gmlu6l/ZYAoZzx2pyucC8Xza35fpRVWLVmUEE= github.com/jtolds/gls v4.2.1+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU= github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= @@ -95,8 +93,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/retailcrm/api-client-go v1.0.6 h1:4Q3e4ve8GOOHIQdq3/wTGqgWuWa1cKMKqmgrTv4FoDU= github.com/retailcrm/api-client-go v1.0.6/go.mod h1:QRoPE2SM6ST7i2g0yEdqm7Iw98y7cYuq3q14Ot+6N8c= -github.com/retailcrm/mg-transport-api-client-go v1.1.10 h1:RR8S5NA6FPVrF6UVXaLwu/gJyKUg5aUObQ97S98M3Yc= -github.com/retailcrm/mg-transport-api-client-go v1.1.10/go.mod h1:AWV6BueE28/6SCoyfKURTo4lF0oXYoOKmHTzehd5vAI= +github.com/retailcrm/mg-transport-api-client-go v1.1.11 h1:jAIOKqkjA2r0v/V6lTHYQsD8q0lFpfpqzAffHAJlhCQ= +github.com/retailcrm/mg-transport-api-client-go v1.1.11/go.mod h1:AWV6BueE28/6SCoyfKURTo4lF0oXYoOKmHTzehd5vAI= github.com/smartystreets/assertions v0.0.0-20180820201707-7c9eb446e3cf h1:6V1qxN6Usn4jy8unvggSJz/NC790tefw8Zdy6OZS5co= github.com/smartystreets/assertions v0.0.0-20180820201707-7c9eb446e3cf/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc= github.com/smartystreets/goconvey v0.0.0-20180222194500-ef6db91d284a h1:JSvGDIbmil4Ui/dDdFBExb7/cmkNjyX5F97oglmvCDo= diff --git a/src/routing.go b/src/routing.go index ccefcb4..72f0060 100644 --- a/src/routing.go +++ b/src/routing.go @@ -145,7 +145,7 @@ func saveHandler(c *gin.Context) { if code == http.StatusInternalServerError { c.Error(err) } else { - c.AbortWithStatusJSON(BadRequest(err.Error())) + c.AbortWithStatusJSON(code, gin.H{"error": err.Error()}) } return } @@ -728,21 +728,23 @@ func getOrderMessage(dataOrder *v1.MessageDataOrder) string { } } - if dataOrder.Delivery.Name != "" { - mb += fmt.Sprintf( - "\n*%s:*\n%s", - getLocalizedMessage("delivery"), - dataOrder.Delivery.Name, - ) + if dataOrder.Delivery != nil { + if dataOrder.Delivery.Name != "" { + mb += fmt.Sprintf( + "\n*%s:*\n%s", + getLocalizedMessage("delivery"), + dataOrder.Delivery.Name, + ) + } - if dataOrder.Delivery.Amount != nil { - if val, ok := currency[strings.ToLower(dataOrder.Delivery.Amount.Currency)]; ok && dataOrder.Delivery.Amount.Value != 0 { + if dataOrder.Delivery.Price != nil { + if val, ok := currency[strings.ToLower(dataOrder.Delivery.Price.Currency)]; ok && dataOrder.Delivery.Price.Value != 0 { mb += fmt.Sprintf( "; %s", getLocalizedTemplateMessage( "cost_currency", map[string]interface{}{ - "Amount": dataOrder.Delivery.Amount.Value, + "Amount": dataOrder.Delivery.Price.Value, "Currency": val, }, ), From 73efd7209b37abab232abb0795e772b3c23073e1 Mon Sep 17 00:00:00 2001 From: DmitryZagorulko Date: Fri, 28 Sep 2018 16:06:42 +0300 Subject: [PATCH 6/7] update api-client, add disable form --- go.mod | 3 ++- go.sum | 6 ++++-- src/routing.go | 5 +++++ src/run.go | 4 ++-- src/utils.go | 1 + static/script.js | 41 ++++++++++++++++++++++++++++++++++++----- static/style.css | 11 +++++++++++ 7 files changed, 61 insertions(+), 10 deletions(-) diff --git a/go.mod b/go.mod index b06da41..e6d2520 100644 --- a/go.mod +++ b/go.mod @@ -30,6 +30,7 @@ require ( github.com/jinzhu/inflection v0.0.0-20180308033659-04140366298a // indirect github.com/jinzhu/now v0.0.0-20180511015916-ed742868f2ae // indirect github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af // indirect + github.com/joho/godotenv v1.3.0 // indirect github.com/json-iterator/go v1.1.5 // indirect github.com/jtolds/gls v4.2.1+incompatible // indirect github.com/kr/pretty v0.1.0 // indirect @@ -43,7 +44,7 @@ require ( github.com/op/go-logging v0.0.0-20160315200505-970db520ece7 github.com/pkg/errors v0.8.0 github.com/pmezard/go-difflib v1.0.0 // indirect - github.com/retailcrm/api-client-go v1.0.6 + github.com/retailcrm/api-client-go v1.0.7 github.com/retailcrm/mg-transport-api-client-go v1.1.11 github.com/smartystreets/assertions v0.0.0-20180820201707-7c9eb446e3cf // indirect github.com/smartystreets/goconvey v0.0.0-20180222194500-ef6db91d284a // indirect diff --git a/go.sum b/go.sum index dd1af3e..c459b47 100644 --- a/go.sum +++ b/go.sum @@ -62,6 +62,8 @@ github.com/jmespath/go-jmespath v0.0.0-20160202185014-0b12d6b521d8 h1:12VvqtR6Ao github.com/jmespath/go-jmespath v0.0.0-20160202185014-0b12d6b521d8/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k= github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af h1:pmfjZENx5imkbgOkpRUYLnmbU7UEFbjtDA2hxJ1ichM= github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k= +github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc= +github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg= github.com/json-iterator/go v1.1.5 h1:gL2yXlmiIo4+t+y32d4WGwOjKGYcGOuyrg46vadswDE= github.com/json-iterator/go v1.1.5/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= github.com/jtolds/gls v4.2.1+incompatible h1:fSuqC+Gmlu6l/ZYAoZzx2pyucC8Xza35fpRVWLVmUEE= @@ -91,8 +93,8 @@ github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw= github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/retailcrm/api-client-go v1.0.6 h1:4Q3e4ve8GOOHIQdq3/wTGqgWuWa1cKMKqmgrTv4FoDU= -github.com/retailcrm/api-client-go v1.0.6/go.mod h1:QRoPE2SM6ST7i2g0yEdqm7Iw98y7cYuq3q14Ot+6N8c= +github.com/retailcrm/api-client-go v1.0.7 h1:j4C2PvPUDP9nAuYWDvJPnYNpkj+LDBgn71kHvxJmSPg= +github.com/retailcrm/api-client-go v1.0.7/go.mod h1:QRoPE2SM6ST7i2g0yEdqm7Iw98y7cYuq3q14Ot+6N8c= github.com/retailcrm/mg-transport-api-client-go v1.1.11 h1:jAIOKqkjA2r0v/V6lTHYQsD8q0lFpfpqzAffHAJlhCQ= github.com/retailcrm/mg-transport-api-client-go v1.1.11/go.mod h1:AWV6BueE28/6SCoyfKURTo4lF0oXYoOKmHTzehd5vAI= github.com/smartystreets/assertions v0.0.0-20180820201707-7c9eb446e3cf h1:6V1qxN6Usn4jy8unvggSJz/NC790tefw8Zdy6OZS5co= diff --git a/src/routing.go b/src/routing.go index 72f0060..94560c6 100644 --- a/src/routing.go +++ b/src/routing.go @@ -60,6 +60,7 @@ func addBotHandler(c *gin.Context) { b.Name = bot.Self.UserName conn := getConnectionById(b.ConnectionID) client := v1.New(conn.MGURL, conn.MGToken) + client.Debug = config.Debug channelSettings := getChannelSettings() if b.Name != "" { @@ -93,6 +94,7 @@ func deleteBotHandler(c *gin.Context) { } var client = v1.New(conn.MGURL, conn.MGToken) + client.Debug = config.Debug data, status, err := client.DeactivateTransportChannel(getBotChannelByToken(b.Token)) if status > http.StatusOK { @@ -367,6 +369,7 @@ func updateBots(conn *Connection, hashSettings string) { bots := conn.getBotsByClientID() if len(bots) > 0 { client := v1.New(conn.MGURL, conn.MGToken) + client.Debug = config.Debug for _, bot := range bots { if bot.ChannelSettingsHash == hashSettings { continue @@ -432,6 +435,7 @@ func telegramWebhookHandler(c *gin.Context) { } var client = v1.New(conn.MGURL, conn.MGToken) + client.Debug = config.Debug if update.Message != nil { if update.Message.Text == "" { @@ -590,6 +594,7 @@ func mgWebhookHandler(c *gin.Context) { return } + bot.Debug = config.Debug setLocale(b.Lang) switch msg.Type { diff --git a/src/run.go b/src/run.go index 4e8e790..74fd3db 100644 --- a/src/run.go +++ b/src/run.go @@ -59,8 +59,8 @@ func setup() *gin.Engine { gin.SetMode(gin.ReleaseMode) } - r := gin.Default() - + r := gin.New() + r.Use(gin.Recovery()) if config.Debug { r.Use(gin.Logger()) } diff --git a/src/utils.go b/src/utils.go index 2fc15ac..bb93420 100644 --- a/src/utils.go +++ b/src/utils.go @@ -35,6 +35,7 @@ func GenerateToken() string { func getAPIClient(url, key string) (*v5.Client, error, int) { client := v5.New(url, key) + client.Debug = config.Debug cr, status, e := client.APICredentials() if e.RuntimeErr != nil { diff --git a/static/script.js b/static/script.js index a2c4cef..60b6e0a 100644 --- a/static/script.js +++ b/static/script.js @@ -13,9 +13,11 @@ $(document).on("change", "select", function(e) { $('#save-crm').on("submit", function(e) { e.preventDefault(); + let formData = formDataToObj($(this).serializeArray()); + disableForm($(this)); send( $(this).attr('action'), - formDataToObj($(this).serializeArray()), + formData, function (data) { sessionStorage.setItem("createdMsg", data.message); @@ -28,17 +30,26 @@ $('#save-crm').on("submit", function(e) { $("#save").on("submit", function(e) { e.preventDefault(); + let formData = formDataToObj($(this).serializeArray()); + disableForm($(this)); send( $(this).attr('action'), - formDataToObj($(this).serializeArray()), + formData, function (data) { - M.toast({html: data.message}); + M.toast({ + html: data.message, + displayLength: 1000, + completeCallback: function(){ + enableForm(); + } + }); } ) }); $("#add-bot").on("submit", function(e) { e.preventDefault(); + disableForm($(this)); send( $(this).attr('action'), { @@ -53,6 +64,7 @@ $("#add-bot").on("submit", function(e) { $("#bots tbody").append(getBotTemplate(data)); $("#token").val(""); $('select').formSelect(); + enableForm(); } ) }); @@ -61,6 +73,7 @@ $(document).on("click", ".delete-bot", function(e) { e.preventDefault(); var but = $(this); var confirmText = JSON.parse(sessionStorage.getItem("confirmText")); + but.addClass('disabled'); $.confirm({ title: false, @@ -90,6 +103,9 @@ $(document).on("click", ".delete-bot", function(e) { }, cancel: { text: confirmText["cancel"], + action: function () { + but.removeClass('disabled'); + }, }, } }); @@ -103,14 +119,17 @@ function send(url, data, callback) { success: callback, error: function (res){ if (res.status >= 400) { - M.toast({html: res.responseJSON.error}) + M.toast({ + html: res.responseJSON.error, + displayLength: 1000, + completeCallback: enableForm() + }) } } }); } function getBotTemplate(data) { - // let bot = JSON.parse(data); tmpl = ` ${data.name} @@ -180,3 +199,15 @@ $( document ).ready(function() { }, 1000); } }); + +function disableForm(elem) { + $(document).find('button.btn').addClass('disabled'); + elem.find(".material-icons").addClass('animate'); + $("form :input").find(":input").prop("disabled", true); +} + +function enableForm() { + $(document).find('button.btn').removeClass('disabled'); + $(document).find(".material-icons").removeClass('animate'); + $("form :input").prop("disabled", false); +} \ No newline at end of file diff --git a/static/style.css b/static/style.css index dcb57df..b7bc072 100644 --- a/static/style.css +++ b/static/style.css @@ -137,3 +137,14 @@ main { .footer-copyright p { color: #9e9e9e; } + +.animate { + transition: all 0.5s ease; + animation: rotate 1s linear infinite; +} + +@keyframes rotate { + from { + transform: rotate(360deg); + } +} From 304d66ee78f0edec2c266c4b71a312b70bc2fba7 Mon Sep 17 00:00:00 2001 From: DmitryZagorulko Date: Fri, 28 Sep 2018 17:06:50 +0300 Subject: [PATCH 7/7] improve makefile, time disable form --- Makefile | 1 + static/script.js | 6 ++++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 0630871..b16f23b 100644 --- a/Makefile +++ b/Makefile @@ -23,6 +23,7 @@ jenkins_test: migrate_test @echo "==> Running tests (result in test-report.xml)" @go get -v -u github.com/jstemmer/go-junit-report @go test ./... -v -cpu 2 -race | /go/bin/go-junit-report -set-exit-code > ./test-report.xml + @go mod tidy fmt: @echo "==> Running gofmt" diff --git a/static/script.js b/static/script.js index 60b6e0a..6b51181 100644 --- a/static/script.js +++ b/static/script.js @@ -122,7 +122,9 @@ function send(url, data, callback) { M.toast({ html: res.responseJSON.error, displayLength: 1000, - completeCallback: enableForm() + completeCallback: function(){ + enableForm() + } }) } } @@ -203,7 +205,7 @@ $( document ).ready(function() { function disableForm(elem) { $(document).find('button.btn').addClass('disabled'); elem.find(".material-icons").addClass('animate'); - $("form :input").find(":input").prop("disabled", true); + $("form :input").prop("disabled", true); } function enableForm() {