Commit 13ebe8bf authored by Fernando Arias's avatar Fernando Arias

First pass at corpus upload commit

* Store package ID on upload
parent e8f11cc0
mutation uploadComplete($projectPath: ID!, $packageId: Int!) {
uploadComplete(projectPath: $projectPath, packageId: $packageId) @client {
errors
}
}
...@@ -7,5 +7,6 @@ query getCorpuses($projectPath: ID!) { ...@@ -7,5 +7,6 @@ query getCorpuses($projectPath: ID!) {
isUploading isUploading
progress progress
cancelSource cancelSource
uploadedPackageId
} }
} }
...@@ -4,6 +4,7 @@ import { publishPackage } from '~/api/packages_api'; ...@@ -4,6 +4,7 @@ import { publishPackage } from '~/api/packages_api';
import axios from '~/lib/utils/axios_utils'; import axios from '~/lib/utils/axios_utils';
import getCorpusesQuery from '../queries/get_corpuses.query.graphql'; import getCorpusesQuery from '../queries/get_corpuses.query.graphql';
import updateProgress from '../mutations/update_progress.mutation.graphql'; import updateProgress from '../mutations/update_progress.mutation.graphql';
import uploadComplete from '../mutations/upload_complete.mutation.graphql';
export default { export default {
Query: { Query: {
...@@ -22,6 +23,7 @@ export default { ...@@ -22,6 +23,7 @@ export default {
isUploading: false, isUploading: false,
progress: 0, progress: 0,
cancelSource: null, cancelSource: null,
uploadedPackageId: null,
__typename: 'UploadState', __typename: 'UploadState',
}; };
}, },
...@@ -91,19 +93,43 @@ export default { ...@@ -91,19 +93,43 @@ export default {
variables: { projectPath }, variables: { projectPath },
}); });
const data = produce(sourceData, (draftState) => { const targetData = produce(sourceData, (draftState) => {
const { uploadState } = draftState; const { uploadState } = draftState;
uploadState.isUploading = true; uploadState.isUploading = true;
uploadState.cancelSource = source; uploadState.cancelSource = source;
}); });
cache.writeQuery({ query: getCorpusesQuery, data, variables: { projectPath } }); cache.writeQuery({ query: getCorpusesQuery, targetData, variables: { projectPath } });
publishPackage( publishPackage(
{ projectPath, name, version: 0, fileName: name, files }, { projectPath, name, version: 0, fileName: name, files },
{ status: 'hidden', select: 'package_file' }, { status: 'hidden', select: 'package_file' },
{ onUploadProgress, cancelToken: source.token }, { onUploadProgress, cancelToken: source.token },
); )
.then(({ data }) => {
client.mutate({
mutation: uploadComplete,
variables: { projectPath, packageId: data.package_id },
});
})
.catch((e) => {
/* TODO: Error handling */
});
},
uploadComplete: (_, { projectPath, packageId }, { cache }) => {
const sourceData = cache.readQuery({
query: getCorpusesQuery,
variables: { projectPath },
});
const data = produce(sourceData, (draftState) => {
const { uploadState } = draftState;
uploadState.isUploading = false;
uploadState.cancelSource = null;
uploadState.uploadedPackageId = packageId;
});
cache.writeQuery({ query: getCorpusesQuery, data, variables: { projectPath } });
}, },
updateProgress: (_, { projectPath, progress }, { cache }) => { updateProgress: (_, { projectPath, progress }, { cache }) => {
const sourceData = cache.readQuery({ const sourceData = cache.readQuery({
...@@ -115,11 +141,6 @@ export default { ...@@ -115,11 +141,6 @@ export default {
const { uploadState } = draftState; const { uploadState } = draftState;
uploadState.isUploading = true; uploadState.isUploading = true;
uploadState.progress = progress; uploadState.progress = progress;
if (progress >= 100) {
uploadState.isUploading = false;
uploadState.cancelSource = null;
}
}); });
cache.writeQuery({ query: getCorpusesQuery, data, variables: { projectPath } }); cache.writeQuery({ query: getCorpusesQuery, data, variables: { projectPath } });
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment