Skip to content

Commit

Permalink
fix: remove jquery from dialogs
Browse files Browse the repository at this point in the history
still need to get GitServerClient working without jquery
  • Loading branch information
ajmacdonald committed Jul 17, 2019
1 parent da186ca commit 794c5ee
Show file tree
Hide file tree
Showing 5 changed files with 240 additions and 73 deletions.
220 changes: 220 additions & 0 deletions src/GitServerClient_no_jquery.js
Original file line number Diff line number Diff line change
@@ -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
}


40 changes: 12 additions & 28 deletions src/Load.js
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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) {
Expand Down
10 changes: 0 additions & 10 deletions src/Save.js
Original file line number Diff line number Diff line change
@@ -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';

Expand Down
22 changes: 3 additions & 19 deletions src/authenticate.js
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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 {
Expand Down
Loading

0 comments on commit 794c5ee

Please sign in to comment.