Skip to content
This repository has been archived by the owner on Mar 1, 2024. It is now read-only.

Commit

Permalink
move secondsSinceExpiration computation into separate function
Browse files Browse the repository at this point in the history
  • Loading branch information
FrancescoCioria committed Oct 5, 2016
1 parent 6810023 commit 540b99e
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions src/CookieBanner.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import { t, props } from 'tcomb-react';
import { cookie as cookieLite } from 'browser-cookie-lite';
import styleUtils from './styleUtils';

const NotEmptyStruct = t.refinement(t.struct, x => Object.keys(x).length > 0);

const Props = {
children: t.maybe(t.ReactChildren),
message: t.maybe(t.String),
Expand All @@ -18,7 +16,7 @@ const Props = {
})),
buttonMessage: t.maybe(t.String),
cookie: t.maybe(t.String),
cookieExpiration: t.maybe(t.union([NotEmptyStruct({
cookieExpiration: t.maybe(t.union([t.struct({
years: t.maybe(t.Number),
days: t.maybe(t.Number),
hours: t.maybe(t.Number)
Expand Down Expand Up @@ -96,18 +94,29 @@ export default class CookieBanner extends React.Component {
}
}

onAccept = () => {
const { cookie, cookieExpiration, onAccept } = this.props;
getSecondsSinceExpiration = cookieExpiration => {
if (t.Integer.is(cookieExpiration)) {
return cookieExpiration;
}

const SECONDS_IN_YEAR = 31536000;
const SECONDS_IN_DAY = 86400;
const SECONDS_IN_HOUR = 3600;

const secondsSinceExpiration = t.Integer.is(cookieExpiration) ?
cookieExpiration :
(cookieExpiration.years * SECONDS_IN_YEAR) + (cookieExpiration.days * SECONDS_IN_DAY) + (cookieExpiration.hours * SECONDS_IN_HOUR);
const _cookieExpiration = {
years: 0, days: 0, hours: 0,
...cookieExpiration
};

const { years, days, hours } = _cookieExpiration;

return (years * SECONDS_IN_YEAR) + (days * SECONDS_IN_DAY) + (hours * SECONDS_IN_HOUR);
}

onAccept = () => {
const { cookie, cookieExpiration, onAccept } = this.props;

cookieLite(cookie, true, secondsSinceExpiration);
cookieLite(cookie, true, this.getSecondsSinceExpiration(cookieExpiration));
onAccept({ cookie });

if (this.state.listeningScroll) {
Expand Down

0 comments on commit 540b99e

Please sign in to comment.