-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Ref] Refactor CreateFileMetadata.tsx to hooks.
Add language attribute to File.
- Loading branch information
Showing
3 changed files
with
75 additions
and
92 deletions.
There are no files selected for viewing
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,101 +1,81 @@ | ||
import * as React from "react"; | ||
import { injectIntl } from "react-intl"; | ||
import withI18n, { HasI18n } from "../../hoc/withI18n"; | ||
import React from "react"; | ||
import { Button, ButtonToolbar, Col, Form, Row } from "reactstrap"; | ||
import UploadFile from "./UploadFile"; | ||
import TermItFile from "../../../model/File"; | ||
import CustomInput from "../../misc/CustomInput"; | ||
import { AssetData } from "../../../model/Asset"; | ||
import { useI18n } from "../../hook/useI18n"; | ||
|
||
interface CreateFileMetadataProps extends HasI18n { | ||
interface CreateFileMetadataProps { | ||
onCreate: (termItFile: TermItFile, file: File) => any; | ||
onCancel: () => void; | ||
} | ||
|
||
interface CreateFileMetadataState extends AssetData { | ||
iri: string; | ||
label: string; | ||
file?: File; | ||
dragActive: boolean; | ||
} | ||
|
||
export class CreateFileMetadata extends React.Component< | ||
CreateFileMetadataProps, | ||
CreateFileMetadataState | ||
> { | ||
constructor(props: CreateFileMetadataProps) { | ||
super(props); | ||
this.state = { | ||
iri: "", | ||
label: "", | ||
file: undefined, | ||
dragActive: false, | ||
}; | ||
} | ||
const CreateFileMetadata: React.FC<CreateFileMetadataProps> = ({ | ||
onCreate, | ||
onCancel, | ||
}) => { | ||
const { i18n } = useI18n(); | ||
const [label, setLabel] = React.useState(""); | ||
const [file, setFile] = React.useState<File>(); | ||
|
||
protected onLabelChange = (e: React.ChangeEvent<HTMLInputElement>): void => { | ||
const label = e.currentTarget.value; | ||
this.setState({ label }); | ||
const onFileSelected = (file: File) => { | ||
setFile(file); | ||
setLabel(file.name); | ||
}; | ||
|
||
public onCreate = () => { | ||
const { file, dragActive, ...data } = this.state; | ||
const onSubmit = () => { | ||
if (file) { | ||
this.props.onCreate(new TermItFile(data), file); | ||
onCreate( | ||
new TermItFile({ | ||
iri: "", | ||
label, | ||
}), | ||
file | ||
); | ||
} | ||
}; | ||
|
||
public setFile = (file: File) => { | ||
this.setState({ file, label: file.name, dragActive: false }); | ||
}; | ||
|
||
public cannotSubmit = () => { | ||
return !this.state.file || this.state.label.trim().length === 0; | ||
const cannotSubmit = () => { | ||
return !file || label.trim().length === 0; | ||
}; | ||
|
||
public render() { | ||
const i18n = this.props.i18n; | ||
|
||
return ( | ||
<Form> | ||
<UploadFile setFile={this.setFile} /> | ||
<Row> | ||
<Col xs={12}> | ||
<CustomInput | ||
name="create-resource-label" | ||
label={i18n("asset.label")} | ||
value={this.state.label} | ||
onChange={this.onLabelChange} | ||
hint={i18n("required")} | ||
/> | ||
</Col> | ||
</Row> | ||
<Row> | ||
<Col xs={12}> | ||
<ButtonToolbar className="d-flex justify-content-center mt-4"> | ||
<Button | ||
id="create-resource-submit" | ||
onClick={this.onCreate} | ||
color="success" | ||
size="sm" | ||
disabled={this.cannotSubmit()} | ||
> | ||
{i18n("file.upload")} | ||
</Button> | ||
<Button | ||
id="create-resource-cancel" | ||
onClick={this.props.onCancel} | ||
color="outline-dark" | ||
size="sm" | ||
> | ||
{i18n("cancel")} | ||
</Button> | ||
</ButtonToolbar> | ||
</Col> | ||
</Row> | ||
</Form> | ||
); | ||
} | ||
} | ||
return ( | ||
<Form> | ||
<UploadFile setFile={onFileSelected} /> | ||
<Row> | ||
<Col xs={12}> | ||
<CustomInput | ||
name="create-resource-label" | ||
label={i18n("asset.label")} | ||
value={label} | ||
onChange={(e) => setLabel(e.target.value)} | ||
hint={i18n("required")} | ||
/> | ||
</Col> | ||
</Row> | ||
<Row> | ||
<Col xs={12}> | ||
<ButtonToolbar className="d-flex justify-content-center mt-4"> | ||
<Button | ||
id="create-resource-submit" | ||
onClick={onSubmit} | ||
color="success" | ||
size="sm" | ||
disabled={cannotSubmit()} | ||
> | ||
{i18n("file.upload")} | ||
</Button> | ||
<Button | ||
id="create-resource-cancel" | ||
onClick={onCancel} | ||
color="outline-dark" | ||
size="sm" | ||
> | ||
{i18n("cancel")} | ||
</Button> | ||
</ButtonToolbar> | ||
</Col> | ||
</Row> | ||
</Form> | ||
); | ||
}; | ||
|
||
export default injectIntl(withI18n(CreateFileMetadata)); | ||
export default CreateFileMetadata; |
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