Skip to content

Commit

Permalink
fix: duplicate request
Browse files Browse the repository at this point in the history
  • Loading branch information
boydaihungst committed Dec 12, 2021
1 parent c5a3f1f commit ff812a2
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 30 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ Install the `altair-graphql-plugin-github-sync` plugin from Avaiable Plugins > A

## Usage

- Click on "Upload" to send your collections to Gist.
- Click on "Donwload" to get your collections from Gist.
- Click on "Upload" to send your collections, app settings, environment to Gist.
- Click on "Donwload" to get your collections, app settings, environment from Gist.

## For developer only

1. Download and Open the Altair GraphQL Client
2. Open the settings modal
3. Toggle "ON" the "Enable experimental features in Altair. Note: The features might be unstable"
4. Add "url:altair-graphql-plugin-github-sync@1.0.0::[url]->[http://localhost:8002]" to the plugins list and hit "Save" at the bottom of the settings modal
4. Add "url:altair-graphql-plugin-github-sync@1.0.1::[url]->[http://localhost:8002]" to the plugins list and hit "Save" at the bottom of the settings modal
5. After refresh Gist Sync panel will show in the left

## Credit
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"manifest_version": 2,
"name": "altair-graphql-plugin-github-sync",
"version": "1.0.1",
"version": "1.0.2",
"display_name": "Altair Github Sync",
"description": "Save altair collections to github gist",
"author_email": "[email protected]",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "altair-graphql-plugin-github-sync",
"description": "Altair Sync Gist plugin",
"version": "1.0.1",
"version": "1.0.2",
"private": false,
"license": "MIT",
"homepage": "https://github.com/boydaihungst/altair-graphql-plugin-github-sync",
Expand Down
46 changes: 21 additions & 25 deletions src/providers/SyncProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { IQueryCollection } from './StorageService';
import merge from 'lodash/merge';
import values from 'lodash/values';
import keyBy from 'lodash/keyBy';
import { STORE_KEY_API_KEY } from '@/constant/gist-provider.enum';
abstract class SyncProvider {
private altairContext: AltairContext;

Expand All @@ -18,20 +19,20 @@ abstract class SyncProvider {
* @returns
*/
mergeQueriesOfCollection(oldQueries: any[], newQueries: any[]) {
const now = this.altairContext.db.now();
const mergedQueries = values(
merge(
keyBy(oldQueries, o => o.id),
keyBy(newQueries, o => o.id),
),
);
merge(keyBy(oldQueries, 'id'), keyBy(newQueries, 'id')),
).map(query => {
return { ...query, created_at: now, updated_at: now };
});
return mergedQueries;
}

createCollection(collection: IQueryCollection) {
const now = this.altairContext.db.now();

collection.queries = collection.queries.map(query => {
return { ...query, id: uuid(), created_at: now, updated_at: now };
return { ...query, created_at: now, updated_at: now };
});
return this.altairContext.db.queryCollections.add({
...collection,
Expand All @@ -41,18 +42,19 @@ abstract class SyncProvider {
}

protected async updateCollection(
collectionTitle: string,
modifiedCollection: IQueryCollection,
oldCollection: IQueryCollection,
newCollection: IQueryCollection,
) {
return this.altairContext.db.queryCollections
.where('title')
.equals(collectionTitle)
.modify((oldCollection, ctx) => {
modifiedCollection.queries = this.mergeQueriesOfCollection(
oldCollection.queries,
modifiedCollection.queries,
);
ctx.value = modifiedCollection;
if (!oldCollection.id) return null;
const mergedQueries = this.mergeQueriesOfCollection(
oldCollection.queries,
newCollection.queries,
);
return await this.altairContext.db.queryCollections
.where('id')
.equals(oldCollection.id)
.modify({
queries: mergedQueries,
});
}

Expand All @@ -65,6 +67,7 @@ abstract class SyncProvider {
if (data.localStorage) {
// Restore localStorage
Object.keys(data.localStorage).forEach(key => {
if (key === STORE_KEY_API_KEY) return;
localStorage.setItem(key, data.localStorage[key]);
});
}
Expand All @@ -77,10 +80,7 @@ abstract class SyncProvider {
.first();
// Update old one with new one
if (oldCollection) {
await this.updateCollection(
collection.title,
merge(oldCollection, collection),
);
await this.updateCollection(oldCollection, collection);
continue;
}
// Create if not exist
Expand All @@ -101,10 +101,6 @@ abstract class SyncProvider {
protected async exportFromAltair(): Promise<string> {
const altairCollections = await this.altairContext.db.queryCollections.toArray();
const altairAppState = await this.altairContext.db.appState.toArray();
// Prevent save app settings
altairAppState.splice(
altairAppState.findIndex(item => item.key === '[altair_]::settings'),
);
const altairsSlectedFiles = await this.altairContext.db.selectedFiles.toArray();

const data: AltairData = {
Expand Down

0 comments on commit ff812a2

Please sign in to comment.