77 * https://github.com/Visual-Vincent/GuiStack
88 *@
99
10+ @using Amazon ;
1011@using GuiStack .Models
1112@using GuiStack .Extensions
1213@using GuiStack .Repositories
1516<div id =" sns-subscribe-to-sqs-modal" class =" cssWindow dark backdropblur text-center" >
1617 <div class =" closeWindowButton" ><a no-href onclick =" closeParentWindow(event)" >×</a ></div >
1718
18- <h2 class =" title" >Subscribe to SQS queue</h2 >
19+ <h2 class =" title" >Subscribe SQS queue to Topic </h2 >
1920 <div class =" queue-selector-container text-left" style =" margin-top : 16px ; margin-bottom : 16px " >
2021 Loading queues...
2122 </div >
2526 </div >
2627</div >
2728
29+ @{
30+ await Html .RenderPartialAsync (" ~/Pages/Shared/_DeleteModal.cshtml" , new DeleteModalModel (" delete-sns-subscription-modal" ));
31+ }
32+
2833<table class =" gs-info-table colored" >
2934 <tbody >
3035 <tr >
8590<table class =" gs-list padded autosize-all-cols-but-first" >
8691 <thead >
8792 <tr >
88- <th >Protocol</th >
8993 <th >Endpoint</th >
94+ <th >Protocol</th >
9095 <th >Owner</th >
9196 <th >Actions</th >
9297 </tr >
9398 </thead >
9499 <tbody >
95- @foreach( var subscription in Model .Subscriptions )
100+ @foreach( var subscription in Model .Subscriptions . OrderBy ( s => s . Protocol ). ThenBy ( s => s . Endpoint ) )
96101 {
97- <tr >
102+ <tr data-arn =" @subscription.Arn" data-endpoint =" @subscription.Endpoint" >
103+ @if (subscription .Protocol == null )
104+ {
105+ <td >@subscription.Endpoint </td >
106+ }
107+ else
108+ {
109+ <td >
110+ @switch(subscription.Protocol.ToLower())
111+ {
112+ case " sqs" :
113+ {
114+ if (Arn .TryParse (subscription .Endpoint , out var arn ))
115+ {
116+ < a href = " /SQS/@arn.Resource" >< i class = " fa-solid fa-database gs-rotate-270" style = " margin-right: 2px" >< / i > @arn .Resource < / a >
117+ }
118+ else
119+ {
120+ <td >@subscription.Endpoint </td >
121+ }
122+ break ;
123+ }
124+ }
125+ </td >
126+ }
98127 <td >@subscription.Protocol </td >
99- <td >@subscription.Endpoint </td >
100- <td >@subscription.Owner </td >
128+ <td >@( ! string .IsNullOrWhiteSpace (subscription .Owner ) ? subscription .Owner : " (unknown)" ) </td >
101129 <td >
102130 <div class =" gs-icons" >
103- <a no-href class =" purple lnk-sns-sub-copyarn" title =" Copy ARN" >
131+ <a no-href class =" purple lnk-sns-sub-copyarn" title =" Copy Endpoint address/ ARN" >
104132 <i class =" fa-solid fa-link" ></i >
105133 </a >
106134 <a no-href class =" red lnk-sns-sub-delete" title =" Delete" >
117145 var subscribeToSqsModal = document .getElementById (" sns-subscribe-to-sqs-modal" );
118146 var queueSelectorContainer = $ (" #sns-subscribe-to-sqs-modal .queue-selector-container" );
119147
148+ $ (" .lnk-sns-sub-copyarn" ).click (sns_CopySubscriptionArn);
149+ $ (" .lnk-sns-sub-delete" ).click (sns_DeleteSubscriptionPrompt);
150+ $ (" #delete-sns-subscription-modal .yes-button" ).click (sns_DeleteSubscription);
151+
152+ const SNS_URL_DELETE_SUBSCRIPTION = " @Url.Action(" DeleteSubscription" , " Subscriptions" , new { subscriptionArn = " __SUBSCRIPTIONARN__ " })" ;
153+
154+ var prompt_SubscriptionArn;
155+
120156 subscribeToSqsModal .addEventListener (" windowopened" , function () {
121157 $ .ajax ({
122158 type: " GET" ,
136172 queueSelectorContainer .html (" Loading queues..." );
137173 });
138174
139- function sns_CreateSubscription_Click ()
175+ async function sns_CreateSubscription_Click ()
140176 {
141177 var selectedItem = gs_GetSelectedTableItem (queueSelectorContainer[0 ]);
142178
146182 return ;
147183 }
148184
149- // TODO: Create subscription
150- alert (selectedItem .getAttribute (" data-queue-name" ));
185+ var queueUrl = selectedItem .getAttribute (" data-url" );
186+
187+ try
188+ {
189+ var response = await fetch (" @Url.Action(" CreateSubscription" , " Subscriptions" )" , {
190+ method: " POST" ,
191+ headers: new Headers ({ " Content-Type" : " application/json" }),
192+ body: JSON .stringify ({
193+ topicArn: " @Model.Topic.TopicARN" ,
194+ protocol: " sqs" ,
195+ endpoint: queueUrl
196+ })
197+ });
198+
199+ if (! response .ok ) {
200+ throw " Failed to create SNS subscription: Server returned HTTP status " + response .status ;
201+ }
202+
203+ window .location .reload (true );
204+ }
205+ catch (error)
206+ {
207+ gs_DisplayError (error);
208+ }
209+
210+ closeWindow (" sns-subscribe-to-sqs-modal" );
211+ }
212+
213+ function sns_CopySubscriptionArn (event )
214+ {
215+ try
216+ {
217+ var arn = gs_GetParentTableRow (event .currentTarget , true ).getAttribute (" data-arn" );
218+ navigator .clipboard .writeText (arn);
219+ }
220+ catch (error)
221+ {
222+ gs_DisplayError (error);
223+ }
224+ }
225+
226+ function sns_DeleteSubscriptionPrompt (event )
227+ {
228+ try
229+ {
230+ var objNameElement = document .querySelector (" #delete-sns-subscription-modal .title .object-name" );
231+ var parentRow = gs_GetParentTableRow (event .currentTarget , true );
232+ var endpoint = parentRow .getAttribute (" data-endpoint" );
233+
234+ prompt_SubscriptionArn = parentRow .getAttribute (" data-arn" );
235+
236+ objNameElement .innerHTML = ' <span style="color: #FFFFFF">the subscription</span> ' + endpoint;
237+ showWindow (" delete-sns-subscription-modal" );
238+ }
239+ catch (error)
240+ {
241+ gs_DisplayError (error);
242+ }
243+ }
244+
245+ async function sns_DeleteSubscription ()
246+ {
247+ try
248+ {
249+ var subscriptionArn = encodeURIComponent (prompt_SubscriptionArn);
250+
251+ var url = SNS_URL_DELETE_SUBSCRIPTION
252+ .replace (" __SUBSCRIPTIONARN__" , subscriptionArn);
253+
254+ var response = await fetch (url, { method: " DELETE" });
255+
256+ if (! response .ok ) {
257+ throw " Failed to delete SNS subscription: Server returned HTTP status " + response .status ;
258+ }
259+
260+ window .location .reload (true );
261+ }
262+ catch (error)
263+ {
264+ gs_DisplayError (error);
265+ }
266+
267+ closeWindow (" delete-sns-subscription-modal" );
151268 }
152269 </script >
0 commit comments