-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #256 from Pinelab-studio/feat/order-client-loading…
…-states Order Client Loading States
- Loading branch information
Showing
8 changed files
with
2,780 additions
and
234 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1 +1,3 @@ | ||
// TODO set correct version number + date and the changes you've made connected to the PR. See this example for the correct format: https://github.com/Pinelab-studio/pinelab-vendure-plugins/blob/main/packages/vendure-plugin-invoices/CHANGELOG.md | ||
# 2.1.0 (2023-09-20) | ||
|
||
- Added loading states for currentUser and activeOrder store ([#256](https://github.com/Pinelab-studio/pinelab-vendure-plugins/pull/256)) |
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"name": "@pinelab/vendure-order-client", | ||
"version": "2.0.0", | ||
"version": "2.1.0", | ||
"description": "A tiny, framework agnostic client for managing active orders and checkout with Vendure.", | ||
"author": "Martijn van de Brug <[email protected]>", | ||
"homepage": "https://pinelab-plugins.com/", | ||
|
@@ -32,5 +32,9 @@ | |
"mitt": "^3.0.0", | ||
"nanostores": "^0.9.2" | ||
}, | ||
"devDependencies": { | ||
"@nanostores/vue": "^0.10.0", | ||
"@vue/cli": "^5.0.8" | ||
}, | ||
"gitHead": "476f36da3aafea41fbf21c70774a30306f1d238f" | ||
} |
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,68 @@ | ||
import { MapStore } from 'nanostores'; | ||
import { ErrorCode, ErrorResult } from './graphql-generated-types'; | ||
|
||
/** | ||
* Interface defining loading and error states per store | ||
*/ | ||
export interface StateStore<T> { | ||
loading: boolean; | ||
error: ErrorResult | undefined; | ||
data: T; | ||
} | ||
|
||
/** | ||
* Set result as data if not ErrorResult, otherwise set as error in store | ||
*/ | ||
export function setResult<T>( | ||
store: MapStore<StateStore<T>>, | ||
result: unknown | ErrorResult | ||
): void { | ||
store.setKey('loading', false); | ||
if ((result as ErrorResult)?.errorCode) { | ||
const errorResult = result as ErrorResult; | ||
store.setKey('error', { | ||
errorCode: errorResult.errorCode, | ||
message: errorResult.message, | ||
}); | ||
} else { | ||
store.setKey('data', result as T); | ||
} | ||
} | ||
|
||
/** | ||
* Handle loading and error state for given store: | ||
* - Sets loading to true | ||
* - Executes given function | ||
* - Sets loading to false | ||
* - Returns result of function | ||
* | ||
* If an error is caught, it's set as error in the store and thrown. | ||
* Loading state is always set back to false | ||
*/ | ||
export function HandleLoadingState(storeName: string) { | ||
return function ( | ||
target: any, | ||
propertyKey: string, | ||
descriptor: PropertyDescriptor | ||
) { | ||
const originalMethod = descriptor.value; // Save a reference to the original method | ||
descriptor.value = async function (this: any, ...args: any[]) { | ||
const store: MapStore<StateStore<any>> = this[storeName]; | ||
store.setKey('loading', true); | ||
if (!store) { | ||
throw new Error(`Store ${storeName} not found`); | ||
} | ||
try { | ||
const result = await originalMethod.apply(this, args); | ||
return result; | ||
} catch (e: any) { | ||
store.setKey('error', { | ||
errorCode: ErrorCode.UnknownError, | ||
message: e?.message, | ||
}); | ||
} finally { | ||
store.setKey('loading', false); | ||
} | ||
}; | ||
}; | ||
} |
Oops, something went wrong.