Skip to content

Commit

Permalink
When the network is unavailable, fall back to the previous account st…
Browse files Browse the repository at this point in the history
…atus
  • Loading branch information
mathieudutour committed Jan 3, 2019
1 parent b3c0917 commit 7d82a86
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 27 deletions.
2 changes: 1 addition & 1 deletion app/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"productName": "Kactus",
"bundleID": "io.kactus.KactusClient",
"companyName": "Kactus.io",
"version": "0.3.15",
"version": "0.3.16",
"main": "./main.js",
"repository": {
"type": "git",
Expand Down
23 changes: 13 additions & 10 deletions app/src/lib/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -763,11 +763,21 @@ export async function createAuthorization(
return { kind: AuthorizationResponseKind.Error, response }
}

type KactusState = {
enterprise: boolean
enterpriseFromOrg: boolean
enterpriseFromOrgAdmin: boolean
premium: boolean
premiumFromOrg: boolean
premiumFromOrgAdmin: boolean
}

/** Fetch the user authenticated by the token. */
export async function fetchUser(
provider: Provider,
endpoint: string,
token: string
token: string,
kactusState?: KactusState
): Promise<Account> {
const api = new API(provider, endpoint, token)
try {
Expand All @@ -793,7 +803,7 @@ export async function fetchUser(
avatarURL,
user.id,
user.name || user.login,
unlockedKactusStatus
unlockedKactusStatus || kactusState || null
)
} catch (e) {
log.warn(`fetchUser: failed with endpoint ${endpoint}`, e)
Expand Down Expand Up @@ -966,14 +976,7 @@ export async function checkUnlockedKactus(
user: IAPIUser,
emails: ReadonlyArray<IAPIEmail>,
endpoint: string
): Promise<{
enterprise: boolean
enterpriseFromOrg: boolean
enterpriseFromOrgAdmin: boolean
premium: boolean
premiumFromOrg: boolean
premiumFromOrgAdmin: boolean
} | null> {
): Promise<KactusState | null> {
try {
const path = `${KactusAPIEndpoint}/checkUnlocked`
const response = await fetch(path, {
Expand Down
7 changes: 6 additions & 1 deletion app/src/lib/stores/accounts-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,5 +282,10 @@ async function updatedAccount(account: Account): Promise<Account> {
)
}

return fetchUser(account.provider, account.endpoint, account.token)
return fetchUser(
account.provider,
account.endpoint,
account.token,
account.getKactusStatus()
)
}
25 changes: 13 additions & 12 deletions app/src/models/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,17 @@ export class Account {
: false
}

public getKactusStatus() {
return {
premium: this.unlockedKactus,
enterprise: this.unlockedEnterpriseKactus,
premiumFromOrg: this.unlockedKactusFromOrg,
enterpriseFromOrg: this.unlockedEnterpriseKactusFromOrg,
premiumFromOrgAdmin: this.unlockedKactusFromOrgAdmin,
enterpriseFromOrgAdmin: this.unlockedEnterpriseKactusFromOrgAdmin,
}
}

public withToken(token: string): Account {
return new Account(
this.provider,
Expand All @@ -92,14 +103,7 @@ export class Account {
this.avatarURL,
this.id,
this.name,
{
premium: this.unlockedKactus,
enterprise: this.unlockedEnterpriseKactus,
premiumFromOrg: this.unlockedKactusFromOrg,
enterpriseFromOrg: this.unlockedEnterpriseKactusFromOrg,
premiumFromOrgAdmin: this.unlockedKactusFromOrgAdmin,
enterpriseFromOrgAdmin: this.unlockedEnterpriseKactusFromOrgAdmin,
}
this.getKactusStatus()
)
}

Expand All @@ -114,6 +118,7 @@ export class Account {
this.id,
this.name,
{
...this.getKactusStatus(),
premium: enterprise
? this.unlockedKactusFromOrg
? this.unlockedKactus
Expand All @@ -124,10 +129,6 @@ export class Account {
: this.unlockedEnterpriseKactusFromOrg
? this.unlockedEnterpriseKactus
: false,
premiumFromOrg: this.unlockedKactusFromOrg,
enterpriseFromOrg: this.unlockedEnterpriseKactusFromOrg,
premiumFromOrgAdmin: this.unlockedKactusFromOrgAdmin,
enterpriseFromOrgAdmin: this.unlockedEnterpriseKactusFromOrgAdmin,
}
)
}
Expand Down
3 changes: 3 additions & 0 deletions changelog.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{
"unreleased": [],
"releases": {
"0.3.16": [
"[Fixed] When the network is unavailable, fall back to the previous account status"
],
"0.3.15": [
"[New] When an update is available for Kactus, release notes can be viewed in Kactus",
"[Fixed] Authenticating to GitHub Enterprise fails when user has no emails defined",
Expand Down
2 changes: 1 addition & 1 deletion plugin/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion plugin/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "kactus",
"version": "0.3.15",
"version": "0.3.16",
"skpm": {
"description": "A proper version control tool for designers",
"main": "../out/plugin.sketchplugin",
Expand Down

0 comments on commit 7d82a86

Please sign in to comment.