/** * WordPress dependencies */ import { createSelector } from '@safe-wordpress/data'; /** * External dependencies */ import { find } from 'lodash'; import { EMPTY_ARRAY } from '@nab/utils'; import type { Account, AccountMode, FastSpringProduct, Invoice, License, Maybe, Quota, Site, SiteId, SubscriptionId, SubscriptionPlan, SubscriptionPeriod, SubscriptionState, FastSpringProductId, } from '@nab/types'; /** * Internal dependencies */ import { State } from './types'; export function getAccount( state: State ): Account { return state.account; } export function getSites( state: State, id?: SubscriptionId ): ReadonlyArray< Site > { if ( ! id ) { return EMPTY_ARRAY; } return state.sites; } export function getSite( state: State, id: SiteId ): Maybe< Site > { return find( state.sites, { id } ); } export function getInvoices( state: State, id: SubscriptionId ): ReadonlyArray< Invoice > { if ( ! id ) { return EMPTY_ARRAY; } return state.invoices; } export const getSubscriptionQuota = createSelector( ( state: State ): Quota => { const { quota, quotaExtra, quotaPerMonth } = state.account; const availableQuota = quota + quotaExtra; const percentage = Math.floor( ( 100 * ( availableQuota + 0.1 ) ) / quotaPerMonth ); return { mode: 'subscription', availableQuota, percentage, }; }, ( state: State ) => [ state.account ] ); export function getSubscriptionId( state: State ): SubscriptionId { return state.account.subscription; } export function getSubscriptionAddons( state: State ): ReadonlyArray< FastSpringProductId > { return state.account.addons || EMPTY_ARRAY; } export function getFullname( state: State ): string { return state.account.fullname; } export function getFirstname( state: State ): string { return state.account.firstname; } export function getLastname( state: State ): string { return state.account.lastname; } export function getEmail( state: State ): string { return state.account.email; } export function getStartDate( state: State ): string { return state.account.startDate; } export function getSubscriptionProduct( state: State ): FastSpringProduct[ 'id' ] { return state.account.productId; } export function getPlan( state: State ): 'free' | SubscriptionPlan { return state.account.plan; } export function getLicense( state: State ): License { return state.account.license; } export function getPhoto( state: State ): string { return state.account.photo; } export function getProductDisplay( state: State ): string { return state.account.productDisplay; } export function getMode( state: State ): AccountMode { return state.account.mode; } export function getPeriod( state: State ): SubscriptionPeriod { return state.account.period; } export function getExtraQuotaUnits( state: State ): number { return ( state.account.addonDetails[ 'nab-pageviews-monthly' as FastSpringProductId ] ?? state.account.addonDetails[ 'nab-pageviews-yearly' as FastSpringProductId ] ?? 0 ); } export function getState( state: State ): SubscriptionState { return state.account.state; } export function getDeactivationDate( state: State ): string { return state.account.deactivationDate; } export function getNextChargeDate( state: State ): string { return state.account.nextChargeDate; } export function getNextChargeTotal( state: State ): string { return state.account.nextChargeTotal; } export function getSitesAllowed( state: State ): number { return state.account.sitesAllowed; } export function getCurrency( state: State ): string { return state.account.currency; } export function getUrlToManagePayments( state: State ): string { return state.account.urlToManagePayments; } export function isAgency( state: State ): boolean { return !! state.account.isAgency; } export function isLocked( state: State ): boolean { return !! state.isPageLocked; } export function isAgencyFullViewEnabled( state: State ): boolean { return isAgency( state ) && !! state.isAgencyFullViewEnabled; }