replace directory bug

pull/1140/head
ioedeveloper 4 years ago
parent 49e0da7c50
commit 7deedbad2b
  1. 47
      libs/remix-ui/file-explorer/src/lib/actions/fileSystem.ts
  2. 56
      libs/remix-ui/file-explorer/src/lib/reducers/fileSystem.ts

@ -151,16 +151,51 @@ export const fetchProviderSuccess = (provider: any) => {
} }
} }
export const fileAddedSuccess = (path: string, files) => {
return {
type: 'FILE_ADDED',
payload: { path, files }
}
}
export const folderAddedSuccess = (path: string, files) => {
return {
type: 'FOLDER_ADDED',
payload: { path, files }
}
}
export const fileRemovedSuccess = (path: string, removePath: string, files) => {
return {
type: 'FILE_REMOVED',
payload: { path, removePath, files }
}
}
export const setProvider = (provider, workspaceName) => (dispatch: React.Dispatch<any>) => { export const setProvider = (provider, workspaceName) => (dispatch: React.Dispatch<any>) => {
if (provider) { if (provider) {
provider.event.register('fileAdded', (filePath) => { provider.event.register('fileAdded', async (filePath) => {
resolveDirectory(provider, extractParentFromKey(filePath) || workspaceName)(dispatch) const path = extractParentFromKey(filePath) || workspaceName
const data = await fetchDirectoryContent(provider, path)
dispatch(fileAddedSuccess(path, data))
}) })
provider.event.register('folderAdded', (folderPath) => { provider.event.register('folderAdded', async (folderPath) => {
resolveDirectory(provider, extractParentFromKey(folderPath) || workspaceName)(dispatch) const path = extractParentFromKey(folderPath) || workspaceName
const data = await fetchDirectoryContent(provider, path)
console.log('data: ', data)
dispatch(folderAddedSuccess(path, data))
}) })
provider.event.register('fileRemoved', (path) => { provider.event.register('fileRemoved', async (removePath) => {
resolveDirectory(provider, extractParentFromKey(path) || workspaceName)(dispatch) const path = extractParentFromKey(removePath) || workspaceName
const data = await fetchDirectoryContent(provider, path)
dispatch(fileRemovedSuccess(path, removePath, data))
})
provider.event.register('fileRenamed', async () => {
}) })
dispatch(fetchProviderSuccess(provider)) dispatch(fetchProviderSuccess(provider))
dispatch(setCurrentWorkspace(workspaceName)) dispatch(setCurrentWorkspace(workspaceName))

@ -161,6 +161,42 @@ export const fileSystemReducer = (state = fileSystemInitialState, action: Action
} }
} }
} }
case 'FILE_ADDED': {
return {
...state,
files: {
...state.files,
files: fileAdded(state.files.workspaceName, action.payload.path, state.files.files, action.payload.files),
isRequesting: false,
isSuccessful: true,
error: null
}
}
}
case 'FOLDER_ADDED': {
return {
...state,
files: {
...state.files,
files: folderAdded(state.files.workspaceName, action.payload.path, state.files.files, action.payload.files),
isRequesting: false,
isSuccessful: true,
error: null
}
}
}
case 'FILE_REMOVED': {
return {
...state,
files: {
...state.files,
files: fileRemoved(state.files.workspaceName, action.payload.path, action.payload.removePath, state.files.files, action.payload.files),
isRequesting: false,
isSuccessful: true,
error: null
}
}
}
default: default:
throw new Error() throw new Error()
} }
@ -168,7 +204,7 @@ export const fileSystemReducer = (state = fileSystemInitialState, action: Action
const resolveDirectory = (root, path: string, files, content) => { const resolveDirectory = (root, path: string, files, content) => {
if (path === root) return { [root]: { ...content[root], ...files[root] } } if (path === root) return { [root]: { ...content[root], ...files[root] } }
const pathArr: string[] = path.split('/') const pathArr: string[] = path.split('/').filter(value => value)
if (pathArr[0] !== root) pathArr.unshift(root) if (pathArr[0] !== root) pathArr.unshift(root)
const _path = pathArr.map((key, index) => index > 1 ? ['child', key] : key).reduce((acc: string[], cur) => { const _path = pathArr.map((key, index) => index > 1 ? ['child', key] : key).reduce((acc: string[], cur) => {
@ -199,3 +235,21 @@ const removeInputField = (root, path: string, files, content) => {
} }
return resolveDirectory(root, path, files, content) return resolveDirectory(root, path, files, content)
} }
const fileAdded = (root, path: string, files, content) => {
return resolveDirectory(root, path, files, content)
}
const folderAdded = (root, path: string, files, content) => {
return resolveDirectory(root, path, files, content)
}
const fileRemoved = (root, path: string, removedPath: string, files, content) => {
if (path === root) {
const allFiles = { [root]: { ...content[root], ...files[root] } }
delete allFiles[root][removedPath]
return allFiles
}
return resolveDirectory(root, path, files, content)
}

Loading…
Cancel
Save