Skip to content

Commit

Permalink
feat: add mappings
Browse files Browse the repository at this point in the history
  • Loading branch information
honzikec committed Aug 30, 2023
1 parent 966f4f1 commit 9024a4f
Show file tree
Hide file tree
Showing 8 changed files with 145 additions and 81 deletions.
12 changes: 4 additions & 8 deletions angular-ngrx-scss/src/app/state/auth/auth.effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 })),
Expand Down
12 changes: 12 additions & 0 deletions angular-ngrx-scss/src/app/state/auth/auth.mappings.ts
Original file line number Diff line number Diff line change
@@ -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,
};
}
1 change: 1 addition & 0 deletions angular-ngrx-scss/src/app/state/profile/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * from './profile.effects';
export * from './profile.reducer';
export * from './profile.selectors';
export * from './profile.state';
export * from './profile.mappings';
52 changes: 7 additions & 45 deletions angular-ngrx-scss/src/app/state/profile/profile.effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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);
Expand Down
59 changes: 59 additions & 0 deletions angular-ngrx-scss/src/app/state/profile/profile.mappings.ts
Original file line number Diff line number Diff line change
@@ -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 };
}
1 change: 1 addition & 0 deletions angular-ngrx-scss/src/app/state/repository/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * from './repository.effects';
export * from './repository.reducer';
export * from './repository.selectors';
export * from './repository.state';
export * from './repository.mappings';
42 changes: 14 additions & 28 deletions angular-ngrx-scss/src/app/state/repository/repository.effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 }))),
);
Expand Down
47 changes: 47 additions & 0 deletions angular-ngrx-scss/src/app/state/repository/repository.mappings.ts
Original file line number Diff line number Diff line change
@@ -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,
};
}

0 comments on commit 9024a4f

Please sign in to comment.