Skip to content

Commit

Permalink
feat(core): Expiration time
Browse files Browse the repository at this point in the history
Set an optional expiration time for the cookie

closes #13
  • Loading branch information
Andrea Zornada committed Jun 6, 2017
1 parent e380559 commit 6faf3de
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 25 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,18 @@ Allows you to decide which name will be used for storing the cookie in the clien
The previous example will generate a `myShinyCookieLaw=true` as soon as the user dismiss the banner.
### expire
| Type | Default value | Description |
| --- | --- | --- |
| number | - | Set a the cookie expiration time (in days) |
###### Example
```html
<cookie-law name="myShinyCookieLaw" expire="7">I'm gonna expire in 1 week!</cookie-law>
```
## Properties
| Name | Type | Description |
Expand Down
6 changes: 3 additions & 3 deletions demo/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
} from '@angular/core';

@Component({
selector: 'demo-app',
selector: 'cookie-demo-app',
template: `
<h1>Angular2-Cookie-Law</h1>
<span>Demo page</span>
Expand All @@ -25,8 +25,8 @@ import {
</a>
</div>
<cookie-law position="top" name="topCookieLaw">
Allo! This is my awesome cookie-law message.
<cookie-law position="top" name="topCookieLaw" [expiration]="7">
Allo! I'm expiring in one week!.
<a href="https://github.com/andreasonny83/angular2-cookie-law">
Click here for more info
</a>
Expand Down
4 changes: 2 additions & 2 deletions demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
<title>angular2-cookie-law demo app</title>
</head>
<body>
<demo-app class="app">
<cookie-demo-app class="app">
Loading...
</demo-app>
</cookie-demo-app>

<script src="bundle.js"></script>
</body>
Expand Down
9 changes: 7 additions & 2 deletions src/cookie-law.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,15 @@ export class CookieLawComponent implements OnInit {
@Input()
public position: CookieLawPosition;

@Input()
public expiration: number;

@Output()
public isSeen = new EventEmitter<boolean>();

constructor (private _service: CookieLawService) { }
constructor (private _service: CookieLawService) {
this.name = 'cookieLawSeen'; // set a default cookie name if not provided
}

public get cookieLawSeen(): boolean {
return this._service.seen(this.name);
Expand All @@ -67,7 +72,7 @@ export class CookieLawComponent implements OnInit {
}

public hasBeenDismissed(): void {
this._service.storeCookie(this.name);
this._service.storeCookie(this.name, this.expiration);
this.seen = true;
this.isSeen.emit(true);
}
Expand Down
10 changes: 9 additions & 1 deletion src/cookie-law.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,20 @@ describe('CookieLawService', () => {
});

it('#seen should now have a cookie stored', () => {
service.storeCookie();
service.storeCookie('cookieLawSeen');

expect(service.seen()).toBe(true);
expect(service.seen('cookieLawSeen')).toBe(true);
});

it('set an expiration time', () => {
service.storeCookie('cookieLawSeen', 1);

expect(service.seen()).toBe(true);
expect(service.seen('cookieLawSeen')).toBe(true);
expect(document.cookie.match('cookieLawSeen').indexOf('cookieLawSeen')).not.toBe(-1);
});

it('should stored different cookie names', () => {
service.storeCookie('testCookie');

Expand Down
18 changes: 12 additions & 6 deletions src/cookie-law.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import { Injectable } from '@angular/core';
@Injectable()
export class CookieLawService {

public seen(cookieName?: string): boolean {
return this.cookieExisits(cookieName || 'cookieLawSeen');
public seen(cookieName: string = 'cookieLawSeen'): boolean {
return this.cookieExisits(cookieName);
}

public storeCookie(cookieName?: string): void {
return this.setCookie(cookieName || 'cookieLawSeen');
public storeCookie(cookieName: string, expiration?: number): void {
return this.setCookie(cookieName, expiration);
}

/**
Expand Down Expand Up @@ -46,7 +46,13 @@ export class CookieLawService {
*
* @param {string} name [the name for the cookie]
*/
private setCookie(name: string): void {
document.cookie = encodeURIComponent(name) + '=true; path=/';
private setCookie(name: string, expiration?: number): void {
const date = new Date();
let expires;

date.setTime(date.getTime() + expiration * 86400000);
expires = '; expires=' + date.toUTCString();

document.cookie = encodeURIComponent(name) + '=true; path=/' + expires;
}
}
22 changes: 11 additions & 11 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1112,13 +1112,13 @@ debug@2.3.3:
dependencies:
ms "0.7.2"

debug@2.6.7:
debug@2.6.7, debug@^2.2.0:
version "2.6.7"
resolved "https://registry.yarnpkg.com/debug/-/debug-2.6.7.tgz#92bad1f6d05bbb6bba22cca88bcd0ec894c2861e"
dependencies:
ms "2.0.0"

debug@2.6.8, debug@^2.2.0, debug@^2.6.8:
debug@2.6.8, debug@^2.6.8:
version "2.6.8"
resolved "https://registry.yarnpkg.com/debug/-/debug-2.6.8.tgz#e731531ca2ede27d188222427da17821d68ff4fc"
dependencies:
Expand Down Expand Up @@ -3533,27 +3533,27 @@ read-pkg@^1.0.0:
normalize-package-data "^2.3.2"
path-type "^1.0.0"

"readable-stream@^2.0.0 || ^1.1.13", readable-stream@^2.0.1, readable-stream@^2.0.2, readable-stream@^2.0.5, readable-stream@^2.0.6, readable-stream@~2.0.0, readable-stream@~2.0.5:
version "2.0.6"
resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-2.0.6.tgz#8f90341e68a53ccc928788dacfcd11b36eb9b78e"
"readable-stream@^2.0.0 || ^1.1.13", readable-stream@^2.1.4, readable-stream@^2.2.6, readable-stream@^2.2.9:
version "2.2.10"
resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-2.2.10.tgz#effe72bb7c884c0dd335e2379d526196d9d011ee"
dependencies:
core-util-is "~1.0.0"
inherits "~2.0.1"
isarray "~1.0.0"
process-nextick-args "~1.0.6"
string_decoder "~0.10.x"
safe-buffer "^5.0.1"
string_decoder "~1.0.0"
util-deprecate "~1.0.1"

readable-stream@^2.1.4, readable-stream@^2.2.6, readable-stream@^2.2.9:
version "2.2.10"
resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-2.2.10.tgz#effe72bb7c884c0dd335e2379d526196d9d011ee"
readable-stream@^2.0.1, readable-stream@^2.0.2, readable-stream@^2.0.5, readable-stream@^2.0.6, readable-stream@~2.0.0, readable-stream@~2.0.5:
version "2.0.6"
resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-2.0.6.tgz#8f90341e68a53ccc928788dacfcd11b36eb9b78e"
dependencies:
core-util-is "~1.0.0"
inherits "~2.0.1"
isarray "~1.0.0"
process-nextick-args "~1.0.6"
safe-buffer "^5.0.1"
string_decoder "~1.0.0"
string_decoder "~0.10.x"
util-deprecate "~1.0.1"

readable-stream@~1.0.2:
Expand Down

0 comments on commit 6faf3de

Please sign in to comment.