|
| 1 | +import { Spinner } from '@jupyterlab/apputils'; |
| 2 | +import { Widget } from '@phosphor/widgets'; |
| 3 | +import { Git, IGitPushPullResult } from './git'; |
| 4 | + |
| 5 | +export enum Operation { |
| 6 | + Pull = 'Pull', |
| 7 | + Push = 'Push' |
| 8 | +} |
| 9 | + |
| 10 | +/** |
| 11 | + * The UI for the content shown within the Git push/pull modal. |
| 12 | + */ |
| 13 | +export class GitPullPushDialog extends Widget { |
| 14 | + spinner: Spinner; |
| 15 | + gitApi: Git; |
| 16 | + body: HTMLElement; |
| 17 | + operation: Operation; |
| 18 | + |
| 19 | + /** |
| 20 | + * Instantiates the dialog and makes the relevant service API call. |
| 21 | + */ |
| 22 | + constructor(currentFileBrowserPath: string, operation: Operation) { |
| 23 | + super(); |
| 24 | + this.operation = operation; |
| 25 | + |
| 26 | + this.body = this.createBody(); |
| 27 | + this.node.appendChild(this.body); |
| 28 | + |
| 29 | + this.spinner = new Spinner(); |
| 30 | + this.node.appendChild(this.spinner.node); |
| 31 | + |
| 32 | + this.gitApi = new Git(); |
| 33 | + this.executeGitApi(currentFileBrowserPath); |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Executes the relevant service API depending on the operation and handles response and errors. |
| 38 | + * @param currentFileBrowserPath the path to the current repo |
| 39 | + */ |
| 40 | + private executeGitApi(currentFileBrowserPath: string) { |
| 41 | + switch (this.operation) { |
| 42 | + case Operation.Pull: |
| 43 | + this.gitApi |
| 44 | + .pull(currentFileBrowserPath) |
| 45 | + .then(response => { |
| 46 | + this.handleResponse(response); |
| 47 | + }) |
| 48 | + .catch(() => this.handleError()); |
| 49 | + break; |
| 50 | + case Operation.Push: |
| 51 | + this.gitApi |
| 52 | + .push(currentFileBrowserPath) |
| 53 | + .then(response => { |
| 54 | + this.handleResponse(response); |
| 55 | + }) |
| 56 | + .catch(() => this.handleError()); |
| 57 | + break; |
| 58 | + default: |
| 59 | + throw new Error(`Invalid operation type: ${this.operation}`); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Handles the response from the server by removing the spinner and showing the appropriate |
| 65 | + * success or error message. |
| 66 | + * @param response the response from the server API call |
| 67 | + */ |
| 68 | + private handleResponse(response: IGitPushPullResult) { |
| 69 | + this.node.removeChild(this.spinner.node); |
| 70 | + this.spinner.dispose(); |
| 71 | + if (response.code !== 0) { |
| 72 | + this.handleError(response.message); |
| 73 | + } else { |
| 74 | + this.handleSuccess(); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + private handleError( |
| 79 | + message: string = 'Unexpected failure. Please check your Jupyter server logs for more details.' |
| 80 | + ): void { |
| 81 | + const label = document.createElement('label'); |
| 82 | + const text = document.createElement('span'); |
| 83 | + text.textContent = `Git ${this.operation} failed with error:`; |
| 84 | + const errorMessage = document.createElement('span'); |
| 85 | + errorMessage.textContent = message; |
| 86 | + errorMessage.setAttribute( |
| 87 | + 'style', |
| 88 | + 'background-color:var(--jp-rendermime-error-background)' |
| 89 | + ); |
| 90 | + label.appendChild(text); |
| 91 | + label.appendChild(document.createElement('p')); |
| 92 | + label.appendChild(errorMessage); |
| 93 | + this.body.appendChild(label); |
| 94 | + } |
| 95 | + |
| 96 | + private handleSuccess(): void { |
| 97 | + const label = document.createElement('label'); |
| 98 | + const text = document.createElement('span'); |
| 99 | + text.textContent = `Git ${this.operation} completed successfully`; |
| 100 | + label.appendChild(text); |
| 101 | + this.body.appendChild(label); |
| 102 | + } |
| 103 | + private createBody(): HTMLElement { |
| 104 | + const node = document.createElement('div'); |
| 105 | + node.className = 'jp-RedirectForm'; |
| 106 | + return node; |
| 107 | + } |
| 108 | +} |
0 commit comments