Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions src/js/components/ImportMenu/MigrateForm/WithMigrationData.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import { createContextHook } from '../../../utils/bootstrap'
import { REST_BASES } from '../../../utils/restAPI'
import { isNetworkAdmin } from '../../../utils/screen'
import { useMigrationOptions } from './WithMigrationOptions'
import { sanitizeImporterName, useMigrationOptions } from './WithMigrationOptions'
import type { RestAPI } from '../../../hooks/useRestAPI'
import type { UseSelection } from '../../../hooks/useSelection'
import type { PropsWithChildren } from 'react'
Expand Down Expand Up @@ -52,11 +52,14 @@
throw new Error(__('Please select snippets to import.', 'code-snippets'))
}

if (!importer) {
const sanitizedImporter = sanitizeImporterName(importer)

if (!sanitizedImporter) {
throw new Error(__('Please select an importer.', 'code-snippets'))
}

const response = await api.post<ImportResponse, ImportRequest>(`${REST_BASES.import.plugins}/${importer}/import`, request)
const requestUrl = `${REST_BASES.import.plugins}/${encodeURIComponent(sanitizedImporter)}/import`
const response = await api.post<ImportResponse, ImportRequest>(requestUrl, request)

if (1 > response.imported.length) {
throw new Error(__('No snippets were imported.', 'code-snippets'))
Expand Down Expand Up @@ -105,7 +108,7 @@

const [Context, useMigrationData] = createContextHook<MigrationDataContext>('useMigrationData')

export const WithMigrationData: React.FC<PropsWithChildren> = ({ children }) => {

Check warning on line 111 in src/js/components/ImportMenu/MigrateForm/WithMigrationData.tsx

View workflow job for this annotation

GitHub Actions / lint / stylelint, eslint, phpcs

Arrow function has too many lines (57). Maximum allowed is 50
const { api } = useRestAPI()
const { selectedImporter, setSelectedImporter, autoAddTags, tagValue } = useMigrationOptions()

Expand All @@ -125,11 +128,20 @@
.then(response => Object.values(response)))
}, [api, fetchImporters])

const isAvailableImporter = importers.some(importer => importer.is_active && importer.name === selectedImporter)

useEffect(() => {
if (selectedImporter && 0 < importers.length && !isAvailableImporter) {
setSelectedImporter('')
}
}, [importers.length, isAvailableImporter, selectedImporter, setSelectedImporter])

useEffect(() => {
if (selectedImporter) {
fetchSnippets(() => api.get<ImportableSnippet[]>(`${REST_BASES.import.plugins}/${selectedImporter}`))
if (isAvailableImporter) {
fetchSnippets(() =>
api.get<ImportableSnippet[]>(`${REST_BASES.import.plugins}/${encodeURIComponent(selectedImporter)}`))
}
}, [api, selectedImporter, fetchSnippets])
}, [api, isAvailableImporter, selectedImporter, fetchSnippets])

const importSnippets = () => {
const request: ImportRequest = {
Expand All @@ -144,7 +156,7 @@
snippetSelection.clearSelection()
}

doImport(() => makeMigrationRequest(api, selectedImporter, request), handleImportSuccess)
doImport(() => makeMigrationRequest(api, isAvailableImporter ? selectedImporter : '', request), handleImportSuccess)
}

const changeSelectedImporter = (newImporter: string) => {
Expand Down
15 changes: 11 additions & 4 deletions src/js/components/ImportMenu/MigrateForm/WithMigrationOptions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ import type { PropsWithChildren } from 'react'

const IMPORTER_QUERY_PARAM = 'from'

export const sanitizeImporterName = (name: string): string =>
name.toLowerCase().replace(/[^a-z0-9-]/g, '')

const fetchImporterFromQueryParam = () =>
sanitizeImporterName(fetchQueryParam(IMPORTER_QUERY_PARAM) ?? '')

export interface MigrationOptionsContext {
autoAddTags: boolean
selectedImporter: string
Expand All @@ -17,14 +23,15 @@ export interface MigrationOptionsContext {
const [Context, useMigrationOptions] = createContextHook<MigrationOptionsContext>('useMigrationOptions')

export const WithMigrationOptions: React.FC<PropsWithChildren> = ({ children }) => {
const [selectedImporter, setSelectedImporterValue] = useState<string>(() => fetchQueryParam(IMPORTER_QUERY_PARAM) ?? '')
const [selectedImporter, setSelectedImporterValue] = useState<string>(fetchImporterFromQueryParam)
const [autoAddTags, setAutoAddTags] = useState(false)
const [tagValue, setTagValue] = useState<string>(() => `imported-${selectedImporter}`)

const setSelectedImporter = useCallback((newImporter: string) => {
updateQueryParams({ [IMPORTER_QUERY_PARAM]: newImporter })
setTagValue(`imported-${newImporter}`)
setSelectedImporterValue(newImporter)
const importer = sanitizeImporterName(newImporter)
updateQueryParams({ [IMPORTER_QUERY_PARAM]: importer })
setTagValue(`imported-${importer}`)
setSelectedImporterValue(importer)
}, [])

const value: MigrationOptionsContext = {
Expand Down
Loading