Skip to content

Commit

Permalink
feat($Exercise, BodyPart): parse exercise link which is sorted by bod…
Browse files Browse the repository at this point in the history
…y part
  • Loading branch information
johnnymillergh committed Jan 23, 2020
1 parent 5a8e7fa commit 65c5b3f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/domain/body-part/exercise-link-sorted-by-body-part.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export class ExerciseLinkSortedByBodyPart {
bodyPartName?: string
link!: string
}
25 changes: 23 additions & 2 deletions src/views/exrx-net/components/body-part.vue
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@
<div v-show="showBodyPartList">
<v-divider/>
<v-card-text>
{{ bodyPartList }}
<h3>Body Part List</h3>
<p>{{ bodyPartList }}</p>
<h3>Exercise Link List (sorted by body part)</h3>
<span >Length: {{ exerciseLinkList.length }}</span>
<p>{{ exerciseLinkList }}</p>
</v-card-text>
</div>
</v-expand-transition>
Expand All @@ -31,6 +35,8 @@ import { exrxNetApi } from '@/requests/exrx-net-api'
import { DomUtil } from '@/utils/dom-util'
import { SaveBodyPartPayload } from '@/domain/body-part/save-body-part-payload'
import { bodyPartApi } from '@/requests/body-part-api'
import { ExerciseLinkSortedByBodyPart } from '@/domain/body-part/exercise-link-sorted-by-body-part'
import { HyperlinkUtil } from '@/utils/hyperlink-util'
export default Vue.extend({
name: 'body-part',
Expand All @@ -39,7 +45,8 @@ export default Vue.extend({
mergedMuscleUnorderedList: {} as Cheerio,
bodyPartList: [] as string[],
loadingSaveBodyPart: false,
showBodyPartList: false
showBodyPartList: false,
exerciseLinkList: [] as ExerciseLinkSortedByBodyPart[]
}),
methods: {
async getAndParseBodyPart (): Promise<any> {
Expand All @@ -48,6 +55,20 @@ export default Vue.extend({
const cheerioInstance = cheerio.load(response)
this.mergedMuscleUnorderedList = DomUtil.mergeSameLevelUnorderedList(cheerioInstance('.col-sm-6').find('div > ul'))
this.bodyPartList = DomUtil.getFirstLevelTextArray(this.mergedMuscleUnorderedList.children())
const invalidLinkRegExp = /#+/
const exerciseListPrefix = 'ExList/'
this.mergedMuscleUnorderedList.find('a').each((index, element) => {
if (!invalidLinkRegExp.test(element.attribs.href)) {
const exerciseLinkSortedByBodyPart = new ExerciseLinkSortedByBodyPart()
exerciseLinkSortedByBodyPart.bodyPartName = element.children[0].data
if (element.attribs.href.search(exerciseListPrefix) < 0) {
exerciseLinkSortedByBodyPart.link = HyperlinkUtil.restorePathToUrl(`/${exerciseListPrefix}${element.attribs.href}`)
} else {
exerciseLinkSortedByBodyPart.link = HyperlinkUtil.restorePathToUrl(`/${element.attribs.href}`)
}
this.exerciseLinkList.push(exerciseLinkSortedByBodyPart)
}
})
} catch (error) {
console.error('Error occurred when sending request `exerciseDirectory`!', error)
this.$toast.error(`Error occurred when sending request \`exerciseDirectory\`! Cause: ${error.message}`)
Expand Down

0 comments on commit 65c5b3f

Please sign in to comment.