time-convertor-ts

Convert time across timezones (typescript)
git clone http://git.hanabi.in/repos/time-convertor-ts.git
Log | Files | Refs | README | LICENSE

commit c51124f3be6b210b1d81043e387de22b41132a45
parent bd8842736ec84012358ee013721e0375a70d7177
Author: Agastya Chandrakant <acagastya@outlook.com>
Date:   Mon, 25 May 2020 10:59:56 +0530

change footer, add interfaces + methods

Diffstat:
Msrc/App/comps/Footer.tsx | 1-
Msrc/utils/index.ts | 44++++++++++++++++++++++++++++++++++++++++----
Msrc/utils/interfaces.tsx | 15+++++++++++++++
3 files changed, 55 insertions(+), 5 deletions(-)

diff --git a/src/App/comps/Footer.tsx b/src/App/comps/Footer.tsx @@ -9,7 +9,6 @@ function Footer<never>(): JSX.Element { style={{ bottom: 0, paddingTop: '1rem', - position: 'fixed', width: 'calc(100vw - 30px', }} > diff --git a/src/utils/index.ts b/src/utils/index.ts @@ -1,11 +1,14 @@ import moment from 'moment-timezone'; +import { IDisplayTime } from '../utils/interfaces'; + /** * @description clears the localStorage */ -function clearTimezones() { +function clearTimezones<never>() { localStorage.clear() } + /** * * @param fmtStr {string} Formatting string (ex. 'HH:mm:ss MMMM DD, YYYY') @@ -13,9 +16,10 @@ function clearTimezones() { * @param timezone {string} Timezone to set * @returns {string} formatted time */ -function displayTime({ fmtStr, time, timezone }: { fmtStr: string; time: moment.Moment; timezone: string }) { +function displayTime({ fmtStr, time, timezone }: IDisplayTime) { return time.tz(timezone).format(fmtStr); } + /** * @description Convert a string like `foo_bar_baz` to `foo bar baz` * @param {string} str @@ -25,15 +29,45 @@ function friendlyStr(str: string): string { } /** + * @description Fetch list of saved time zones from the local Storage + * @returns {string[]} Array of saved time zones + */ +function getSavedZones<never>(): string[] | [] { + // query localStorage to get JSON as string + const savedData: string = localStorage.getItem('zones') || '{}'; + // extract array + const savedZones: string[] = JSON.parse(savedData).list || []; + return savedZones; +} + +/** + * @param {string} zone zone to be removed from the localStorage + * @description Removes one zone from localStorage + */ +function removeTimeZone(zone: string = ''): void { + // get saved zones + const savedZones = getSavedZones(); + // filter out + const updatedZones = savedZones.filter((savedZone) => savedZone !== zone); + // stringify JSON + const newZonesStr = JSON.stringify({ list: updatedZones }); + // save to localStorage + localStorage.setItem('zones', newZonesStr); +} + +/** * * @param timezone {string[]} An array of timezones * @description takes in an array of timezones and updates the localStorage */ function saveTimezones(timezone: string[] | [] = []) { - const savedData: string = localStorage.getItem('zones') || '{}'; - const savedZones: string[] = JSON.parse(savedData).list || []; + + const savedZones = getSavedZones(); + // new array of sorted time zones const newZones = Array.from(new Set([...savedZones, ...timezone])).sort(); + // stringify JSON const newZonesStr = JSON.stringify({ list: newZones }); + // save to localStorage localStorage.setItem('zones', newZonesStr); } @@ -49,6 +83,8 @@ export { clearTimezones, displayTime, friendlyStr, + getSavedZones, + removeTimeZone, saveTimezones, unfriendlyStr }; diff --git a/src/utils/interfaces.tsx b/src/utils/interfaces.tsx @@ -8,6 +8,21 @@ export interface ICurrentConvertor { setTZ2: React.Dispatch<React.SetStateAction<string>>; } +export interface IDisplayTime { + fmtStr: string; + time: moment.Moment; + timezone: string; +} + +export interface ISavedTimezones { + time: moment.Moment; +} + +export interface IShowSavedTimezones { + savedZones: string[]; + time: moment.Moment; +} + export interface ISimpleConvertor { time: moment.Moment; TZ1: string;