From 9024a4f4c27a4a41fc13227311971209a83cc9bf Mon Sep 17 00:00:00 2001 From: Jan Kaiser Date: Wed, 30 Aug 2023 19:00:27 +0200 Subject: [PATCH] feat: add mappings --- .../src/app/state/auth/auth.effects.ts | 12 ++-- .../src/app/state/auth/auth.mappings.ts | 12 ++++ .../src/app/state/profile/index.ts | 1 + .../src/app/state/profile/profile.effects.ts | 52 +++------------- .../src/app/state/profile/profile.mappings.ts | 59 +++++++++++++++++++ .../src/app/state/repository/index.ts | 1 + .../state/repository/repository.effects.ts | 42 +++++-------- .../state/repository/repository.mappings.ts | 47 +++++++++++++++ 8 files changed, 145 insertions(+), 81 deletions(-) create mode 100644 angular-ngrx-scss/src/app/state/auth/auth.mappings.ts create mode 100644 angular-ngrx-scss/src/app/state/profile/profile.mappings.ts create mode 100644 angular-ngrx-scss/src/app/state/repository/repository.mappings.ts diff --git a/angular-ngrx-scss/src/app/state/auth/auth.effects.ts b/angular-ngrx-scss/src/app/state/auth/auth.effects.ts index d78b8ddfa..4e806cb6e 100644 --- a/angular-ngrx-scss/src/app/state/auth/auth.effects.ts +++ b/angular-ngrx-scss/src/app/state/auth/auth.effects.ts @@ -26,7 +26,7 @@ import { userTokenExists, } from './auth.actions'; import { selectAuthUserName } from './auth.selectors'; -import { AuthUserData } from './auth.state'; +import { userApiToAuthUserData } from './auth.mappings'; @Injectable() export class AuthEffects { @@ -81,13 +81,9 @@ export class AuthEffects { distinctUntilChanged(), exhaustMap(() => this.userService.getAuthenticatedUserInfo().pipe( - map((userData) => { - const user: AuthUserData = { - avatar: userData.avatar_url, - email: userData.email, - username: userData.login, - }; - return fetchAuthenticatedUserDataSuccess({ userData: user }); + map((apiResponse) => { + const userData = userApiToAuthUserData(apiResponse); + return fetchAuthenticatedUserDataSuccess({ userData }); }), catchError((error) => of(fetchAuthenticatedUserDataFailure({ error })), diff --git a/angular-ngrx-scss/src/app/state/auth/auth.mappings.ts b/angular-ngrx-scss/src/app/state/auth/auth.mappings.ts new file mode 100644 index 000000000..89adcaaa5 --- /dev/null +++ b/angular-ngrx-scss/src/app/state/auth/auth.mappings.ts @@ -0,0 +1,12 @@ +import { UserApiResponse } from '../user'; +import { AuthUserData } from './auth.state'; + +export function userApiToAuthUserData( + apiResponse: UserApiResponse, +): AuthUserData { + return { + avatar: apiResponse.avatar_url, + email: apiResponse.email, + username: apiResponse.login, + }; +} diff --git a/angular-ngrx-scss/src/app/state/profile/index.ts b/angular-ngrx-scss/src/app/state/profile/index.ts index ce242baca..e303f592b 100644 --- a/angular-ngrx-scss/src/app/state/profile/index.ts +++ b/angular-ngrx-scss/src/app/state/profile/index.ts @@ -3,3 +3,4 @@ export * from './profile.effects'; export * from './profile.reducer'; export * from './profile.selectors'; export * from './profile.state'; +export * from './profile.mappings'; diff --git a/angular-ngrx-scss/src/app/state/profile/profile.effects.ts b/angular-ngrx-scss/src/app/state/profile/profile.effects.ts index 066ebd24c..fbcc62af7 100644 --- a/angular-ngrx-scss/src/app/state/profile/profile.effects.ts +++ b/angular-ngrx-scss/src/app/state/profile/profile.effects.ts @@ -2,13 +2,12 @@ import { Injectable } from '@angular/core'; import { Actions, createEffect, ofType } from '@ngrx/effects'; import { catchError, combineLatest, map, of, switchMap } from 'rxjs'; import { UserService } from 'src/app/user/services/user.service'; -import { UserState } from '../user'; import { fetchProfile, fetchProfileFailure, fetchProfileSuccess, } from './profile.actions'; -import { UserOrgsState, UserReposState } from './profile.state'; +import { profileApiToUserStateMapping } from './profile.mappings'; @Injectable() export class ProfileEffects { @@ -22,49 +21,12 @@ export class ProfileEffects { this.userService.getUserRepos(username), ]).pipe( map(([userData, orgsData, reposData]) => { - const user: UserState = { - avatar: userData.avatar_url, - bio: userData.bio, - blog: userData.blog, - company: userData.company, - email: userData.email, - followers: userData.followers, - following: userData.following, - location: userData.location, - name: userData.name, - twitterUsername: userData.twitter_username, - username: userData.login, - type: userData.type, - }; - const orgs: UserOrgsState[] = orgsData.map((org) => ({ - id: org.id, - login: org.login, - avatar_url: org.avatar_url, - })); - const repos: UserReposState[] = reposData.map((repo) => ({ - name: repo.name, - description: repo.description, - language: repo.language, - stargazers_count: repo.stargazers_count, - forks_count: repo.forks_count, - private: repo.private, - updated_at: repo.updated_at, - fork: repo.fork, - archived: repo.archived, - license: repo.license - ? { - key: repo.license.key, - name: repo.license.name, - spdx_id: repo.license.spdx_id, - url: repo.license.url, - node_id: repo.license.node_id, - } - : null, - owner: { - login: repo.owner.login, - }, - })); - return fetchProfileSuccess({ data: { user, orgs, repos } }); + const data = profileApiToUserStateMapping( + userData, + orgsData, + reposData, + ); + return fetchProfileSuccess({ data }); }), catchError((error) => { console.error(error); diff --git a/angular-ngrx-scss/src/app/state/profile/profile.mappings.ts b/angular-ngrx-scss/src/app/state/profile/profile.mappings.ts new file mode 100644 index 000000000..a0f956f9c --- /dev/null +++ b/angular-ngrx-scss/src/app/state/profile/profile.mappings.ts @@ -0,0 +1,59 @@ +import { UserApiResponse, UserState } from '../user'; +import { + ProfileState, + UserOrgsApiResponse, + UserOrgsState, + UserReposApiResponse, + UserReposState, +} from './profile.state'; + +export function profileApiToUserStateMapping( + userData: UserApiResponse, + orgsData: UserOrgsApiResponse, + reposData: UserReposApiResponse, +): ProfileState { + const user: UserState = { + avatar: userData.avatar_url, + bio: userData.bio, + blog: userData.blog, + company: userData.company, + email: userData.email, + followers: userData.followers, + following: userData.following, + location: userData.location, + name: userData.name, + twitterUsername: userData.twitter_username, + username: userData.login, + type: userData.type, + }; + const orgs: UserOrgsState[] = orgsData.map((org) => ({ + id: org.id, + login: org.login, + avatar_url: org.avatar_url, + })); + const repos: UserReposState[] = reposData.map((repo) => ({ + name: repo.name, + description: repo.description, + language: repo.language, + stargazers_count: repo.stargazers_count, + forks_count: repo.forks_count, + private: repo.private, + updated_at: repo.updated_at, + fork: repo.fork, + archived: repo.archived, + license: repo.license + ? { + key: repo.license.key, + name: repo.license.name, + spdx_id: repo.license.spdx_id, + url: repo.license.url, + node_id: repo.license.node_id, + } + : null, + owner: { + login: repo.owner.login, + }, + })); + + return { user, orgs, repos }; +} diff --git a/angular-ngrx-scss/src/app/state/repository/index.ts b/angular-ngrx-scss/src/app/state/repository/index.ts index 51aa987aa..0b859d22d 100644 --- a/angular-ngrx-scss/src/app/state/repository/index.ts +++ b/angular-ngrx-scss/src/app/state/repository/index.ts @@ -3,3 +3,4 @@ export * from './repository.effects'; export * from './repository.reducer'; export * from './repository.selectors'; export * from './repository.state'; +export * from './repository.mappings'; diff --git a/angular-ngrx-scss/src/app/state/repository/repository.effects.ts b/angular-ngrx-scss/src/app/state/repository/repository.effects.ts index 18588ab61..7145198fa 100644 --- a/angular-ngrx-scss/src/app/state/repository/repository.effects.ts +++ b/angular-ngrx-scss/src/app/state/repository/repository.effects.ts @@ -18,7 +18,8 @@ import { fetchRepositoryFailure, fetchRepositorySuccess, } from './repository.actions'; -import { FileContents, RepositoryState } from './repository.state'; +import { FileContents } from './repository.state'; +import { reposApiToRepoStateMapping } from './repository.mappings'; @Injectable() export class RepositoryEffects { @@ -64,33 +65,18 @@ export class RepositoryEffects { repoLabels$, ).pipe( map(([info, prCount, contents, readme, milestones, labels]) => { - const allData: RepositoryState = { - path: path ?? '', - description: info.description, - forkCount: info.forks_count, - issueCount: info.open_issues_count, - ownerName: owner, - prCount: prCount, - repoName: info.name, - starCount: info.stargazers_count, - tags: info.topics, - tree: contents, - activeBranch: branch ?? info.default_branch, - selectedFile: null, - openPullRequests: null, - closedPullRequests: null, - openIssues: null, - closedIssues: null, - visibility: info.visibility, - watchCount: info.watchers_count, - website: info.homepage, - readme: readme?.content || '', - milestones: milestones || [], - labels: labels || [], - pullsFilterParams: null, - issuesFilterParams: null, - }; - return fetchRepositorySuccess({ repoData: allData }); + const repoData = reposApiToRepoStateMapping( + info, + prCount, + contents, + readme, + milestones, + labels, + owner, + path, + branch, + ); + return fetchRepositorySuccess({ repoData }); }), catchError((error) => of(fetchRepositoryFailure({ error }))), ); diff --git a/angular-ngrx-scss/src/app/state/repository/repository.mappings.ts b/angular-ngrx-scss/src/app/state/repository/repository.mappings.ts new file mode 100644 index 000000000..16b6c9f3d --- /dev/null +++ b/angular-ngrx-scss/src/app/state/repository/repository.mappings.ts @@ -0,0 +1,47 @@ +import { + IssueLabel, + Milestone, + ReadmeApiResponse, + RepoApiResponse, + RepoContentsApiResponse, + RepositoryState, +} from './repository.state'; + +export function reposApiToRepoStateMapping( + info: RepoApiResponse, + prCount: number, + contents: RepoContentsApiResponse[], + readme: ReadmeApiResponse | null, + milestones: Milestone[], + labels: IssueLabel[], + owner: string, + path?: string, + branch?: string, +): RepositoryState { + return { + path: path ?? '', + description: info.description, + forkCount: info.forks_count, + issueCount: info.open_issues_count, + ownerName: owner, + prCount: prCount, + repoName: info.name, + starCount: info.stargazers_count, + tags: info.topics, + tree: contents, + activeBranch: branch ?? info.default_branch, + selectedFile: null, + openPullRequests: null, + closedPullRequests: null, + openIssues: null, + closedIssues: null, + visibility: info.visibility, + watchCount: info.watchers_count, + website: info.homepage, + readme: readme?.content || '', + milestones: milestones || [], + labels: labels || [], + pullsFilterParams: null, + issuesFilterParams: null, + }; +}