Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create package "@eventespresso/config" #1302

Merged
merged 23 commits into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion domains/core/admin/blocks/src/event-attendees/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { BlockEditProps } from '@wordpress/blocks';
import type { AttendeesOrderByFields, EntityId, Order, RegistrationStatus } from '@eventespresso/data';

// TODO: consolidate data types
// LATER: consolidate data types
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a separate GH issue now for this and related LATERs - #1301

export interface EventAttendeesAttributes {
event: EntityId;
datetime: EntityId;
Expand Down
10 changes: 5 additions & 5 deletions domains/core/admin/blocks/src/services/apollo/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import type {
* @see plugins/event-espresso-core/core/domain/services/graphql/types
*/

// TODO: consolidate data types
// LATER: consolidate data types
export interface Attendee extends Entity, Address {
avatar?: string;
bio?: string;
Expand All @@ -31,14 +31,14 @@ export type AttendeeEdge<Connection = 'EspressoRootQueryAttendeesConnection'> =

export type AttendeesList = AttendeeList<AttendeeEdge>;

// TODO: consolidate data types
// LATER: consolidate data types
export interface Event extends Entity {
description: string;
name: string;
shortDescription: string;
}

// TODO: consolidate data types
// LATER: consolidate data types
export interface EventData {
espressoEvent: Event;
}
Expand All @@ -47,7 +47,7 @@ export type EventEdge<Connection = 'RootQueryToEspressoEventConnection'> = Entit

export type EventsList = EventList<EventEdge>;

// TODO: consolidate data types
// LATER: consolidate data types
export interface Datetime extends Entity {
name: string;
}
Expand All @@ -56,7 +56,7 @@ export type DatetimeEdge<Connection = 'EspressoRootQueryDatetimesConnection'> =

export type DatetimesList = DatetimeList<DatetimeEdge>;

// TODO: consolidate data types
// LATER: consolidate data types
export interface Ticket extends Entity {
name: string;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { Entity, EntityId } from '@eventespresso/data';
import type { TpcPriceModifier } from '@eventespresso/tpc';
import type { Ticket, UpdateTicketInput } from '@eventespresso/edtr-services';

// TODO: consolidate data types
// LATER: consolidate data types
export interface DefaultTicket extends Entity, Omit<UpdateTicketInput, 'prices' | 'id'>, Pick<Ticket, 'userId'> {
deletedPrices?: Array<EntityId>;
prices?: Array<TpcPriceModifier>;
Expand Down
4 changes: 2 additions & 2 deletions domains/wpUser/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import type { EntityMetaMap } from '@eventespresso/services';

export type ticketMeta = EntityMetaMap;

// TODO: consolidate data types
// LATER: consolidate data types
export type CapabilityOptions = {
[optgroup: string]: {
[capability: string]: string; // label
};
};

// TODO: consolidate data types
// LATER: consolidate data types
export interface WpUserData {
capabilityOptions?: CapabilityOptions;
ticketMeta?: ticketMeta;
Expand Down
3 changes: 2 additions & 1 deletion eslint/levels.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module.exports = [
/* LEVEL 0 */ ['types'],
/* LEVEL -1 */ ['types'],
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Basement!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI... those indexes are just for display purposes... you could have just changed them to go from 1 - 10, or 1 - 11, or whatever

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I figured that. Didn't want to introduce unnecessary change to PR hence -1.

/* LEVEL 0 */ ['config'],
/* LEVEL 1 */ ['constants', 'i18n', 'events', 'ioc', 'toaster', 'utils'],
/* LEVEL 2 */ ['data', 'dates', 'hooks', 'icons', 'plugins', 'slot-fill', 'registry', 'styles', 'storage'],
/* LEVEL 3 */ ['adapters', 'predicates'],
Expand Down
18 changes: 18 additions & 0 deletions packages/config/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"author": "Event Espresso",
"name": "@eventespresso/config",
"repository": {
"type": "git",
"url": "git+https://github.com/eventespresso/barista.git"
},
"private": true,
"version": "1.0.0",
"main": "src/index.ts",
"scripts": {
"lint": "eslint ./src/**/*.{ts,tsx} --format=codeframe",
"lint-fix": "eslint ./src/**/*.{js,jsx,ts,tsx} --format=codeframe --fix",
"update-deps": "npx ncu -u"
},
"sideEffects": false,
"license": "GPL-3.0"
}
5 changes: 5 additions & 0 deletions packages/config/src/BrandName.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Type } from '.';

export function BrandName(config?: Partial<Type.BrandName>): Type.BrandName {
return config ?? 'Event Espresso';
}
26 changes: 26 additions & 0 deletions packages/config/src/Currency.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import type { Type } from '.';

export const Currency = (config?: Partial<Type.Currency>): Type.Currency => {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { subunits: exclude, ...defaults } = defaultCurrency;
const subunits = Subunits(config);
return { ...defaults, ...config, subunits };
};

function Subunits(config?: Partial<Type.Currency>): Type.Currency['subunits'] {
if (config?.subunits) return config.subunits;
const dp = config?.decimalPlaces ?? defaultCurrency.decimalPlaces;
return Math.pow(10, dp);
}

const defaultCurrency: Type.Currency = {
code: 'USD',
singularLabel: 'Dollar',
pluralLabel: 'Dollars',
sign: '$',
signB4: false,
decimalPlaces: 2,
decimalMark: '.',
thousandsSeparator: ',',
subunits: 100, // Math.pow(10, 2);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Explanation at how we arrive at number 100

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right, based on the decimal places

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My interpretation of the default value came from here:

subunits: config?.subunits >= 0 ? config.subunits : Math.pow(10, config?.decimalPlaces || 2),

By default value, I mean when the config and all subsequent properties are undefined.

};
18 changes: 18 additions & 0 deletions packages/config/src/DateTimeFormats.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import type { Type } from '.';

export const DateTimeFormats = (config?: Partial<Type.DateTimeFormats>): Type.DateTimeFormats => {
const date = config?.dateFormat ?? defaultsFormats.date;
const time = config?.timeFormat ?? defaultsFormats.time;
const format = config?.dateTimeFormat ?? date + ' ' + time;

return {
dateFormat: date,
timeFormat: time,
dateTimeFormat: format,
};
};

const defaultsFormats = {
date: 'YYYY-MM-DD',
time: 'HH:mm:ss',
} as const;
36 changes: 36 additions & 0 deletions packages/config/src/Factory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import {
Type,
EventEspressoData,
BrandName,
Currency,
DateTimeFormats,
Locale,
Nonce,
SiteUrls,
Timezone,
WpDebug,
User,
GeneralSettings,
SitePermissions,
} from '.';

export class Factory {
constructor(
private readonly config?: EventEspressoData['config'],
private readonly api?: EventEspressoData['api']
) {}

get = (): Type.Config => ({
brandName: BrandName(this.config?.coreDomain.brandName),
currency: Currency(this.config?.siteCurrency),
currentUser: User(this.config?.currentUser),
generalSettings: GeneralSettings(this.config?.generalSettings),
dateTimeFormats: DateTimeFormats(this.config?.generalSettings),
locale: Locale(this.config?.locale),
nonce: Nonce(this.api?.restApiNonce),
sitePermissions: SitePermissions(this.config?.sitePermissions),
siteUrl: SiteUrls(this.config?.siteUrls),
timezone: Timezone(this.config?.locale.siteTimezone),
wp_debug: WpDebug(this.config?.wp_debug),
});
}
11 changes: 11 additions & 0 deletions packages/config/src/GeneralSettings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Type } from './types';

export function GeneralSettings(config?: Partial<Type.GeneralSettings>): Type.GeneralSettings {
return { ...defaultSettings, ...config };
}

const defaultSettings: Type.GeneralSettings = {
dateFormat: '',
timeFormat: '',
timezone: '',
};
13 changes: 13 additions & 0 deletions packages/config/src/Locale.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Timezone, Type } from '.';

export const Locale = (config?: Partial<Type.Locale>): Type.Locale => ({
user: createLocale(config?.user ?? defaultLocale),
site: createLocale(config?.site ?? defaultLocale),
siteTimezone: Timezone(config?.siteTimezone),
});

const defaultLocale = 'en-US';

function createLocale(string: string): string {
return string.replace('_', '-');
}
5 changes: 5 additions & 0 deletions packages/config/src/Nonce.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import type { Type } from '.';

export function Nonce(config?: Type.Nonce) {
return config ?? '';
}
5 changes: 5 additions & 0 deletions packages/config/src/SitePermissions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import type { Type } from '.';

export function SitePermissions(config?: Type.SitePermissions): Type.SitePermissions {
return config ?? [];
}
11 changes: 11 additions & 0 deletions packages/config/src/SiteUrls.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import type { Type } from '.';

export const SiteUrls = (config?: Partial<Type.SiteUrls>): Type.SiteUrls => ({
...defaultUrls,
...config,
});

const defaultUrls: Type.SiteUrls = {
admin: '',
home: '',
};
12 changes: 12 additions & 0 deletions packages/config/src/Timezone.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import type { Type } from '.';

export const Timezone = (config?: Partial<Type.Timezone>): Type.Timezone => ({
...defaultTimezone,
...config,
});

const defaultTimezone: Type.Timezone = {
city: 'UTC',
name: 'UTC',
offset: 0,
};
19 changes: 19 additions & 0 deletions packages/config/src/User.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import type { Type } from '.';

export function User(user?: Type.User) {
return user ?? defaultUser;
}

const defaultUser: Type.User = {
capabilities: [],
description: '',
email: '',
firstName: '',
id: '',
lastName: '',
locale: '',
name: '',
nicename: '',
nickname: '',
username: '',
};
5 changes: 5 additions & 0 deletions packages/config/src/WpDebug.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import type { Type } from '.';

export function WpDebug(bool?: Type.WpDebug) {
return bool ?? false;
}
16 changes: 16 additions & 0 deletions packages/config/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export * from './types';

export * from './Currency';
export * from './DateTimeFormats';
export * from './Locale';
export * from './SiteUrls';
export * from './Timezone';
export * from './BrandName';
export * from './Factory';
export * from './Nonce';
export * from './WpDebug';
export * from './User';
export * from './GeneralSettings';
export * from './SitePermissions';

export * as test from './test';
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { EventEspressoDomData } from '../../types';
import type { EventEspressoData } from '..';

export const mockEspressoDomData: EventEspressoDomData = {
export const mockData: EventEspressoData = {
api: {
graphqlEndpoint: 'http://www.dev.test/graphql',
restApiBaseUrl: 'http://www.dev.test/wp-json/',
Expand Down
1 change: 1 addition & 0 deletions packages/config/src/test/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { mockData } from './data';
2 changes: 2 additions & 0 deletions packages/config/src/types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './window';
export * from './types';
68 changes: 68 additions & 0 deletions packages/config/src/types/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import type { User as _User } from '@eventespresso/data';

export module Type {
export interface Currency {
code: string;
singularLabel: string;
pluralLabel: string;
sign: string;
signB4: boolean;
decimalPlaces: number;
decimalMark: string;
thousandsSeparator: string;
subunits: number;
}

export interface DateTimeFormats {
dateFormat: string;
timeFormat: string;
dateTimeFormat: string;
}

export interface Locale {
user: string;
site: string;
siteTimezone: Timezone;
}

export interface SiteUrls {
admin: string;
home: string;
}

export interface Timezone {
city: string;
name: string;
offset: number;
}

export interface GeneralSettings {
dateFormat: string;
timeFormat: string;
timezone: string;
}

export interface Config {
brandName: BrandName;
currency: Currency;
currentUser: User; // LATER: consolidate data types
dateTimeFormats: DateTimeFormats;
generalSettings: GeneralSettings;
locale: Locale;
nonce: Nonce;
sitePermissions?: SitePermissions;
siteUrl: SiteUrls;
timezone: Timezone;
wp_debug: WpDebug;
}

export type BrandName = string;

export type Nonce = string;

export type WpDebug = boolean;

export type User = _User;

export type SitePermissions = string[]; // LATER: consolidate data types
}
Loading
Loading