Skip to content

Commit 41adfa0

Browse files
authored
Merge pull request #18 from aws-samples/fix-reward-condition
fix: update the reward condition and activity tracking
2 parents c332bb6 + 6360f59 commit 41adfa0

File tree

5 files changed

+209
-40
lines changed

5 files changed

+209
-40
lines changed

src/component/Player.jsx

Lines changed: 54 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,22 @@ import '../static/css/Videoplayer.css';
1010
import { API, graphqlOperation } from 'aws-amplify';
1111
import {
1212
createReward, updateReward,
13-
createProfile, updateProfile
13+
createProfile, updateProfile,
14+
createTrack, updateTrack
1415
} from '../graphql/mutations';
1516
import {
16-
listRewards, listProfiles, getProfile
17+
listRewards, listProfiles, getProfile, getTrack
1718
} from '../graphql/queries';
1819

1920
// components
2021
import { format } from './Duration';
2122
import { Survey } from './Survey';
2223

2324
export 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});

src/graphql/mutations.js

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,57 @@ export const deleteComment = /* GraphQL */ `
178178
}
179179
}
180180
`;
181+
export const createTrack = /* GraphQL */ `
182+
mutation CreateTrack(
183+
$input: CreateTrackInput!
184+
$condition: ModelTrackConditionInput
185+
) {
186+
createTrack(input: $input, condition: $condition) {
187+
classId
188+
userId
189+
completion
190+
played
191+
createdAt
192+
updatedAt
193+
owner
194+
__typename
195+
}
196+
}
197+
`;
198+
export const updateTrack = /* GraphQL */ `
199+
mutation UpdateTrack(
200+
$input: UpdateTrackInput!
201+
$condition: ModelTrackConditionInput
202+
) {
203+
updateTrack(input: $input, condition: $condition) {
204+
classId
205+
userId
206+
completion
207+
played
208+
createdAt
209+
updatedAt
210+
owner
211+
__typename
212+
}
213+
}
214+
`;
215+
export const deleteTrack = /* GraphQL */ `
216+
mutation DeleteTrack(
217+
$input: DeleteTrackInput!
218+
$condition: ModelTrackConditionInput
219+
) {
220+
deleteTrack(input: $input, condition: $condition) {
221+
classId
222+
userId
223+
completion
224+
played
225+
createdAt
226+
updatedAt
227+
owner
228+
__typename
229+
}
230+
}
231+
`;
181232
export const createReward = /* GraphQL */ `
182233
mutation CreateReward(
183234
$input: CreateRewardInput!
@@ -187,8 +238,6 @@ export const createReward = /* GraphQL */ `
187238
id
188239
classId
189240
userId
190-
completion
191-
played
192241
point
193242
createdAt
194243
updatedAt
@@ -206,8 +255,6 @@ export const updateReward = /* GraphQL */ `
206255
id
207256
classId
208257
userId
209-
completion
210-
played
211258
point
212259
createdAt
213260
updatedAt
@@ -225,8 +272,6 @@ export const deleteReward = /* GraphQL */ `
225272
id
226273
classId
227274
userId
228-
completion
229-
played
230275
point
231276
createdAt
232277
updatedAt

