| 39 | } |
| 40 | |
| 41 | saveGist() { |
| 42 | const { user } = this.props.env; |
| 43 | const { scratchPaper, titles, files, lastFiles, editingFile } = this.props.current; |
| 44 | const gist = { |
| 45 | description: titles[titles.length - 1], |
| 46 | files: {}, |
| 47 | }; |
| 48 | files.forEach(file => { |
| 49 | gist.files[file.name] = { |
| 50 | content: file.content, |
| 51 | }; |
| 52 | }); |
| 53 | lastFiles.forEach(lastFile => { |
| 54 | if (!(lastFile.name in gist.files)) { |
| 55 | gist.files[lastFile.name] = null; |
| 56 | } |
| 57 | }); |
| 58 | gist.files['algorithm-visualizer'] = { |
| 59 | content: 'https://algorithm-visualizer.org/', |
| 60 | }; |
| 61 | const save = gist => { |
| 62 | if (!user) return Promise.reject(new Error('Sign In Required')); |
| 63 | if (scratchPaper && scratchPaper.login) { |
| 64 | if (scratchPaper.login === user.login) { |
| 65 | return GitHubApi.editGist(scratchPaper.gistId, gist); |
| 66 | } else { |
| 67 | return GitHubApi.forkGist(scratchPaper.gistId).then(forkedGist => GitHubApi.editGist(forkedGist.id, gist)); |
| 68 | } |
| 69 | } |
| 70 | return GitHubApi.createGist(gist); |
| 71 | }; |
| 72 | save(gist) |
| 73 | .then(refineGist) |
| 74 | .then(newScratchPaper => { |
| 75 | this.props.setScratchPaper(newScratchPaper); |
| 76 | this.props.setEditingFile(newScratchPaper.files.find(file => file.name === editingFile.name)); |
| 77 | if (!(scratchPaper && scratchPaper.gistId === newScratchPaper.gistId)) { |
| 78 | this.props.history.push(`/scratch-paper/${newScratchPaper.gistId}`); |
| 79 | } |
| 80 | }) |
| 81 | .then(this.props.loadScratchPapers) |
| 82 | .catch(this.handleError); |
| 83 | } |
| 84 | |
| 85 | hasPermission() { |
| 86 | const { scratchPaper } = this.props.current; |