@/store#AppState TypeScript Examples

The following examples show how to use @/store#AppState. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: database.actions.ts    From passwords-fountain with MIT License 6 votes vote down vote up
databaseActions = {
    setClient: async (
        appState: AppState,
        masterKey: string,
        adminKey: string
    ): Promise<Partial<AppState>> => {
        const { setupClient } = await import('./database.service');
        const { encrypt, decrypt } = await import(
            '@/modules/cipher/cipher.service'
        );

        try {
            const decryptedAdminKey =
                adminKey === selectAdminKey()
                    ? (decrypt(adminKey, masterKey) as string)
                    : adminKey;

            const client: Client = await setupClient({
                secret: decryptedAdminKey,
            });

            const encryptedAdminKey = encrypt(decryptedAdminKey, masterKey);
            localStorage.setItem(
                adminKeyLocalStorageKeyName,
                encryptedAdminKey
            );
            return merge({ client });
        } catch (err) {
            callAction(overlayActions.hideGlobalLoader);
            callAction(
                overlayActions.showSnackbar,
                'snackbar.couldNotConnectToDB',
                'error'
            );
            return merge({});
        }
    },
}
Example #2
Source File: overlay.actions.ts    From passwords-fountain with MIT License 6 votes vote down vote up
overlayActions = {
    hideSnackbar(appState: AppState, messageId: number): Partial<AppState> {
        return merge({
            snackbarMessages: appState.overlay.snackbarMessages.filter(
                (message: SnackbarMessage) => message.id !== messageId
            ),
        });
    },
    showSnackbar(
        appState: AppState,
        messageKey: string,
        type: SnackbarType
    ): Partial<AppState> {
        return merge({
            snackbarMessages: [
                ...appState.overlay.snackbarMessages,
                {
                    id: Date.now(),
                    messageKey,
                    type,
                },
            ],
        });
    },
    showGlobalLoader(): Partial<AppState> {
        return merge({ isGlobalLoaderVisible: true });
    },
    hideGlobalLoader(): Partial<AppState> {
        return merge({ isGlobalLoaderVisible: false });
    },
} as const
Example #3
Source File: store.ts    From passwords-fountain with MIT License 5 votes vote down vote up
mergeState = <T>(moduleName: keyof AppState) => (
    changedState: Partial<T>
): Partial<AppState> => ({
    [moduleName]: { ...store.getState()[moduleName], ...changedState },
})
Example #4
Source File: store.ts    From passwords-fountain with MIT License 5 votes vote down vote up
callAction = (
    action: Action<AppState>,
    ...args: any[]
): Promise<void> | void => {
    return store.action(action)(...args);
}
Example #5
Source File: database.selectors.ts    From passwords-fountain with MIT License 5 votes vote down vote up
selectDatabase = (state: AppState): DatabaseState => state.database
Example #6
Source File: overlay.selectors.ts    From passwords-fountain with MIT License 5 votes vote down vote up
selectOverlay = (state: AppState): OverlayState => state.overlay
Example #7
Source File: passwordList.selectors.ts    From passwords-fountain with MIT License 5 votes vote down vote up
selectPasswordList = (state: AppState): PasswordListState =>
    state.passwordList
