Skip to content

Commit

Permalink
Prevent save the same custom query (#772)
Browse files Browse the repository at this point in the history
  • Loading branch information
postrowinski committed Apr 7, 2023
1 parent 7199992 commit b779db3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
21 changes: 18 additions & 3 deletions mwdb/web/src/components/RecentView/QuickQuery.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { AuthContext, Capability } from "@mwdb-web/commons/auth";
import { ConfirmationModal } from "@mwdb-web/commons/ui";

import QuickQueryAddModal from "./QuickQueryAddModal";
import { toast } from "react-toastify";

function QuickQueryItem(props) {
return (
Expand Down Expand Up @@ -88,6 +89,19 @@ export default function QuickQuery(props) {
}
};

const checkIfQueryNotExist = () => {
const isQueriesIncludeNewQuery = queries
.map((x) => x.query)
.includes(props.query);
if (isQueriesIncludeNewQuery) {
toast("You are trying to add a query that already exists.", {
type: "error",
});
return false;
}
return true;
};

const predefinedQueryBadges = [
!api.remote && (
<UploaderQueryItem
Expand Down Expand Up @@ -165,10 +179,10 @@ export default function QuickQuery(props) {
style={{ cursor: "pointer" }}
data-toggle="tooltip"
title="Save current search as Quick query"
onClick={(ev) => {
ev.preventDefault();
if (props.canAddQuickQuery) {
onClick={() => {
if (props.canAddQuickQuery && checkIfQueryNotExist()) {
setAddModalOpen(true);
props.setQueryError("");
} else {
props.setQueryError(
"Provided query must be submitted before adding to Quick query bar"
Expand Down Expand Up @@ -202,6 +216,7 @@ export default function QuickQuery(props) {
error={modalError}
onError={setModalError}
onSubmit={addQuery}
queries={queries}
onRequestModalClose={() => {
setModalError(null);
setAddModalOpen(false);
Expand Down
14 changes: 10 additions & 4 deletions mwdb/web/src/components/RecentView/QuickQueryAddModal.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import React, { useEffect, useState } from "react";
import { ConfirmationModal } from "@mwdb-web/commons/ui";

export default function QuickQueryAddModal(props) {
const { isOpen, onSubmit, onRequestModalClose, error, onError } = props;
const { isOpen, onSubmit, onRequestModalClose, error, onError, queries } =
props;
const [value, setValue] = useState("");

useEffect(() => {
Expand All @@ -14,10 +15,15 @@ export default function QuickQueryAddModal(props) {
const handleSubmit = (ev) => {
if (!value) {
onError("Please set name for your quick query.");
} else {
ev.preventDefault();
onSubmit(value);
return;
}
const names = queries.map((x) => x.name);
if (names.includes(value)) {
onError("This query name already exists");
return;
}
ev.preventDefault();
onSubmit(value);
};

const handleClose = (ev) => {
Expand Down

0 comments on commit b779db3

Please sign in to comment.