Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
T
tdt4242-base-APU
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Tor Martin Frøberg Wang
tdt4242-base-APU
Commits
b6eeb3c4
Commit
b6eeb3c4
authored
4 years ago
by
Haakon Gunleiksrud
Browse files
Options
Downloads
Patches
Plain Diff
Fixed too nested code in workouts.js
parent
656bdb86
No related branches found
Branches containing commit
No related tags found
2 merge requests
!31
Complete exercise 3
,
!28
Frontendsmells
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
frontend/www/scripts/workouts.js
+51
-42
51 additions, 42 deletions
frontend/www/scripts/workouts.js
with
51 additions
and
42 deletions
frontend/www/scripts/workouts.js
+
51
−
42
View file @
b6eeb3c4
...
...
@@ -7,67 +7,76 @@ async function fetchWorkouts(ordering) {
let
data
=
await
response
.
json
();
let
workouts
=
data
.
results
;
let
container
=
document
.
getElementById
(
'
div-content
'
);
await
convertWorkoutDataToFrontend
(
workouts
);
for
(
const
workout
of
workouts
){
return
workouts
;
}
}
let
workoutLikes
=
await
sendRequest
(
"
GET
"
,
`
${
HOST
}
/api/workoutLiking/
${
workout
.
id
}
`
);
async
function
convertWorkoutDataToFrontend
(
workouts
){
let
container
=
document
.
getElementById
(
'
div-content
'
);
let
workoutLikesData
=
[
true
,
1
]
for
(
const
workout
of
workouts
){
if
(
workoutLikes
.
ok
){
workoutLikesData
=
await
workoutLikes
.
json
()
}
let
workoutLikes
=
await
sendRequest
(
"
GET
"
,
`
${
HOST
}
/api/workoutLiking/
${
workout
.
id
}
`
);
let
templateWorkout
=
document
.
querySelector
(
"
#template-workout
"
);
let
cloneWorkout
=
templateWorkout
.
content
.
cloneNode
(
true
);
let
workoutLikesData
=
[
true
,
1
]
let
aWorkout
=
cloneWorkout
.
querySelector
(
"
a
"
);
aWorkout
.
href
=
`workout.html?id=
${
workout
.
id
}
`
;
if
(
workoutLikes
.
ok
){
workoutLikesData
=
await
workoutLikes
.
json
()
}
let
h5
=
aWorkout
.
querySelector
(
"
h5
"
);
h5
.
textContent
=
workout
.
name
;
let
templateWorkout
=
document
.
querySelector
(
"
#template-workout
"
);
let
cloneWorkout
=
templateWorkout
.
content
.
cloneNode
(
true
)
;
let
localDate
=
new
Date
(
workout
.
date
);
let
aWorkout
=
cloneWorkout
.
querySelector
(
"
a
"
);
aWorkout
.
href
=
`workout.html?id=
${
workout
.
id
}
`
;
let
table
=
aWorkout
.
querySelector
(
"
table
"
);
let
rows
=
table
.
querySelectorAll
(
"
tr
"
);
rows
[
0
].
querySelectorAll
(
"
td
"
)[
1
].
textContent
=
localDate
.
toLocaleDateString
();
// Date
rows
[
1
].
querySelectorAll
(
"
td
"
)[
1
].
textContent
=
localDate
.
toLocaleTimeString
();
// Time
rows
[
2
].
querySelectorAll
(
"
td
"
)[
1
].
textContent
=
workout
.
owner_username
;
//Owner
rows
[
3
].
querySelectorAll
(
"
td
"
)[
1
].
textContent
=
workout
.
exercise_instances
.
length
;
// Exercises
let
h5
=
aWorkout
.
querySelector
(
"
h5
"
);
h5
.
textContent
=
workout
.
name
;
rows
[
4
].
querySelectorAll
(
"
td
"
)[
1
].
textContent
=
workoutLikesData
[
1
]
let
localDate
=
new
Date
(
workout
.
date
);
let
likeButton
=
rows
[
4
].
querySelectorAll
(
"
td
"
)[
2
].
querySelector
(
"
.like-button
"
)
let
table
=
aWorkout
.
querySelector
(
"
table
"
);
let
rows
=
table
.
querySelectorAll
(
"
tr
"
);
rows
[
0
].
querySelectorAll
(
"
td
"
)[
1
].
textContent
=
localDate
.
toLocaleDateString
();
// Date
rows
[
1
].
querySelectorAll
(
"
td
"
)[
1
].
textContent
=
localDate
.
toLocaleTimeString
();
// Time
rows
[
2
].
querySelectorAll
(
"
td
"
)[
1
].
textContent
=
workout
.
owner_username
;
//Owner
rows
[
3
].
querySelectorAll
(
"
td
"
)[
1
].
textContent
=
workout
.
exercise_instances
.
length
;
// Exercises
if
(
!
workoutLikesData
[
0
]){
likeButton
.
classList
.
add
(
"
active
"
)
}
rows
[
4
].
querySelectorAll
(
"
td
"
)[
1
].
textContent
=
workoutLikesData
[
1
]
likeButton
.
addEventListener
(
"
click
"
,
async
function
(
e
)
{
e
.
preventDefault
();
let
likeButton
=
rows
[
4
].
querySelectorAll
(
"
td
"
)[
2
].
querySelector
(
"
.like-button
"
)
if
(
!
this
.
classList
.
contains
(
"
active
"
)){
this
.
classList
.
add
(
"
active
"
);
this
.
classList
.
add
(
"
animated
"
);
generateClones
(
this
);
if
(
!
workoutLikesData
[
0
]){
likeButton
.
classList
.
add
(
"
active
"
)
}
let
likeWorkoutResponse
=
await
sendRequest
(
"
POST
"
,
`
${
HOST
}
/api/workoutLiking/
${
workout
.
id
}
/`
,
{}
);
await
addEventListenersForLikeButtons
(
workout
,
likeButton
,
rows
);
if
(
likeWorkoutResponse
.
ok
){
let
likeWorkoutData
=
await
likeWorkoutResponse
.
json
()
rows
[
4
].
querySelectorAll
(
"
td
"
)[
1
].
textContent
=
likeWorkoutData
[
1
]
likeButton
.
classList
.
add
(
"
active
"
)
}
}
})
container
.
appendChild
(
aWorkout
);
}
}
async
function
addEventListenersForLikeButtons
(
workout
,
likeButton
,
rows
){
likeButton
.
addEventListener
(
"
click
"
,
async
function
(
e
)
{
e
.
preventDefault
();
if
(
!
this
.
classList
.
contains
(
"
active
"
)){
this
.
classList
.
add
(
"
active
"
);
this
.
classList
.
add
(
"
animated
"
);
generateClones
(
this
);
let
likeWorkoutResponse
=
await
sendRequest
(
"
POST
"
,
`
${
HOST
}
/api/workoutLiking/
${
workout
.
id
}
/`
,
{});
container
.
appendChild
(
aWorkout
);
if
(
likeWorkoutResponse
.
ok
){
let
likeWorkoutData
=
await
likeWorkoutResponse
.
json
()
rows
[
4
].
querySelectorAll
(
"
td
"
)[
1
].
textContent
=
likeWorkoutData
[
1
]
likeButton
.
classList
.
add
(
"
active
"
)
}
}
return
workouts
;
}
})
}
function
createWorkout
()
{
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment