PluginProbe
Code Snippets / 4.0.0-beta.2
Code Snippets v4.0.0-beta.2
4.0.0-beta.2 3.10.2 3.10.1 3.10.0 3.10.0-beta.2 3.10.0-beta.1 4.0.0-beta.1 3.9.6 trunk 2.10.0 2.10.1 2.12.0 2.12.1 2.13.0 2.13.1 2.13.2 2.13.3 2.14.0 2.14.1 2.14.2 2.14.3 2.14.4 2.14.5 2.14.6 3.0.0 All 65 releases
code-snippets / js / components / ImportMenu / UploadForm / SelectFiles / UploadButton.tsx

UploadButton.tsx in Code Snippets 4.0.0-beta.2, at js/components/ImportMenu/UploadForm/SelectFiles/UploadButton.tsx

80 lines 2.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { __ } from '@wordpress/i18n'
2 import React from 'react'
3 import { useRestAPI } from '../../../../hooks/useRestAPI'
4 import { unpackErrorResponse } from '../../../../utils/errors'
5 import { REST_BASES } from '../../../../utils/restAPI'
6 import { Button } from '../../../common/Button'
7 import type { Dispatch, SetStateAction } from 'react'
8 import type { ImportableSnippetSchema } from '../../../../types/schema/ImportableSnippetSchema'
9 import type { ImportResult } from '../SelectSnippets/ImportResultDisplay'
10
11 interface FileParseResponse {
12 snippets: ImportableSnippetSchema[]
13 total_count: number
14 message: string
15 warnings?: string[]
16 }
17
18 export interface UploadedFile {
19 id: string
20 name: string
21 file: File
22 }
23
24 export interface UploadButtonProps {
25 onSuccess: (snippets: ImportableSnippetSchema[]) => void
26 selectedFiles: UploadedFile[] | undefined
27 setImportResult: (result: ImportResult | undefined) => void
28 isUploading: boolean
29 setIsUploading: Dispatch<SetStateAction<boolean>>
30 }
31
32 export const UploadButton: React.FC<UploadButtonProps> = ({ isUploading, setIsUploading, selectedFiles, onSuccess, setImportResult }) => {
33 const { api } = useRestAPI()
34
35 const handleUpload = () => {
36 if (!selectedFiles || 0 === selectedFiles.length) {
37 alert(__('Please select files to upload.', 'code-snippets'))
38 return
39 }
40
41 setIsUploading(true)
42 setImportResult(undefined)
43
44 const formData = new FormData()
45
46 for (const selectedFile of selectedFiles) {
47 formData.append('files[]', selectedFile.file)
48 }
49
50 api.post<FileParseResponse, FormData>(
51 `${REST_BASES.import.files}/parse`,
52 formData,
53 { headers: { 'Content-Type': 'multipart/form-data' } })
54 .then(({ snippets, message, warnings }) => {
55 onSuccess(snippets)
56
57 if (warnings && 0 < warnings.length) {
58 setImportResult({ step: 'upload', success: true, message, warnings })
59 }
60 })
61 .catch((error: unknown) => {
62 console.error('Parse error:', error)
63 setImportResult({ step: 'upload', success: false, message: unpackErrorResponse(error) })
64 })
65 .finally(() => setIsUploading(false))
66 }
67
68 return (
69 <Button
70 primary
71 onClick={handleUpload}
72 disabled={!selectedFiles || 0 === selectedFiles.length || isUploading}
73 >
74 {isUploading
75 ? __('Uploading files…', 'code-snippets')
76 : __('Upload files', 'code-snippets')}
77 </Button>
78 )
79 }
80