From 8070b7f8b5c57fc87b2b98914aa661200d68906b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Zrim=C5=A1ek?= Date: Tue, 8 Jan 2019 08:07:24 +0100 Subject: [PATCH] Fix Date and Time display formats on JS side --- .../protocols/import_export/import.js.erb | 25 +++++++++++++++++-- app/controllers/application_controller.rb | 10 ++++++++ config/routes.rb | 6 +++++ 3 files changed, 39 insertions(+), 2 deletions(-) diff --git a/app/assets/javascripts/protocols/import_export/import.js.erb b/app/assets/javascripts/protocols/import_export/import.js.erb index b4845bddf..6ecc22fdb 100644 --- a/app/assets/javascripts/protocols/import_export/import.js.erb +++ b/app/assets/javascripts/protocols/import_export/import.js.erb @@ -115,8 +115,29 @@ function importProtocolFromFile( $("#protocol_authors").val($(this).children("authors").text()); $("#import_protocol_description").val($(this).children("description").text()); } - $("#protocol_created_at").val($(this).children("created_at").text()); - $("#protocol_updated_at").val($(this).children("updated_at").text()); + + $.ajax({ + url: "/helpers/to_user_date_format.json", + type: "GET", + data: { "timestamp": $(this).children("created_at").text(), + "ts_format": "full" }, + dataType: "json", + async: false, + success: function (data) { + $("#protocol_created_at").val(data.ts); + } + }); + $.ajax({ + url: "/helpers/to_user_date_format.json", + type: "GET", + data: { "timestamp": $(this).children("updated_at").text(), + "ts_format": "full" }, + dataType: "json", + async: false, + success: function (data) { + $("#protocol_updated_at").val(data.ts); + } + }); }); // PREVIEW diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 607e5185c..da2720603 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -36,6 +36,16 @@ class ApplicationController < ActionController::Base Team.find_by_id(current_user.current_team_id) end + def to_user_date_format + ts = I18n.l(Time.parse(params[:timestamp]), + format: params[:ts_format].to_sym) + respond_to do |format| + format.json do + render json: { ts: ts }, status: :ok + end + end + end + protected def render_403(style = 'danger') diff --git a/config/routes.rb b/config/routes.rb index 77462cbe8..9ca15b04f 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -53,6 +53,12 @@ Rails.application.routes.draw do get 'forbidden', to: 'application#forbidden', as: 'forbidden' get 'not_found', to: 'application#not_found', as: 'not_found' + # JS backend helpers + get 'helpers/to_user_date_format', + to: 'application#to_user_date_format', + as: 'to_user_date_format', + defaults: { format: 'json' } + # Settings resources :users, only: :index # needed for testing signup # needed for testing edit passowrd