Browse Source

WIP, adding JS and api time entry create

denney-fix-saving-dates
Jason Denney 11 years ago
parent
commit
069a11f480
  1. 32
      app/assets/javascripts/time_entries.js
  2. 5
      app/assets/stylesheets/bootstrap_and_overrides.css.less
  3. 30
      app/controllers/api/v1/time_entries_controller.rb
  4. 10
      app/views/time_entries/new.haml
  5. 2
      config/routes.rb

32
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);
}
});
});
});

5
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;
}

30
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

10
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}"}

2
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

Loading…
Cancel
Save