storj/web/satellite/src/store/index.ts

98 lines
3.8 KiB
TypeScript
Raw Normal View History

2019-01-24 20:15:10 +00:00
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
import Vue from 'vue';
import Vuex from 'vuex';
import { ApiKeysApiGql } from '@/api/apiKeys';
import { AuthHttpApi } from '@/api/auth';
import { BucketsApiGql } from '@/api/buckets';
import { CreditsApiGql } from '@/api/credits';
import { PaymentsHttpApi } from '@/api/payments';
import { ProjectMembersApiGql } from '@/api/projectMembers';
2019-09-09 11:33:39 +01:00
import { ProjectsApiGql } from '@/api/projects';
2019-11-26 16:54:42 +00:00
import { ReferralHttpApi } from '@/api/referral';
2019-09-09 11:33:39 +01:00
import { ProjectUsageApiGql } from '@/api/usage';
import { notProjectRelatedRoutes, router } from '@/router';
import { ApiKeysState, makeApiKeysModule } from '@/store/modules/apiKeys';
2019-09-09 11:33:39 +01:00
import { appStateModule } from '@/store/modules/appState';
import { makeBucketsModule } from '@/store/modules/buckets';
2019-09-09 11:33:39 +01:00
import { makeCreditsModule } from '@/store/modules/credits';
import { makeNotificationsModule, NotificationsState } from '@/store/modules/notifications';
import { makePaymentsModule, PaymentsState } from '@/store/modules/payments';
import { makeProjectMembersModule, ProjectMembersState } from '@/store/modules/projectMembers';
import { makeProjectsModule, PROJECTS_MUTATIONS, ProjectsState } from '@/store/modules/projects';
2019-11-26 16:54:42 +00:00
import { makeReferralModule, ReferralState } from '@/store/modules/referral';
import { makeUsageModule, UsageState } from '@/store/modules/usage';
import { makeUsersModule, USER_ACTIONS } from '@/store/modules/users';
import { CreditUsage } from '@/types/credits';
import { User } from '@/types/users';
Vue.use(Vuex);
export class StoreModule<S> {
public state: S;
public mutations: any;
public actions: any;
public getters?: any;
}
// TODO: remove it after we will use modules as classes and use some DI framework
const authApi = new AuthHttpApi();
const apiKeysApi = new ApiKeysApiGql();
const creditsApi = new CreditsApiGql();
const bucketsApi = new BucketsApiGql();
const projectMembersApi = new ProjectMembersApiGql();
const projectsApi = new ProjectsApiGql();
const projectUsageApi = new ProjectUsageApiGql();
const paymentsApi = new PaymentsHttpApi();
2019-11-26 16:54:42 +00:00
const referralApi = new ReferralHttpApi();
class ModulesState {
public notificationsModule: NotificationsState;
public apiKeysModule: ApiKeysState;
public appStateModule;
public creditsModule: CreditUsage;
public projectMembersModule: ProjectMembersState;
public paymentsModule: PaymentsState;
public usersModule: User;
public projectsModule: ProjectsState;
public usageModule: UsageState;
2019-11-26 16:54:42 +00:00
public referralModule: ReferralState;
}
// Satellite store (vuex)
export const store = new Vuex.Store<ModulesState>({
modules: {
notificationsModule: makeNotificationsModule(),
apiKeysModule: makeApiKeysModule(apiKeysApi),
appStateModule,
creditsModule: makeCreditsModule(creditsApi),
projectMembersModule: makeProjectMembersModule(projectMembersApi),
paymentsModule: makePaymentsModule(paymentsApi),
usersModule: makeUsersModule(authApi),
projectsModule: makeProjectsModule(projectsApi),
usageModule: makeUsageModule(projectUsageApi),
bucketUsageModule: makeBucketsModule(bucketsApi),
2019-11-26 16:54:42 +00:00
referralModule: makeReferralModule(referralApi),
},
});
store.subscribe((mutation, state) => {
const currentRouteName = router.currentRoute.name;
const satelliteName = state.appStateModule.satelliteName;
switch (mutation.type) {
case PROJECTS_MUTATIONS.REMOVE:
document.title = `${router.currentRoute.name} | ${satelliteName}`;
break;
case PROJECTS_MUTATIONS.SELECT_PROJECT:
if (currentRouteName && !notProjectRelatedRoutes.includes(currentRouteName)) {
document.title = `${state.projectsModule.selectedProject.name} | ${currentRouteName} | ${satelliteName}`;
}
}
});
export default store;