65 lines
1.7 KiB
TypeScript
65 lines
1.7 KiB
TypeScript
import {DepartureTime} from "../../types";
|
|
|
|
const TOKEN_KEY = "token";
|
|
|
|
/**
|
|
* Uloží token do local storage prohlížeče.
|
|
*
|
|
* @param token token
|
|
*/
|
|
export const storeToken = (token: string) => {
|
|
localStorage.setItem(TOKEN_KEY, token);
|
|
}
|
|
|
|
/**
|
|
* Vrátí token z local storage, pokud tam je.
|
|
*
|
|
* @returns token nebo null
|
|
*/
|
|
export const getToken = (): string | null => {
|
|
return localStorage.getItem(TOKEN_KEY);
|
|
}
|
|
|
|
/**
|
|
* Odstraní token z local storage, pokud tam je.
|
|
*/
|
|
export const deleteToken = () => {
|
|
localStorage.removeItem(TOKEN_KEY);
|
|
}
|
|
|
|
/**
|
|
* Vrátí human-readable reprezentaci předaného data a času pro zobrazení.
|
|
* Příklady:
|
|
* - dnes 10:52
|
|
* - 10.05.2023 10:52
|
|
*/
|
|
export function getHumanDateTime(datetime: Date) {
|
|
let hours = String(datetime.getHours()).padStart(2, '0');
|
|
let minutes = String(datetime.getMinutes()).padStart(2, "0");
|
|
if (new Date().toDateString() === datetime.toDateString()) {
|
|
return `dnes ${hours}:${minutes}`;
|
|
} else {
|
|
let day = String(datetime.getDate()).padStart(2, '0');
|
|
let month = String(datetime.getMonth() + 1).padStart(2, "0");
|
|
let year = datetime.getFullYear();
|
|
return `${day}.${month}.${year} ${hours}:${minutes}`;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Vrátí true, pokud je předaný čas větší než aktuální čas.
|
|
*/
|
|
export function isInTheFuture(time: DepartureTime) {
|
|
const now = new Date();
|
|
const currentHours = now.getHours();
|
|
const currentMinutes = now.getMinutes();
|
|
const currentDate = now.toDateString();
|
|
const [hours, minutes] = time.split(':').map(Number);
|
|
|
|
if (currentDate === now.toDateString()) {
|
|
return hours > currentHours || (hours === currentHours && minutes > currentMinutes);
|
|
}
|
|
return true;
|
|
}
|
|
|