-
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.
Merge pull request #7 from chouglesaud/feature/decouple-business-logi…
…c-with-framework Feature/decouple business logic with framework
- Loading branch information
Showing
58 changed files
with
383 additions
and
320 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 const apiConfig = { | ||
githubBaseUrl: 'https://api.github.com/', | ||
}; |
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
File renamed without changes.
File renamed without changes.
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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,30 @@ | ||
import { apiConfig } from '@/config/apiConfig'; | ||
import { BaseAPI } from '@/shared/services/BaseAPI'; | ||
|
||
type GithubBaseTreeOptions = { | ||
owner: string; | ||
name: string; | ||
branch: string; | ||
accessToken?: string | null; | ||
}; | ||
|
||
export class TreeService extends BaseAPI { | ||
public nodes: any[]; | ||
constructor() { | ||
super(); | ||
this.baseUrl = apiConfig.githubBaseUrl; | ||
this.nodes = []; | ||
} | ||
public async getGithubBaseTree(option: GithubBaseTreeOptions): Promise<any> { | ||
const url = `repos/${option.owner}/${option.name}/git/trees/${option.branch}?recursive=1`; | ||
|
||
return await this.axiosInstance.get(`${this.baseUrl}${url}`, { | ||
headers: option.accessToken | ||
? { | ||
accept: 'application/vnd.github.v3+json', | ||
authorization: `token ${option.accessToken}`, | ||
} | ||
: null, | ||
}); | ||
} | ||
} |
8 changes: 4 additions & 4 deletions
8
src/modules/Tree/domain/githubPjaxAdapter.ts → ...odules/Tree/services/githubPjaxService.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 |
---|---|---|
@@ -1,12 +1,12 @@ | ||
import PjaxAdapter, { PjaxAdapterProps } from '@/modules/util/PjaxAdapter/PjaxAdapter'; | ||
import PjaxService, { PjaxServiceProps } from '@/shared/services/PjaxService'; | ||
|
||
const config: PjaxAdapterProps = { | ||
const config: PjaxServiceProps = { | ||
host: 'https://github.com', | ||
query: | ||
'_pjax=%23js-repo-pjax-container%2C%20div%5Bitemtype%3D%22http%3A%2F%2Fschema.org%2FSoftwareSourceCode%22%5D%20main%2C%20%5Bdata-pjax-container%5D', | ||
pjaxContainer: | ||
'#js-repo-pjax-container, div[itemtype="http://schema.org/SoftwareSourceCode"] main, [data-pjax-container]', | ||
}; | ||
const githubPjaxAdapter = new PjaxAdapter(config); | ||
const githubPjaxService = new PjaxService(config); | ||
|
||
export { githubPjaxAdapter }; | ||
export { githubPjaxService }; |
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,5 @@ | ||
import { githubPjaxService } from './githubPjaxService'; | ||
import { TreeService } from './TreeService'; | ||
|
||
const treeService = new TreeService(); | ||
export { treeService, githubPjaxService }; |
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,62 @@ | ||
import store from '@/shared/infra/store'; | ||
import GithubScraper from '@/shared/utils/GithubScraper'; | ||
import GithubUrl from '@/shared/utils/GithubUrl'; | ||
import { Tree } from '../models/Tree'; | ||
import { treeService } from '../services'; | ||
import { TreeUtil } from '../utils/TreeUtil'; | ||
import { githubBlobAndTreeUrlPattern } from '@/shared/utils/githubUrlPatterns'; | ||
|
||
export default { | ||
async loadTree({ state, dispatch }) { | ||
store.state['loading'] = true; | ||
|
||
const { owner, name } = GithubUrl.getOwnerAndRepoName(), | ||
branch = GithubScraper.getBranchName(), | ||
repoMetadata = { | ||
owner, | ||
name, | ||
branch, | ||
url: window.location.href, | ||
}, | ||
accessToken = localStorage.getItem('pacrepo'); | ||
|
||
try { | ||
const response = await treeService.getGithubBaseTree({ | ||
owner, | ||
name, | ||
branch, | ||
accessToken, | ||
}); | ||
const apiNodes: any[] = response.data.tree, | ||
nodesInTreeFirstOrder = await TreeUtil.getNodesInTreeFirstOrder(apiNodes), | ||
sortedNodesByLevel = await TreeUtil.getNodesSortedByLevel(nodesInTreeFirstOrder), | ||
{ nodes } = Tree.createBy(sortedNodesByLevel); | ||
|
||
store.state['apiStatus'] = 200; | ||
state['repoMetadata'] = repoMetadata; | ||
state['items'] = TreeUtil.convertObjectToArray(nodes); | ||
store.state['loading'] = false; | ||
} catch (error) { | ||
store.state['apiStatus'] = 401; | ||
state['repoMetadata'] = {}; | ||
store.state['loading'] = false; | ||
} | ||
await dispatch('updateOpenNodes'); | ||
}, | ||
async updateOpenNodes({ state }): Promise<void> { | ||
const url = state['repoMetadata']['url']; | ||
const branch = state['repoMetadata']['branch']; | ||
|
||
const matchedData = await githubBlobAndTreeUrlPattern({ branch }).match(url); | ||
|
||
if (GithubUrl.hasTypeBlobAndTree(matchedData)) { | ||
const fullPath = GithubUrl.getFullPathFrom(matchedData); | ||
if (fullPath.length > 0) { | ||
state['open'] = [...state['openNodes'], ...GithubUrl.getIndividualPathListFrom(fullPath)]; | ||
} | ||
} | ||
}, | ||
collapseAll({ state }): void { | ||
state['open'] = []; | ||
}, | ||
}; |
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,8 @@ | ||
import state from './state'; | ||
import actions from './actions'; | ||
|
||
export const tree: any = { | ||
namespaced: true, | ||
state, | ||
actions, | ||
}; |
6 changes: 3 additions & 3 deletions
6
src/store/modules/tree/state.ts → src/modules/Tree/store/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
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,37 @@ | ||
export class TreeUtil { | ||
public static async getNodesInTreeFirstOrder(nodes: any[]): Promise<any[]> { | ||
const treeList: any = []; | ||
const blobList: any = []; | ||
|
||
nodes.forEach((node: any) => { | ||
const treeNode = { | ||
path: node.path, | ||
type: node.type, | ||
}; | ||
if (treeNode.type === 'tree') { | ||
treeList.push(treeNode); | ||
} else { | ||
blobList.push(treeNode); | ||
} | ||
}); | ||
return [...treeList, ...blobList]; | ||
} | ||
public static async getNodesSortedByLevel(nodes: any[]): Promise<any[]> { | ||
const sortedNodes: any = []; | ||
nodes.forEach((node: any) => { | ||
const depth = node.path.split('/').length - 1; | ||
if (!sortedNodes[depth]) { | ||
sortedNodes[depth] = []; | ||
} | ||
sortedNodes[depth].push(node); | ||
}); | ||
return sortedNodes; | ||
} | ||
public static convertObjectToArray(obj: {}): any[] { | ||
const array: any = []; | ||
for (const key in obj) { | ||
array.push(obj[key]); | ||
} | ||
return array; | ||
} | ||
} |
Oops, something went wrong.