From 069a11f480a8fff7ecaf3810f0ad74a0e906463d Mon Sep 17 00:00:00 2001 From: Jason Denney Date: Sat, 19 Apr 2014 21:06:29 -0400 Subject: [PATCH] WIP, adding JS and api time entry create --- app/assets/javascripts/time_entries.js | 32 +++++++++++++++++++ .../bootstrap_and_overrides.css.less | 5 +++ .../api/v1/time_entries_controller.rb | 30 +++++++++++++++++ app/views/time_entries/new.haml | 10 +++--- config/routes.rb | 2 ++ 5 files changed, 75 insertions(+), 4 deletions(-) create mode 100644 app/controllers/api/v1/time_entries_controller.rb diff --git a/app/assets/javascripts/time_entries.js b/app/assets/javascripts/time_entries.js index 25743e4..2f1acb7 100644 --- a/app/assets/javascripts/time_entries.js +++ b/app/assets/javascripts/time_entries.js @@ -1,4 +1,5 @@ $(document).ready(function(){ + var currentdate = new Date(); $("#date_id").datepicker().on('changeDate', function(ev){ $("#date_id").datepicker('hide'); @@ -7,4 +8,35 @@ $(document).ready(function(){ $("#start_time_id").timepicker(); $("#end_time_id").timepicker(); + + $("#add_time_entry_submit").click(function(){ + date = $("#date_id").val(); + start_date = new Date(date + " " + $("#start_time_id").val()); + end_date = new Date(date + " " + $("#end_time_id").val()); + + json_data = { time_entries: [{ + start_date: start_date.toISOString(), + end_date: end_date.toISOString(), + log_action_id: parseInt($('input[name=action_id]:checked').val()), + bike_id: parseInt($("#bike_id").val()), + description: $("#description_id").val(), + }]}; + console.log(json_data); + + $.ajax({ + url: $("#add_time_entry_submit").data("url"), + type: "POST", + data: JSON.stringify(json_data), + contentType: 'application/json', + dataType: "json", + success: function(data, status, xhr){ + console.log(data); + //window.location = data.bikes[0].id; + }, + error: function(data, status ){ + console.log(data); + displayFormErrors(data.responseJSON); + } + }); + }); }); diff --git a/app/assets/stylesheets/bootstrap_and_overrides.css.less b/app/assets/stylesheets/bootstrap_and_overrides.css.less index d838852..1d0efba 100644 --- a/app/assets/stylesheets/bootstrap_and_overrides.css.less +++ b/app/assets/stylesheets/bootstrap_and_overrides.css.less @@ -40,3 +40,8 @@ body { .inline-block { display: inline-block; } + +.control-group.error .btn-group > .btn { + color: #b94a48; + border-color: #b94a48; +} diff --git a/app/controllers/api/v1/time_entries_controller.rb b/app/controllers/api/v1/time_entries_controller.rb new file mode 100644 index 0000000..dc5447e --- /dev/null +++ b/app/controllers/api/v1/time_entries_controller.rb @@ -0,0 +1,30 @@ +class Api::V1::TimeEntriesController < Api::V1::BaseController + def create + if params[:time_entries] && time_entry = params[:time_entries].first + puts time_entry.inspect + time_entry_defaults = { + loggable_type: "User", + loggable_id: current_user.id, + log_action_type: "ActsAsLoggable::UserAction"} + time_entry.merge(time_entry_defaults) + + if time_entry[:bike_id] >= 0 + copy_defaults = { + copy_log: true, + copy_type: 'Bike', + copy_id: time_entry[:bike_id], + copy_action_type: 'ActsAsLoggable::BikeAction', + copy_action_id: 4 + } + time_entry.merge( copy_defaults ) + end + + @time_entry = ::ActsAsLoggable::Log.new(time_entry.except(:bike_id)) + if !@time_entry.save + render json: { errors: @time_entry.errors }, status: 422 and return + end + else + render json: { errors: [EXPECTED_TIME_ENTRY]}, status: 422 and return + end + end +end diff --git a/app/views/time_entries/new.haml b/app/views/time_entries/new.haml index 8a4f12e..de4b173 100644 --- a/app/views/time_entries/new.haml +++ b/app/views/time_entries/new.haml @@ -11,11 +11,13 @@ .controls{ class: "bootstrap-timepicker"} %label Start %input{id: "start_time_id", placeholder: "Time ID", type: "text", class: "input-small" } + .hidden{ id: "start_date" } .help-block .control-group .controls %label End %input{id: "end_time_id", placeholder: "Time ID", type: "text", class: "input-small" } + .hidden{ id: "end_date" } .help-block .control-group .controls @@ -26,15 +28,15 @@ %input{ type: "radio", name: "action_id", value: 1} Personal %label{ class: "btn btn-default"} %input{ type: "radio", name: "action_id", value: 2} Staff - %input{ id: "bike_style_id", type: "hidden"} + .hidden{ id: "log_action_id" } .help-block .control-group .controls %label - = select_tag(:bike_brand_id, options_for_select(@bikes)) + = select_tag(:bike_id, options_for_select(@bikes)) .control-group .controls - %textarea{id: "work_description", placeholder: "Work description", class: "input-lg" } + %textarea{id: "description_id", placeholder: "Work description", class: "input-lg" } .control-group .controls - %input{id: "add_bike_submit", value: "Add Time Entry", type: "button", class: "btn btn-lg btn-block btn-primary", "data-url" => "#{api_create_bike_path}"} + %input{id: "add_time_entry_submit", value: "Add Time Entry", type: "button", class: "btn btn-lg btn-block btn-primary", "data-url" => "#{api_create_time_entry_path}"} diff --git a/config/routes.rb b/config/routes.rb index 568210b..8e666b0 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -28,6 +28,8 @@ Velocipede::Application.routes.draw do get 'task_lists/:id' => "task_lists#show", as: "api_task_list" put 'tasks/update' => "tasks#update", as: "api_update_task" + + post 'time_entries/create' => "time_entries#create", as: "api_create_time_entry" end end