|
| 1 | +import { getSettings, loadAJS } from './utils'; |
| 2 | + |
| 3 | +const App = function(props) { |
| 4 | + const [loading, setLoading] = React.useState(false); |
| 5 | + const [wk, setWk] = React.useState(''); |
| 6 | + const [ajsLoaded, setAjsLoaded] = React.useState(false); |
| 7 | + const [dirty, setDirty] = React.useState(false); |
| 8 | + const [editorVisible, setEditorVisible] = React.useState(false); |
| 9 | + const editor = React.useRef(null); |
| 10 | + |
| 11 | + React.useEffect(() => { |
| 12 | + const wk = localStorage.getItem('segWk'); |
| 13 | + const settings = localStorage.getItem('segSettings'); |
| 14 | + |
| 15 | + wk && setWk(wk); |
| 16 | + const settingsObject = JSON.parse(settings); |
| 17 | + |
| 18 | + if (wk && Object.keys(settingsObject).length) { |
| 19 | + // load AJS |
| 20 | + loadAJS(wk, settingsObject); |
| 21 | + setAjsLoaded(true); |
| 22 | + setEditorVisible(true); |
| 23 | + } |
| 24 | + |
| 25 | + const container = document.getElementById('jsoneditor'); |
| 26 | + const options = { |
| 27 | + onChange: () => { |
| 28 | + setDirty(true); |
| 29 | + } |
| 30 | + }; |
| 31 | + editor.current = new JSONEditor(container, options); |
| 32 | + editor.current.set(settingsObject); |
| 33 | + editor.current.expand({ |
| 34 | + path: ['integrations'], |
| 35 | + isExpand: true, |
| 36 | + recursive: false |
| 37 | + }); |
| 38 | + }, []); |
| 39 | + |
| 40 | + const loadSettings = async () => { |
| 41 | + setLoading(true); |
| 42 | + const settingsJSON = await getSettings(wk); |
| 43 | + const settings = JSON.stringify(settingsJSON, null, 2); |
| 44 | + localStorage.setItem('segWk', wk); |
| 45 | + localStorage.setItem('segSettings', settings); |
| 46 | + editor.current.set(settingsJSON); |
| 47 | + setLoading(false); |
| 48 | + setEditorVisible(true); |
| 49 | + |
| 50 | + if (ajsLoaded) { |
| 51 | + setDirty(true); |
| 52 | + } else { |
| 53 | + loadAJS(wk, settingsJSON); |
| 54 | + setAjsLoaded(true); |
| 55 | + } |
| 56 | + }; |
| 57 | + |
| 58 | + const handleReload = () => { |
| 59 | + const settingsObject = editor.current.get(); |
| 60 | + const settings = JSON.stringify(settingsObject); |
| 61 | + |
| 62 | + localStorage.setItem('segWk', wk); |
| 63 | + localStorage.setItem('segSettings', settings); |
| 64 | + location.reload(); |
| 65 | + }; |
| 66 | + |
| 67 | + return ( |
| 68 | + <div> |
| 69 | + <form> |
| 70 | + <fieldset> |
| 71 | + <label htmlFor="writeKey">Writekey: </label> |
| 72 | + <input |
| 73 | + type="text" |
| 74 | + placeholder="Segment writekey" |
| 75 | + id="writeKey" |
| 76 | + onChange={e => { |
| 77 | + setWk(e.target.value); |
| 78 | + }} |
| 79 | + value={wk} |
| 80 | + /> |
| 81 | + <input |
| 82 | + className="pure-button pure-button-primary" |
| 83 | + type="button" |
| 84 | + value="Fetch Writekey" |
| 85 | + onClick={loadSettings} |
| 86 | + /> |
| 87 | + <input |
| 88 | + className="pure-button pure-button-primary" |
| 89 | + type="button" |
| 90 | + value="Reload and init AJS" |
| 91 | + onClick={handleReload} |
| 92 | + disabled={!dirty} |
| 93 | + /> |
| 94 | + </fieldset> |
| 95 | + </form> |
| 96 | + <div |
| 97 | + id="jsoneditor" |
| 98 | + style={{ height: '90vh', display: editorVisible ? 'block' : 'none' }} |
| 99 | + ></div> |
| 100 | + </div> |
| 101 | + ); |
| 102 | +}; |
| 103 | + |
| 104 | +export default App; |
0 commit comments