{"id":16045470,"url":"https://github.com/madhusudhand/ngrx-helpers","last_synced_at":"2025-03-17T15:30:30.997Z","repository":{"id":33278505,"uuid":"155607890","full_name":"madhusudhand/ngrx-helpers","owner":"madhusudhand","description":"A library to simplify development with ngrx","archived":false,"fork":false,"pushed_at":"2023-01-07T06:59:01.000Z","size":2735,"stargazers_count":3,"open_issues_count":17,"forks_count":6,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-28T00:38:32.786Z","etag":null,"topics":["angular","angular6","ngrx","ngrx-store"],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/madhusudhand.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-10-31T18:49:34.000Z","updated_at":"2022-12-12T05:06:12.000Z","dependencies_parsed_at":"2022-08-26T15:40:14.665Z","dependency_job_id":null,"html_url":"https://github.com/madhusudhand/ngrx-helpers","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/madhusudhand%2Fngrx-helpers","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/madhusudhand%2Fngrx-helpers/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/madhusudhand%2Fngrx-helpers/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/madhusudhand%2Fngrx-helpers/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/madhusudhand","download_url":"https://codeload.github.com/madhusudhand/ngrx-helpers/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243864822,"owners_count":20360360,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["angular","angular6","ngrx","ngrx-store"],"created_at":"2024-10-09T00:04:04.994Z","updated_at":"2025-03-17T15:30:30.496Z","avatar_url":"https://github.com/madhusudhand.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ngrx-helpers\n\nA library to simplify ngrx implementation in Angular Apps.\n\n## Getting started\n\nFor complete reference, look at the [Example App](https://github.com/madhusudhand/ngrx-helpers/tree/master/src).\n\n### App Module configuration\n\nimport `NgrxHelperModule.forRoot()` in app.module along with other ngrx modules.\n\n### Effects\n\nThe helper class `NgrxEffect` simplifies the effect configuration.\nIt lets you configure effects with minimal inputs such as action, endpint and http method.\n\n```ts\n@Injectable()\nexport class UserEffects extends NgrxEffect {\n\n  constructor(\n    action$: Actions,\n    httpClient: HttpClient\n  ) {\n    super(action$, httpClient);\n  }\n\n  @Effect() userInfo = super.effect({\n    action: APP_ACTIONS.GET_USER,\n    type: 'get',\n    endpoint: 'https://api.github.com/users/{0}',\n  });\n}\n```\n\nEvery effect emits three actions (*_RESOLVING, *_RESOLVED, *_ERROR) as follows.\n\n* **GET_USER_RESOLVING**: emitted immediately when the action GET_USER dispatched which helps setting corresponding loaders.\n* **GET_USER_RESOLVED**: emitted when the api gets successfully resolved.\n* **GET_USER_ERROR**: emitted when api fails with 4XX or 5XX status codes.\n\n**Note:** endpint can be configured with params which can be sent at the time of dispatching action.\n\n### Reducer\n\n```ts\nexport interface UserState {\n  readonly userInfo: NgrxObject\u003cany\u003e;\n}\n\nconst defaultState: UserState = {\n  userInfo: {\n    state: DATA_STATE.INITIAL,\n    data: null\n  },\n};\n\nexport function UserReducer(state = defaultState, action) {\n  switch (action.type) {\n    case APP_ACTIONS.GET_USER_RESOLVING:\n      return StoreUtil.setResolving(state, 'userInfo', null);\n\n    case APP_ACTIONS.GET_USER_RESOLVED:\n      return StoreUtil.setResolved(state, 'userInfo', action.payload.data);\n\n    case APP_ACTIONS.GET_USER_ERROR:\n      return StoreUtil.setError(state, 'userInfo', 'Error Message');\n\n    default:\n      return state;\n  }\n}\n```\n\n### Dispatch action\n\n```ts\nthis.store.dispatch({\n  type: APP_ACTIONS.GET_USER,\n  payload: {\n    pathParams: ['octocat'],\n    // queryParams: [{ name: 'value' }],\n  },\n});\n```\n\n### Subscribe a state\n\nSubscriptions are simplified by extending the component from the helper class `NgrxStoreSubscription`.\n\n```ts\nexport class AppComponent extends NgrxStoreSubscription implements OnInit {\n  userInfo = {};\n\n  constructor(private store: Store\u003cany\u003e) {\n    super(store);\n  }\n\n  ngOnInit() {\n    super.getState({\n      feature: 'APP', // the feature of the reducer\n      reducer: 'USER_REDUCER', // name of the reducer\n      state: 'userInfo', // name state from the store\n    }).subscribe((data) =\u003e {\n      this.userInfo = data;\n    });\n\n    // refer to the example app to know how reducers registered for a feature\n  }\n}\n```\n\n### Managing Template\n\nEvery reducer gets resolved with data with the following format.\n\n```ts\n{\n  state: DATA_STATE, // which can be RESOLVING, RESOLVED, ERROR\n  data: any, // data which is set in the reducer\n}\n```\n\nThis format helps us set the appropriate view in the component template.\n\n```html\n\u003cdiv [ngrxView]=\"userInfo.state\"\u003e\n  \u003cdiv *ngrxViewResolving\u003eLoading...\u003c/div\u003e\n  \u003cdiv *ngrxViewError\u003eError fetching user info\u003c/div\u003e\n  \u003cdiv *ngrxViewResolved\u003e\n    {{userInfo.data | json}}\n  \u003c/div\u003e\n\u003c/div\u003e\n```\n\nwith the help of directives, component automatically responds based on the state from the store.\n\n### Summary\n\nWhen the action *GET_USER* gets dispatched\n\n* it emits **GET_USER_RESOLVING**\n  * which will set the `state` in store to 'RESOLVING'\n  * the component will render ***ngrxViewResolving**. (a loader screen)\n* if api call fails, it emits **GET_USER_ERROR**\n  * which will set the `state` in store to 'RESOLVED'\n  * the component will render ***ngrxViewError**. (error screen)\n* if api call fails, it emits **GET_USER_RESOLVED**\n  * which will set the `state` in store to 'RESOLVED'\n  * the component will render ***ngrxViewResolved**. (appropriate UI for user data)\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmadhusudhand%2Fngrx-helpers","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmadhusudhand%2Fngrx-helpers","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmadhusudhand%2Fngrx-helpers/lists"}