Created
April 16, 2020 22:10
-
-
Save InukVT/3f83705c7627c52861194ade2fb5f20d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Different ad types | |
| */ | |
| enum AdType | |
| { | |
| /** No formal partnership */ | |
| Affiliate = "Affiliate", | |
| /** No formal partnerhsip */ | |
| Ad = "Ad", | |
| /** Formal partnership */ | |
| Sponser = "Sponser" | |
| } | |
| class Ad | |
| { | |
| constructor (ad: {title: string, | |
| description: string, | |
| link: string, | |
| type: string}) | |
| { | |
| this.title = ad.title | |
| this.description = ad.description | |
| this.link = ad.link | |
| switch(ad.type) | |
| { | |
| case "Affiliate": | |
| this.type = AdType.Affiliate | |
| break | |
| case "Ad": | |
| this.type = AdType.Ad | |
| break | |
| } | |
| } | |
| title: string; | |
| description: string; | |
| link: string; | |
| /** This is the ad type */ | |
| type: AdType; | |
| } | |
| /** | |
| * Mock Ad data for the server | |
| */ | |
| let mockAds = [ | |
| { | |
| "title":"Revolut", | |
| "description": "A next generation banking system", | |
| "link":"https://www.inuk.blog", | |
| "type":"Affiliate" | |
| }, | |
| { | |
| "title":"NordVPN", | |
| "description":"A simple to use VPN service", | |
| "link": "https://www.inuk.blog", | |
| "type":"Affiliate" | |
| } | |
| ] | |
| let mockResponse: Ad[] = mockAds.map (mockAd => { return new Ad(mockAd) } ) | |
| /** Loads the ad and returns an appropriate dev | |
| * @return The ads div | |
| */ | |
| function loadAd (ad: Ad) | |
| { | |
| console.log("Rendering ad for " + ad.title) | |
| let typeNode = document.createTextNode (ad.type) | |
| let titleNode = document.createTextNode (ad.title) | |
| let descriptionNode = document.createTextNode (ad.description) | |
| let link = document.createElement ("a") | |
| link.setAttribute ("href", ad.link) | |
| link.appendChild (typeNode) | |
| link.appendChild (titleNode) | |
| link.appendChild (descriptionNode) | |
| let footer = document.getElementsByTagName ('footer') | |
| footer[0].insertAdjacentElement ( "beforebegin", link) | |
| } | |
| let random = Math.floor ( Math.random() * mockResponse.length ) | |
| let ad = mockResponse[random] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment