-
Notifications
You must be signed in to change notification settings - Fork 3.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
✨Add support for AMP Story Quiz Reaction API calls #26242
Changes from 1 commit
a0a4f47
3926f1f
5a8d518
e2b8343
9a8f3d9
58465b6
d2ea63d
e5e4774
f74115d
00beac1
fe5ad26
6f43a5a
dd78c9c
6d2eec5
bce0b65
41e9e47
1e7eabb
8967ff5
6fda2c5
f2f0aa0
2995c72
7301496
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,7 @@ import {StateProperty, getStoreService} from './amp-story-store-service'; | |
import {closest} from '../../../src/dom'; | ||
import {createShadowRootWithStyle} from './utils'; | ||
import {dev} from '../../../src/log'; | ||
import {dict} from '../../../src/utils/object'; | ||
import {getRequestService} from './amp-story-request-service'; | ||
import {htmlFor} from '../../../src/static-template'; | ||
import {toArray} from '../../../src/types'; | ||
|
@@ -409,27 +410,29 @@ export class AmpStoryQuiz extends AMP.BaseElement { | |
|
||
let URL = this.element.getAttribute('endpoint'); | ||
jackbsteinberg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
const requestVars = { | ||
reactionType: STORY_REACTION_TYPE_QUIZ, | ||
reactionId: '', // combine url & attribute | ||
}; | ||
const requestVars = dict({ | ||
'reactionType': STORY_REACTION_TYPE_QUIZ, | ||
'reactionId': `page=${this.storeService_.get( | ||
jackbsteinberg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
StateProperty.CURRENT_PAGE_ID | ||
)}&url=${this.win.location.href}`, | ||
jackbsteinberg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}); | ||
|
||
const requestOptions = { | ||
method: 'GET', | ||
}; | ||
const requestOptions = dict({ | ||
'method': 'GET', | ||
}); | ||
|
||
if (reactionValue !== undefined) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are you deciding whether we're fetching the data or answering the quiz with this test? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The way I have it now is that I have two methods,
|
||
requestVars.reactionValue = reactionValue; | ||
requestOptions.method = 'POST'; | ||
requestOptions.body = requestVars; | ||
requestVars['reactionValue'] = reactionValue; | ||
requestOptions['method'] = 'POST'; | ||
requestOptions['body'] = requestVars; | ||
} else { | ||
URL += `?reactionType=${ | ||
requestVars.reactionType | ||
}&reactionId=${encodeURIComponent(requestVars.reactionId)}`; | ||
requestVars['reactionType'] | ||
}&reactionId=${encodeURIComponent(requestVars['reactionId'])}`; | ||
} | ||
jackbsteinberg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
return this.getClientId_().then(clientId => { | ||
requestVars.clientId = clientId; | ||
requestVars['clientId'] = clientId; | ||
return this.requestService_.executeRequest(URL, null, requestOptions); | ||
jackbsteinberg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}); | ||
} | ||
|
@@ -454,24 +457,37 @@ export class AmpStoryQuiz extends AMP.BaseElement { | |
* @private | ||
*/ | ||
handleSuccessfulDataRetrieval_(response) { | ||
if (!('data' in response)) { | ||
return; | ||
jackbsteinberg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
this.responseData_ = { | ||
totalCount: response.data.totalResponseCount, | ||
data: response.data.responses, | ||
}; | ||
|
||
this.hasUserSelection_ = response.data.hasUserResponded; | ||
if (this.hasUserSelection_) { | ||
jackbsteinberg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
this.quizEl_.classList.add('i-amphtml-story-quiz-post-selection'); | ||
|
||
const selectedOptionKey = this.responseData_.data.find( | ||
response => response.selectedByUser | ||
).reactionValue; | ||
|
||
this.quizEl_ | ||
.querySelectorAll('.i-amphtml-story-quiz-option') | ||
[selectedOptionKey].classList.add( | ||
'i-amphtml-story-quiz-option-selected' | ||
const options = this.quizEl_.querySelectorAll( | ||
'.i-amphtml-story-quiz-option' | ||
); | ||
|
||
if (selectedOptionKey >= options.length) { | ||
dev().error( | ||
TAG, | ||
`Quiz #${this.element.getAttribute('id')} does not have option ${ | ||
answerChoiceOptions[selectedOptionKey] | ||
}, but user selected option ${answerChoiceOptions[selectedOptionKey]}` | ||
); | ||
return; | ||
} | ||
|
||
this.quizEl_.classList.add('i-amphtml-story-quiz-post-selection'); | ||
jackbsteinberg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
[selectedOptionKey].classList.add('i-amphtml-story-quiz-option-selected'); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can revisit later but I'd really like us to consider both approaches to providing configuration to a quiz before we launch, parameters vs JSON
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can sync about this option a bit more tomorrow offline