@@ -10,18 +10,22 @@ import '../static/css/Videoplayer.css';
1010import { API , graphqlOperation } from 'aws-amplify' ;
1111import {
1212 createReward , updateReward ,
13- createProfile , updateProfile
13+ createProfile , updateProfile ,
14+ createTrack , updateTrack
1415} from '../graphql/mutations' ;
1516import {
16- listRewards , listProfiles , getProfile
17+ listRewards , listProfiles , getProfile , getTrack
1718} from '../graphql/queries' ;
1819
1920// components
2021import { format } from './Duration' ;
2122import { Survey } from './Survey' ;
2223
2324export function Player ( props ) {
24- const [ playtime , setPlaytime ] = useState ( 0 ) ;
25+ const [ played , setPlayed ] = useState ( 0 ) ;
26+ const [ marker , setMarker ] = useState ( 0 ) ;
27+ const [ duration , setDuration ] = useState ( 0 ) ;
28+ const interval = 30 ;
2529
2630 return (
2731 < Container >
@@ -36,22 +40,26 @@ export function Player(props) {
3640 controls = { true }
3741 light = { false }
3842 pip = { false }
43+ onPlay = { ( ) => {
44+ setMarker ( played + interval ) ;
45+ } }
3946 onEnded = { ( ) => {
40- updateRewardApi ( props . user , props . classId , true , 20 ) ;
47+ if ( Math . round ( played ) >= Math . floor ( duration ) ) {
48+ updateTrackApi ( props . user , props . classId , props . uid , - 1 , true ) ;
4149 }
42- }
43- onDuration = { ( e ) => { console . log ( e ) } }
50+ } }
51+ onDuration = { ( e ) => {
52+ setDuration ( Math . floor ( e ) ) ;
53+ } }
4454 onProgress = { ( e ) => {
45- var checkpoint = playtime + 30 ;
46- if ( e . playedSeconds > checkpoint ) {
47- updateRewardApi ( props . user , props . classId , checkpoint ) ;
48- updateProfileRewardApi ( props . uid , props . user ) ;
49- setPlaytime ( checkpoint ) ;
50- }
55+ var checkpoint = marker + interval ;
56+ setPlayed ( e . playedSeconds ) ;
57+
58+ if ( played > checkpoint ) {
59+ updateTrackApi ( props . user , props . classId , props . uid , checkpoint ) ;
60+ setMarker ( checkpoint ) ;
5161 }
52- }
53- onPause = { ( ) => { console . log ( "playing false, paused" ) } }
54- onPlay = { ( ) => { console . log ( "playing true" ) } }
62+ } }
5563 />
5664 </ Box >
5765 < SpaceBetween direction = "vertical" size = "s" >
@@ -67,33 +75,57 @@ export function Player(props) {
6775}
6876
6977// graphql apis
70- function updateProfileRewardApi ( id , user , point = 10 ) {
78+ function updateProfileRewardApi ( id , user , classId , point = 10 ) {
7179 try {
7280 API . graphql ( graphqlOperation ( getProfile , { id : id } ) ) . then (
7381 ( result ) => {
74- if ( result . data . getProfile == null ) {
82+ var item = result . data . getProfile ;
83+ if ( item == null ) {
7584 API . graphql ( graphqlOperation ( createProfile , {
7685 input : { id : id , userId : user , point : point }
7786 } ) ) ;
7887 }
7988 else {
80- var item = result . data . getProfile ;
8189 API . graphql ( graphqlOperation ( updateProfile , {
8290 input : { id : item . id , _version : item . _version , point : ( point + item . point ) }
8391 } ) ) ;
8492 }
93+
94+ API . graphql ( graphqlOperation ( createReward , {
95+ input : { userId : user , classId : classId , point : point }
96+ } ) ) ;
8597 } ) ;
8698 }
8799 catch ( e ) {
88100 console . log ( { e} ) ;
89101 }
90102}
91103
92- function updateRewardApi ( user , classId , played , complete = false , point = 10 ) {
104+ function updateTrackApi ( user , classId , uid , played , complete = false ) {
93105 try {
94- API . graphql ( graphqlOperation ( createReward , {
95- input : { userId : user , classId : classId , completion : complete , played : played , point : point }
96- } ) ) ;
106+ API . graphql ( graphqlOperation ( getTrack , { classId : classId , userId : user } ) ) . then (
107+ ( result ) => {
108+ var item = result . data . getTrack ;
109+ if ( item == null ) {
110+ API . graphql ( graphqlOperation ( createTrack , {
111+ input : { classId : classId , userId : user , completion : complete , played : played }
112+ } ) ) ;
113+ updateProfileRewardApi ( uid , user , classId ) ;
114+ }
115+ else {
116+ if ( ! item . completion ) {
117+ API . graphql ( graphqlOperation ( updateTrack , {
118+ input : {
119+ classId : item . classId ,
120+ userId : item . userId ,
121+ _version : item . _version ,
122+ completion : complete ,
123+ played : ( played + item . played )
124+ }
125+ } ) ) ;
126+ updateProfileRewardApi ( uid , user , classId ) ;
127+ } }
128+ } ) ;
97129 }
98130 catch ( e ) {
99131 console . log ( { e} ) ;
0 commit comments