1+ @*
2+ * This Source Code Form is subject to the terms of the Mozilla Public
3+ * License, v. 2.0. If a copy of the MPL was not distributed with this
4+ * file, You can obtain one at https://mozilla.org/MPL/2.0/.
5+ *
6+ * Copyright © Vincent Bengtsson & Contributors 2022-2023
7+ * https://github.com/Visual-Vincent/GuiStack
8+ *@
9+
10+ @page " {topic?}"
11+
12+ @using System .Net
13+ @using Amazon .SimpleNotificationService
14+ @model GuiStack .Pages .SNS .IndexModel
15+
16+ @{
17+ ViewData [" Title" ] = " SNS Topics" ;
18+ ViewData [" TopicArn" ] = Model .Topic ;
19+
20+ bool hasTopicArn = ! string .IsNullOrWhiteSpace (Model .Topic );
21+ }
22+
23+ @if (hasTopicArn )
24+ {
25+ <h1 >@Model.Topic </h1 >
26+ }
27+ else
28+ {
29+ <div id =" new-sns-topic-modal" class =" cssWindow dark backdropblur text-center" >
30+ <div class =" closeWindowButton" ><a no-href onclick =" closeParentWindow(event)" >×</a ></div >
31+
32+ <h2 class =" title" >New SNS topic </h2 >
33+ <p >
34+ <input type =" text" class =" name-textbox text-center" maxlength =" 80" style =" width : 400px " />
35+ </p >
36+ <p >
37+ <input type =" checkbox" id =" new-sns-topic-fifo-checkbox" class =" fifo-checkbox" />
38+ <label for =" new-sns-topic-fifo-checkbox" >FIFO </label >
39+ </p >
40+
41+ <div class =" modal-buttons text-center" >
42+ <button onclick =" sns_CreateTopic()" >Create </button >
43+ </div >
44+ </div >
45+
46+ <div style =" display : flex ; align-items : center " >
47+ <h1 >SNS topics </h1 >
48+ <div style =" text-align : right ; font-size : 1.5em ; flex-grow : 1 " >
49+ <a no-href onclick =" showWindow('new-sns-topic-modal')" class =" gs-icon-stack initial-white neon-green" >
50+ <i class =" fa-solid fa-square-envelope" style =" margin-right : 4px " ></i >
51+ <i class =" bi bi-plus-circle-fill gs-icon-overlay stroked" style =" color : #000000 " ></i >
52+ </a >
53+ </div >
54+ </div >
55+ }
56+
57+ <div id =" list-container" >
58+ @try
59+ {
60+ if (! hasTopicArn )
61+ {
62+ await Html .RenderPartialAsync (" ~/Pages/SNS/_TopicsTable.cshtml" , await Model .SNSRepository .GetTopicsAsync ());
63+ }
64+ else
65+ {
66+ // TODO:
67+ // await Html.RenderPartialAsync("~/Pages/SNS/_TopicInfo.cshtml", await Model.SNSRepository.GetTopicAttributesAsync(Model.Topic));
68+ }
69+ }
70+ catch(AmazonSimpleNotificationServiceException ex)
71+ {
72+ if (ex .StatusCode == HttpStatusCode .NotFound )
73+ {
74+ < h2 class = " error-text" > Topic not found < / h2 >
75+ }
76+ else
77+ {
78+ if (! hasTopicArn )
79+ {
80+ < h2 class = " error-text" > Failed to fetch topics : < / h2 >
81+ }
82+ else
83+ {
84+ <h2 class =" error-text" >Failed to fetch topics contents : </h2 >
85+ }
86+
87+ <p class =" error-text" >@ex.Message </p >
88+ }
89+ }
90+ catch(Exception ex)
91+ {
92+ if (! hasTopicArn )
93+ {
94+ < h2 class = " error-text" > Failed to fetch topics : < / h2 >
95+ }
96+ else
97+ {
98+ <h2 class =" error-text" >Failed to fetch topic contents : </h2 >
99+ }
100+
101+ <p class =" error-text" >@ex.Message </p >
102+ }
103+ </div >
104+
105+ @if (! hasTopicArn )
106+ {
107+ <script type =" text/javascript" >
108+ async function sns_CreateTopic ()
109+ {
110+ try
111+ {
112+ var topicName = document .querySelector (" #new-sns-topic-modal .name-textbox" ).value ;
113+ var isFifo = document .getElementById (" new-sns-topic-fifo-checkbox" ).checked ;
114+
115+ var response = await fetch (" @Url.Action(" CreateTopic" , " Topics" )" , {
116+ method: " PUT" ,
117+ headers: new Headers ({ " Content-Type" : " application/json" }),
118+ body: JSON .stringify ({
119+ topicName: topicName,
120+ isFifo: isFifo
121+ })
122+ });
123+
124+ if (! response .ok ) {
125+ throw " Failed to create SNS topic: Server returned HTTP status " + response .status ;
126+ }
127+
128+ window .location .reload (true );
129+ }
130+ catch (error)
131+ {
132+ gs_DisplayError (error);
133+ }
134+
135+ closeWindow (" new-sns-topic-modal" );
136+ }
137+ </script >
138+ }
0 commit comments