From ac866592023ae296e07e0251d8128c428da131f1 Mon Sep 17 00:00:00 2001 From: Patrick Fic <> Date: Wed, 7 Oct 2020 10:25:51 -0700 Subject: [PATCH] Removed circular dependencies from project. --- client/madge-graph.svg | 434 +----------------- client/src/App/App.container.jsx | 146 +----- .../documents-upload.utility.js | 2 +- ...obs-documents-gallery.delete.component.jsx | 2 +- .../shop-template-add.component.jsx | 2 +- .../shop-template-test-render.component.jsx | 2 +- client/src/firebase/firebase.utils.js | 20 +- .../pages/manage/manage.page.component.jsx | 2 +- .../redux/application/application.sagas.js | 2 +- client/src/redux/messaging/messaging.sagas.js | 2 +- client/src/utils/CleanAxios.js | 27 ++ client/src/utils/GraphQLClient.js | 118 +++++ client/src/utils/RenderTemplate.js | 4 +- client/src/utils/dates.js | 170 ------- 14 files changed, 169 insertions(+), 764 deletions(-) create mode 100644 client/src/utils/CleanAxios.js create mode 100644 client/src/utils/GraphQLClient.js delete mode 100644 client/src/utils/dates.js diff --git a/client/madge-graph.svg b/client/madge-graph.svg index 282ef553a..3f75254d9 100644 --- a/client/madge-graph.svg +++ b/client/madge-graph.svg @@ -4,436 +4,10 @@ - - + + G - - - -src/App/App.container.jsx - -src/App/App.container.jsx - - - -src/App/App.jsx - -src/App/App.jsx - - - -src/App/App.container.jsx->src/App/App.jsx - - - - - -src/firebase/firebase.utils.js - -src/firebase/firebase.utils.js - - - -src/App/App.container.jsx->src/firebase/firebase.utils.js - - - - - -src/components/error-boundary/error-boundary.component.jsx - -src/components/error-boundary/error-boundary.component.jsx - - - -src/App/App.jsx->src/components/error-boundary/error-boundary.component.jsx - - - - - -src/pages/manage/manage.page.container.jsx - -src/pages/manage/manage.page.container.jsx - - - -src/App/App.jsx->src/pages/manage/manage.page.container.jsx - - - - - -src/redux/store.js - -src/redux/store.js - - - -src/firebase/firebase.utils.js->src/redux/store.js - - - - - -src/components/error-boundary/error-boundary.component.jsx->src/firebase/firebase.utils.js - - - - - -src/pages/manage/manage.page.component.jsx - -src/pages/manage/manage.page.component.jsx - - - -src/pages/manage/manage.page.container.jsx->src/pages/manage/manage.page.component.jsx - - - - - -src/components/bill-detail-edit/bill-detail-edit.container.jsx - -src/components/bill-detail-edit/bill-detail-edit.container.jsx - - - -src/components/jobs-documents-gallery/jobs-documents-gallery.container.jsx - -src/components/jobs-documents-gallery/jobs-documents-gallery.container.jsx - - - -src/components/bill-detail-edit/bill-detail-edit.container.jsx->src/components/jobs-documents-gallery/jobs-documents-gallery.container.jsx - - - - - -src/components/jobs-documents-gallery/jobs-documents-gallery.component.jsx - -src/components/jobs-documents-gallery/jobs-documents-gallery.component.jsx - - - -src/components/jobs-documents-gallery/jobs-documents-gallery.container.jsx->src/components/jobs-documents-gallery/jobs-documents-gallery.component.jsx - - - - - -src/components/bill-enter-modal/bill-enter-modal.container.jsx - -src/components/bill-enter-modal/bill-enter-modal.container.jsx - - - -src/components/documents-upload/documents-upload.utility.js - -src/components/documents-upload/documents-upload.utility.js - - - -src/components/bill-enter-modal/bill-enter-modal.container.jsx->src/components/documents-upload/documents-upload.utility.js - - - - - -src/components/documents-upload/documents-upload.utility.js->src/App/App.container.jsx - - - - - -src/components/documents-upload/documents-upload.utility.js->src/firebase/firebase.utils.js - - - - - -src/components/email-overlay/email-overlay.container.jsx - -src/components/email-overlay/email-overlay.container.jsx - - - -src/components/email-overlay/email-overlay.container.jsx->src/firebase/firebase.utils.js - - - - - -src/utils/RenderTemplate.js - -src/utils/RenderTemplate.js - - - -src/components/email-overlay/email-overlay.container.jsx->src/utils/RenderTemplate.js - - - - - -src/utils/RenderTemplate.js->src/App/App.container.jsx - - - - - -src/components/jobs-documents-gallery/jobs-documents-gallery.delete.component.jsx - -src/components/jobs-documents-gallery/jobs-documents-gallery.delete.component.jsx - - - -src/components/jobs-documents-gallery/jobs-documents-gallery.component.jsx->src/components/jobs-documents-gallery/jobs-documents-gallery.delete.component.jsx - - - - - -src/components/jobs-documents-gallery/jobs-documents-gallery.delete.component.jsx->src/App/App.container.jsx - - - - - -src/components/jobs-documents-gallery/jobs-documents-gallery.delete.component.jsx->src/firebase/firebase.utils.js - - - - - -src/components/shop-template-add/shop-template-add.component.jsx - -src/components/shop-template-add/shop-template-add.component.jsx - - - -src/components/shop-template-add/shop-template-add.component.jsx->src/App/App.container.jsx - - - - - -src/components/shop-template-add/shop-template-add.component.jsx->src/firebase/firebase.utils.js - - - - - -src/components/shop-template-editor/shop-template-editor.component.jsx - -src/components/shop-template-editor/shop-template-editor.component.jsx - - - -src/components/shop-template-test-render/shop-template-test-render.component.jsx - -src/components/shop-template-test-render/shop-template-test-render.component.jsx - - - -src/components/shop-template-editor/shop-template-editor.component.jsx->src/components/shop-template-test-render/shop-template-test-render.component.jsx - - - - - -src/components/shop-template-test-render/shop-template-test-render.component.jsx->src/App/App.container.jsx - - - - - -src/components/shop-template-test-render/shop-template-test-render.component.jsx->src/utils/RenderTemplate.js - - - - - -src/components/shop-template-editor/shop-template-editor.container.jsx - -src/components/shop-template-editor/shop-template-editor.container.jsx - - - -src/components/shop-template-editor/shop-template-editor.container.jsx->src/components/shop-template-editor/shop-template-editor.component.jsx - - - - - -src/components/shop-templates-list/shop-templates-list.container.jsx - -src/components/shop-templates-list/shop-templates-list.container.jsx - - - -src/components/shop-templates-list/shop-templates-list.container.jsx->src/components/shop-template-add/shop-template-add.component.jsx - - - - - -src/redux/root.saga.js - -src/redux/root.saga.js - - - -src/redux/store.js->src/redux/root.saga.js - - - - - -src/pages/bills/bills.page.container.jsx - -src/pages/bills/bills.page.container.jsx - - - -src/pages/bills/bills.page.container.jsx->src/components/bill-detail-edit/bill-detail-edit.container.jsx - - - - - -src/pages/manage/manage.page.component.jsx->src/App/App.container.jsx - - - - - -src/pages/manage/manage.page.component.jsx->src/components/error-boundary/error-boundary.component.jsx - - - - - -src/pages/manage/manage.page.component.jsx->src/components/bill-enter-modal/bill-enter-modal.container.jsx - - - - - -src/pages/manage/manage.page.component.jsx->src/components/email-overlay/email-overlay.container.jsx - - - - - -src/pages/manage/manage.page.component.jsx->src/pages/bills/bills.page.container.jsx - - - - - -src/pages/shop-templates/shop-templates.container.js - -src/pages/shop-templates/shop-templates.container.js - - - -src/pages/manage/manage.page.component.jsx->src/pages/shop-templates/shop-templates.container.js - - - - - -src/pages/shop-templates/shop-templates.container.js->src/components/shop-template-editor/shop-template-editor.container.jsx - - - - - -src/pages/shop-templates/shop-templates.container.js->src/components/shop-templates-list/shop-templates-list.container.jsx - - - - - -src/redux/application/application.sagas.js - -src/redux/application/application.sagas.js - - - -src/redux/application/application.sagas.js->src/App/App.container.jsx - - - - - -src/redux/messaging/messaging.sagas.js - -src/redux/messaging/messaging.sagas.js - - - -src/redux/messaging/messaging.sagas.js->src/App/App.container.jsx - - - - - -src/redux/messaging/messaging.sagas.js->src/firebase/firebase.utils.js - - - - - -src/redux/root.saga.js->src/redux/application/application.sagas.js - - - - - -src/redux/root.saga.js->src/redux/messaging/messaging.sagas.js - - - - - -src/redux/tech/tech.sagas.js - -src/redux/tech/tech.sagas.js - - - -src/redux/root.saga.js->src/redux/tech/tech.sagas.js - - - - - -src/redux/user/user.sagas.js - -src/redux/user/user.sagas.js - - - -src/redux/root.saga.js->src/redux/user/user.sagas.js - - - - - -src/redux/tech/tech.sagas.js->src/firebase/firebase.utils.js - - - - - -src/redux/user/user.sagas.js->src/firebase/firebase.utils.js - - - + diff --git a/client/src/App/App.container.jsx b/client/src/App/App.container.jsx index 58199bc5a..afc980a1e 100644 --- a/client/src/App/App.container.jsx +++ b/client/src/App/App.container.jsx @@ -1,160 +1,16 @@ import { ApolloProvider } from "@apollo/react-common"; import { ConfigProvider } from "antd"; import enLocale from "antd/es/locale/en_US"; -import { ApolloLink } from "apollo-boost"; -import { InMemoryCache } from "apollo-cache-inmemory"; -import ApolloClient from "apollo-client"; -import { split } from "apollo-link"; -import { setContext } from "@apollo/client/link/context"; -import { HttpLink } from "@apollo/client/link/http"; //"apollo-link-http"; -import apolloLogger from "apollo-link-logger"; -import { RetryLink } from "@apollo/client/link/retry"; -import { WebSocketLink } from "@apollo/client/link/ws"; -import { getMainDefinition } from "apollo-utilities"; -import axios from "axios"; import LogRocket from "logrocket"; import moment from "moment"; import React from "react"; import GlobalLoadingBar from "../components/global-loading-bar/global-loading-bar.component"; -import { auth } from "../firebase/firebase.utils"; -import errorLink from "../graphql/apollo-error-handling"; +import client from "../utils/GraphQLClient"; import App from "./App"; - moment.locale("en-US"); -//if (process.env.NODE_ENV === "prodution") -axios.defaults.baseURL = - process.env.REACT_APP_AXIOS_BASE_API_URL || "https://api.imex.online/"; -console.log( - " process.env.REACT_APP_AXIOS_BASE_API_URL ", - process.env.REACT_APP_AXIOS_BASE_API_URL -); -export const axiosAuthInterceptorId = axios.interceptors.request.use( - async (config) => { - if (!config.headers.Authorization) { - const token = - auth.currentUser && (await auth.currentUser.getIdToken(true)); - if (token) { - config.headers.Authorization = `Bearer ${token}`; - } - } - - return config; - }, - (error) => Promise.reject(error) -); - -export const cleanAxios = axios.create(); -cleanAxios.interceptors.request.eject(axiosAuthInterceptorId); - if (process.env.NODE_ENV === "production") LogRocket.init("gvfvfw/bodyshopapp"); -const httpLink = new HttpLink({ - uri: process.env.REACT_APP_GRAPHQL_ENDPOINT, -}); - -const wsLink = new WebSocketLink({ - uri: process.env.REACT_APP_GRAPHQL_ENDPOINT_WS, - options: { - lazy: true, - reconnect: true, - connectionParams: async () => { - const token = - auth.currentUser && (await auth.currentUser.getIdToken(true)); - if (token) { - return { - headers: { - authorization: token ? `Bearer ${token}` : "", - }, - }; - } - }, - }, -}); - -const subscriptionMiddleware = { - applyMiddleware: async (options, next) => { - options.authToken = - auth.currentUser && (await auth.currentUser.getIdToken(true)); - next(); - }, -}; -wsLink.subscriptionClient.use([subscriptionMiddleware]); - -const link = split( - // split based on operation type - ({ query }) => { - const definition = getMainDefinition(query); - // console.log( - // "##Intercepted GQL Transaction : " + - // definition.operation + - // "|" + - // definition.name.value + - // "##", - // query - // ); - return ( - definition.kind === "OperationDefinition" && - definition.operation === "subscription" - ); - }, - wsLink, - httpLink -); - -const authLink = setContext((_, { headers }) => { - return ( - auth.currentUser && - auth.currentUser.getIdToken().then((token) => { - if (token) { - return { - headers: { - ...headers, - authorization: token ? `Bearer ${token}` : "", - }, - }; - } else { - return { headers }; - } - }) - ); -}); - -const retryLink = new RetryLink({ - delay: { - initial: 500, - max: 5, - jitter: true, - }, - attempts: { - max: 5, - retryIf: (error, _operation) => !!error, - }, -}); - -const middlewares = []; -if (process.env.NODE_ENV === "development") { - middlewares.push(apolloLogger); -} - -middlewares.push(retryLink.concat(errorLink.concat(authLink.concat(link)))); - -const cache = new InMemoryCache({}); - -export const client = new ApolloClient({ - link: ApolloLink.from(middlewares), - cache, - connectToDevTools: process.env.NODE_ENV !== "production", - defaultOptions: { - query: { - fetchPolicy: "network-only", - }, - watchQuery: { - fetchPolicy: "network-only", - }, - }, -}); - export default function AppContainer() { return ( diff --git a/client/src/components/documents-upload/documents-upload.utility.js b/client/src/components/documents-upload/documents-upload.utility.js index 025dfc4d1..5ae6f986f 100644 --- a/client/src/components/documents-upload/documents-upload.utility.js +++ b/client/src/components/documents-upload/documents-upload.utility.js @@ -1,7 +1,7 @@ import { notification } from "antd"; import axios from "axios"; import i18n from "i18next"; -import { axiosAuthInterceptorId, client } from "../../App/App.container"; +import client, { axiosAuthInterceptorId } from "../../utils/CleanAxios"; import { logImEXEvent } from "../../firebase/firebase.utils"; import { INSERT_NEW_DOCUMENT } from "../../graphql/documents.queries"; //Context: currentUserEmail, bodyshop, jobid, invoiceid diff --git a/client/src/components/jobs-documents-gallery/jobs-documents-gallery.delete.component.jsx b/client/src/components/jobs-documents-gallery/jobs-documents-gallery.delete.component.jsx index 62914c0e5..6fda5e113 100644 --- a/client/src/components/jobs-documents-gallery/jobs-documents-gallery.delete.component.jsx +++ b/client/src/components/jobs-documents-gallery/jobs-documents-gallery.delete.component.jsx @@ -4,9 +4,9 @@ import { Button, notification, Popconfirm } from "antd"; import axios from "axios"; import React, { useState } from "react"; import { useTranslation } from "react-i18next"; -import { cleanAxios } from "../../App/App.container"; import { logImEXEvent } from "../../firebase/firebase.utils"; import { DELETE_DOCUMENT } from "../../graphql/documents.queries"; +import cleanAxios from "../../utils/CleanAxios"; //Context: currentUserEmail, bodyshop, jobid, invoiceid export default function JobsDocumentsDeleteButton({ diff --git a/client/src/components/shop-template-add/shop-template-add.component.jsx b/client/src/components/shop-template-add/shop-template-add.component.jsx index d8d021a00..47a071652 100644 --- a/client/src/components/shop-template-add/shop-template-add.component.jsx +++ b/client/src/components/shop-template-add/shop-template-add.component.jsx @@ -14,7 +14,7 @@ import { import { selectBodyshop } from "../../redux/user/user.selectors"; import { TemplateList } from "../../utils/TemplateConstants"; import { logImEXEvent } from "../../firebase/firebase.utils"; -import { client } from "../../App/App.container"; +import client from "../../utils/GraphQLClient"; const mapStateToProps = createStructuredSelector({ //currentUser: selectCurrentUser diff --git a/client/src/components/shop-template-test-render/shop-template-test-render.component.jsx b/client/src/components/shop-template-test-render/shop-template-test-render.component.jsx index 6b7ad63c4..438bc8912 100644 --- a/client/src/components/shop-template-test-render/shop-template-test-render.component.jsx +++ b/client/src/components/shop-template-test-render/shop-template-test-render.component.jsx @@ -8,7 +8,7 @@ import React, { useState } from "react"; import { useTranslation } from "react-i18next"; import { connect } from "react-redux"; import { createStructuredSelector } from "reselect"; -import { client } from "../../App/App.container"; +import client from "../../utils/GraphQLClient"; import { selectBodyshop } from "../../redux/user/user.selectors"; import { displayTemplateInWindowNoprint } from "../../utils/RenderTemplate"; diff --git a/client/src/firebase/firebase.utils.js b/client/src/firebase/firebase.utils.js index 6d7f4a3a4..9d4255c2e 100644 --- a/client/src/firebase/firebase.utils.js +++ b/client/src/firebase/firebase.utils.js @@ -1,10 +1,10 @@ +import "firebase/analytics"; import firebase from "firebase/app"; -import "firebase/firestore"; import "firebase/auth"; import "firebase/database"; -import "firebase/analytics"; +import "firebase/firestore"; import "firebase/messaging"; -import { store } from "../redux/store"; +//import { store } from "../redux/store"; const config = JSON.parse(process.env.REACT_APP_FIREBASE_CONFIG); firebase.initializeApp(config); @@ -48,14 +48,14 @@ try { export { messaging }; export const logImEXEvent = (eventName, additionalParams, stateProp = null) => { - const state = stateProp || store.getState(); + // const state = stateProp || store.getState(); const eventParams = { - shop: - (state.user && state.user.bodyshop && state.user.bodyshop.shopname) || - null, - user: - (state.user && state.user.currentUser && state.user.currentUser.email) || - null, + // shop: + // (state.user && state.user.bodyshop && state.user.bodyshop.shopname) || + // null, + // user: + // (state.user && state.user.currentUser && state.user.currentUser.email) || + // null, ...additionalParams, }; analytics.logEvent(eventName, eventParams); diff --git a/client/src/pages/manage/manage.page.component.jsx b/client/src/pages/manage/manage.page.component.jsx index 8239e793d..18ca730a1 100644 --- a/client/src/pages/manage/manage.page.component.jsx +++ b/client/src/pages/manage/manage.page.component.jsx @@ -6,7 +6,7 @@ import { useTranslation } from "react-i18next"; import { connect } from "react-redux"; import { Route, Switch } from "react-router-dom"; import { createStructuredSelector } from "reselect"; -import { client } from "../../App/App.container"; +import client from "../../utils/GraphQLClient"; import BreadCrumbs from "../../components/breadcrumbs/breadcrumbs.component"; import ChatAffixContainer from "../../components/chat-affix/chat-affix.container"; import ConflictComponent from "../../components/conflict/conflict.component"; diff --git a/client/src/redux/application/application.sagas.js b/client/src/redux/application/application.sagas.js index f90453ba9..69bd72263 100644 --- a/client/src/redux/application/application.sagas.js +++ b/client/src/redux/application/application.sagas.js @@ -1,6 +1,6 @@ import { all, takeLatest, call, put } from "redux-saga/effects"; import ApplicationActionTypes from "./application.types"; -import { client } from "../../App/App.container"; +import client from "../../utils/GraphQLClient"; import { QUERY_SCHEDULE_LOAD_DATA } from "../../graphql/appointments.queries"; import { scheduleLoadFailure, diff --git a/client/src/redux/messaging/messaging.sagas.js b/client/src/redux/messaging/messaging.sagas.js index 8d45112a6..c3db8d974 100644 --- a/client/src/redux/messaging/messaging.sagas.js +++ b/client/src/redux/messaging/messaging.sagas.js @@ -1,7 +1,7 @@ import axios from "axios"; import phone from "phone"; import { all, call, put, select, takeLatest } from "redux-saga/effects"; -import { client } from "../../App/App.container"; +import client from "../../utils/GraphQLClient"; import { logImEXEvent } from "../../firebase/firebase.utils"; import { CONVERSATION_ID_BY_PHONE, diff --git a/client/src/utils/CleanAxios.js b/client/src/utils/CleanAxios.js new file mode 100644 index 000000000..1f7b6591a --- /dev/null +++ b/client/src/utils/CleanAxios.js @@ -0,0 +1,27 @@ +import axios from "axios"; +import { auth } from "../firebase/firebase.utils"; + +if (process.env.NODE_ENV === "prodution") { + axios.defaults.baseURL = + process.env.REACT_APP_AXIOS_BASE_API_URL || "https://api.imex.online/"; +} + +export const axiosAuthInterceptorId = axios.interceptors.request.use( + async (config) => { + if (!config.headers.Authorization) { + const token = + auth.currentUser && (await auth.currentUser.getIdToken(true)); + if (token) { + config.headers.Authorization = `Bearer ${token}`; + } + } + + return config; + }, + (error) => Promise.reject(error) +); + +const cleanAxios = axios.create(); +cleanAxios.interceptors.request.eject(axiosAuthInterceptorId); + +export default cleanAxios; diff --git a/client/src/utils/GraphQLClient.js b/client/src/utils/GraphQLClient.js new file mode 100644 index 000000000..b827008ae --- /dev/null +++ b/client/src/utils/GraphQLClient.js @@ -0,0 +1,118 @@ +import { setContext } from "@apollo/client/link/context"; +import { HttpLink } from "@apollo/client/link/http"; //"apollo-link-http"; +import { RetryLink } from "@apollo/client/link/retry"; +import { WebSocketLink } from "@apollo/client/link/ws"; +import { ApolloLink } from "apollo-boost"; +import { InMemoryCache } from "apollo-cache-inmemory"; +import ApolloClient from "apollo-client"; +import { split } from "apollo-link"; +import apolloLogger from "apollo-link-logger"; +import { getMainDefinition } from "apollo-utilities"; +import { auth } from "../firebase/firebase.utils"; +import errorLink from "../graphql/apollo-error-handling"; + +const httpLink = new HttpLink({ + uri: process.env.REACT_APP_GRAPHQL_ENDPOINT, +}); + +const wsLink = new WebSocketLink({ + uri: process.env.REACT_APP_GRAPHQL_ENDPOINT_WS, + options: { + lazy: true, + reconnect: true, + connectionParams: async () => { + const token = + auth.currentUser && (await auth.currentUser.getIdToken(true)); + if (token) { + return { + headers: { + authorization: token ? `Bearer ${token}` : "", + }, + }; + } + }, + }, +}); + +const subscriptionMiddleware = { + applyMiddleware: async (options, next) => { + options.authToken = + auth.currentUser && (await auth.currentUser.getIdToken(true)); + next(); + }, +}; +wsLink.subscriptionClient.use([subscriptionMiddleware]); + +const link = split( + // split based on operation type + ({ query }) => { + const definition = getMainDefinition(query); + // console.log( + // "##Intercepted GQL Transaction : " + + // definition.operation + + // "|" + + // definition.name.value + + // "##", + // query + // ); + return ( + definition.kind === "OperationDefinition" && + definition.operation === "subscription" + ); + }, + wsLink, + httpLink +); + +const authLink = setContext((_, { headers }) => { + return ( + auth.currentUser && + auth.currentUser.getIdToken().then((token) => { + if (token) { + return { + headers: { + ...headers, + authorization: token ? `Bearer ${token}` : "", + }, + }; + } else { + return { headers }; + } + }) + ); +}); + +const retryLink = new RetryLink({ + delay: { + initial: 500, + max: 5, + jitter: true, + }, + attempts: { + max: 5, + retryIf: (error, _operation) => !!error, + }, +}); + +const middlewares = []; +if (process.env.NODE_ENV === "development") { + middlewares.push(apolloLogger); +} + +middlewares.push(retryLink.concat(errorLink.concat(authLink.concat(link)))); + +const cache = new InMemoryCache({}); + +export default new ApolloClient({ + link: ApolloLink.from(middlewares), + cache, + connectToDevTools: process.env.NODE_ENV !== "production", + defaultOptions: { + query: { + fetchPolicy: "network-only", + }, + watchQuery: { + fetchPolicy: "network-only", + }, + }, +}); diff --git a/client/src/utils/RenderTemplate.js b/client/src/utils/RenderTemplate.js index fd64a8b0a..0f91bed13 100644 --- a/client/src/utils/RenderTemplate.js +++ b/client/src/utils/RenderTemplate.js @@ -1,7 +1,7 @@ +import axios from "axios"; import gql from "graphql-tag"; import { QUERY_TEMPLATES_BY_NAME } from "../graphql/templates.queries"; -import axios from "axios"; -import { client } from "../App/App.container"; +import client from "../utils/GraphQLClient"; export default async function RenderTemplate(templateObject, bodyshop) { const { data: templateRecords } = await client.query({ diff --git a/client/src/utils/dates.js b/client/src/utils/dates.js deleted file mode 100644 index 60d0c25ef..000000000 --- a/client/src/utils/dates.js +++ /dev/null @@ -1,170 +0,0 @@ -/* eslint no-fallthrough: off */ -import * as dates from "date-arithmetic"; - -export { - milliseconds, - seconds, - minutes, - hours, - month, - startOf, - endOf, - add, - eq, - gte, - gt, - lte, - lt, - inRange, - min, - max -} from "date-arithmetic"; - -const MILLI = { - seconds: 1000, - minutes: 1000 * 60, - hours: 1000 * 60 * 60, - day: 1000 * 60 * 60 * 24 -}; - -const MONTHS = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]; - -export function monthsInYear(year) { - let date = new Date(year, 0, 1); - - return MONTHS.map(i => dates.month(date, i)); -} - -export function firstVisibleDay(date, localizer) { - let firstOfMonth = dates.startOf(date, "month"); - - return dates.startOf(firstOfMonth, "week", localizer.startOfWeek()); -} - -export function lastVisibleDay(date, localizer) { - let endOfMonth = dates.endOf(date, "month"); - - return dates.endOf(endOfMonth, "week", localizer.startOfWeek()); -} - -export function visibleDays(date, localizer) { - let current = firstVisibleDay(date, localizer), - last = lastVisibleDay(date, localizer), - days = []; - - while (dates.lte(current, last, "day")) { - days.push(current); - current = dates.add(current, 1, "day"); - } - - return days; -} - -export function ceil(date, unit) { - let floor = dates.startOf(date, unit); - - return dates.eq(floor, date) ? floor : dates.add(floor, 1, unit); -} - -export function range(start, end, unit = "day") { - let current = start, - days = []; - - while (dates.lte(current, end, unit)) { - days.push(current); - current = dates.add(current, 1, unit); - } - - return days; -} - -export function merge(date, time) { - if (time == null && date == null) return null; - - if (time == null) time = new Date(); - if (date == null) date = new Date(); - - date = dates.startOf(date, "day"); - date = dates.hours(date, dates.hours(time)); - date = dates.minutes(date, dates.minutes(time)); - date = dates.seconds(date, dates.seconds(time)); - return dates.milliseconds(date, dates.milliseconds(time)); -} - -export function eqTime(dateA, dateB) { - return ( - dates.hours(dateA) === dates.hours(dateB) && - dates.minutes(dateA) === dates.minutes(dateB) && - dates.seconds(dateA) === dates.seconds(dateB) - ); -} - -export function isJustDate(date) { - return ( - dates.hours(date) === 0 && - dates.minutes(date) === 0 && - dates.seconds(date) === 0 && - dates.milliseconds(date) === 0 - ); -} - -export function duration(start, end, unit, firstOfWeek) { - if (unit === "day") unit = "date"; - return Math.abs( - dates[unit](start, undefined, firstOfWeek) - - dates[unit](end, undefined, firstOfWeek) - ); -} - -export function diff(dateA, dateB, unit) { - if (!unit || unit === "milliseconds") return Math.abs(+dateA - +dateB); - - // the .round() handles an edge case - // with DST where the total won't be exact - // since one day in the range may be shorter/longer by an hour - return Math.round( - Math.abs( - +dates.startOf(dateA, unit) / MILLI[unit] - - +dates.startOf(dateB, unit) / MILLI[unit] - ) - ); -} - -export function total(date, unit) { - let ms = date.getTime(), - div = 1; - - switch (unit) { - case "week": - div *= 7; - case "day": - div *= 24; - case "hours": - div *= 60; - case "minutes": - div *= 60; - case "seconds": - div *= 1000; - } - - return ms / div; -} - -export function week(date) { - var d = new Date(date); - d.setHours(0, 0, 0); - d.setDate(d.getDate() + 4 - (d.getDay() || 7)); - return Math.ceil(((d - new Date(d.getFullYear(), 0, 1)) / 8.64e7 + 1) / 7); -} - -export function today() { - return dates.startOf(new Date(), "day"); -} - -export function yesterday() { - return dates.add(dates.startOf(new Date(), "day"), -1, "day"); -} - -export function tomorrow() { - return dates.add(dates.startOf(new Date(), "day"), 1, "day"); -}