Skip to content

Commit 3c59611

Browse files
committed
Implement youtube partial
1 parent 0cd497d commit 3c59611

6 files changed

Lines changed: 165 additions & 3 deletions

File tree

data/osa-1/2-tulostaminen-ja-lukeminen.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ Ohjelmointitehtävien tehtävänannot löytyvät kurssimateriaalista (eli materi
8989

9090
Voit katsoa ohjeet aloittamiseen myös seuraavalta videolta.
9191

92-
<%= partial 'partials/youtube\_2', locals: { id: 'lxehAkYVEGo' } %>
92+
<youtube id="lxehAkYVEGo"></youtube>
9393

9494
<% partial 'partials/exercise', locals: { name: 'Hiekkalaatikko' } do %>
9595

gatsby-node.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ exports.createPages = ({ actions, graphql }) => {
1616
return graphql(`
1717
{
1818
allMarkdownRemark(
19-
sort: { order: DESC, fields: [frontmatter___title] }
19+
sort: { order: DESC, fields: [frontmatter___path] }
2020
limit: 1000
2121
) {
2222
edges {
@@ -33,7 +33,12 @@ exports.createPages = ({ actions, graphql }) => {
3333
return Promise.reject(result.errors)
3434
}
3535

36-
result.data.allMarkdownRemark.edges.forEach(({ node }) => {
36+
result.data.allMarkdownRemark.edges.sort((a, b) => {
37+
a = a.node.frontmatter.path.toLowerCase();
38+
b = b.node.frontmatter.path.toLowerCase();
39+
40+
return a > b ? -1 : b > a ? 1 : 0;
41+
}).forEach(({ node }) => {
3742
createPage({
3843
path: node.frontmatter.path,
3944
component: blogPostTemplate,

package-lock.json

Lines changed: 60 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
"react-helmet": "^5.2.0",
2323
"react-motion": "^0.5.2",
2424
"react-scrollspy": "^3.3.5",
25+
"react-youtube": "^7.8.0",
26+
"redux-action-analytics": "0.0.3",
2527
"rehype-react": "^3.1.0",
2628
"store": "^2.0.12",
2729
"styled-components": "^4.1.1",

src/partials/Youtube.js

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import React from 'react'
2+
import PagesContext from '../contexes/PagesContext'
3+
4+
import styled from 'styled-components'
5+
6+
import ReduxActionAnalytics from 'redux-action-analytics'
7+
import * as storejs from 'store'
8+
import YouTube from 'react-youtube'
9+
10+
let analytics = undefined
11+
12+
initAnalytics()
13+
14+
function initAnalytics() {
15+
analytics = new ReduxActionAnalytics(
16+
'https://usage.testmycode.io/api/v0/data',
17+
'youtube-watch-stats',
18+
'youtube-watch-stats',
19+
10000,
20+
() => {
21+
const user = storejs.get('tmc.user')
22+
if (user === undefined) {
23+
return {}
24+
}
25+
return {
26+
username: user.username,
27+
}
28+
}
29+
)
30+
}
31+
32+
function onPlayerStateChange(event) {
33+
const player = event.target
34+
const eventCode = event.data
35+
const action = Object.entries(YouTube.PlayerState).find(o => {
36+
return o[1] == eventCode
37+
})[0]
38+
logAction(action, player)
39+
}
40+
41+
function onPlaybackRateChange(event) {
42+
const player = event.target
43+
logAction('PLAYBACK_SPEED_CHANGED', player)
44+
}
45+
46+
function logAction(action, player) {
47+
const happenedAt = new Date().getTime()
48+
const videoTime = player.getCurrentTime()
49+
const youtubeIdentifier = player.___youtube_identifier
50+
const playBackRate = player.getPlaybackRate()
51+
const snapshot = {
52+
action: action,
53+
video_time: videoTime,
54+
youtube_identifier: youtubeIdentifier,
55+
happened_at_milliseconds: happenedAt,
56+
playback_speed: playBackRate,
57+
}
58+
analytics.saveAction(snapshot)
59+
}
60+
61+
const VideoWrapper = styled.div`
62+
position: relative;
63+
padding-bottom: 56.25%; /* 16:9 */
64+
padding-top: 25px;
65+
height: 0;
66+
67+
iframe {
68+
position: absolute;
69+
top: 0;
70+
left: 0;
71+
width: 100%;
72+
height: 100%;
73+
}
74+
`
75+
76+
export default props => {
77+
return (
78+
<VideoWrapper>
79+
<YouTube
80+
videoId={props.id}
81+
onStateChange={onPlayerStateChange}
82+
onPlaybackRateChange={onPlaybackRateChange}
83+
opts={{
84+
height: '390',
85+
width: '640',
86+
playerVars: {
87+
modestbranding: true,
88+
},
89+
}}
90+
/>
91+
</VideoWrapper>
92+
)
93+
}

src/partials/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,15 @@ import PagesInThisSection from './PagesInThisSection'
2525
import ProgrammingExercise from './ProgrammingExercise'
2626
import TextBox from './TextBox'
2727
import SampleOutput from './SampleOutput'
28+
import Youtube from './Youtube'
2829

2930
const mapping = {
3031
test: Test,
3132
'pages-in-this-section': PagesInThisSection,
3233
'programming-exercise': ProgrammingExercise,
3334
'text-box': TextBox,
3435
'sample-output': SampleOutput,
36+
'youtube': Youtube,
3537
}
3638

3739
export default () => {

0 commit comments

Comments
 (0)