Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error handling #186

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,11 @@
"es6-promise": "^4.2.8",
"formik": "^2.2.9",
"graphql": "^15.7.2",
"neverthrow": "^4.2.2",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-router-dom": "^5.3.0",
"react-toastify": "^8.0.3",
"regenerator-runtime": "^0.13.9"
},
"devDependencies": {
Expand Down
File renamed without changes.
22 changes: 20 additions & 2 deletions src/taskpane/components/App.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import React, { useEffect, useState, ReactElement } from "react";
import { Switch, Route } from "react-router-dom";
import { ToastContainer } from "react-toastify";
import Progress from "./Progress";
import Login from "../pages/login";
import Layout from "./Layout";
import ProtectedRoutes from "../../utils/ProtectedRoutes";
import Layout from "../layout/Layout";
import ProtectedRoutes from "../router/ProtectedRoutes";
import CiteSupport from "../../utils/citesupport";
import data from "../../utils/data";

Expand Down Expand Up @@ -31,6 +32,23 @@ function App(props: AppProps): ReactElement {
}
return (
<div>
<ToastContainer
position="top-center"
closeButton={false}
hideProgressBar
newestOnTop
closeOnClick
limit={5}
rtl={false}
draggable={false}
pauseOnHover
autoClose={3000} // 3 seconds
bodyStyle={{ padding: 0 }}
style={{ marginTop: 45, padding: 0 }}
toastClassName={() =>
"relative flex p-1 overflow-auto min-h-100vh box-border rounded-md justify-between cursor-pointer"
}
/>
<Switch>
<Route path="/login">
<Login />
Expand Down
36 changes: 36 additions & 0 deletions src/taskpane/components/Toasts.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { MessageBar, MessageBarButton, MessageBarType } from "@fluentui/react";
import React from "react";
import { toast } from "react-toastify";

export const successToast = (message: string): void => {
toast(
<MessageBar messageBarType={MessageBarType.success} isMultiline={false}>
{message}
</MessageBar>
);
};

export const errorToastWithActionButton = (message: string): void => {
toast(
({ closeToast }) => (
<MessageBar
actions={
<div>
<MessageBarButton onClick={closeToast}>Yes</MessageBarButton>
</div>
}
messageBarType={MessageBarType.blocked}
isMultiline
>
{message}
</MessageBar>
),
{
style: {
height: "100vh",
},
autoClose: false,
closeOnClick: false,
}
);
};
2 changes: 1 addition & 1 deletion src/taskpane/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { AppContainer } from "react-hot-loader";
import { initializeIcons, ThemeProvider } from "@fluentui/react";
import { HashRouter as Router } from "react-router-dom";
import App from "./components/App";
import client from "../utils/apolloClient";
import client from "../plugins/apolloClient";

initializeIcons();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ import {
} from "@fluentui/react";
import React, { ReactElement } from "react";
import { useLogoutMutation } from "../../generated/graphql";
import client from "../../utils/apolloClient";
import client from "../../plugins/apolloClient";
import CitationStyle from "../pages/citationStyle";
import Dashboard from "../pages/dashboard";
import Wrapper from "./Wrapper";
import Wrapper from "../components/Wrapper";
import CiteSupport from "../../utils/citesupport";

interface LayoutProps {
Expand Down
2 changes: 2 additions & 0 deletions src/taskpane/pages/citationStyle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
import { Stack } from "@fluentui/react";
import Preference from "../../utils/user-preference";
import CiteSupport from "../../utils/citesupport";
import { successToast } from "../components/Toasts";

const theme: ITheme = getTheme();
const { palette, semanticColors, fonts } = theme;
Expand Down Expand Up @@ -93,6 +94,7 @@ function CitationStyle({ citeSupport }: CitationStyleProps): JSX.Element {
setCurrentStyle(ev.currentTarget.id);
Preference.setCitationStyle(ev.currentTarget.id);
await citeSupport.initDocument();
successToast("Citation Style Updated Successfully");
};

// Sync with doc settings
Expand Down
10 changes: 7 additions & 3 deletions src/taskpane/pages/dashboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { CitationItem } from "citeproc";
import data from "../../utils/data";
import ReferenceList, { bib } from "../components/ReferenceList";
import SearchField from "../components/SearchField";
import "react-toastify/dist/ReactToastify.css";
import CiteSupport from "../../utils/citesupport";

interface DashboardProps {
Expand Down Expand Up @@ -164,9 +165,12 @@ function Dashboard({ citeSupport }: DashboardProps): ReactElement {
const isCitationValue = await citeSupport.wordApi.isCitationSelected();
if (itemsInSelectedCitation) {
resetAllReferences();
setReferenceState(itemsInCitation);
setItemsInSelectedCitation(itemsInCitation);
setCitationSelected(() => isCitationValue);
if (itemsInCitation.isOk()) {
setReferenceState(itemsInCitation.value);
setItemsInSelectedCitation(itemsInCitation.value);
}
if (isCitationValue.isOk())
setCitationSelected(() => isCitationValue.value);
} else if (itemsInSelectedCitation.current.length) {
resetAllReferences();
setItemsInSelectedCitation([]);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from "react";
import { Redirect, Route } from "react-router-dom";
import { useMeQuery } from "../generated/graphql";
import Progress from "../taskpane/components/Progress";
import { useMeQuery } from "../../generated/graphql";
import Progress from "../components/Progress";

interface ProtectedRoutesProps {
children: JSX.Element;
Expand All @@ -26,7 +26,7 @@ function ProtectedRoutes({
<Route
{...rest}
render={({ location }) =>
!data.me ? (
!data ? (
children
) : (
<Redirect
Expand Down
23 changes: 17 additions & 6 deletions src/utils/citesupport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ class CiteSupport {
this.debug("upsertCitation()");
const isCitationSelected = await this.wordApi.isCitationSelected();
const citationData = this.convertCitationDataToCustomFormat(data);
if (isCitationSelected) {
if (isCitationSelected.isOk() && isCitationSelected.value) {
await this.wordApi.updateCitations(citationData);
} else {
await this.wordApi.insertNewCitation(citationData[0]);
Expand Down Expand Up @@ -318,16 +318,19 @@ class CiteSupport {
if (citationStyle) {
this.config.defaultStyle = citationStyle;
}
this.config.citationByIndex = await this.wordApi.getCitationByIndex();
const citationByIndex = await this.wordApi.getCitationByIndex();
if (citationByIndex.isOk()) {
this.config.citationByIndex = citationByIndex.value;
}
}

/**
* Update the citationByIndex array after every edit or delete operation
*/
async updateCitationByIndex(): Promise<void> {
const citationByIndex = await this.wordApi.getCitationByIndex();
if (citationByIndex) {
this.config.citationByIndex = citationByIndex;
if (citationByIndex.isOk()) {
this.config.citationByIndex = citationByIndex.value;
}
}

Expand Down Expand Up @@ -356,9 +359,17 @@ class CiteSupport {
let i = 0;
let offset = 0;
if (!isCitation) {
i = await this.wordApi.getPositionOfNewCitation();
const positionOfNewCitation =
await this.wordApi.getPositionOfNewCitation();
if (positionOfNewCitation.isOk()) {
i = positionOfNewCitation.value;
}
} else {
i = await this.wordApi.getPositionOfSelectedCitation();
const positionOfSelectedCitation =
await this.wordApi.getPositionOfSelectedCitation();
if (positionOfSelectedCitation.isOk()) {
i = positionOfSelectedCitation.value;
}
offset = 1;
}
if (this.config.citationByIndex.slice(0, i).length) {
Expand Down
Loading