@@ -17,12 +17,14 @@ import {
1717
1818// Constants
1919import {
20- telegrafGetFailed ,
21- telegrafCreateFailed ,
22- telegrafUpdateFailed ,
23- telegrafDeleteFailed ,
2420 addTelegrafLabelFailed ,
21+ cloneTelegrafSuccess ,
22+ getTelegrafConfigFailed ,
2523 removeTelegrafLabelFailed ,
24+ telegrafCreateFailed ,
25+ telegrafDeleteFailed ,
26+ telegrafGetFailed ,
27+ telegrafUpdateFailed ,
2628} from 'src/shared/copy/notifications'
2729
2830// Utils
@@ -108,6 +110,60 @@ export const createTelegraf = (telegraf: Telegraf) => async (
108110 }
109111}
110112
113+ const cloneTelegrafLabels = (
114+ sourceTelegraf : Telegraf ,
115+ destinationTelegraf : Telegraf
116+ ) => async ( dispatch : Dispatch < Action > ) => {
117+ try {
118+ const pendingLabels = sourceTelegraf . labels . map ( labelID =>
119+ postTelegrafsLabel ( {
120+ telegrafID : destinationTelegraf . id ,
121+ data : { labelID} ,
122+ } )
123+ )
124+
125+ const mappedLabels = await Promise . all ( pendingLabels )
126+
127+ if (
128+ mappedLabels . length &&
129+ mappedLabels . some ( label => label . status !== 201 )
130+ ) {
131+ throw new Error (
132+ 'An error occurred cloning the labels for this telegraf config'
133+ )
134+ }
135+ dispatch ( notify ( cloneTelegrafSuccess ( ) ) )
136+ } catch {
137+ dispatch ( notify ( addTelegrafLabelFailed ( ) ) )
138+ }
139+ }
140+
141+ export const cloneTelegraf = ( telegraf : Telegraf ) => async (
142+ dispatch : Dispatch < Action >
143+ ) => {
144+ let clonedTelegraf
145+
146+ // Step 1: create a new telegraf
147+ try {
148+ const response = await postTelegraf ( { data : telegraf } )
149+
150+ if ( response . status !== 201 ) {
151+ throw new Error ( response . data . message )
152+ }
153+
154+ clonedTelegraf = response . data
155+ } catch ( error ) {
156+ console . error ( error )
157+ dispatch ( notify ( telegrafCreateFailed ( ) ) )
158+ }
159+
160+ // Step 2: clone the labels
161+ cloneTelegrafLabels ( telegraf , clonedTelegraf ) ( dispatch )
162+
163+ // Step 3: refresh the cloned telegraf in the UI to show the labels
164+ refreshTelegraf ( clonedTelegraf ) ( dispatch )
165+ }
166+
111167export const updateTelegraf = ( telegraf : Telegraf ) => async (
112168 dispatch : Dispatch < Action >
113169) => {
@@ -249,3 +305,29 @@ export const getTelegraf = (telegrafConfigID: string) => async () => {
249305 throw error
250306 }
251307}
308+
309+ // adds a telegraf with its latest properties to the state's resources
310+ export const refreshTelegraf = ( telegraf : Telegraf ) => async (
311+ dispatch : Dispatch < Action >
312+ ) => {
313+ try {
314+ const response = await apiGetTelegraf ( {
315+ telegrafID : telegraf . id ,
316+ headers : { Accept : 'application/json' } ,
317+ } )
318+
319+ if ( response . status !== 200 ) {
320+ throw new Error ( response . data . message )
321+ }
322+
323+ const refreshedTelegraf = response . data
324+ const normTelegraf = normalize < Telegraf , TelegrafEntities , string > (
325+ refreshedTelegraf ,
326+ telegrafSchema
327+ )
328+ dispatch ( addTelegraf ( normTelegraf ) )
329+ } catch ( error ) {
330+ console . error ( error )
331+ dispatch ( notify ( getTelegrafConfigFailed ( ) ) )
332+ }
333+ }
0 commit comments