Skip to content

Commit 9131374

Browse files
authored
feat: add generic script loader (#5555)
1 parent 4454a5b commit 9131374

File tree

3 files changed

+97
-1
lines changed

3 files changed

+97
-1
lines changed

src/dataExplorer/components/resources/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ interface Resources {
2323

2424
export const RESOURCES: Resources = {}
2525
// eslint-disable-next-line no-extra-semi
26-
;[].forEach(mod => {
26+
;[require('./types/script')].forEach(mod => {
2727
mod.default((def: ResourceRegistration) => {
2828
if (RESOURCES.hasOwnProperty(def.type)) {
2929
throw new Error(
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import React from 'react'
2+
3+
export default () => <h1>script editor</h1>
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import {ResourceType} from 'src/types/resources'
2+
import editor from './editor'
3+
import {CLOUD} from 'src/shared/constants'
4+
5+
const {getScript, patchScript, postScript} = CLOUD
6+
? require('src/client/scriptsRoutes')
7+
: {
8+
getScript: false,
9+
patchScript: false,
10+
postScript: false,
11+
}
12+
13+
export default function script(register) {
14+
register({
15+
type: ResourceType.Scripts,
16+
editor,
17+
init: id => {
18+
if (!id || !CLOUD) {
19+
return Promise.resolve({
20+
type: ResourceType.Scripts,
21+
flux: '',
22+
data: {
23+
name: `Untitled Script: ${new Date().toISOString()}`,
24+
description: 'default description',
25+
},
26+
})
27+
}
28+
29+
return getScript({
30+
scriptID: id,
31+
}).then(resp => {
32+
if (resp.status === 200) {
33+
return {
34+
type: ResourceType.Scripts,
35+
flux: resp.data.script,
36+
data: resp.data,
37+
}
38+
}
39+
40+
return {
41+
type: ResourceType.Scripts,
42+
flux: '',
43+
data: {
44+
name: `Untitled Script: ${new Date().toISOString()}`,
45+
description: 'default description',
46+
},
47+
}
48+
})
49+
},
50+
persist: resource => {
51+
const data = JSON.parse(JSON.stringify(resource.data))
52+
data.script = resource.flux
53+
54+
if (!CLOUD) {
55+
return resource
56+
}
57+
58+
if (data.id) {
59+
return patchScript({
60+
scriptID: data.id,
61+
data: {
62+
name: data.name,
63+
description: data.description,
64+
script: data.script,
65+
},
66+
}).then(() => {
67+
return resource
68+
})
69+
}
70+
71+
return postScript({
72+
data: {
73+
name: data.name,
74+
description: data.description,
75+
script: data.script,
76+
language: 'flux',
77+
},
78+
}).then(resp => {
79+
if (resp.status === 201) {
80+
return {
81+
...resource,
82+
data: {
83+
...data,
84+
id: resp.data.id,
85+
},
86+
}
87+
}
88+
89+
return resource
90+
})
91+
},
92+
})
93+
}

0 commit comments

Comments
 (0)