Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e64db78

Browse files
committed
Fix upload
1 parent 43c8faa commit e64db78

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

site/src/pages/CreateTemplatePage/TemplateUpload.tsx

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { makeStyles } from "@material-ui/core/styles"
22
import { Stack } from "components/Stack/Stack"
3-
import { FC, useRef } from "react"
3+
import { FC, DragEvent, useRef } from "react"
44
import UploadIcon from "@material-ui/icons/CloudUploadOutlined"
55
import { useClickable } from "hooks/useClickable"
66
import CircularProgress from "@material-ui/core/CircularProgress"
@@ -9,6 +9,32 @@ import IconButton from "@material-ui/core/IconButton"
99
import RemoveIcon from "@material-ui/icons/DeleteOutline"
1010
import FileIcon from "@material-ui/icons/FolderOutlined"
1111

12+
const useTarDrop = (
13+
callback: (file: File) => void,
14+
): {
15+
onDragOver: (e: DragEvent<HTMLDivElement>) => void
16+
onDrop: (e: DragEvent<HTMLDivElement>) => void
17+
} => {
18+
const onDragOver = (e: DragEvent<HTMLDivElement>) => {
19+
e.preventDefault()
20+
}
21+
22+
const onDrop = (e: DragEvent<HTMLDivElement>) => {
23+
e.preventDefault()
24+
const file = e.dataTransfer.files[0]
25+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition -- file can be undefined
26+
if (!file || file.type !== "application/x-tar") {
27+
return
28+
}
29+
callback(file)
30+
}
31+
32+
return {
33+
onDragOver,
34+
onDrop,
35+
}
36+
}
37+
1238
export interface TemplateUploadProps {
1339
isUploading: boolean
1440
onUpload: (file: File) => void
@@ -24,6 +50,7 @@ export const TemplateUpload: FC<TemplateUploadProps> = ({
2450
}) => {
2551
const styles = useStyles()
2652
const inputRef = useRef<HTMLInputElement>(null)
53+
const tarDrop = useTarDrop(onUpload)
2754
const clickable = useClickable(() => {
2855
if (inputRef.current) {
2956
inputRef.current.click()
@@ -58,6 +85,7 @@ export const TemplateUpload: FC<TemplateUploadProps> = ({
5885
[styles.disabled]: isUploading,
5986
})}
6087
{...clickable}
88+
{...tarDrop}
6189
>
6290
<Stack alignItems="center" spacing={1}>
6391
{isUploading ? (

site/src/xServices/createTemplate/createTemplateXService.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -405,9 +405,9 @@ export const createTemplateMachine =
405405
assignJobLogs: assign({ jobLogs: (_, { data }) => data }),
406406
},
407407
guards: {
408-
isExampleProvided: ({ exampleId }) => exampleId !== undefined,
409-
isNotUsingExample: ({ exampleId }) => exampleId === undefined,
410-
hasFile: ({ file }) => file !== undefined,
408+
isExampleProvided: ({ exampleId }) => Boolean(exampleId),
409+
isNotUsingExample: ({ exampleId }) => !exampleId,
410+
hasFile: ({ file }) => Boolean(file),
411411
hasFailed: (_, { data }) => data.job.status === "failed",
412412
hasMissingParameters: (_, { data }) =>
413413
Boolean(

0 commit comments

Comments
 (0)