-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a0bfe8e
commit 361129b
Showing
13 changed files
with
229 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
21
packages/core/state/src/lib/+state/timezone/timezone.actions.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
65
packages/core/state/src/lib/+state/timezone/timezone.effects.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
35
packages/core/state/src/lib/+state/timezone/timezone.facade.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
48
packages/core/state/src/lib/+state/timezone/timezone.reducer.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
34
packages/core/state/src/lib/+state/timezone/timezone.selectors.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
packages/core/types/src/lib/interfaces/state/timezone.state.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'>; |