Example #8
Source File: overlay.actions.spec.ts    From passwords-fountain with MIT License 4 votes vote down vote up
describe('Overlay actions', () => {
    afterEach(() => {
        jest.restoreAllMocks();
    });

    describe('hideSnackbar', () => {
        it('removes snackbar message from snackbarMessages list', () => {
            const state = {
                overlay: {
                    snackbarMessages: [
                        { id: 0, messageKey: 'Hello', type: 'success' },
                        { id: 1, messageKey: 'Bye', type: 'error' },
                    ],
                },
            };
            jest.spyOn(store, 'getState').mockReturnValue(state as any);
            const newState = overlayActions.hideSnackbar(state as AppState, 0);

            expect(newState).toEqual({
                overlay: {
                    snackbarMessages: [
                        { id: 1, messageKey: 'Bye', type: 'error' },
                    ],
                },
            });
        });
    });

    describe('showSnackbar', () => {
        it('adds snackbar message to the end of snackbarMessages list ', () => {
            const state = {
                overlay: {
                    snackbarMessages: [
                        { id: 0, messageKey: 'Hello', type: 'success' },
                        { id: 1, messageKey: 'Bye', type: 'error' },
                    ],
                },
            };
            jest.spyOn(store, 'getState').mockReturnValue(state as any);

            const newState = overlayActions.showSnackbar(
                state as AppState,
                'Nice to meet you',
                'info'
            );

            expect(newState).toEqual({
                overlay: {
                    snackbarMessages: [
                        { id: 0, messageKey: 'Hello', type: 'success' },
                        { id: 1, messageKey: 'Bye', type: 'error' },
                        {
                            id: expect.any(Number),
                            messageKey: 'Nice to meet you',
                            type: 'info',
                        },
                    ],
                },
            });
        });
    });

    describe('showGlobalLoader', () => {
        it('changes isGlobalLoaderVisible to TRUE', () => {
            jest.spyOn(store, 'getState').mockReturnValue({
                overlay: {
                    isGlobalLoaderVisible: false,
                },
            } as any);
            const newState = overlayActions.showGlobalLoader();

            expect(newState).toEqual({
                overlay: { isGlobalLoaderVisible: true },
            });
        });
    });

    describe('hideGlobalLoader', () => {
        it('changes isGlobalLoaderVisible to FALSE', () => {
            jest.spyOn(store, 'getState').mockReturnValue({
                overlay: {
                    isGlobalLoaderVisible: true,
                },
            } as any);
            const newState = overlayActions.hideGlobalLoader();

            expect(newState).toEqual({
                overlay: { isGlobalLoaderVisible: false },
            });
        });
    });
});
Example #9
Source File: passwordList.actions.ts    From passwords-fountain with MIT License 4 votes vote down vote up
passwordListActions = {
    switchOptionPanelVariant: (
        appState: AppState,
        optionPanelVariantName: OptionsPanelVariantName
    ): Partial<AppState> => {
        return merge({
            currentOptionPanelVariantName: optionPanelVariantName,
        });
    },
    resetSelectedAndDecryptedEntity: (): Partial<AppState> => {
        return merge({
            selectedAndDecryptedEntity: {} as PasswordEntityPayloadReferable,
        });
    },
    setSelectedAndDecryptedEntity: (
        appState: AppState,
        entity: PasswordEntityPayloadReferable
    ): Partial<AppState> => {
        return merge({
            selectedAndDecryptedEntity: entity,
        });
    },
    fetchPasswords: async (
        appState: AppState,
        masterKey: string,
        adminKey: string,
        forceReconnect = false
    ): Promise<Partial<AppState>> => {
        callAction(overlayActions.showGlobalLoader);
        const { fetchAllPasswordEntities } = await import(
            '@/modules/database/database.service'
        );

        if (!selectIsClientSet(appState) || forceReconnect) {
            await callAction(databaseActions.setClient, masterKey, adminKey);
        }

        const client = selectClient(store.getState()) as Client;

        try {
            const passwords = await fetchAllPasswordEntities(client);
            callAction(overlayActions.hideGlobalLoader);
            callAction(
                overlayActions.showSnackbar,
                'snackbar.passwordsFetchedSuccessfully',
                'success'
            );
            callAction(
                passwordListActions.switchOptionPanelVariant,
                optionsPanelVariantNames.entityFormCollapsed
            );

            return merge({ passwords });
        } catch (err) {
            callAction(overlayActions.hideGlobalLoader);
            callAction(
                overlayActions.showSnackbar,
                'snackbar.couldNotFetchPasswords',
                'error'
            );
            return merge({});
        }
    },
    addNewPassword: async (
        appState: AppState,
        newEntityPayload: PasswordEntityPayload,
        masterKey: string
    ): Promise<Partial<AppState>> => {
        callAction(overlayActions.showGlobalLoader);
        const { createPasswordEntity } = await import(
            '@/modules/database/database.service'
        );
        const { encrypt } = await import('@/modules/cipher/cipher.service');

        const client = selectClient(store.getState()) as Client;

        try {
            const encryptedPasswordEntity = encrypt(
                {
                    login: newEntityPayload.login,
                    password: newEntityPayload.password,
                },
                masterKey,
                true
            );
            await createPasswordEntity(client, {
                label: newEntityPayload.label,
                value: encryptedPasswordEntity,
            });
            callAction(overlayActions.hideGlobalLoader);
            callAction(
                passwordListActions.switchOptionPanelVariant,
                optionsPanelVariantNames.entityFormCollapsed
            );
        } catch (err) {
            callAction(overlayActions.hideGlobalLoader);
            callAction(
                overlayActions.showSnackbar,
                'snackbar.couldNotCreateNewPassword',
                'error'
            );
        } finally {
            return merge({});
        }
    },
    editPassword: async (
        appState: AppState,
        entityPayload: PasswordEntityPayloadReferable,
        masterKey: string
    ): Promise<Partial<AppState>> => {
        callAction(overlayActions.showGlobalLoader);
        const { updatePasswordEntity } = await import(
            '@/modules/database/database.service'
        );
        const { encrypt } = await import('@/modules/cipher/cipher.service');

        const client = selectClient(store.getState()) as Client;

        try {
            const encryptedPasswordEntity = encrypt(
                {
                    login: entityPayload.login,
                    password: entityPayload.password,
                },
                masterKey,
                true
            );
            await updatePasswordEntity(client, entityPayload.refId, {
                label: entityPayload.label,
                value: encryptedPasswordEntity,
            });
            callAction(overlayActions.hideGlobalLoader);
            callAction(passwordListActions.resetSelectedAndDecryptedEntity);
            callAction(
                passwordListActions.switchOptionPanelVariant,
                optionsPanelVariantNames.entityFormCollapsed
            );
        } catch (err) {
            callAction(overlayActions.hideGlobalLoader);
            callAction(
                overlayActions.showSnackbar,
                'snackbar.couldNotEditPassword',
                'error'
            );
        } finally {
            return merge({});
        }
    },
    removePassword: async (
        appState: AppState,
        refId: string
    ): Promise<Partial<AppState>> => {
        callAction(overlayActions.showGlobalLoader);
        const { deletePasswordEntity } = await import(
            '@/modules/database/database.service'
        );

        const client = selectClient(store.getState()) as Client;

        try {
            await deletePasswordEntity(client, refId);
            callAction(overlayActions.hideGlobalLoader);
        } catch (err) {
            callAction(overlayActions.hideGlobalLoader);
            callAction(
                overlayActions.showSnackbar,
                'snackbar.couldNotRemovePassword',
                'error'
            );
        } finally {
            return merge({});
        }
    },
}