From 794c5ee58b5a469c0aa2acaddb1bae049b146bfb Mon Sep 17 00:00:00 2001 From: Andrew Date: Wed, 17 Jul 2019 13:17:25 -0400 Subject: [PATCH] fix: remove jquery from dialogs still need to get GitServerClient working without jquery --- src/GitServerClient_no_jquery.js | 220 +++++++++++++++++++++++++++++++ src/Load.js | 40 ++---- src/Save.js | 10 -- src/authenticate.js | 22 +--- src/index.js | 21 +-- 5 files changed, 240 insertions(+), 73 deletions(-) create mode 100644 src/GitServerClient_no_jquery.js diff --git a/src/GitServerClient_no_jquery.js b/src/GitServerClient_no_jquery.js new file mode 100644 index 0000000..0501529 --- /dev/null +++ b/src/GitServerClient_no_jquery.js @@ -0,0 +1,220 @@ +'use strict'; + +const Cookies = require('js-cookie'); + +let baseUrl = ''; +function setServerURL(url) { + baseUrl = url; +} + +let isGitLab = false; +function useGitLab(useIt) { + isGitLab = useIt; +} + +function callCWRCGitWithToken(path, fetchOptions={}, params) { + var url = new URL(`${baseUrl}${path}`); + if (params !== undefined) { + Object.keys(params).forEach(key => url.searchParams.append(key, params[key])); + } + + if (fetchOptions.method === undefined) { + fetchOptions.method = 'GET'; + } + + fetchOptions.mode = 'cors'; + fetchOptions.redirect = 'follow'; + + fetchOptions.credentials = 'include'; + if (fetchOptions.headers === undefined || fetchOptions.headers['Content-Type'] === undefined) { + fetchOptions.headers = { + 'Content-Type': 'application/json' + } + } + const theJWT = Cookies.get('cwrc-token'); + if (theJWT) { + fetchOptions.headers['cwrc-token'] = theJWT; + } + + return fetch(url, fetchOptions).then((response) => { + return response.json().then((data)=>{ + return Promise.resolve(data); + }, (error)=>{ + return Promise.reject(response); + }) + },(error)=>{ + return Promise.reject(error); + }) +} + +function getDetailsForGithubUser(user) { + var url = `/users/${user}`; + return callCWRCGitWithToken(url); +} + +function getDetailsForOrg(org) { + var url = `/orgs/${org}`; + return callCWRCGitWithToken(url); +} + +function createRepo(repo, description, isPrivate) { + var url = `/user/repos`; + if (isGitLab) { + url=`/projects?name=`+repo + } + const fetchOptions = { + method: 'POST', + body: {repo, isPrivate, description} + }; + return callCWRCGitWithToken(url, fetchOptions); +} + +function createOrgRepo(org, repo, description, isPrivate) { + var url = `/orgs/${org}/repos`; + const fetchOptions = { + method: 'POST', + body: {repo, isPrivate, description} + }; + return callCWRCGitWithToken(url, fetchOptions); +} + +function getReposForGithubUser(githubName, page = 1, per_page = 20) { + var url = `/users/${githubName}/repos`; + if (isGitLab) { + url=`/users/${githubName}/projects`; + } + return callCWRCGitWithToken(url, {}, {page, per_page}); +} + +function getReposForAuthenticatedGithubUser(page, per_page, affiliation) { + if (Cookies.get('cwrc-token')) { + var url = `/user/repos`; + if (isGitLab) { + url= `/projects`; + } + return callCWRCGitWithToken(url, {}, {page, per_page, affiliation}).then(result=>result); + } else { + return Promise.reject("Not logged in!"); + } +} + +function getRepoContents(githubName) { + var url = `/repos/${githubName}`; + if (isGitLab) { + url= `/projects/'${githubName}/repository/tree`; + } + return callCWRCGitWithToken(url).then(result=>{ + return result + }, error=>{ + console.log('the error in gitserverclient.getRepoContents:'); + console.log(error) + return error + }); +} + +function getRepoContentsByDrillDown(githubName) { + var url = `/repos/${githubName}/full`; + if (isGitLab) { + url = `/projects/${githubName}/full`; + } + return callCWRCGitWithToken(url); +} + +// repoName here is the combined owner/repo, e.g., 'jchartrand/someRepoName' + +function getDoc(repoName, branch, path){ + var url = `/repos/${repoName}/contents` + if (isGitLab) { + url = `/projects/${repoName}/repository/files/${encodeURI(path)}/raw?ref=master` + } + return callCWRCGitWithToken(url, {}, {branch, path}); +} + +function getInfoForAuthenticatedUser() { + if (Cookies.get('cwrc-token')) { + var url = `/users`; + if (isGitLab) { + url = `/users`; + } + return callCWRCGitWithToken(url).then(result=>result.data); + } else { + return Promise.reject("Not logged in!"); + } +} + +function getPermissionsForGithubUser(owner, repo, username) { + var url = `/repos/${owner}/${repo}/collaborators/${username}/permission`; + return callCWRCGitWithToken(url).then(result=>result.data.permission,(fail)=>'none') +} + +// sha is optional. +// If provided, the doc will be updated against that SHA. +// If not, and there is an existing doc, the file will be updated against the latest SHA in the repo. + +// TODO this doesn't work in this non-jquery version +function saveDoc(repo, path, content, branch, message, sha) { + var data = {content, sha, branch, path, message}; + var url = `/repos/${repo}/doc` + if (isGitLab) { + url = `/projects/${repo}/repository/files/${path}` + } + return callCWRCGitWithToken(url, {method: 'PUT', headers: {'Content-Type': 'application/json'}, body: data}) +} + +function saveAsPullRequest(repo, path, content, branch, message, title, sha) { + var url = `/repos/${repo}/pr`; + var data = {sha, branch, path, message, content, title} + + return callCWRCGitWithToken(url, {method: 'PUT', body: data}) +} + +function getTemplates() { + var url = `/templates`; + return callCWRCGitWithToken(url).then(result=>result.data) +} + +function getTemplate(templateName) { + var url = `/templates/${templateName}`; + return callCWRCGitWithToken(url, {'Content-Type': 'text/xml'}) +} + +function searchCode(query, per_page, page) { + var url = `/search/code`; + if (isGitLab) { + url = `/search?scope=projects`; + } + return callCWRCGitWithToken(url, {}, {q: query, page, per_page}).then(result=>{ + return result + }) +} + +function searchRepos(query, per_page, page) { + var url = `/search/repositories`; + return callCWRCGitWithToken(url, {}, {q: query, page, per_page}).then(result=>{ + return result + }) +} + +module.exports = { + setServerURL: setServerURL, + useGitLab: useGitLab, + getDetailsForGithubUser: getDetailsForGithubUser, + getDetailsForOrg: getDetailsForOrg, + getReposForGithubUser: getReposForGithubUser, + getPermissionsForGithubUser: getPermissionsForGithubUser, + getReposForAuthenticatedGithubUser: getReposForAuthenticatedGithubUser, + saveDoc: saveDoc, + saveAsPullRequest: saveAsPullRequest, + createRepo: createRepo, + createOrgRepo: createOrgRepo, + getRepoContents: getRepoContents, + getRepoContentsByDrillDown: getRepoContentsByDrillDown, + getDoc: getDoc, + getInfoForAuthenticatedUser: getInfoForAuthenticatedUser, + getTemplates: getTemplates, + getTemplate: getTemplate, + searchCode: searchCode, + searchRepos: searchRepos +} + + diff --git a/src/Load.js b/src/Load.js index 49e089b..8a359ab 100644 --- a/src/Load.js +++ b/src/Load.js @@ -1,15 +1,5 @@ 'use strict'; -let $ = window.cwrcQuery -if ($ === undefined) { - let prevJQuery = window.jQuery - $ = require('jquery') - window.jQuery = $ - require('bootstrap') - window.jQuery = prevJQuery - window.cwrcQuery = $ -} - import React, {Component, Fragment} from 'react' import { Modal, Button, Tabs, Tab, Well, Grid, Row, Col, PanelGroup, Panel, ListGroup, ListGroupItem, ToggleButtonGroup, ToggleButton, ControlLabel, FormGroup, FormControl, Checkbox, Glyphicon } from 'react-bootstrap'; import parseLinks from 'parse-link-header'; @@ -24,45 +14,39 @@ import SearchInput from "./SearchInput.js"; const RESULTS_PER_PAGE = 10; function getReposForGithubUser(user, requestedPage, resultsPerPage=RESULTS_PER_PAGE) { - let dfd = $.Deferred(); - cwrcGit.getReposForGithubUser(user, requestedPage, resultsPerPage).then((results)=>{ - dfd.resolve({ + return cwrcGit.getReposForGithubUser(user, requestedPage, resultsPerPage).then((results)=>{ + return Promise.resolve({ items: results.data, lastPage: getLastPage(results, requestedPage) }); }, (fail)=>{ - dfd.reject(fail); - }) - return dfd.promise(); + return Promise.reject(fail); + }); } function getReposForAuthenticatedGithubUser(requestedPage, affiliation='owner', resultsPerPage=RESULTS_PER_PAGE) { - let dfd = $.Deferred(); - cwrcGit.getReposForAuthenticatedGithubUser(requestedPage, resultsPerPage, affiliation).then((results)=>{ - dfd.resolve({ + return cwrcGit.getReposForAuthenticatedGithubUser(requestedPage, resultsPerPage, affiliation).then((results)=>{ + return Promise.resolve({ items: results.data, lastPage: getLastPage(results, requestedPage) }); }, (fail)=>{ - dfd.reject(fail); - }) - return dfd.promise(); + return Promise.reject(fail); + }); } function getSearchResults(gitName, searchTerms, requestedPage, resultsPerPage=RESULTS_PER_PAGE) { - let dfd = $.Deferred(); let queryString = 'language:xml '; if (searchTerms) queryString += '"' + searchTerms + '" '; if (gitName) queryString += "user:" + gitName; - cwrcGit.searchCode(queryString, resultsPerPage, requestedPage).then((results)=>{ - dfd.resolve({ + return cwrcGit.searchCode(queryString, resultsPerPage, requestedPage).then((results)=>{ + return Promise.resolve({ items: results.data.items, lastPage: getLastPage(results, requestedPage) }); }, (fail)=>{ - dfd.reject(fail); - }); - return dfd.promise(); + return Promise.reject(fail); + });; } function getLastPage(results, requestedPage) { diff --git a/src/Save.js b/src/Save.js index 8178139..06829d0 100644 --- a/src/Save.js +++ b/src/Save.js @@ -1,15 +1,5 @@ 'use strict'; -let $ = window.cwrcQuery -if ($ === undefined) { - let prevJQuery = window.jQuery - $ = require('jquery') - window.jQuery = $ - require('bootstrap') - window.jQuery = prevJQuery - window.cwrcQuery = $ -} - import React, {Fragment, Component} from 'react' import { Modal, Grid, Row, Col, Button, Label, Form, FormControl, FormGroup, ControlLabel } from 'react-bootstrap'; diff --git a/src/authenticate.js b/src/authenticate.js index 4c772ea..9ee6239 100644 --- a/src/authenticate.js +++ b/src/authenticate.js @@ -1,15 +1,5 @@ 'use strict'; -let $ = window.cwrcQuery -if ($ === undefined) { - let prevJQuery = window.jQuery - $ = require('jquery') - window.jQuery = $ - require('bootstrap') - window.jQuery = prevJQuery - window.cwrcQuery = $ -} - let Cookies = require('js-cookie'); import cwrcGit from './GitServerClient.js'; @@ -22,23 +12,17 @@ function isAuthenticated() { } function getUserInfo() { - let dfd = $.Deferred(); - - cwrcGit.getInfoForAuthenticatedUser() + return cwrcGit.getInfoForAuthenticatedUser() .then((info) => { let user = { userUrl: info.html_url, userName: info.name, userId: info.login } - console.log('got user info', user); - dfd.resolve(user); + return Promise.resolve(user); }, (error) => { - console.warn('cwrc-git-dialogs error:', error) - dfd.reject(error); + return Promise.reject(error); }); - - return dfd.promise(); } class AuthenticateDialog extends Component { diff --git a/src/index.js b/src/index.js index a47bcca..d7f5f49 100644 --- a/src/index.js +++ b/src/index.js @@ -1,16 +1,5 @@ 'use strict'; -// shared instance of bootstraped jquery for entity and git dialogs -let $ = window.cwrcQuery -if ($ === undefined) { - let prevJQuery = window.jQuery - $ = require('jquery') - window.jQuery = $ - require('bootstrap') - window.jQuery = prevJQuery - window.cwrcQuery = $ -} - import React, {Component} from 'react' import ReactDOM from 'react-dom'; import { Modal, Button, Label } from 'react-bootstrap'; @@ -71,7 +60,7 @@ function saveWrap(writer) { document.querySelector('#'+renderId) ); dialogInstance.setState({show: true}); - $('#'+dialogId).addClass('cwrc'); + document.querySelector('#'+dialogId).classList.add('cwrc'); } function loadWrap(writer, shouldOverwrite = false) { @@ -92,7 +81,7 @@ function loadWrap(writer, shouldOverwrite = false) { document.querySelector('#'+renderId) ); dialogInstance.setState({show: true, confirmLoad: true}); - $('#'+dialogId).addClass('cwrc'); + document.querySelector('#'+dialogId).classList.add('cwrc'); } function logOutWrap() { @@ -101,7 +90,7 @@ function logOutWrap() { document.querySelector('#'+renderId) ); dialogInstance.setState({show: true,}); - $('#'+dialogId).addClass('cwrc'); + document.querySelector('#'+dialogId).classList.add('cwrc'); } function getUserInfo() { @@ -193,13 +182,13 @@ class GitDialog extends Component { cwrcGit.setServerURL(serverURL); cwrcGit.useGitLab(isGitLab); return cwrcGit.getDoc(repo, 'master', path) - .done((result)=>{ + .then((result)=>{ setDocumentInfo(repo, path); this.handleClose(); setTimeout(()=>{ _writer.setDocument(result.doc); }, 50) - }).fail((error)=>{ + },(error)=>{ setDocumentInfo(undefined, undefined); this.setState({error: `There was an error loading the document from: ${repo}${path}`}); });