date-fns#addSeconds JavaScript Examples
The following examples show how to use
date-fns#addSeconds.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: item.js From Hack-the-October2020 with GNU General Public License v3.0 | 6 votes |
/**
* Calculate the time it will take to respawn this tile
*
* @param {string} respawnTime Time in short notation (eg: '1hr 4m 18s')
* @returns {integer}
*/
static calculateRespawnTime(respawnTime) {
// MOVE TO OBJECT ENGINE CLASS AND EXTEND FROM HERE?
const pickedUpAt = new Date();
const respawnsIn = respawnTime;
const add = {
hours: Item.parseTime(respawnsIn, 'h'),
minutes: Item.parseTime(respawnsIn, 'm'),
seconds: Item.parseTime(respawnsIn, 's'),
};
let timeToAdd = 0;
if (typeof (add.hours) === 'number') timeToAdd = addHours(pickedUpAt, add.hours);
if (typeof (add.minutes) === 'number') timeToAdd = addMinutes(pickedUpAt, add.minutes);
if (typeof (add.seconds) === 'number') timeToAdd = addSeconds(pickedUpAt, add.seconds);
return timeToAdd;
}
Example #2
Source File: login.js From lnft with GNU Affero General Public License v3.0 | 6 votes |
export async function post({ locals, request }) {
let { q } = locals;
try {
let body = await request.json();
const res = await serverApi.url("/login").post(body).res();
body = await res.json();
let { jwt_expires_in, jwt_token } = body;
let tokenExpiry = parseInt(jwt_expires_in / 1000);
let { currentuser } = await q(getUser, undefined, {
authorization: `Bearer ${jwt_token}`,
});
body.user = currentuser[0];
return {
body,
headers: {
"set-cookie": [
res.headers.get("set-cookie").split(",").slice(0, 2).join(""),
cookie.serialize("token", jwt_token, {
httpOnly: true,
maxAge: tokenExpiry,
sameSite: "lax",
path: "/",
expires: addSeconds(new Date(), tokenExpiry),
}),
],
},
};
} catch (e) {
console.log("Login error", e);
return {
body: { message: "Login failed" },
status: 500,
};
}
}
Example #3
Source File: useTimeLeftBeforePrize.js From pooltogether-community-ui with MIT License | 6 votes |
useTimeLeftBeforePrize = () => {
const { data: poolChainValues } = usePoolChainValues()
const poolValueSecondsLeft = parseInt(
poolChainValues.prize.prizePeriodRemainingSeconds.toString(),
10
)
const [secondsLeft, setSecondsLeft] = useState(poolValueSecondsLeft)
useEffect(() => {
if (poolValueSecondsLeft > secondsLeft) {
setSecondsLeft(poolValueSecondsLeft)
}
}, [poolValueSecondsLeft])
useInterval(() => {
const newRemainder = secondsLeft - 1
if (newRemainder >= 0) {
setSecondsLeft(newRemainder)
}
}, 1000)
const currentDate = new Date(Date.now())
const futureDate = addSeconds(currentDate, secondsLeft)
const { days, hours, minutes, seconds } = subtractDates(futureDate, currentDate)
const timeRemaining = Boolean(days || hours || minutes || seconds)
return { days, hours, minutes, seconds, timeRemaining }
}
Example #4
Source File: useTimeCountdown.js From pooltogether-governance-ui with MIT License | 6 votes |
useTimeCountdown = (initialSecondsLeft, onZero, countBy = 1000) => {
const [secondsLeft, setSecondsLeft] = useState(initialSecondsLeft)
const [isCleared, clearInterval] = useInterval(() => {
const secondsToCountBy = msToSeconds(countBy).toNumber()
const newRemainder = secondsLeft - secondsToCountBy
setSecondsLeft(newRemainder)
}, countBy)
useEffect(() => {
if (secondsLeft <= 0) {
onZero?.()
clearInterval()
}
}, [secondsLeft])
const currentDate = new Date(Date.now())
const futureDate = addSeconds(currentDate, secondsLeft)
const { days, hours, minutes, seconds } = subtractDates(futureDate, currentDate)
return { days, hours, minutes, seconds, secondsLeft }
}
Example #5
Source File: useTimeCountdown.js From v3-ui with MIT License | 6 votes |
useTimeCountdown = (initialSecondsLeft, countBy = 1000) => {
const [secondsLeft, setSecondsLeft] = useState(initialSecondsLeft < 0 ? 0 : initialSecondsLeft)
useInterval(() => {
const secondsToCountBy = msToSeconds(countBy)
const newRemainder = secondsLeft - secondsToCountBy
if (newRemainder >= 0) {
setSecondsLeft(newRemainder)
}
}, countBy)
const currentDate = new Date(Date.now())
const futureDate = addSeconds(currentDate, secondsLeft)
const { days, hours, minutes, seconds } = subtractDates(futureDate, currentDate)
return { days, hours, minutes, seconds, secondsLeft }
}
Example #6
Source File: index.js From lnft with GNU Affero General Public License v3.0 | 5 votes |
export async function handle({ event, resolve }) {
let {
request: { headers },
url: { pathname },
} = event;
const cookies = cookie.parse(headers.get("cookie") || "");
let { refresh_token, token: jwt } = cookies;
let user, setCookie;
try {
decode(jwt);
} catch (e) {
try {
if (!pathname.includes(".json") && refresh_token) {
let res = await hbp
.headers({ cookie: `refresh_token=${refresh_token}` })
.url("/auth/token/refresh")
.get()
.res();
let body = await res.json();
let { jwt_token, jwt_expires_in } = body;
jwt = jwt_token;
let tokenExpiry = parseInt(jwt_expires_in / 1000);
setCookie = [
res.headers.get("set-cookie").split(",").slice(0, 2).join(""),
cookie.serialize("token", jwt_token, {
httpOnly: true,
maxAge: tokenExpiry,
sameSite: "lax",
path: "/",
expires: addSeconds(new Date(), tokenExpiry),
}),
];
}
} catch (e) {
setCookie = [
cookie.serialize("refresh_token", "", {
path: "/",
expires: new Date(0),
}),
];
}
}
let q = getQ({ authorization: `Bearer ${jwt}` });
event.locals = { jwt, q };
if (jwt) {
try {
let { currentuser } = await q(getUser);
user = currentuser[0];
} catch (e) {
// console.log(e);
}
}
event.locals.user = user;
const response = await resolve(event);
if (setCookie && pathname !== "/auth/login")
response.headers.set("set-cookie", setCookie);
return response;
}
Example #7
Source File: refresh.json.js From lnft with GNU Affero General Public License v3.0 | 5 votes |
export async function get({ request: { headers } }) {
try {
const cookies = cookie.parse(headers.get("cookie") || "");
let { refresh_token, token: jwt } = cookies;
if (!refresh_token) throw new Error("no refresh token");
let res = await hbp
.headers(headers)
.url(`/auth/token/refresh?refresh_token=${refresh_token}`)
.get()
.res();
let body = await res.json();
let { jwt_token, jwt_expires_in } = body;
let tokenExpiry = parseInt(jwt_expires_in / 1000);
return {
body,
headers: {
"set-cookie": [
res.headers.get("set-cookie").split(",").slice(0, 2).join(""),
cookie.serialize("token", jwt_token, {
...opts,
maxAge: tokenExpiry,
expires: addSeconds(new Date(), tokenExpiry),
}),
],
},
};
} catch (e) {
return {
body: {},
status: 200,
headers: new Headers({
"set-cookie": [
cookie.serialize("token", "", {
...opts,
expires: new Date(0),
}),
cookie.serialize("refresh_token", "", {
...opts,
expires: new Date(0),
}),
].join(","),
}),
};
}
}