-
Notifications
You must be signed in to change notification settings - Fork 351
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Browser File IO - add Apollo Client and relate-scripts component in sidebar #1201
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
/* | ||
* Copyright (c) 2002-2020 "Neo4j," | ||
* Neo4j Sweden AB [http://neo4j.com] | ||
* This file is part of Neo4j. | ||
* Neo4j is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
import { useEffect, useState } from 'react' | ||
import { withBus } from 'react-suber' | ||
import { connect } from 'react-redux' | ||
import MyScripts from '@relate-by-ui/saved-scripts' | ||
import { useQuery, gql, useMutation } from '@apollo/client' | ||
import path from 'path' | ||
|
||
import * as editor from 'shared/modules/editor/editorDuck' | ||
import { executeCommand } from 'shared/modules/commands/commandsDuck' | ||
import { SLASH } from 'shared/services/export-favorites' | ||
import { | ||
getProjectFilesQueryVars, | ||
removeProjectFileMutationVars, | ||
mapRelateFavorites, | ||
ProjectFile, | ||
Favorite | ||
} from './relate-scripts.utils' | ||
|
||
const GET_PROJECT_FILES = gql` | ||
query GetProject($projectId: String!, $filterValue: String!) { | ||
getProject(name: $projectId) { | ||
files( | ||
filters: [{ field: "extension", type: EQUALS, value: $filterValue }] | ||
) { | ||
name | ||
directory | ||
extension | ||
downloadToken | ||
} | ||
} | ||
} | ||
` | ||
const DELETE_PROJECT_FILE = gql` | ||
mutation RemoveFile($projectId: String!, $filePath: String!) { | ||
removeProjectFile(name: $projectId, filePath: $filePath) { | ||
name | ||
directory | ||
} | ||
} | ||
` | ||
|
||
interface ProjectFilesResult { | ||
getProject: { files: ProjectFile[] } | ||
} | ||
|
||
interface ProjectVariables { | ||
projectId: string | ||
} | ||
|
||
function RelateScripts(props: any): JSX.Element { | ||
// @todo: handling loading and error?? Pass to MyScripts?? | ||
const { data, refetch } = useQuery<ProjectFilesResult, ProjectVariables>( | ||
GET_PROJECT_FILES, | ||
{ | ||
variables: getProjectFilesQueryVars | ||
} | ||
) | ||
const [removeFavorite] = useMutation(DELETE_PROJECT_FILE) | ||
|
||
const [scripts, setScripts] = useState<Favorite[]>([]) | ||
|
||
useEffect(() => { | ||
let isStillMounted = true | ||
if (data) { | ||
const getRelateFilePromises = data.getProject.files.map( | ||
mapRelateFavorites | ||
) | ||
Promise.all(getRelateFilePromises).then((files: Favorite[]) => { | ||
if (isStillMounted) { | ||
setScripts(files) | ||
} | ||
}) | ||
} | ||
return () => { | ||
isStillMounted = false | ||
} | ||
}, [data]) | ||
|
||
useEffect(() => { | ||
// refetch only once sidebar is closed once | ||
// i.e. 'data' is already present | ||
if (data && refetch) { | ||
refetch() | ||
} | ||
}, []) | ||
|
||
return MyScripts({ | ||
...props, | ||
scripts, | ||
isRelateScripts: true, | ||
onRemoveScript: favorite => { | ||
const directory = favorite.path.substring(1) // @todo: adding in SLASH to path | ||
const filePath = path.join(directory, favorite.name) | ||
return removeFavorite({ | ||
variables: removeProjectFileMutationVars(filePath), | ||
update: (cache, { data: { removeProjectFile } }) => { | ||
const data = cache.readQuery<ProjectFilesResult>({ | ||
query: GET_PROJECT_FILES, | ||
variables: getProjectFilesQueryVars | ||
}) | ||
const filteredProjectFiles = data?.getProject.files.filter( | ||
file => | ||
file.directory !== removeProjectFile.directory || | ||
file.name !== removeProjectFile.name | ||
) | ||
cache.writeQuery({ | ||
query: GET_PROJECT_FILES, | ||
data: { | ||
getProject: { | ||
files: filteredProjectFiles | ||
} | ||
}, | ||
variables: getProjectFilesQueryVars | ||
}) | ||
} | ||
}) | ||
} | ||
}) | ||
} | ||
|
||
const mapFavoritesStateToProps = () => { | ||
return { | ||
scriptsNamespace: SLASH, | ||
title: 'Project Scripts' | ||
} | ||
} | ||
|
||
const mapFavoritesDispatchToProps = ( | ||
dispatch: any, | ||
ownProps: { bus: { send: any } } | ||
) => ({ | ||
onSelectScript: (favorite: any) => | ||
ownProps.bus.send( | ||
editor.EDIT_CONTENT, | ||
editor.editContent(favorite.id, favorite.contents) | ||
), | ||
onExecScript: (favorite: any) => dispatch(executeCommand(favorite.contents)), | ||
onExportScripts: Function.prototype, | ||
onUpdateFolder: Function.prototype, | ||
onRemoveFolder: Function.prototype | ||
}) | ||
|
||
const mergeProps = (stateProps: any, dispatchProps: any) => { | ||
return { | ||
...stateProps, | ||
...dispatchProps | ||
} | ||
} | ||
|
||
export default withBus( | ||
connect( | ||
mapFavoritesStateToProps, | ||
mapFavoritesDispatchToProps, | ||
mergeProps | ||
)(RelateScripts) | ||
) |
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 |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import remote from 'services/remote' | ||
import { SLASH, CYPHER_FILE_EXTENSION } from 'shared/services/export-favorites' | ||
import { pick } from 'lodash-es' | ||
import uuid from 'uuid' | ||
|
||
export interface ProjectFile { | ||
downloadToken: string | ||
name: string | ||
directory: string | ||
} | ||
|
||
export interface Favorite { | ||
id: string | ||
name: string | ||
path: string | ||
contents: string | ||
} | ||
|
||
// @todo: add documentation... | ||
|
||
// will be a function once projectid is available | ||
export const getProjectFilesQueryVars = { | ||
projectId: 'project-03688c10-a811-4c0c-85d4-581e88c2183a', | ||
filterValue: CYPHER_FILE_EXTENSION | ||
} | ||
|
||
export const removeProjectFileMutationVars = ( | ||
filePath: string | ||
): { projectId: string; filePath: string } => ({ | ||
...pick(getProjectFilesQueryVars, ['projectId']), | ||
filePath | ||
}) | ||
|
||
export const mapRelateFavorites = async ({ | ||
downloadToken, | ||
name, | ||
directory | ||
}: ProjectFile): Promise<Favorite> => ({ | ||
id: uuid.v4(), | ||
name, | ||
path: directory.startsWith('.') ? SLASH : `${SLASH}${directory}`, // @todo: need to look into this | ||
contents: await getProjectCypherFileContents(downloadToken, name) | ||
}) | ||
|
||
const getProjectCypherFileContents = (token: string, name: string) => | ||
remote | ||
.request('GET', `/files/${token}/${name}`) | ||
.then(body => body.text()) | ||
.catch(e => console.log('++err', e)) // ? |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
proxy for now until we use params passed from Desktop and a potential CORS issue that was being experienced