using Microsoft.AspNetCore.Components; using Newtonsoft.Json; using Telerik.Blazor.Components; using TSC2.Server; namespace TSC2.Client.Pages { public partial class Home : ComponentBase { TelerikMap? MapRef { get; set; } public double Zoom { get; set; } = 1; public static double[] Center = [40.68, 74.04]; public string MapHeight { get; set; } = "800px"; public string ShopName { get; set; } = ""; public string CSZ { get; set; } = ""; public string[] Subdomains { get; set; } = new string[] { "a", "b", "c" }; public string UrlTemplate { get; set; } = "https://#= subdomain #.tile.openstreetmap.org/#= zoom #/#= x #/#= y #.png"; public string Attribution { get; set; } = "© OpenStreetMap contributors"; private bool initialized = false; // User info string ip = ""; LocationDetails? userLoc; protected override async Task OnInitializedAsync() // Executes every page load; run inexpensive rendering code here { if (userLoc != null) { Center = [userLoc.latitude, userLoc.longitude]; Zoom = 13; } InitializeMapMarkers(Center); MapRef?.Refresh(); } protected override async Task OnAfterRenderAsync(bool firstRender) // Executes the first time the component is rendered; expensive/run-once code is run here { // to ensure that API and service calls are not done multiple times. if (!firstRender) return; HttpClient client = new(); var ip = await client.GetStringAsync("https://api.ipify.org"); // TODO: replace with internal IP lookup and add additional services as needed var response = await client.GetStringAsync(String.Format("https://ipapi.co/{0}/json/", ip)); Console.WriteLine(DateTime.Now.ToString() + " | " + response); userLoc = JsonConvert.DeserializeObject(response); if (userLoc != null) { Center = [userLoc.latitude, userLoc.longitude]; Zoom = 13; } InitializeMapMarkers(Center); MapRef?.Refresh(); } public async void OnMarkerClick(MapMarkerClickEventArgs args) { var dataItem = args.DataItem as MarkerModel; CurrentSelection = dataItem.Title; CurrentBlurb = dataItem.Blurb; await ToggleModal(); Console.WriteLine(dataItem.Title); } public List MapMarkers { get; set; } = new List() { new MarkerModel() { Title = "" } }; private void InitializeMapMarkers(double[] coords) { // Server call to load map markers into a list of tuples List<(double[], string)> localMarkers = MapDriver.InitializeMarkers(coords); foreach (var current in localMarkers) { MapMarkers.Add(new MarkerModel() { LatLng = current.Item1, Title = current.Item2, Blurb = string.Format("This is {0}'s blurb. This will be replaced when a lookup is created.", current.Item2) }); } } public void SubmitLocation() { string str = String.Format(("Shop Name: {0} \n CSZ: {1}"), ShopName, CSZ); Console.WriteLine(str); // Search based on the input provided in the ShopName and CSZ fields // TODO // Call Geocoding API to get coordinates for making the map jump // TODO var jsonString = "{\"latitude\": \"42.295920\",\"longitude\":\"-83.444780\"}"; LocationDetails? json = JsonConvert.DeserializeObject(jsonString); // Move the map if (json != null) { Center = [json.latitude, json.longitude]; Zoom = 13; } InitializeMapMarkers(Center); if (MapRef != null) MapRef.Refresh(); } public class MarkerModel { public double[] LatLng { get; set; } = [42.4649, -83.3684]; public string Title { get; set; } = ""; public string Blurb { get; set; } = ""; } private class LocationDetails { public string ip { get; set; } = ""; public string network { get; set; } = ""; public string version { get; set; } = ""; public string city { get; set; } = ""; public string region { get; set; } = ""; public string region_code { get; set; } = ""; public string country { get; set; } = ""; public string country_code { get; set; } = ""; public string country_code_iso3 { get; set; } = ""; public string country_capital { get; set; } = ""; public string country_tld { get; set; } = ""; public string continent_code { get; set; } = ""; public bool in_eu { get; set; } = false; public string postal { get; set; } = ""; public double latitude { get; set; } = -1; public double longitude { get; set; } = -1; public string timezone { get; set; } = ""; public string utc_offset { get; set; } = ""; public string country_calling_code { get; set; } = ""; public string currency { get; set; } = ""; public string currency_name { get; set; } = ""; public string languages { get; set; } = ""; public string country_area { get; set; } = ""; public string country_population { get; set; } = ""; public string asn { get; set; } = ""; public string org { get; set; } = ""; } public TelerikPopover? Popover { get; set; } public string CurrentSelection { get; set; } = "123412341234onetwothreefour"; public string CurrentBlurb { get; set; } = "This is a blurb for a shop. I believe it is rather dastardly."; public bool isVisible = false; public async Task ToggleModal() { if (isVisible) isVisible = false; else isVisible = true; await Task.Delay(1); } } }