From 303a0e985b5f00653d94296b8970e585e55e9e11 Mon Sep 17 00:00:00 2001 From: Henry Oswald Date: Thu, 27 Feb 2014 17:35:35 +0000 Subject: [PATCH] improved the publish button process so it talks to the templates-api and lets the user know what is going on --- .../Templates/TemplatesController.coffee | 4 ++ .../Templates/TemplatesPublisher.coffee | 10 +++ services/web/app/coffee/router.coffee | 4 ++ services/web/app/views/templates.jade | 15 +++-- .../ProjectMembersManager.coffee | 63 ++++++++++++++++--- .../Templates/TemplatesControllerTests.coffee | 17 ++++- .../Templates/TemplatesPublisherTests.coffee | 24 ++++++- 7 files changed, 121 insertions(+), 16 deletions(-) diff --git a/services/web/app/coffee/Features/Templates/TemplatesController.coffee b/services/web/app/coffee/Features/Templates/TemplatesController.coffee index 75b667d6e9..2b720e58a6 100644 --- a/services/web/app/coffee/Features/Templates/TemplatesController.coffee +++ b/services/web/app/coffee/Features/Templates/TemplatesController.coffee @@ -37,6 +37,10 @@ module.exports = logger.log user_id:user_id, project_id:project_id, "reciving request to unpublish project as template" TemplatesPublisher.unpublish user_id, project_id, callback + getTemplateDetails: (user_id, project_id, callback)-> + TemplatesPublisher.getTemplateDetails user_id, project_id, callback + + setCompiler = (project_id, compiler, callback)-> if compiler? ProjectOptionsHandler.setCompiler project_id, compiler, callback diff --git a/services/web/app/coffee/Features/Templates/TemplatesPublisher.coffee b/services/web/app/coffee/Features/Templates/TemplatesPublisher.coffee index b1b57dd2af..64c42054be 100644 --- a/services/web/app/coffee/Features/Templates/TemplatesPublisher.coffee +++ b/services/web/app/coffee/Features/Templates/TemplatesPublisher.coffee @@ -17,5 +17,15 @@ module.exports = callback() + getTemplateDetails: (user_id, project_id, callback)-> + url = buildUrl(user_id, project_id)+"/details" + request.get url, (err, res, body)-> + try + json = JSON.parse body + catch err + return callback err + callback(err, json) + + buildUrl = (user_id, project_id)-> url = "#{settings.apis.templates_api.url}/templates-api/user/#{user_id}/project/#{project_id}" diff --git a/services/web/app/coffee/router.coffee b/services/web/app/coffee/router.coffee index 8e9e8d4a3d..c724173e3d 100644 --- a/services/web/app/coffee/router.coffee +++ b/services/web/app/coffee/router.coffee @@ -358,3 +358,7 @@ module.exports = class Router client.on "getLastTimePollHappned", (callback)-> EditorController.getLastTimePollHappned(callback) + + client.on "getPublishedDetails", (user_id, callback)-> + AuthorizationManager.ensureClientCanViewProject client, (error, project_id) => + TemplatesController.getTemplateDetails user_id, project_id, callback diff --git a/services/web/app/views/templates.jade b/services/web/app/views/templates.jade index 480b7ee8f6..5e1865b2b3 100644 --- a/services/web/app/views/templates.jade +++ b/services/web/app/views/templates.jade @@ -289,18 +289,25 @@ |   Share URL - script(type="text/template")#publishProject + script(type="text/template")#publishProjectTemplate -if(session && session.user && session.user.isAdmin) .box .page-header h2 Publish Project + #publishedAsTemplateArea(style="display:none;") + a#templateLink(href='{{canonicalUrl}}') View Template + .btn.btn-warning#unPublishProjectAsTemplate unpublish project as template + #problemWithPublishingArea(style="display:none;") + p There is a problem with our publishing service, please try again in a few minutes. + #publishWorkingArea(style="display:none;") + p Working..... + #unpublishedAsTemplateArea(style="display:none;") + .btn.btn-success#publishProjectAsTemplate Publish project as template div - .btn#publishProjectAsTemplate Publish project as template - .btn#unPublishProjectAsTemplate unpublish project as template - .row textarea.span6#projectDescription {{description}} + script(type="text/template")#settingsPanelTemplate .fullEditorArea.projectSettings include project/partials/manage diff --git a/services/web/public/coffee/project-members/ProjectMembersManager.coffee b/services/web/public/coffee/project-members/ProjectMembersManager.coffee index 9181c83466..e8ef12ae78 100644 --- a/services/web/public/coffee/project-members/ProjectMembersManager.coffee +++ b/services/web/public/coffee/project-members/ProjectMembersManager.coffee @@ -167,7 +167,7 @@ define [ @options.manager.removeMember(@model) PublishProjectView = Backbone.View.extend - template: $("#publishProject").html() + template: $("#publishProjectTemplate").html() events: "click #publishProjectAsTemplate": "publishProjectAsTemplate" @@ -181,18 +181,63 @@ define [ this.model.bind('change', this.render) render: -> - viewModel = description:@model.get("description") + viewModel = + description: @model.get("description") + canonicalUrl: @model.get("template.canonicalUrl") + isPublished: @model.get("template.isPublished") + $(@el).html $(Mustache.to_html(@template, viewModel)) + @publishedArea = $('#publishedAsTemplateArea') + @unpublishedArea = $('#unpublishedAsTemplateArea') + @refreshPublishStatus() + + + refreshPublishStatus: -> + @ide.socket.emit "getPublishedDetails", @ide.user.get("id"), (err, details)=> + if err? + return @showError() + + @model.set("template.isPublished", details.exists) + if details.exists + @model.set("template.canonicalUrl", details.canonicalUrl) + @publishedArea.show() + @unpublishedArea.hide() + else + @publishedArea.hide() + @unpublishedArea.show() + + showError: -> + $('#problemWithPublishingArea').show() + + showWorking: -> + $('#publishWorkingArea').show() + + hideWorking: -> + $('#publishWorkingArea').hide() + + publishProjectAsTemplate: -> + @showWorking() + @unpublishedArea.hide() + @ide.socket.emit "publishProjectAsTemplate", @ide.user.get("id"), (err)=> + @hideWorking() + if err? + @showError() + else + @refreshPublishStatus() + + unPublishProjectAsTemplate: -> + @showWorking() + @publishedArea.hide() + @ide.socket.emit "unPublishProjectAsTemplate", @ide.user.get("id"), (err)=> + @hideWorking() + if err? + @showError() + else + @refreshPublishStatus() updateDescription: -> newDescription = $('#projectDescription').val() - this.model.set("description", newDescription) - - publishProjectAsTemplate: -> - @ide.socket.emit "publishProjectAsTemplate", @ide.user.get("id"), -> - - unPublishProjectAsTemplate: -> - @ide.socket.emit "unPublishProjectAsTemplate", @ide.user.get("id"), -> + @model.set("description", newDescription) SocialSharingView = Backbone.View.extend template: $("#socialSharingTemplate").html() diff --git a/services/web/test/UnitTests/coffee/Templates/TemplatesControllerTests.coffee b/services/web/test/UnitTests/coffee/Templates/TemplatesControllerTests.coffee index 744d8c7996..61cd8d3543 100644 --- a/services/web/test/UnitTests/coffee/Templates/TemplatesControllerTests.coffee +++ b/services/web/test/UnitTests/coffee/Templates/TemplatesControllerTests.coffee @@ -24,6 +24,7 @@ describe 'Templates Controller', -> @TemplatesPublisher = publish: sinon.stub() unpublish:sinon.stub() + getTemplateDetails: sinon.stub() @controller = SandboxedModule.require modulePath, requires: '../Uploads/ProjectUploadManager':@ProjectUploadManager '../Project/ProjectOptionsHandler':@ProjectOptionsHandler @@ -132,4 +133,18 @@ describe 'Templates Controller', -> @controller.createProjectFromZipTemplate @req, res - describe '', -> + describe 'getTemplateDetails', -> + + it "should return an error the templatePublisher", (done)-> + error = "error" + @TemplatesPublisher.getTemplateDetails.callsArgWith(2, error) + @controller.getTemplateDetails @user_id, @project_id, (passedError)=> + passedError.should.equal error + done() + + it "should return the details", (done)-> + details = {exists:true} + @TemplatesPublisher.getTemplateDetails.callsArgWith(2, null, details) + @controller.getTemplateDetails @user_id, @project_id, (err, passedDetails)=> + details.should.equal passedDetails + done() diff --git a/services/web/test/UnitTests/coffee/Templates/TemplatesPublisherTests.coffee b/services/web/test/UnitTests/coffee/Templates/TemplatesPublisherTests.coffee index a279dd0764..b97c124e2d 100644 --- a/services/web/test/UnitTests/coffee/Templates/TemplatesPublisherTests.coffee +++ b/services/web/test/UnitTests/coffee/Templates/TemplatesPublisherTests.coffee @@ -4,7 +4,7 @@ assert = require('assert') path = require('path') sinon = require('sinon') modulePath = path.join __dirname, '../../../../app/js/Features/Templates/TemplatesPublisher' - +expect = require("chai").expect describe 'Templates publish', -> @@ -12,6 +12,7 @@ describe 'Templates publish', -> @request = post: sinon.stub().callsArgWith(1) del: sinon.stub().callsArgWith(1) + get: sinon.stub() @settings = apis: templates_api: @@ -39,4 +40,23 @@ describe 'Templates publish', -> @TemplatesPublisher.unpublish @user_id, @project_id, => uri = "#{@settings.apis.templates_api.url}/templates-api/user/#{@user_id}/project/#{@project_id}" @request.del.calledWith(uri).should.equal true - done() \ No newline at end of file + done() + + + describe "getTemplateDetails", -> + it "should make a get request to templates api", (done)-> + body = + exists:true + @request.get.callsArgWith(1, null, null, JSON.stringify(body)) + @TemplatesPublisher.getTemplateDetails @user_id, @project_id, (err, details)=> + uri = "#{@settings.apis.templates_api.url}/templates-api/user/#{@user_id}/project/#{@project_id}/details" + @request.get.calledWith(uri).should.equal true + assert.deepEqual details, body + done() + + + it "should catch an error thrown from trying to parse bad json", (done)-> + @request.get.callsArgWith(1, null, null, "") + @TemplatesPublisher.getTemplateDetails @user_id, @project_id, (err, details)=> + expect(err).to.exist + done()