Skip to content

Commit

Permalink
Make the Stimulus controller to expire links using the expiration in …
Browse files Browse the repository at this point in the history
…the query string.
  • Loading branch information
priyadi committed Oct 8, 2023
1 parent d56a053 commit 8d3cb8a
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 14 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* Add a Stimulus controller to disable expired links.
* Add `temporary_url_autoexpire` Twig function.
* Add expiration in the query string of the resulting URL.
* Make the Stimulus controller to expire links using the expiration in the
query string.

## 1.2.1

Expand Down
19 changes: 18 additions & 1 deletion assets/dist/autoexpire.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,30 @@ import { Controller } from '@hotwired/stimulus';
export default class extends Controller {
connect() {
let element = this.element;
let href = element.getAttribute('href');
if (!href) {
return;
}
let url = new URL(href, 'http://example.com/');
if (!url) {
return;
}
let expiration = parseInt(url.searchParams.get('expiration'));
let secondsToExpiration = 1800;
if (expiration) {
let now = new Date().getTime() / 1000;
secondsToExpiration = expiration - now;
}
if (secondsToExpiration < 0) {
secondsToExpiration = 1;
}
setTimeout(function () {
element.classList.add('disabled');
element.setAttribute('disabled', 'disabled');
element.setAttribute('tabindex', '-1');
element.setAttribute('aria-disabled', 'true');
element.setAttribute('title', 'This link has expired. Please refresh the page, and try again.');
element.href = '#';
}, 1800000);
}, secondsToExpiration * 1000);
}
}
50 changes: 37 additions & 13 deletions assets/src/autoexpire.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,40 @@
'use strict';
'use strict'

import { Controller } from '@hotwired/stimulus'

import { Controller } from '@hotwired/stimulus';
export default class extends Controller {
connect() {
let element = this.element;
setTimeout(function () {
element.classList.add('disabled');
element.setAttribute('disabled', 'disabled');
element.setAttribute('tabindex', '-1');
element.setAttribute('aria-disabled', 'true');
element.setAttribute('title', 'This link has expired. Please refresh the page, and try again.');
element.href = '#';
}, 1800000);
}
connect() {
let element = this.element

let href = element.getAttribute('href')
if (!href) {
return;
}

let url = new URL(href, 'http://example.com/')
if (!url) {
return;
}

let expiration = parseInt(url.searchParams.get('expiration'))
let secondsToExpiration = 1800

if (expiration) {
let now = new Date().getTime() / 1000
secondsToExpiration = expiration - now
}

if (secondsToExpiration < 0) {
secondsToExpiration = 1
}

setTimeout(function () {
element.classList.add('disabled')
element.setAttribute('disabled', 'disabled')
element.setAttribute('tabindex', '-1')
element.setAttribute('aria-disabled', 'true')
element.setAttribute('title', 'This link has expired. Please refresh the page, and try again.')
element.href = '#'
}, secondsToExpiration * 1000)
}
}

0 comments on commit 8d3cb8a

Please sign in to comment.