Skip to content

Commit

Permalink
Merge pull request #3163 from specklesystems/oguzhan/cnx-549-set-sele…
Browse files Browse the repository at this point in the history
…cted-account-to-be-the-default-account

Feat(dui3): CNX-549 set selected account to be the default account
  • Loading branch information
didimitrie authored Oct 7, 2024
2 parents ae30e21 + 6cc5382 commit d19d02d
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
1 change: 1 addition & 0 deletions packages/dui3/components/accounts/Menu.vue
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ const { accounts, defaultAccount, userSelectedAccount, isLoading } =
const selectAccount = (acc: DUIAccount) => {
userSelectedAccount.value = acc
accountStore.setUserSelectedAccount(acc) // saves the selected account id into DUI3Config.db for later use
showAccountsDialog.value = false
void trackEvent('DUI3 Action', { name: 'Account change' })
}
Expand Down
2 changes: 1 addition & 1 deletion packages/dui3/components/wizard/ProjectSelector.vue
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,9 @@ watch(searchText, () => {
emit('search-text-update', searchText.value)
})
// TODO: this function is never triggered!! remove or evaluate
const selectAccount = (account: DUIAccount) => {
selectedAccountId.value = account.accountInfo.id
void trackEvent('DUI3 Action', { name: 'Account Select' }, account.accountInfo.id)
}
Expand Down
6 changes: 6 additions & 0 deletions packages/dui3/lib/bindings/definitions/IConfigBinding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ export interface IConfigBinding extends IBinding<IConfigBindingEvents> {
getIsDevMode: () => Promise<boolean>
getConfig: () => Promise<ConnectorConfig>
updateConfig: (config: ConnectorConfig) => void
setUserSelectedAccountId: (accountId: string) => void
getUserSelectedAccountId: () => Promise<AccountsConfig>
}

export interface IConfigBindingEvents extends IBindingSharedEvents {}
Expand All @@ -24,5 +26,9 @@ export type ConnectorConfig = {
darkTheme: boolean
}

export type AccountsConfig = {
userSelectedAccountId: string
}

// Useless, but will do for now :)
export class MockedConfigBinding extends BaseBridge {}
27 changes: 26 additions & 1 deletion packages/dui3/store/accounts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const accountTestQuery = gql`

export const useAccountStore = defineStore('accountStore', () => {
const app = useNuxtApp()
const { $accountBinding } = app
const { $accountBinding, $configBinding } = app

const hostAppStore = useHostAppStore()

Expand All @@ -60,6 +60,16 @@ export const useAccountStore = defineStore('accountStore', () => {
return userSelectedAccount.value || defaultAccount.value
})

const setUserSelectedAccount = (acc: DUIAccount) => {
userSelectedAccount.value = acc
try {
// NOTE: for the safe merge!
$configBinding.setUserSelectedAccountId(acc.accountInfo.id) // not need to await, fire and forget?
} catch (error) {
console.warn(error)
}
}

const testAccounts = async () => {
isLoading.value = true

Expand Down Expand Up @@ -224,13 +234,28 @@ export const useAccountStore = defineStore('accountStore', () => {
void testAccounts()
})

const init = async () => {
await refreshAccounts()
try {
const accountsConfig = await $configBinding.getUserSelectedAccountId()
userSelectedAccount.value = accounts.value.find(
(a) => a.accountInfo.id === accountsConfig.userSelectedAccountId
) as DUIAccount
} catch (error) {
console.warn(error)
}
}

init()

app.vueApp.provide(ApolloClients, apolloClients)
return {
isLoading,
accounts,
defaultAccount,
activeAccount,
userSelectedAccount,
setUserSelectedAccount,
accountByServerUrl,
isAccountExistsById,
isAccountExistsByServer,
Expand Down

0 comments on commit d19d02d

Please sign in to comment.