Skip to content

Commit

Permalink
Merge branch 'develop' into TASK-387-sort-and-rendering-fix-for-dropd…
Browse files Browse the repository at this point in the history
…own-shifts
  • Loading branch information
Qwebeck authored Mar 18, 2021
2 parents 610c227 + e541824 commit 067aee3
Show file tree
Hide file tree
Showing 10 changed files with 193 additions and 134 deletions.
29 changes: 19 additions & 10 deletions src/api/local-storage-provider.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
} from "../common-models/schedule-data.model";

import {
getRevisionTypeFromKey,
MonthRevision,
PersistenceStoreProvider,
RevisionKey,
Expand All @@ -37,7 +38,6 @@ export class LocalStorageProvider extends PersistenceStoreProvider {
super();
this.storage = new PouchDB(DATABASE_NAME);
}

async reloadDb(): Promise<void> {
try {
await this.storage.destroy();
Expand All @@ -61,10 +61,16 @@ export class LocalStorageProvider extends PersistenceStoreProvider {
const oppositeRevision = await this.getMonthRevision(
monthDataModel.scheduleKey.getRevisionKey(oppositeRevisionType)
);
const isMonthInFuture = VerboseDateHelper.isMonthInFuture(
monthDataModel.scheduleKey.month,
monthDataModel.scheduleKey.year
);

if (
_.isNil(oppositeRevision) ||
oppositeRevision.isAutoGenerated ||
oppositeRevisionType === "actual"
oppositeRevisionType === "actual" ||
isMonthInFuture
) {
await this.saveMonthRevision(oppositeRevisionType, monthDataModel);
}
Expand All @@ -86,6 +92,7 @@ export class LocalStorageProvider extends PersistenceStoreProvider {
// eslint-disable-next-line no-console
error.status !== 404 && console.error(error);
}

const monthRev: MonthRevision = {
_id: revisionKey,
data: monthDataModel,
Expand All @@ -112,8 +119,11 @@ export class LocalStorageProvider extends PersistenceStoreProvider {
);

if (daysMissingFromNextMonth !== 0) {
const nextMonthRevisionKey = getScheduleKey(scheduleDataModel).nextMonthKey.getRevisionKey(
type
);
await this.updateMonthPartBasedOnScheduleDM(
getScheduleKey(scheduleDataModel).nextMonthKey.getRevisionKey(type),
nextMonthRevisionKey,
scheduleDataModel,
daysMissingFromNextMonth,
"HEAD"
Expand All @@ -128,8 +138,8 @@ export class LocalStorageProvider extends PersistenceStoreProvider {
updatePosition: ArrayPositionPointer
): Promise<void> {
try {
const document = await this.storage.get(revisionKey);
const updatedMonthDataModel = document.data;
const updatedMonthDataModel = await this.getMonthRevision(revisionKey);
if (_.isNil(updatedMonthDataModel)) return;

const newShifts = _.cloneDeep(updatedMonthDataModel.shifts);

Expand Down Expand Up @@ -160,11 +170,10 @@ export class LocalStorageProvider extends PersistenceStoreProvider {
};

validateMonthDM(updatedMonthDataModel);
this.storage.put({
_rev: document._rev,
_id: revisionKey,
data: updatedMonthDataModel,
});
await this.saveBothMonthRevisionsIfNeeded(
getRevisionTypeFromKey(revisionKey),
updatedMonthDataModel
);
} catch (error) {
// eslint-disable-next-line no-console
error.status !== 404 && console.error(error);
Expand Down
50 changes: 17 additions & 33 deletions src/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ import ScssVars from "./assets/styles/styles/custom/_variables.module.scss";
import { ApplicationStateModel } from "./state/models/application-state.model";
import { ScheduleKey } from "./api/persistance-store.model";
import { AppMode, useAppConfig } from "./state/app-config-context";
import * as Sentry from "@sentry/react";
import AppErrorModal from "./components/common-components/modal/app-error-modal/app-error.modal.component";
import { cropScheduleDMToMonthDM } from "./logic/schedule-container-convertion/schedule-container-convertion";

const useStyles = makeStyles(() => ({
Expand Down Expand Up @@ -104,39 +102,25 @@ function App(): JSX.Element {
fetchGlobalState();
}, [fetchGlobalState]);

const [open, setIsOpen] = useState(false);
const fallback = useCallback(
({ resetError }): JSX.Element => (
<AppErrorModal onClick={resetError} open={open} setOpen={setIsOpen} />
),
[open, setIsOpen]
);

const onError = useCallback((): void => {
setIsOpen(true);
}, [setIsOpen]);

return (
<Sentry.ErrorBoundary fallback={fallback} onError={onError}>
<NotificationProvider>
<JiraLikeDrawerProvider>
<Switch>
<Route path="/">
<Box className={classes.root}>
<Box className={classes.content}>
<HeaderComponent />
<RouteButtonsComponent tabs={tabs} disabled={disableRouteButtons} />
</Box>
<Box className={classes.drawer}>
<JiraLikeDrawer width={690} />
</Box>
<NotificationProvider>
<JiraLikeDrawerProvider>
<Switch>
<Route path="/">
<Box className={classes.root}>
<Box className={classes.content}>
<HeaderComponent />
<RouteButtonsComponent tabs={tabs} disabled={disableRouteButtons} />
</Box>
<Box className={classes.drawer}>
<JiraLikeDrawer width={690} />
</Box>
{isElectron() ? <></> : <NetlifyProFooter />}
</Route>
</Switch>
</JiraLikeDrawerProvider>
</NotificationProvider>
</Sentry.ErrorBoundary>
</Box>
{isElectron() ? <></> : <NetlifyProFooter />}
</Route>
</Switch>
</JiraLikeDrawerProvider>
</NotificationProvider>
);
}

Expand Down
27 changes: 27 additions & 0 deletions src/components/app-error-boundary/app-error-boundary.component.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */

import React, { ReactNode, useCallback, useState } from "react";
import AppErrorModal from "../common-components/modal/app-error-modal/app-error.modal.component";
import * as Sentry from "@sentry/react";

interface AppErrorBoundaryOptions {
children: ReactNode;
}

export function AppErrorBoundary(props: AppErrorBoundaryOptions): JSX.Element {
const [open, setIsOpen] = useState(false);
const fallback = useCallback(
({ resetError }): JSX.Element => (
<AppErrorModal onClick={resetError} open={open} setOpen={setIsOpen} />
),
[open, setIsOpen]
);

const onError = useCallback((): void => {
setIsOpen(true);
}, [setIsOpen]);

return <Sentry.ErrorBoundary fallback={fallback} onError={onError} {...props} />;
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
import React from "react";
import React, { useEffect } from "react";
import DrawerHeader from "./drawer-header.component";
import { Box } from "@material-ui/core";
import { makeStyles, Theme } from "@material-ui/core/styles";
import { useJiraLikeDrawer } from "./jira-like-drawer-context";
import ScssVars from "../../../assets/styles/styles/custom/_variables.module.scss";
import { useSelector } from "react-redux";
import { ApplicationStateModel } from "../../../state/models/application-state.model";

export interface StyleProps {
width: number;
Expand All @@ -25,12 +27,19 @@ const useStyles = makeStyles<Theme, StyleProps>({
});

export default function JiraLikeDrawer(width): JSX.Element {
const isEditMode = useSelector(
(state: ApplicationStateModel) => state.actualState.mode === "edit"
);
const classes = useStyles(width);
const { title, open, setOpen, childrenComponent } = useJiraLikeDrawer();

useEffect(() => {
if (!isEditMode) setOpen(false);
}, [isEditMode, setOpen]);

return (
<Box>
{title && open && setOpen && childrenComponent && (
{title && open && setOpen && childrenComponent && isEditMode && (
<Box className={classes.drawer}>
<DrawerHeader title={title} setOpen={setOpen}>
{childrenComponent}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,14 @@ export default function ReportIssueModal(options: ReportIssueModalOptions): JSX.
});
}

// Only for testing purposes
if (
process.env.REACT_APP_TEST_MODE &&
issueDescription.toLowerCase() === process.env.REACT_APP_ERROR_WORKER
) {
throw new Error("[TEST MODE] Error message was entered");
}

const body = (
<div className="report-issue-modal-body">
{isSent && <p>Wysłano powiadomienie o błędzie.</p>}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export function ScheduleComponent({
.map((worker) => worker.toLowerCase())
.includes(process.env.REACT_APP_ERROR_WORKER ?? "ERROR")
) {
throw new Error("Schedule in dev mode includes error user");
throw new Error("[TEST MODE] Error user was added");
}

return (
Expand Down
19 changes: 6 additions & 13 deletions src/helpers/month.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class MonthHelper {
): T[] {
const { daysMissingFromNextMonth } = this.calculateMissingFullWeekDays(scheduleKey);
const monthLen = monthData.length;
let lastWeek: T[] = [];
let lastWeek: T[];
if (daysMissingFromNextMonth > 0) {
const daysFromCurrentMonthInLastWeek = NUMBER_OF_DAYS_IN_WEEK - daysMissingFromNextMonth;
const currentMonthDataPart = monthData.slice(
Expand All @@ -34,7 +34,7 @@ export class MonthHelper {
}

if (lastWeek.length !== NUMBER_OF_DAYS_IN_WEEK) {
throw new Error(`Week must have ${NUMBER_OF_DAYS_IN_WEEK} days`);
throw new Error(`Week must have ${NUMBER_OF_DAYS_IN_WEEK} days not ${lastWeek.length}`);
}

return lastWeek;
Expand All @@ -56,15 +56,7 @@ export class MonthHelper {
}

static findFirstMonthMondayIdx(year: number, month: number): number {
return _.findIndex(
_.range(1, 1 + NUMBER_OF_DAYS_IN_WEEK).map((day, idx) => {
return {
weekDay: new Date(year, month, day).getDay(),
idx,
};
}),
(day) => day.weekDay === 1
);
return (NUMBER_OF_DAYS_IN_WEEK - new Date(year, month).getDay() + 1) % NUMBER_OF_DAYS_IN_WEEK;
}

static daysInMonth(month = 0, year = 0): number[] {
Expand All @@ -81,8 +73,9 @@ export class MonthHelper {
const firstMonthDay = new Date(year, month, 1).getDay();
const lastMonthDay = new Date(year, month + 1, 0).getDay();
return {
daysMissingFromPrevMonth: firstMonthDay === 0 ? 6 : firstMonthDay - 1,
daysMissingFromNextMonth: lastMonthDay === 0 ? 0 : 7 - lastMonthDay,
daysMissingFromPrevMonth:
(firstMonthDay + NUMBER_OF_DAYS_IN_WEEK - 1) % NUMBER_OF_DAYS_IN_WEEK,
daysMissingFromNextMonth: (NUMBER_OF_DAYS_IN_WEEK - lastMonthDay) % NUMBER_OF_DAYS_IN_WEEK,
};
}

Expand Down
25 changes: 14 additions & 11 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import thunkMiddleware from "redux-thunk";
import { appReducer } from "./state/app.reducer";
import { createBrowserHistory } from "history";
import { composeWithDevTools } from "redux-devtools-extension";
import { AppErrorBoundary } from "./components/app-error-boundary/app-error-boundary.component";

const history = createBrowserHistory();

Expand All @@ -42,17 +43,19 @@ const composedEnhancer = composeWithDevTools(
export const appStore = createStore(appReducer, composedEnhancer);

ReactDOM.render(
<DndProvider backend={HTML5Backend}>
<Router history={history}>
<React.StrictMode>
<Provider store={appStore}>
<AppConfigProvider>
<App />
</AppConfigProvider>
</Provider>
</React.StrictMode>
</Router>
</DndProvider>,
<AppErrorBoundary>
<DndProvider backend={HTML5Backend}>
<Router history={history}>
<React.StrictMode>
<Provider store={appStore}>
<AppConfigProvider>
<App />
</AppConfigProvider>
</Provider>
</React.StrictMode>
</Router>
</DndProvider>
</AppErrorBoundary>,
document.getElementById("root")
);
/* eslint-disable @typescript-eslint/no-explicit-any */
Expand Down
Loading

0 comments on commit 067aee3

Please sign in to comment.