Skip to content

Commit

Permalink
chore: state timezone
Browse files Browse the repository at this point in the history
  • Loading branch information
yosvelquintero committed Sep 13, 2023
1 parent a0bfe8e commit 361129b
Show file tree
Hide file tree
Showing 13 changed files with 229 additions and 5 deletions.
1 change: 1 addition & 0 deletions packages/core/config/src/lib/constants/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export const STORE: Readonly<IStoreConstant> = {
authnState: 'authnStateV1',
localeState: 'localeStateV1',
routerState: 'routerStateV1',
timezoneState: 'timezoneStateV1',
},
config: {
app: {
Expand Down
3 changes: 3 additions & 0 deletions packages/core/state/src/lib/+state/timezone/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export { TimezoneEffects } from './timezone.effects';
export { TimezoneFacade } from './timezone.facade';
export { timezoneReducer } from './timezone.reducer';
21 changes: 21 additions & 0 deletions packages/core/state/src/lib/+state/timezone/timezone.actions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { createAction, props } from '@ngrx/store';

import {
IIoRestorecommerceResourcebaseReadRequest,
IoRestorecommerceTimezoneTimezone,
} from '@console-core/graphql';

export const timezoneReadRequest = createAction(
'[TIMEZONE] Read request',
props<{ payload: IIoRestorecommerceResourcebaseReadRequest }>()
);

export const timezoneReadRequestSuccess = createAction(
'[TIMEZONE] Read request success',
props<{ payload: IoRestorecommerceTimezoneTimezone[] }>()
);

export const timezoneReadRequestFail = createAction(
'[TIMEZONE] Read request fail',
props<{ error: string }>()
);
65 changes: 65 additions & 0 deletions packages/core/state/src/lib/+state/timezone/timezone.effects.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { Injectable } from '@angular/core';
import { Actions, createEffect, ofType } from '@ngrx/effects';
import { catchError, map, of, switchMap, tap } from 'rxjs';

import { IoRestorecommerceTimezoneTimezone } from '@console-core/graphql';
import { ENotificationTypes } from '@console-core/types';

import { TimezoneService } from '../../services';
import { AppFacade } from '../app';

import * as timezoneActions from './timezone.actions';

@Injectable()
export class TimezoneEffects {
timezoneReadRequest$ = createEffect(() => {
return this.actions$.pipe(
ofType(timezoneActions.timezoneReadRequest),
switchMap(({ payload }) =>
this.timezoneService.read(payload).pipe(
map((result) => {
const data = result?.data?.master_data?.timezone?.Read?.details;

if (data?.operationStatus?.code !== 200 || !data?.items?.length) {
return timezoneActions.timezoneReadRequestFail({
error: 'unknown error',
});
}

return timezoneActions.timezoneReadRequestSuccess({
payload: data?.items.map(
(item) => item.payload as IoRestorecommerceTimezoneTimezone
),
});
}),
catchError((error: Error) =>
of(
timezoneActions.timezoneReadRequestFail({ error: error.message })
)
)
)
)
);
});

handleNotificationErrors$ = createEffect(
() => {
return this.actions$.pipe(
ofType(timezoneActions.timezoneReadRequestFail),
tap(({ error }) => {
this.appFacade.addNotification({
content: error ?? 'unknown error',
type: ENotificationTypes.ERROR,
});
})
);
},
{ dispatch: false }
);

constructor(
private readonly actions$: Actions,
private readonly appFacade: AppFacade,
private readonly timezoneService: TimezoneService
) {}
}
35 changes: 35 additions & 0 deletions packages/core/state/src/lib/+state/timezone/timezone.facade.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { Injectable } from '@angular/core';
import { Store } from '@ngrx/store';

import { IIoRestorecommerceResourcebaseReadRequest } from '@console-core/graphql';

import * as timezoneActions from './timezone.actions';
import * as timezoneSelectors from './timezone.selectors';

@Injectable()
export class TimezoneFacade {
// Selectors
readonly timezoneIds$ = this.store.select(
timezoneSelectors.selectTimezoneIds
);
readonly timezoneEntities$ = this.store.select(
timezoneSelectors.selectTimezoneEntities
);
readonly allTimezones$ = this.store.select(
timezoneSelectors.selectAllTimezones
);
readonly timezoneTotal$ = this.store.select(
timezoneSelectors.selectTimezoneTotal
);
readonly actionStatus$ = this.store.select(
timezoneSelectors.selectActionStatus
);
readonly error$ = this.store.select(timezoneSelectors.selectError);

// Actions
readonly timezoneReadRequest = (
payload: IIoRestorecommerceResourcebaseReadRequest
) => this.store.dispatch(timezoneActions.timezoneReadRequest({ payload }));

constructor(private readonly store: Store) {}
}
48 changes: 48 additions & 0 deletions packages/core/state/src/lib/+state/timezone/timezone.reducer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { EntityAdapter, createEntityAdapter } from '@ngrx/entity';
import { Action, createReducer, on } from '@ngrx/store';

import { IIoRestorecommerceTimezoneTimezone } from '@console-core/graphql';
import { EActionStatus, ITimezoneState } from '@console-core/types';

import * as timezoneActions from './timezone.actions';

export const adapter: EntityAdapter<IIoRestorecommerceTimezoneTimezone> =
createEntityAdapter<IIoRestorecommerceTimezoneTimezone>();

export const initialState: ITimezoneState = adapter.getInitialState({
selectedId: null,
actionStatus: EActionStatus.INIT,
error: null,
});

const reducer = createReducer<ITimezoneState>(
initialState,
on(
timezoneActions.timezoneReadRequest,
(state): ITimezoneState => ({
...state,
actionStatus: EActionStatus.CREATED,
})
),
on(
timezoneActions.timezoneReadRequestSuccess,
(state, { payload }): ITimezoneState =>
adapter.setAll(payload, {
...state,
actionStatus: EActionStatus.SUCCEEDED,
})
),
on(
timezoneActions.timezoneReadRequestFail,
(state, { error }): ITimezoneState => ({
...state,
actionStatus: EActionStatus.FAILED,
error,
})
)
);

export const timezoneReducer = (
state: ITimezoneState | undefined,
action: Action
) => reducer(state, action);
34 changes: 34 additions & 0 deletions packages/core/state/src/lib/+state/timezone/timezone.selectors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { createFeatureSelector, createSelector } from '@ngrx/store';

import { STORE } from '@console-core/config';
import { ITimezoneState } from '@console-core/types';

import { adapter } from './timezone.reducer';

export const selectTimezone = createFeatureSelector<ITimezoneState>(
STORE.states.timezoneState
);

const { selectIds, selectEntities, selectAll, selectTotal } =
adapter.getSelectors();

export const selectTimezoneIds = createSelector(selectTimezone, selectIds);

export const selectTimezoneEntities = createSelector(
selectTimezone,
selectEntities
);

export const selectAllTimezones = createSelector(selectTimezone, selectAll);

export const selectTimezoneTotal = createSelector(selectTimezone, selectTotal);

export const selectError = createSelector(
selectTimezone,
(state: ITimezoneState) => state.error
);

export const selectActionStatus = createSelector(
selectTimezone,
(state: ITimezoneState) => state.actionStatus
);
10 changes: 7 additions & 3 deletions packages/core/state/src/lib/core-state.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,19 @@ import {
LocaleFacade,
localeReducer,
RouterFacade,
TimezoneEffects,
TimezoneFacade,
timezoneReducer,
} from './+state';
import { AccountService, ApiService } from './services';

const facades = [
AccountFacade,
AppFacade,
AuthnFacade,
LocaleFacade,
RouterFacade,
TimezoneFacade,
];
const services = [ApiService, AccountService];

@NgModule({
imports: [
Expand All @@ -41,8 +43,10 @@ const services = [ApiService, AccountService];
EffectsModule.forFeature([AuthnEffects]),
StoreModule.forFeature(STORE.states.localeState, localeReducer),
EffectsModule.forFeature([LocaleEffects]),
StoreModule.forFeature(STORE.states.timezoneState, timezoneReducer),
EffectsModule.forFeature([TimezoneEffects]),
StoreModule.forFeature(STORE.states.routerState, fromRouter.routerReducer),
],
providers: [...facades, ...services],
providers: [...facades],
})
export class CoreStateModule {}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class TimezoneService {
private readonly masterDataTimezoneReadGQL: MasterDataTimezoneReadGQL
) {}

timezoneRead(
read(
payload: IIoRestorecommerceResourcebaseReadRequest
): Observable<ApolloQueryResult<MasterDataTimezoneReadQuery>> {
return this.masterDataTimezoneReadGQL.fetch({
Expand Down
1 change: 1 addition & 0 deletions packages/core/types/src/lib/interfaces/state/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export * from './authn.state';
export * from './locale.state';
export * from './router.state';
export * from './store.state';
export * from './timezone.state';
11 changes: 11 additions & 0 deletions packages/core/types/src/lib/interfaces/state/timezone.state.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { EntityState } from '@ngrx/entity';

import { IIoRestorecommerceTimezoneTimezone } from '@console-core/graphql';

import { IBaseStore } from './store.state';

export interface ITimezoneState
extends EntityState<IIoRestorecommerceTimezoneTimezone>,
IBaseStore {
selectedId: string | null;
}
1 change: 1 addition & 0 deletions packages/core/types/src/lib/interfaces/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export interface IStoreConstant {
readonly authnState: 'authnStateV1';
readonly localeState: 'localeStateV1';
readonly routerState: 'routerStateV1';
readonly timezoneState: 'timezoneStateV1';
};
readonly config: {
readonly app: {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/types/src/lib/types/new-notification.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { INotification } from '../interfaces';

export type TNewNotification = Omit<INotification, 'title' | 'date'>;
export type TNewNotification = Pick<INotification, 'type' | 'content'>;

0 comments on commit 361129b

Please sign in to comment.