Skip to content

Commit

Permalink
A lot of the extract view working.
Browse files Browse the repository at this point in the history
  • Loading branch information
JSv4 committed May 29, 2024
1 parent 531acde commit d4688e8
Show file tree
Hide file tree
Showing 11 changed files with 251 additions and 89 deletions.
159 changes: 127 additions & 32 deletions frontend/src/components/widgets/modals/CreateExtractModal.tsx
Original file line number Diff line number Diff line change
@@ -1,44 +1,139 @@
import React, { ReactNode, useEffect, useState } from "react";
import { Button, Modal, Icon } from "semantic-ui-react";
import _ from "lodash";
import { CorpusType, FieldsetType } from "../../../graphql/types";
import React, { useState, useEffect } from "react";
import {
Modal,
Form,
FormInput,
FormGroup,
FormButton,
FormField,
} from "semantic-ui-react";

export interface CreateExtractModalProps {
import { useReactiveVar, useQuery, useMutation } from "@apollo/client";
import { selectedCorpus, selectedFieldset } from "../../../graphql/cache";
import {
REQUEST_GET_EXTRACT,
RequestGetExtractInput,
GetExtractOutput,
} from "../../../graphql/queries";
import { CorpusDropdown } from "../selectors/CorpusDropdown";
import { FieldsetDropdown } from "../selectors/FieldsetDropdown";
import {
REQUEST_CREATE_EXTRACT,
RequestCreateExtractInputType,
RequestCreateExtractOutputType,
} from "../../../graphql/mutations";

interface ExtractModalProps {
open: boolean;
onClose: () => void;
onSubmit: () => void;
corpus?: CorpusType | undefined;
fieldset?: FieldsetType | undefined;
children?: React.ReactChild | React.ReactChild[];
extractId?: string;
fieldsetId?: string;
corpusId?: string;
}

export function CreateExtractModal({
export const CreateExtractModal: React.FC<ExtractModalProps> = ({
open,
corpus,
fieldset,
children,
onClose,
onSubmit,
}: CreateExtractModalProps) {
extractId,
fieldsetId,
corpusId,
}) => {
const [name, setName] = useState("");
const [description, setDescription] = useState("");
const selected_corpus = useReactiveVar(selectedCorpus);
const selected_fieldset = useReactiveVar(selectedFieldset);

const { loading, error, data } = useQuery<
GetExtractOutput,
RequestGetExtractInput
>(REQUEST_GET_EXTRACT, {
variables: { id: extractId || "" },
skip: !extractId,
});

const [
createExtract,
{ loading: createExtractLoading, error: createExtractError },
] = useMutation<
RequestCreateExtractOutputType,
RequestCreateExtractInputType
>(REQUEST_CREATE_EXTRACT);

useEffect(() => {
if (data?.extract) {
setName(data.extract.name);
}
}, [data]);

const handleSubmit = async () => {
if (!extractId) {
try {
const { data } = await createExtract({
variables: {
corpusId: selected_corpus?.id || corpusId || "",
name,
fieldsetId: selected_fieldset?.id || fieldsetId || "",
},
});
if (data?.createExtract.ok) {
onClose();
} else {
console.error("Failed to create extract:", data?.createExtract.msg);
}
} catch (error) {
console.error("Error creating extract:", error);
}
}
};

if (loading) {
return <div>Loading...</div>;
}

if (error) {
return <div>Error: {error.message}</div>;
}

return (
<Modal closeIcon open={open} onClose={() => onClose()}>
<Modal.Content></Modal.Content>
<Modal.Actions>
<Button basic color="grey" onClick={() => onClose()}>
<Icon name="remove" /> Close
</Button>
<Button
color="green"
inverted
onClick={() => console.log("Do the thing")}
>
<Icon name="checkmark" />
Create
</Button>
</Modal.Actions>
<Modal open={open} onClose={onClose}>
<Modal.Header>
{extractId ? "Edit Extract" : "Create Extract"}
</Modal.Header>
<Modal.Content>
<Form onSubmit={handleSubmit}>
<FormGroup>
<FormInput
placeholder="Name"
name="name"
value={name}
onChange={(e, { value }) => setName(value)}
style={{ minWidth: "50vw important!" }}
/>
</FormGroup>
{!corpusId && (
<FormGroup>
<FormField>
<label>Corpus</label>
<CorpusDropdown />
</FormField>
</FormGroup>
)}

{!fieldsetId && (
<FormGroup>
<FormField>
<label>Fieldset</label>
<FieldsetDropdown />
</FormField>
</FormGroup>
)}
<FormButton
content="Submit"
style={{ marginTop: "1vh" }}
disabled={loading}
loading={loading}
/>
</Form>
</Modal.Content>
</Modal>
);
}
};
19 changes: 14 additions & 5 deletions frontend/src/components/widgets/selectors/CorpusDropdown.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { SyntheticEvent, useEffect, useState } from "react";
import React, { SyntheticEvent, useCallback, useEffect, useState } from "react";
import { useQuery, useReactiveVar } from "@apollo/client";
import { Dropdown, DropdownProps } from "semantic-ui-react";
import {
Expand All @@ -24,9 +24,11 @@ export const CorpusDropdown: React.FC = () => {
GetCorpusesOutputs,
GetCorpusesInputs
>(GET_CORPUSES, {
variables: {
textSearch: searchQuery,
},
variables: searchQuery
? {
textSearch: searchQuery,
}
: {},
});

// If the searchQuery changes... refetch corpuses.
Expand All @@ -38,11 +40,18 @@ export const CorpusDropdown: React.FC = () => {
? data.corpuses.edges.map((edge) => edge.node)
: [];

const debouncedSetSearchQuery = useCallback(
_.debounce((query: string) => {
setSearchQuery(query);
}, 500),
[]
);

const handleSearchChange = (
event: React.SyntheticEvent<HTMLElement>,
{ searchQuery }: { searchQuery: string }
) => {
setSearchQuery(searchQuery);
debouncedSetSearchQuery(searchQuery);
};

const handleSelectionChange = (
Expand Down
23 changes: 20 additions & 3 deletions frontend/src/components/widgets/selectors/FieldsetDropdown.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { SyntheticEvent, useEffect, useState } from "react";
import React, { SyntheticEvent, useCallback, useEffect, useState } from "react";
import { useQuery, useReactiveVar } from "@apollo/client";
import { Dropdown, DropdownProps } from "semantic-ui-react";
import {
Expand All @@ -20,7 +20,16 @@ export const FieldsetDropdown: React.FC = () => {
const selected_fieldset = useReactiveVar(selectedFieldset);

const { loading, error, data, refetch } = useQuery<GetFieldsetsOutputs>(
REQUEST_GET_FIELDSETS
REQUEST_GET_FIELDSETS,
{
variables: searchQuery
? {
searchText: searchQuery,
}
: {},
fetchPolicy: "network-only",
notifyOnNetworkStatusChange: true,
}
);

// If the searchQuery changes... refetch fieldsets.
Expand All @@ -32,11 +41,18 @@ export const FieldsetDropdown: React.FC = () => {
? data.fieldsets.edges.map((edge) => edge.node)
: [];

const debouncedSetSearchQuery = useCallback(
_.debounce((query: string) => {
setSearchQuery(query);
}, 500),
[]
);

const handleSearchChange = (
event: React.SyntheticEvent<HTMLElement>,
{ searchQuery }: { searchQuery: string }
) => {
setSearchQuery(searchQuery);
debouncedSetSearchQuery(searchQuery);
};

const handleSelectionChange = (
Expand Down Expand Up @@ -83,6 +99,7 @@ export const FieldsetDropdown: React.FC = () => {
onChange={handleSelectionChange}
onSearchChange={handleSearchChange}
loading={loading}
style={{ minWidth: "50vw important!" }}
/>
);
};
24 changes: 12 additions & 12 deletions frontend/src/extracts/ExtractDataGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { useMutation, useQuery } from "@apollo/client";
import { toast } from "react-toastify";

import { ColumnDetails } from "./ColumnDetails";
import { ColumnType, ExtractType, DatacellType } from "../graphql/types";
import { ExtractType } from "../graphql/types";
import { REQUEST_GET_EXTRACT } from "../graphql/queries";
import { REQUEST_START_EXTRACT } from "../graphql/mutations";
import { REQUEST_CREATE_EXTRACT } from "../graphql/mutations";

interface ExtractDataGridProps {
extractId: string;
Expand All @@ -21,7 +21,7 @@ export const ExtractDataGrid: React.FC<ExtractDataGridProps> = ({
variables: { id: extractId },
}
);
const [startExtract] = useMutation(REQUEST_START_EXTRACT);
const [startExtract] = useMutation(REQUEST_CREATE_EXTRACT);

const handleStartExtract = async () => {
try {
Expand Down Expand Up @@ -49,9 +49,9 @@ export const ExtractDataGrid: React.FC<ExtractDataGridProps> = ({
<Table celled>
<Table.Header>
<Table.Row>
{columns.map((column) => (
<Table.HeaderCell key={column.id}>
{column.query}
{columns.edges.map((columnEdge) => (
<Table.HeaderCell key={columnEdge.node.id}>
{columnEdge.node.query}
</Table.HeaderCell>
))}
</Table.Row>
Expand All @@ -60,9 +60,9 @@ export const ExtractDataGrid: React.FC<ExtractDataGridProps> = ({
{datacells?.edges ? (
datacells.edges.map((row) => (
<Table.Row key={row?.node?.id}>
{columns.map((column: ColumnType) => (
<Table.Cell key={column.id}>
{row && row.node ? row.node.data[column.id] : ""}
{columns.edges.map((columnEdge) => (
<Table.Cell key={columnEdge.node.id}>
{row && row.node ? row.node.data[columnEdge.node.id] : ""}
</Table.Cell>
))}
</Table.Row>
Expand All @@ -75,10 +75,10 @@ export const ExtractDataGrid: React.FC<ExtractDataGridProps> = ({
{!extract.started && (
<>
<h3>Edit Columns</h3>
{columns.map((column) => (
{columns.edges.map((columnEdge) => (
<ColumnDetails
key={column.id}
column={column}
key={columnEdge.node.id}
column={columnEdge.node}
fieldsetId={extract.fieldset.id}
onSave={refetch}
/>
Expand Down
6 changes: 3 additions & 3 deletions frontend/src/extracts/FieldsetDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ export const FieldsetDetails: React.FC<FieldsetDetailsProps> = ({
</Form.Field>
</Form>
<h3>Columns</h3>
{fieldset.columns.map((column: ColumnType) => (
{fieldset.columns.edges.map((columnEdge) => (
<ColumnDetails
key={column.id}
column={column}
key={columnEdge.node.id}
column={columnEdge.node}
fieldsetId={fieldset.id}
onSave={onSave}
/>
Expand Down
32 changes: 26 additions & 6 deletions frontend/src/graphql/mutations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1293,23 +1293,23 @@ export const REQUEST_UPDATE_COLUMN = gql`
}
`;

export interface RequestStartExtractOutputType {
startExtract: {
export interface RequestCreateExtractOutputType {
createExtract: {
msg: string;
ok: boolean;
obj: ExtractType;
};
}

export interface RequestStartExtractInputType {
export interface RequestCreateExtractInputType {
corpusId: string;
name: string;
fieldsetId: string;
}

export const REQUEST_START_EXTRACT = gql`
mutation StartExtract($corpusId: ID!, $name: String!, $fieldsetId: ID!) {
startExtract(corpusId: $corpusId, name: $name, fieldsetId: $fieldsetId) {
export const REQUEST_CREATE_EXTRACT = gql`
mutation CreateExtract($corpusId: ID!, $name: String!, $fieldsetId: ID!) {
createExtract(corpusId: $corpusId, name: $name, fieldsetId: $fieldsetId) {
msg
ok
obj {
Expand All @@ -1319,3 +1319,23 @@ export const REQUEST_START_EXTRACT = gql`
}
}
`;

export interface RequestStartExtractOutputType {
startExtract: {
msg: string;
ok: boolean;
};
}

export interface RequestStartExtractInputType {
extractId: string;
}

export const REQUEST_START_EXTRACT = gql`
mutation StartExtract($extractId: ID!) {
startExtract(extractId: $extractId) {
msg
ok
}
}
`;
Loading

0 comments on commit d4688e8

Please sign in to comment.