Skip to content

Commit

Permalink
Merge pull request #21 from dabigjoe6/timezone
Browse files Browse the repository at this point in the history
Send time and timezone as part of register payload
  • Loading branch information
dabigjoe6 committed Sep 2, 2023
2 parents 4deabf4 + 8fb726e commit db7f456
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 14 deletions.
8 changes: 6 additions & 2 deletions src/contexts/Auth.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import * as React from "react";
import moment from 'moment-timezone';
import { useLocation, useNavigate } from "react-router-dom";
import { toast } from "react-toastify";
import { SIGNED_IN_TOKEN, SIGNED_IN_USER_KEY } from "../config";
import { StatusCallback } from "../types.js";
import { SettingsI } from "./Settings.js";
import { offsetTimesToGMTZero } from "../helpers/offsetTimeToGMTZero";

const BASE_URL = process.env.REACT_APP_BASE_URL;

Expand Down Expand Up @@ -89,12 +91,13 @@ export const AuthProvider = ({ children }: { children: React.ReactNode }) => {

const registerUser = async ({ email, password }: LoginDetails, callback: StatusCallback) => {
try {
const usersTimeZone = moment.tz.guess();
const response = await fetch(BASE_URL + "/auth/register", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ email, password }),
body: JSON.stringify({ email, password, time: offsetTimesToGMTZero(["09:00"], usersTimeZone), timeZone: usersTimeZone }),
});

const data = await response.json();
Expand Down Expand Up @@ -152,9 +155,10 @@ export const AuthProvider = ({ children }: { children: React.ReactNode }) => {

const signUserInWithGoogle = async (code: string, callback: StatusCallback) => {
try {
const usersTimeZone = moment.tz.guess();
const response = await fetch(BASE_URL + "/auth/oauth_login", {
method: "POST",
body: JSON.stringify({ code }),
body: JSON.stringify({ code, time: offsetTimesToGMTZero(["09:00"], usersTimeZone), timeZone: usersTimeZone}),
headers: {
"Content-Type": "application/json",
},
Expand Down
14 changes: 2 additions & 12 deletions src/contexts/Settings.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import * as React from 'react';
import moment from 'moment-timezone';
import { toast } from 'react-toastify';
import { StatusCallback } from '../types';
import { AuthContext } from './Auth';
import moment from 'moment-timezone';
import { offsetTimesToGMTZero } from '../helpers/offsetTimeToGMTZero';

const BASE_URL = process.env.REACT_APP_BASE_URL;

Expand Down Expand Up @@ -55,17 +56,6 @@ export const SettingsContext = React.createContext<SettingsContextI>({
export const SettingsProvider = ({ children }: { children: React.ReactNode }) => {
const { user, token, signUserOut, updateUserSettings } = React.useContext(AuthContext);

const offsetTimesToGMTZero = (times: Array<string>, timezone: string): Array<string> => {
const gmtZeroOffset = moment.tz("GMT+0").utcOffset();

return times.map((time) => {
const datetime = moment.tz(`${moment().format("YYYY-MM-DD")} ${time}`, timezone);
const offsetted = datetime.utcOffset(gmtZeroOffset);

return offsetted.format("HH:mm");
});
}

const pauseDigest = async (cb: StatusCallback) => {
try {
if (user && user._id) {
Expand Down
12 changes: 12 additions & 0 deletions src/helpers/offsetTimeToGMTZero.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import moment from 'moment-timezone';

export const offsetTimesToGMTZero = (times: Array<string>, timezone: string): Array<string> => {
const gmtZeroOffset = moment.tz("GMT+0").utcOffset();

return times.map((time) => {
const datetime = moment.tz(`${moment().format("YYYY-MM-DD")} ${time}`, timezone);
const offsetted = datetime.utcOffset(gmtZeroOffset);

return offsetted.format("HH:mm");
});
}

0 comments on commit db7f456

Please sign in to comment.