From f4d63ae4e3f19dd17a4e33a9eb4e75b36e3efe44 Mon Sep 17 00:00:00 2001 From: nilsding Date: Sat, 3 Jan 2015 18:41:10 +0100 Subject: [PATCH] users now can answer other people's questions --- app/assets/javascripts/question.coffee | 35 ++++++++++++++++++++++++++ app/views/question/show.html.haml | 28 +++++++++++++++------ 2 files changed, 56 insertions(+), 7 deletions(-) create mode 100644 app/assets/javascripts/question.coffee diff --git a/app/assets/javascripts/question.coffee b/app/assets/javascripts/question.coffee new file mode 100644 index 00000000..b788b51b --- /dev/null +++ b/app/assets/javascripts/question.coffee @@ -0,0 +1,35 @@ +$(document).on "keydown", "textarea#q-answer", (evt) -> + qid = $(this)[0].dataset.id + if evt.keyCode == 13 and evt.ctrlKey + # trigger warning: + $("button#q-answer[data-q-id=#{qid}]").trigger 'click' + + +$(document).on "click", "button#q-answer", -> + btn = $(this) + btn.button "loading" + qid = btn[0].dataset.qId + $("textarea#q-answer[data-q=#{qid}]").attr "readonly", "readonly" + + shareTo = [] + ($ "input[type=checkbox][name=share][data-q-id=#{qid}]:checked").each (i, share) -> + shareTo.push share.dataset.service + + $.ajax + url: '/ajax/answer' + type: 'POST' + data: + id: qid + answer: $("textarea#q-answer[data-id=#{qid}]").val() + share: JSON.stringify shareTo + inbox: false + success: (data, status, jqxhr) -> + if data.success + $("div#q-answer-box").slideUp() + showNotification data.message, data.success + error: (jqxhr, status, error) -> + console.log jqxhr, status, error + showNotification "An error occurred, a developer should check the console for details", false + complete: (jqxhr, status) -> + btn.button "reset" + $("textarea#q-answer[data-id=#{qid}]").removeAttr "readonly" diff --git a/app/views/question/show.html.haml b/app/views/question/show.html.haml index 9f66d917..7535bf82 100644 --- a/app/views/question/show.html.haml +++ b/app/views/question/show.html.haml @@ -5,13 +5,27 @@ - unless @question.author_is_anonymous %a.pull-left{href: show_user_profile_path(@question.user.screen_name)} %img.img-rounded.answerbox--img{src: gravatar_url(@question.user)} - .media-body - %h6.text-muted.media-heading.answerbox--question-user - = user_screen_name @question.user, @question.author_is_anonymous - asked - = time_ago_in_words(@question.created_at) - ago - %p.answerbox--question-text= @question.content + .media-body + %h6.text-muted.media-heading.answerbox--question-user + = user_screen_name @question.user, @question.author_is_anonymous + asked + = time_ago_in_words(@question.created_at) + ago + %p.answerbox--question-text= @question.content + + - if user_signed_in? and !current_user.answered? @question + #q-answer-box + Add your Senf here: + .panel-body + %textarea#q-answer.form-control{placeholder: 'Write your answer here...', data: { id: @question.id }} + %br/ + %button#q-answer.btn.btn-success{data: { q_id: @question.id }} + Answer + - current_user.services.each do |service| + %label + %input{type: 'checkbox', name: 'share', checked: :checked, data: { q_id: @question.id, service: service.provider }} + Post to + = service.provider.capitalize - @answers.each do |a| = render 'shared/answerbox', a: a, show_question: false