-
-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
251 additions
and
89 deletions.
There are no files selected for viewing
159 changes: 127 additions & 32 deletions
159
frontend/src/components/widgets/modals/CreateExtractModal.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.