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

feature/MWB-766 #259

Merged
merged 1 commit into from
Sep 9, 2024
Merged
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
99 changes: 58 additions & 41 deletions mapping_workbench/frontend/src/pages/app/ontology-files/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import DialogContent from "@mui/material/DialogContent";
import {Seo} from 'src/components/seo';
import {Layout as AppLayout} from 'src/layouts/app';
import {ontologyFilesApi as sectionApi} from 'src/api/ontology-files';
import {ontologyTermsApi} from "../../../api/ontology-terms";
import {useDialog} from 'src/hooks/use-dialog';
import {usePageView} from 'src/hooks/use-page-view';
import {FileUploader} from 'src/sections/app/files-form//file-uploader';
Expand All @@ -24,24 +25,25 @@ import {sessionApi} from "src/api/session";
import {Prism as SyntaxHighlighter} from "react-syntax-highlighter";
import CircularProgress from "@mui/material/CircularProgress";
import {Box} from "@mui/system";
import {toastError, toastLoad, toastSuccess} from "../../../components/app-toast";

const useItemsSearch = (items) => {
const [state, setState] = useState({
filters: {},
page: sectionApi.DEFAULT_PAGE,
rowsPerPage: sectionApi.DEFAULT_ROWS_PER_PAGE,
sort: {
sort: {
column: "filename",
direction: "desc"
},
search: '',
searchColumns:["filename"],
searchColumns: ["filename"],
});

const searchItems = state.search ? items.filter(item => {
let returnItem = null;
state.searchColumns.forEach(column => {
if(item[column]?.toLowerCase()?.includes(state.search.toLowerCase()))
if (item[column]?.toLowerCase()?.includes(state.search.toLowerCase()))
returnItem = item
})
return returnItem
Expand All @@ -50,22 +52,22 @@ const useItemsSearch = (items) => {

const filteredItems = searchItems.filter((item) => {
let returnItem = item;
Object.entries(state.filters).forEach(filter=> {
Object.entries(state.filters).forEach(filter => {
const [key, value] = filter
if(value !== "" && value !== undefined && typeof item[key] === "boolean" && item[key] !== (value == "true"))
if (value !== "" && value !== undefined && typeof item[key] === "boolean" && item[key] !== (value == "true"))
returnItem = null
if(value !== undefined && typeof item[key] === "string" && !item[key].toLowerCase().includes(value.toLowerCase))
if (value !== undefined && typeof item[key] === "string" && !item[key].toLowerCase().includes(value.toLowerCase))
returnItem = null
})
return returnItem
})

const sortedItems = () => {
const sortedItems = () => {
const sortColumn = state.sort.column
if(!sortColumn) {
if (!sortColumn) {
return searchItems
} else {
return searchItems.sort((a,b) => {
return searchItems.sort((a, b) => {
if (typeof a[sortColumn] === "string")
return state.sort.direction === "asc" ?
a[sortColumn]?.localeCompare(b[sortColumn]) :
Expand All @@ -74,18 +76,18 @@ const useItemsSearch = (items) => {
return state.sort.direction === "asc" ?
a[sortColumn] - b[sortColumn] :
b[sortColumn] - a[sortColumn]
})
})
}
}

const pagedItems = sortedItems().filter((item, i) => {
const pageSize = state.page * state.rowsPerPage
if((pageSize <= i && pageSize + state.rowsPerPage > i) || state.rowsPerPage < 0)
if ((pageSize <= i && pageSize + state.rowsPerPage > i) || state.rowsPerPage < 0)
return item
})

const handleSearchItems = (filters) => {
setState(prevState => ({...prevState, search: filters }))
setState(prevState => ({...prevState, search: filters}))
}


Expand All @@ -111,14 +113,18 @@ const useItemsSearch = (items) => {
}

const handleSort = (column, desc) => {
setState(prevState=> ({ ...prevState, sort: {column,
direction: prevState.sort.column === column
? prevState.sort.direction === "desc"
? "asc"
: "desc"
: desc
? "desc"
: "asc"}}))
setState(prevState => ({
...prevState, sort: {
column,
direction: prevState.sort.column === column
? prevState.sort.direction === "desc"
? "asc"
: "desc"
: desc
? "desc"
: "asc"
}
}))
}

const handleRowsPerPageChange = event => {
Expand Down Expand Up @@ -150,25 +156,36 @@ const Page = () => {

usePageView();

useEffect(() => {
useEffect(() => {
handleItemsGet();
// eslint-disable-next-line react-hooks/exhaustive-deps
},[]);

// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

const handleDiscover = () => {
const toastId = toastLoad('Discovering terms ...')
ontologyTermsApi.discoverTerms()
.then(res => {
toastSuccess(`${res.task_name} successfully started.`, toastId)
})
.catch(err => toastError(`Discovering terms failed: ${err.message}.`, toastId))
};

const handleItemsGet = () => {
sectionApi.getOntologyFiles()
.then(res => setState(res))
.catch(err => console.error(err));
}
sectionApi.getOntologyFiles()
.then(res => setState(res))
.catch(err => console.error(err));
}

const afterFileUpload = () => {
handleItemsGet()
handleDiscover()
}

const handleItemGet = (name) => {
detailsDialog.handleOpen({load: true, fileName: name})
sectionApi.getOntologyFile(name)
.then(res => detailsDialog.handleOpen({content: res.content, fileName: res.filename}))
.catch(err => console.log(err));
detailsDialog.handleOpen({load: true, fileName: name})
sectionApi.getOntologyFile(name)
.then(res => detailsDialog.handleOpen({content: res.content, fileName: res.filename}))
.catch(err => console.log(err));
}

return (
Expand Down Expand Up @@ -250,24 +267,24 @@ const Page = () => {
</Grid>

<Dialog
open={detailsDialog.open}
onClose={detailsDialog.handleClose}
fullWidth
maxWidth='xl'
open={detailsDialog.open}
onClose={detailsDialog.handleClose}
fullWidth
maxWidth='xl'
>
<DialogTitle>
{detailsDialog.data?.fileName}
</DialogTitle>
<DialogContent>
{
detailsDialog.data?.load ?
<Box sx={{ display: 'flex', justifyContent: 'center', marginY:10 }}>
<CircularProgress />
</Box>:
<Box sx={{display: 'flex', justifyContent: 'center', marginY: 10}}>
<CircularProgress/>
</Box> :
<SyntaxHighlighter
language="turtle"
wrapLines
lineProps={{ style: { wordBreak: 'break-all', whiteSpace: 'pre-wrap' } }}>
lineProps={{style: {wordBreak: 'break-all', whiteSpace: 'pre-wrap'}}}>
{detailsDialog.data?.content}
</SyntaxHighlighter>
}
Expand All @@ -276,7 +293,7 @@ const Page = () => {
<FileUploader
onClose={uploadDialog.handleClose}
open={uploadDialog.open}
onGetItems={handleItemsGet}
onGetItems={afterFileUpload}
sectionApi={fileResourcesApi}
onlyAcceptedFormats
disableSelectFormat
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import {RouterLink} from 'src/components/router-link';
import {FormTextField} from "../../../components/app/form/text-field";
import {toastError, toastLoad, toastSuccess} from "../../../components/app-toast";
import {sessionApi} from "../../../api/session";
import {ontologyTermsApi as sectionApi} from "../../../api/ontology-terms";


export const EditForm = (props) => {
Expand All @@ -31,17 +30,6 @@ export const EditForm = (props) => {
type: item.type ?? '',
};

const handleDiscover = () => {
const toastId = toastLoad('Discovering terms ...')
sectionApi.discoverTerms()
.then(res => {
toastSuccess(`${res.task_name} successfully started.`, toastId)
router.push({
pathname: paths.app.ontology_terms.index,
});
})
.catch(err => toastError(`Discovering terms failed: ${err.message}.`, toastId))
};

const formik = useFormik({
initialValues: initialValues,
Expand All @@ -68,15 +56,7 @@ export const EditForm = (props) => {
helpers.setSubmitting(false);
toastSuccess(sectionApi.SECTION_ITEM_TITLE + ' ' + (itemctx.isNew ? "created" : "updated"), toastId);
if (response) {
handleDiscover()

// if (itemctx.isNew) {
// router.push({
// pathname: paths.app.ontology_terms.index,
// });
// } else if (itemctx.isStateable) {
// itemctx.setState(response);
// }
}
} catch (err) {
console.error(err);
Expand Down
Loading