Skip to content

Commit

Permalink
added update docbase feature
Browse files Browse the repository at this point in the history
  • Loading branch information
cophilot committed Jan 27, 2024
1 parent 3eee597 commit bbf7c91
Show file tree
Hide file tree
Showing 8 changed files with 399 additions and 140 deletions.
Empty file removed .github/workflows/.gitkeep
Empty file.
42 changes: 39 additions & 3 deletions src/components/AttributeAdder/AttributeAdder.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,31 @@
import { useEffect, useState } from 'react';
import './AttributeAdder.scss';
import Icon from '../Icon/Icon';
import { useIsDocbaseTaskRunning } from '../../providers/DocBaseTaskProvider';

interface Props {
populateAble: boolean;
onListChange: (list: string[]) => void;
populateAble?: boolean;
rerunAble?: boolean;
onRerun?: (list: string[]) => void;
initialList?: string[];
}

/**
* A component that allows the user to add attributes to there document base
*/
function AttributeAdder({
populateAble = true,
onListChange,
populateAble = false,
rerunAble = false,
initialList = [],
// eslint-disable-next-line @typescript-eslint/no-unused-vars
onRerun = (_list: string[]) => {},
}: Props) {
const [attList, setAttList] = useState<string[]>([]);

const isDocBaseTaskRunning = useIsDocbaseTaskRunning();

useEffect(
() => {
if (
Expand Down Expand Up @@ -57,11 +65,28 @@ function AttributeAdder({
}
};

const isListEqualToInitialList = () => {
if (attList.length !== initialList.length) {
return false;
}
attList.forEach((att) => {
if (!initialList.includes(att)) {
return false;
}
});
return true;
};

return (
<div className="AttributeAdder">
{rerunAble && (
<p>
<i>Modify the attributes to rerun the document base.</i>
</p>
)}
{attList.length === 0 && (
<p>
<i>Enter a attribute...</i>
<i>Enter an attribute...</i>
</p>
)}
{attList.map((att, i) => {
Expand Down Expand Up @@ -121,6 +146,17 @@ function AttributeAdder({
Attributes
</button>
)}
{rerunAble &&
attList.length > 0 &&
!isListEqualToInitialList() &&
!isDocBaseTaskRunning() && (
<button
className="btn populateBtn"
onClick={() => onRerun(attList)}
>
<i className="bi bi-play icon"></i>Rerun
</button>
)}
</div>
);
}
Expand Down
19 changes: 16 additions & 3 deletions src/components/DocbaseViewer/DocbaseViewer.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { useUpdateDocbaseAttributesTask } from '../../providers/DocBaseTaskProvider';
import DocBase from '../../types/DocBase';
import AttributeAdder from '../AttributeAdder/AttributeAdder';
import NuggetDocumentViewer from '../NuggetViewer/NuggetDocumentViewer';
Expand All @@ -13,6 +14,8 @@ interface Props {
* @param docBase The docbase to view
*/
function DocbaseViewer({ docBase, onClose }: Props) {
const updateDocbaseAttributesTask = useUpdateDocbaseAttributesTask();

return (
<>
<div
Expand All @@ -25,14 +28,24 @@ function DocbaseViewer({ docBase, onClose }: Props) {
<i className="bi bi-x-lg"></i>
</div>
<div className="DocbaseViewer">
<h1>{docBase.name}</h1>
<h1>
DocBase: <i>{docBase.name}</i>
</h1>
<h2>Attributes</h2>
<AttributeAdder
populateAble={false}
rerunAble={true}
onListChange={(list: string[]) => {
docBase.attributes = list;
}}
initialList={docBase.attributes}
onRerun={(list: string[]) => {
updateDocbaseAttributesTask(
docBase.organizationId,
docBase.name,
list
);
onClose();
}}
></AttributeAdder>
{/* <ul className="ver">
{docBase.attributes.map((attribute, index) => {
Expand All @@ -47,7 +60,7 @@ function DocbaseViewer({ docBase, onClose }: Props) {
{docBase.docs.map((doc, index) => {
return <NuggetDocumentViewer key={index} doc={doc} />;
})}
<button className="btn mb" onClick={onClose}>
<button className="btn mb ml mt" onClick={onClose}>
Close
</button>
</div>
Expand Down
5 changes: 3 additions & 2 deletions src/components/Navbar/Navbar.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Link, useNavigate } from 'react-router-dom';
import './Navbar.scss';
import { useState } from 'react';
import { useGetUsername } from '../../providers/UserProvider';
import { useGetUsername, useLoggedIn } from '../../providers/UserProvider';
import { useIsDarkTheme, useToggleTheme } from '../../providers/ThemeProvider';
import Icon from '../Icon/Icon';

Expand All @@ -12,6 +12,7 @@ function Navbar() {
const navigate = useNavigate();

const getUserName = useGetUsername();
const isLoggedIn = useLoggedIn();

const [username] = useState(getUserName());

Expand Down Expand Up @@ -70,7 +71,7 @@ function Navbar() {
{/* <i className="bi bi-question-circle icon"></i> */}
Help
</Link>
{username !== '' ? (
{isLoggedIn() ? (
<Link to={'/profile'}>
<div className="profilePicture">
{username.slice(0, 1).toUpperCase()}
Expand Down
139 changes: 138 additions & 1 deletion src/providers/DocBaseTaskProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ const DocBaseTaskContext = React.createContext({
_documentIDs: number[],
_attributes: string[]
) => {},
updateDocbaseAttributesTask: (
_organizationId: number,
_baseName: string,
_attributes: string[]
) => {},
loadDocbaseTask: (_organizationId: number, _baseName: string) => {},
});

Expand All @@ -46,6 +51,17 @@ export function useCreateDocbaseTask() {
return context.createDocbaseTask;
}

// eslint-disable-next-line react-refresh/only-export-components
export function useUpdateDocbaseAttributesTask() {
const context = React.useContext(DocBaseTaskContext);
if (!context) {
throw new Error(
'useUpdateDocbaseAttributesTask must be used within a DocBaseTaskProvider'
);
}
return context.updateDocbaseAttributesTask;
}

// eslint-disable-next-line react-refresh/only-export-components
export function useLoadDocbaseTask() {
const context = React.useContext(DocBaseTaskContext);
Expand Down Expand Up @@ -146,7 +162,11 @@ export function DocBaseTaskProvider({ children }: Props) {
setLoadingScreen(false);
playAudio(MyAudio.SUCCESS);

const docBase = new DocBase(baseName, attributes);
const docBase = new DocBase(
baseName,
organizationId,
attributes
);
for (const nugget of res.meta.document_base_to_ui.msg
.nuggets) {
try {
Expand Down Expand Up @@ -190,6 +210,121 @@ export function DocBaseTaskProvider({ children }: Props) {
}, intervalTime);
};

const updateDocbaseAttributesTask = async (
organizationId: number,
baseName: string,
attributes: string[]
) => {
if (isRunning) {
Logger.warn(
'Docbase task is already running, cannot start another'
);
return;
}

// start the task
const taskId = await APIService.updateDocumentBaseAttributes(
organizationId,
baseName,
attributes
);

if (taskId == undefined) {
showNotification('Error', 'Failed to update Docbase ' + baseName);
return;
}

Logger.log('Task: Update Attributes Docbase ' + baseName);
Logger.log('Task ID: ' + taskId);

sessionStorage.setItem('docbaseId', taskId);
setLoadingScreen(
true,
'Updating Docbase ' + baseName + '...',
'Please wait...',
taskId
);

setLoadingScreenLock(true);
setIsRunning(true);

const updateInterval = setInterval(() => {
// TODO use type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
APIService.getTaskStatus(taskId).then((res): any => {
Logger.log(res);
if (
res == undefined ||
res.state.toUpperCase().trim() === 'FAILURE'
) {
setLoadingScreenLock(false);
setLoadingScreen(false);
playAudio(MyAudio.ERROR);

showNotification(
'Error',
'Failed to update Docbase ' + baseName
);
sessionStorage.removeItem('docbaseId');
setDocBase(undefined);
setIsRunning(false);
clearInterval(updateInterval);
return;
}

if (res.state === 'SUCCESS') {
setLoadingScreenLock(false);
setLoadingScreen(false);
playAudio(MyAudio.SUCCESS);

const docBase = new DocBase(
baseName,
organizationId,
attributes
);
for (const nugget of res.meta.document_base_to_ui.msg
.nuggets) {
try {
docBase.addNugget(
nugget.document.name,
nugget.document.text,
nugget.start_char,
nugget.end_char
);
} catch (error) {
showNotification(
'Error',
'Something went wrong translating the nuggets.'
);
}
}
sessionStorage.removeItem('docbaseId');
setDocBase(docBase);
setIsRunning(false);
clearInterval(updateInterval);
return;
}

let info = res.state;

if (res.meta.status !== undefined) {
info = res.meta.status;
}

info += info.endsWith('...') ? '' : '...';

// update loading screen
setLoadingScreen(
true,
'Updating Docbase ' + baseName + '...',
info,
taskId,
true
);
});
}, intervalTime);
};

const loadDocbaseTask = async (
organizationId: number,
baseName: string
Expand Down Expand Up @@ -255,6 +390,7 @@ export function DocBaseTaskProvider({ children }: Props) {

const docBase = new DocBase(
baseName,
organizationId,
res.meta.document_base_to_ui.msg.attributes ?? []
);
for (const nugget of res.meta.document_base_to_ui.msg
Expand Down Expand Up @@ -316,6 +452,7 @@ export function DocBaseTaskProvider({ children }: Props) {
isDocbaseTaskRunning: isDocbaseTaskRunning,
createDocbaseTask: createDocbaseTask,
loadDocbaseTask: loadDocbaseTask,
updateDocbaseAttributesTask: updateDocbaseAttributesTask,
}}
>
{docBase && <DocbaseViewer docBase={docBase} onClose={onClose} />}
Expand Down
4 changes: 3 additions & 1 deletion src/types/DocBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import NuggetDocument from './NuggetDocument';
*/
class DocBase {
readonly name: string;
readonly organizationId: number;
attributes: string[];
docs: NuggetDocument[];

constructor(name: string, attributes: string[]) {
constructor(name: string, organizationId: number, attributes: string[]) {
this.name = name;
this.organizationId = organizationId;
this.attributes = attributes;
this.docs = [];
}
Expand Down
Loading

0 comments on commit bbf7c91

Please sign in to comment.