|
| 1 | +import Component from '@glimmer/component'; |
| 2 | +import { tracked } from '@glimmer/tracking'; |
| 3 | +import { task, timeout } from 'ember-concurrency'; |
| 4 | +import fade from 'ember-animated/transitions/fade'; |
| 5 | +import { action } from '@ember/object'; |
| 6 | + |
| 7 | +interface Signature { |
| 8 | + Element: HTMLDivElement; |
| 9 | +} |
| 10 | + |
| 11 | +export default class CommunitySolutionCardFeedbackSectionComponent extends Component<Signature> { |
| 12 | + transition = fade; |
| 13 | + |
| 14 | + @tracked tempHasVotedRecently = false; |
| 15 | + @tracked tempHasUpvoted = false; |
| 16 | + @tracked tempHasDownvoted = false; |
| 17 | + |
| 18 | + @action |
| 19 | + handleDownvoteButtonClick() { |
| 20 | + // No feedback if the user is "reversing" their vote |
| 21 | + if (this.tempHasDownvoted) { |
| 22 | + this.tempHasDownvoted = false; |
| 23 | + this.tempHasVotedRecently = false; |
| 24 | + |
| 25 | + return; |
| 26 | + } |
| 27 | + |
| 28 | + this.flashSuccessMessageTask.perform(); |
| 29 | + this.tempHasUpvoted = false; |
| 30 | + this.tempHasDownvoted = true; |
| 31 | + } |
| 32 | + |
| 33 | + @action |
| 34 | + handleUpvoteButtonClick() { |
| 35 | + // No feedback if the user is "reversing" their vote |
| 36 | + if (this.tempHasUpvoted) { |
| 37 | + this.tempHasUpvoted = false; |
| 38 | + this.tempHasVotedRecently = false; |
| 39 | + |
| 40 | + return; |
| 41 | + } |
| 42 | + |
| 43 | + this.flashSuccessMessageTask.perform(); |
| 44 | + this.tempHasDownvoted = false; |
| 45 | + this.tempHasUpvoted = true; |
| 46 | + } |
| 47 | + |
| 48 | + flashSuccessMessageTask = task({ keepLatest: true }, async () => { |
| 49 | + this.tempHasVotedRecently = true; |
| 50 | + await timeout(2000); |
| 51 | + this.tempHasVotedRecently = false; |
| 52 | + }); |
| 53 | +} |
| 54 | + |
| 55 | +declare module '@glint/environment-ember-loose/registry' { |
| 56 | + export default interface Registry { |
| 57 | + 'CoursePage::CourseStageStep::CommunitySolutionCard::FeedbackSection': typeof CommunitySolutionCardFeedbackSectionComponent; |
| 58 | + } |
| 59 | +} |
0 commit comments