src/graphql/queries.js

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,14 +117,58 @@ export const listComments = /* GraphQL */ `
117117
}
118118
}
119119
`;
120+
export const getTrack = /* GraphQL */ `
121+
query GetTrack($classId: String!, $userId: ID!) {
122+
getTrack(classId: $classId, userId: $userId) {
123+
classId
124+
userId
125+
completion
126+
played
127+
createdAt
128+
updatedAt
129+
owner
130+
__typename
131+
}
132+
}
133+
`;
134+
export const listTracks = /* GraphQL */ `
135+
query ListTracks(
136+
$classId: String
137+
$userId: ModelIDKeyConditionInput
138+
$filter: ModelTrackFilterInput
139+
$limit: Int
140+
$nextToken: String
141+
$sortDirection: ModelSortDirection
142+
) {
143+
listTracks(
144+
classId: $classId
145+
userId: $userId
146+
filter: $filter
147+
limit: $limit
148+
nextToken: $nextToken
149+
sortDirection: $sortDirection
150+
) {
151+
items {
152+
classId
153+
userId
154+
completion
155+
played
156+
createdAt
157+
updatedAt
158+
owner
159+
__typename
160+
}
161+
nextToken
162+
__typename
163+
}
164+
}
165+
`;
120166
export const getReward = /* GraphQL */ `
121167
query GetReward($id: ID!) {
122168
getReward(id: $id) {
123169
id
124170
classId
125171
userId
126-
completion
127-
played
128172
point
129173
createdAt
130174
updatedAt
@@ -144,8 +188,6 @@ export const listRewards = /* GraphQL */ `
144188
id
145189
classId
146190
userId
147-
completion
148-
played
149191
point
150192
createdAt
151193
updatedAt

src/graphql/schema.graphql

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,17 @@ type Comment @model @auth(rules: [{ allow: owner }, { allow: private, operations
3232
owners: [String]
3333
}
3434

35+
type Track @model @auth(rules: [{ allow: owner }]) {
36+
classId: String! @primaryKey(sortKeyFields: ["userId"])
37+
userId: ID!
38+
completion: Boolean
39+
played: String
40+
}
41+
3542
type Reward @model @auth(rules: [{ allow: owner }]) {
3643
id: ID!
3744
classId: String!
3845
userId: String!
39-
completion: Boolean
40-
played: String
4146
point: String
4247
}
4348

src/graphql/subscriptions.js

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,57 @@ export const onDeleteComment = /* GraphQL */ `
160160
}
161161
}
162162
`;
163+
export const onCreateTrack = /* GraphQL */ `
164+
subscription OnCreateTrack(
165+
$filter: ModelSubscriptionTrackFilterInput
166+
$owner: String
167+
) {
168+
onCreateTrack(filter: $filter, owner: $owner) {
169+
classId
170+
userId
171+
completion
172+
played
173+
createdAt
174+
updatedAt
175+
owner
176+
__typename
177+
}
178+
}
179+
`;
180+
export const onUpdateTrack = /* GraphQL */ `
181+
subscription OnUpdateTrack(
182+
$filter: ModelSubscriptionTrackFilterInput
183+
$owner: String
184+
) {
185+
onUpdateTrack(filter: $filter, owner: $owner) {
186+
classId
187+
userId
188+
completion
189+
played
190+
createdAt
191+
updatedAt
192+
owner
193+
__typename
194+
}
195+
}
196+
`;
197+
export const onDeleteTrack = /* GraphQL */ `
198+
subscription OnDeleteTrack(
199+
$filter: ModelSubscriptionTrackFilterInput
200+
$owner: String
201+
) {
202+
onDeleteTrack(filter: $filter, owner: $owner) {
203+
classId
204+
userId
205+
completion
206+
played
207+
createdAt
208+
updatedAt
209+
owner
210+
__typename
211+
}
212+
}
213+
`;
163214
export const onCreateReward = /* GraphQL */ `
164215
subscription OnCreateReward(
165216
$filter: ModelSubscriptionRewardFilterInput
@@ -169,8 +220,6 @@ export const onCreateReward = /* GraphQL */ `
169220
id
170221
classId
171222
userId
172-
completion
173-
played
174223
point
175224
createdAt
176225
updatedAt
@@ -188,8 +237,6 @@ export const onUpdateReward = /* GraphQL */ `
188237
id
189238
classId
190239
userId
191-
completion
192-
played
193240
point
194241
createdAt
195242
updatedAt
@@ -207,8 +254,6 @@ export const onDeleteReward = /* GraphQL */ `
207254
id
208255
classId
209256
userId
210-
completion
211-
played
212257
point
213258
createdAt
214259
updatedAt

0 commit comments

Comments
 (0)