Skip to content

Commit

Permalink
migrate redux actions to valtio
Browse files Browse the repository at this point in the history
  • Loading branch information
frontegg-david committed Jul 21, 2024
1 parent 03a573f commit 8fb4ff6
Showing 1 changed file with 21 additions and 41 deletions.
62 changes: 21 additions & 41 deletions packages/vue/src/auth/service.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { AuthPluginOptions } from './interfaces';
import { FronteggPluginService, PluginConfig, PluginOptions } from '../interfaces';
import { FronteggStore } from '@frontegg/redux-store';
import { defaultFronteggRoutes, FronteggStore } from '@frontegg/redux-store';
import {
authActions,
LoginActions,
SocialLoginActions,
ActivateAccountActions,
Expand All @@ -15,25 +14,10 @@ import {
TeamActions,
ApiTokensActions,
SecurityPolicyActions,
tenantsActions,
loginActions,
socialLoginsActions,
activateAccountActions,
acceptInvitationActions,
forgotPasswordActions,
signUpActions,
profileActions,
ssoActions,
mfaActions,
teamActions,
apiTokensActions,
securityPolicyActions,
User,
AuthState,
authInitialState,
TenantsActions,
AuthPageRoutes,
AuthActions,
} from '@frontegg/redux-store';
import { ActionsHolder } from './ActionsHolder';
import VueRouter from 'vue-router';
Expand All @@ -42,8 +26,8 @@ export class FronteggAuthService implements FronteggPluginService {
pluginConfig!: PluginConfig;
router?: VueRouter | null;
private readonly _routes!: AuthPageRoutes;
private store?: FronteggStore;
private state: AuthState = authInitialState;
private store!: FronteggStore;
private state!: AuthState;
loginActions!: LoginActions;
socialLoginsActions!: SocialLoginActions;
activateAccountActions!: ActivateAccountActions;
Expand All @@ -60,7 +44,7 @@ export class FronteggAuthService implements FronteggPluginService {

constructor({ router, ...options }: AuthPluginOptions) {
this._routes = {
...authInitialState.routes,
...defaultFronteggRoutes,
...(options as any).routes,
};
this.router = router;
Expand Down Expand Up @@ -88,34 +72,30 @@ export class FronteggAuthService implements FronteggPluginService {
init = (options: PluginOptions, store: FronteggStore) => {
this.store = store;

Object.entries({
loginActions,
socialLoginsActions,
activateAccountActions,
acceptInvitationActions,
forgotPasswordActions,
signUpActions,
profileActions,
ssoActions,
mfaActions,
teamActions,
apiTokensActions,
securityPolicyActions,
tenantsActions,
}).forEach(([ key, actions ]: any) => {
Object.assign(this, { [key]: bindActionCreators(actions, this.store!.dispatch) });
});

const detachableActions: any = bindActionCreators(authActions, this.store!.dispatch);
ActionsHolder.setActions(detachableActions);

this.loginActions = store.stateActions.auth.loginActions;
this.socialLoginsActions = store.stateActions.auth.socialLoginActions;
this.activateAccountActions = store.stateActions.auth.activateAccountActions;
this.acceptInvitationActions = store.stateActions.auth.acceptInvitationActions;
this.forgotPasswordActions = store.stateActions.auth.forgotPasswordActions;
this.signUpActions = store.stateActions.auth.signUpActions;
this.profileActions = store.stateActions.auth.profileActions;
this.ssoActions = store.stateActions.auth.ssoActions;
this.mfaActions = store.stateActions.auth.mfaActions;
this.teamActions = store.stateActions.auth.teamActions;
this.apiTokensActions = store.stateActions.auth.apiTokensActions;
this.securityPolicyActions = store.stateActions.auth.securityPolicyActions;
this.tenantsActions = store.stateActions.auth.tenantsActions;

ActionsHolder.setActions(store.actions);

if (!options.hostedLoginBox) {
this.store.subscribe(this.storeSubscriber);
}
};

storeSubscriber = () => {
this.state = this.store!.getState().auth;
this.state = this.store.getState().auth;

let notifyChange = false;
if (this.state.user !== this.userRef) {
Expand Down

0 comments on commit 8fb4ff6

Please sign in to comment.