From 1e1879468075b2b0706397763992ab410fd02193 Mon Sep 17 00:00:00 2001 From: "Ismail H. Ayaz" Date: Sun, 21 Apr 2024 16:22:40 +0300 Subject: [PATCH] feat: human-readable rate limit errors --- src/i18n/en.ts | 10 +++++++--- src/ui/utils/form_generics.ts | 3 ++- src/ui/utils/helpers.ts | 26 ++++++++++++++++++++++++++ 3 files changed, 35 insertions(+), 4 deletions(-) diff --git a/src/i18n/en.ts b/src/i18n/en.ts index a2d85bd..6afe16f 100755 --- a/src/i18n/en.ts +++ b/src/i18n/en.ts @@ -1,8 +1,8 @@ - export default { common: { allow: 'Allow', continue: 'Continue', + days: 'days', edit: 'Edit', field: 'Field', fields: { @@ -15,10 +15,14 @@ export default { username: 'Username' }, hide: 'Hide', + hours: 'hours', + minutes: 'minutes', reject: 'Reject', + seconds: 'seconds', show: 'Show', submit: 'Submit', - verify: 'Verify' + verify: 'Verify', + years: 'years' }, verifyEmail: { checkText: "We\'ve sent an email to {email}, please click the link included" + @@ -83,7 +87,7 @@ export default { passwords_not_match: "Passwords doesn't match", user_not_found: 'User not found', weak_password: 'Password is too weak', - too_many_requests: 'You have ben trying too fast. Try again in {retry} seconds.', + too_many_requests: 'You have ben trying too fast. Try again in {retry}.', account_blocked: 'Your account is blocked. Please check your email for further instructions.', }, fillMissing: { diff --git a/src/ui/utils/form_generics.ts b/src/ui/utils/form_generics.ts index 6847c45..12a8b91 100755 --- a/src/ui/utils/form_generics.ts +++ b/src/ui/utils/form_generics.ts @@ -10,6 +10,7 @@ import type { IWidgetSettings, WidgetModes } from '../interfaces'; +import { secondsToReadable } from './helpers.ts'; import { deepToRaw, toReactive } from './to_reactive'; import type { Translator } from './translator'; import { translatorKey } from './translator'; @@ -133,7 +134,7 @@ export function useGenericForm( form.value.toggleAlert({ path: `errors.${e.error}`, args: { - retry: retryAfter + retry: secondsToReadable(retryAfter, translator), } }) } else { diff --git a/src/ui/utils/helpers.ts b/src/ui/utils/helpers.ts index b62a669..d85f27c 100644 --- a/src/ui/utils/helpers.ts +++ b/src/ui/utils/helpers.ts @@ -1,5 +1,7 @@ import type { PropType } from 'vue'; +import type { Translator } from './translator.ts'; + export type EventProp void> = F export const EventProp = () => [Function, Array] as PropType> @@ -37,3 +39,27 @@ export function isEmpty(obj: any){ return true } } + +/** + * https://stackoverflow.com/a/34270811 + * + * Translates seconds into human-readable format of seconds, minutes, hours, days, and years + * + */ +export function secondsToReadable( seconds: number, translator: Translator ): string { + const levels = [ + [Math.floor(seconds / 31536000), translator.t('common.years')], + [Math.floor(seconds % 31536000 / 86400), translator.t('common.days')], + [Math.floor(seconds % 31536000 % 86400 / 3600), translator.t('common.hours')], + [Math.floor(seconds % 31536000 % 86400 % 3600 / 60), translator.t('common.minutes')], + [seconds % 31536000 % 86400 % 3600 % 60, translator.t('common.seconds')], + ] as [number ,string][]; + + let returnText = ''; + + for (let i = 0, max = levels.length; i < max; i++) { + if ( levels[i][0] === 0 ) continue; + returnText += ` ${ levels[i][0] } ${ levels[i][0] === 1 ? levels[i][1].substring(0, levels[i][1].length-1): levels[i][1]}`; + } + return returnText.trim(); +}