-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added countdown for mobile confirmation
- Loading branch information
Oleg Hasjanov
authored and
Oleg Hasjanov
committed
Feb 12, 2024
1 parent
c4b140c
commit 8e79fcb
Showing
16 changed files
with
179 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
app/controllers/phone_confirmations/send_sms_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
class PhoneConfirmations::SendSmsController < ApplicationController | ||
before_action :authenticate_user! | ||
before_action :set_phone_confirmation | ||
before_action :authorize_user | ||
|
||
include RecaptchaValidatable | ||
recaptcha_action 'phone_confirmation' | ||
|
||
def create | ||
if current_user.allow_to_send_sms_again? && recaptcha_valid | ||
PhoneConfirmationJob.perform_now(@phone_confirmation.user.id) | ||
|
||
flash[:notice] = I18n.t('phone_confirmations.create.sms_sent') | ||
else | ||
flash[:alert] = I18n.t('phone_confirmations.create.sms_not_sent') | ||
end | ||
|
||
@show_checkbox_recaptcha = true unless @success | ||
|
||
redirect_to new_user_phone_confirmation_path(@phone_confirmation.user.uuid), status: :see_other, turbo: false | ||
end | ||
|
||
private | ||
|
||
def set_phone_confirmation | ||
@phone_confirmation = PhoneConfirmation.new(current_user) | ||
end | ||
|
||
def authorize_user | ||
authorize! :manage, PhoneConfirmation | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module PhoneConfirmationsHelper | ||
def render_send_in_button? | ||
Setting.find_by(code: 'require_phone_confirmation').retrieve && current_user.mobile_phone_confirmed_at.nil? && current_user.mobile_phone.present? | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { Controller } from "@hotwired/stimulus" | ||
|
||
export default class extends Controller { | ||
static values = { | ||
date: String, | ||
refreshInterval: { type: Number, default: 1000 }, | ||
messageTimer: String, | ||
defaultMessageTimer: String | ||
} | ||
|
||
static targets = [ "button" ] | ||
|
||
connect() { | ||
this.update = this.update.bind(this); | ||
|
||
if (this.dateValue) { | ||
this.endTime = new Date(this.dateValue).getTime(); | ||
|
||
this.update(); | ||
|
||
this.timer = setInterval(() => { | ||
this.update(); | ||
}, this.refreshIntervalValue); | ||
} else { | ||
console.log("Missing data-timeleft-date-value attribute", this.element); | ||
} | ||
} | ||
|
||
disconnect() { | ||
this.stopTimer(); | ||
} | ||
|
||
stopTimer() { | ||
if(this.timer) { | ||
clearInterval(this.timer); | ||
} | ||
} | ||
|
||
update() { | ||
let difference = this.timeDifference(); | ||
|
||
if (difference < 0) { | ||
var expiredMsg = $("#timer_message").data("expiredMessage"); | ||
$("#timer_message").html(expiredMsg); | ||
this.stopTimer(); | ||
this.buttonTarget.removeAttribute("disabled"); | ||
this.buttonTarget.innerHTML = this.defaultMessageTimerValue; | ||
} else { | ||
this.buttonTarget.setAttribute("disabled", "disabled"); | ||
|
||
let minutes = Math.floor((difference % (1000 * 60 * 60)) / (1000 * 60)); | ||
let seconds = Math.floor((difference % (1000 * 60)) / 1000); | ||
|
||
this.buttonTarget.innerHTML = `${this.messageTimerValue} ${minutes}:${seconds}` | ||
} | ||
} | ||
|
||
timeDifference() { | ||
const convertedToTimeZone = this.convertDateToTimeZone(new Date().getTime(), 'Europe/Tallinn'); | ||
return this.endTime - new Date(convertedToTimeZone).getTime(); | ||
} | ||
|
||
convertDateToTimeZone(date, timeZone) { | ||
return new Intl.DateTimeFormat('en-US', { | ||
timeZone: timeZone, | ||
year: 'numeric', | ||
month: '2-digit', | ||
day: '2-digit', | ||
hour: '2-digit', | ||
minute: '2-digit', | ||
second: '2-digit', | ||
}).format(date); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class AddSmsSendTimestampToUsers < ActiveRecord::Migration[7.0] | ||
def change | ||
add_column :users, :mobile_phone_confirmed_sms_send_at, :datetime | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters