Skip to content

Commit

Permalink
wiki add wiki2
Browse files Browse the repository at this point in the history
  • Loading branch information
JoinTyang committed May 14, 2024
1 parent 690929d commit 15b63d5
Show file tree
Hide file tree
Showing 17 changed files with 743 additions and 266 deletions.
2 changes: 0 additions & 2 deletions frontend/src/components/wiki-list-view/wiki-list-item.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,7 @@ class WikiListItem extends Component {
let userProfileURL = `${siteRoot}profile/${encodeURIComponent(wiki.owner)}/`;
let fileIconUrl = Utils.getDefaultLibIconUrl(false);
let isOldVersion = wiki.version !== 'v2';
// let publishedUrl = `${siteRoot}published/${encodeURIComponent(wiki.slug)}/`;
let publishedUrl = `${siteRoot}published/${encodeURIComponent(wiki.slug)}/`;
// let editUrl = `${siteRoot}edit-wiki/${encodeURIComponent(wiki.slug)}/`;
let editUrl = `${siteRoot}edit-wiki/${wiki.id}/`;

const desktopItem = (
Expand Down
20 changes: 10 additions & 10 deletions frontend/src/pages/wiki2/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class Wiki extends Component {
};

getWikiConfig = () => {
wikiAPI.getWikiConfig(wikiId).then(res => {
wikiAPI.getWiki2Config(wikiId).then(res => {
const { wiki_config, repo_id } = res.data.wiki;
this.setState({
config: new WikiConfig(JSON.parse(wiki_config) || {}),
Expand All @@ -95,7 +95,7 @@ class Wiki extends Component {
};

saveWikiConfig = (wikiConfig, onSuccess, onError) => {
wikiAPI.updateWikiConfig(wikiId, JSON.stringify(wikiConfig)).then(res => {
wikiAPI.updateWiki2Config(wikiId, JSON.stringify(wikiConfig)).then(res => {
this.setState({
config: new WikiConfig(wikiConfig || {}),
});
Expand Down Expand Up @@ -152,11 +152,11 @@ class Wiki extends Component {
};

loadIndexNode = () => {
wikiAPI.listWikiDir(wikiId, '/').then(res => {
wikiAPI.listWiki2Dir(wikiId, '/').then(res => {
let tree = this.state.treeData;
this.addFirstResponseListToNode(res.data.dirent_list, tree.root);
let indexNode = tree.getNodeByPath(this.indexPath);
wikiAPI.getWikiFileContent(wikiId, indexNode.path).then(res => {
wikiAPI.getWiki2FileContent(wikiId, indexNode.path).then(res => {
this.setState({
treeData: tree,
indexNode: indexNode,
Expand Down Expand Up @@ -186,7 +186,7 @@ class Wiki extends Component {
});

this.removePythonWrapper();
wikiAPI.getWikiFileContent(wikiId, filePath).then(res => {
wikiAPI.getWiki2FileContent(wikiId, filePath).then(res => {
let data = res.data;
this.setState({
isDataLoading: false,
Expand All @@ -211,7 +211,7 @@ class Wiki extends Component {

loadDirentList = (dirPath) => {
this.setState({isDataLoading: true});
wikiAPI.listWikiDir(wikiId, dirPath).then(res => {
wikiAPI.listWiki2Dir(wikiId, dirPath).then(res => {
let direntList = res.data.dirent_list.map(item => {
let dirent = new Dirent(item);
return dirent;
Expand Down Expand Up @@ -241,7 +241,7 @@ class Wiki extends Component {
let tree = this.state.treeData.clone();
let node = tree.getNodeByPath(path);
if (!node.isLoaded) {
wikiAPI.listWikiDir(wikiId, node.path).then(res => {
wikiAPI.listWiki2Dir(wikiId, node.path).then(res => {
this.addResponseListToNode(res.data.dirent_list, node);
let parentNode = tree.getNodeByPath(node.parentNode.path);
parentNode.isExpanded = true;
Expand All @@ -262,7 +262,7 @@ class Wiki extends Component {
if (Utils.isMarkdownFile(path)) {
path = Utils.getDirName(path);
}
wikiAPI.listWikiDir(wikiId, path, true).then(res => {
wikiAPI.listWiki2Dir(wikiId, path, true).then(res => {
let direntList = res.data.dirent_list;
let results = {};
for (let i = 0; i < direntList.length; i++) {
Expand Down Expand Up @@ -425,7 +425,7 @@ class Wiki extends Component {
if (!node.isLoaded) {
let tree = this.state.treeData.clone();
node = tree.getNodeByPath(node.path);
wikiAPI.listWikiDir(wikiId, node.path).then(res => {
wikiAPI.listWiki2Dir(wikiId, node.path).then(res => {
this.addResponseListToNode(res.data.dirent_list, node);
tree.collapseNode(node);
this.setState({treeData: tree});
Expand Down Expand Up @@ -473,7 +473,7 @@ class Wiki extends Component {
let tree = this.state.treeData.clone();
node = tree.getNodeByPath(node.path);
if (!node.isLoaded) {
wikiAPI.listWikiDir(wikiId, node.path).then(res => {
wikiAPI.listWiki2Dir(wikiId, node.path).then(res => {
this.addResponseListToNode(res.data.dirent_list, node);
this.setState({treeData: tree});
});
Expand Down
6 changes: 3 additions & 3 deletions frontend/src/pages/wiki2/side-panel.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import deepCopy from 'deep-copy';
import { gettext, siteRoot, repoID, slug, username, permission, isEditWiki } from '../../utils/constants';
import { gettext, siteRoot, repoID, username, permission, isEditWiki } from '../../utils/constants';
import toaster from '../../components/toast';
import Loading from '../../components/loading';
// import TreeView from '../../components/tree-view/tree-view';
Expand Down Expand Up @@ -390,13 +390,13 @@ class SidePanel extends Component {
<div className={`side-panel wiki-side-panel ${this.props.closeSideBar ? '': 'left-zero'}`}>
<div className="side-panel-top panel-top">
{src && <img src={src} width="32" height="32" alt='' className='mr-2' />}
<h4 className="ml-0 mb-0">{wiki_name || slug}</h4>
<h4 className="ml-0 mb-0">{wiki_name}</h4>
</div>
<div id="side-nav" className="wiki-side-nav" role="navigation">
{this.renderContent() }
{(username && permission) && (
<div className="text-left p-2">
<a href={siteRoot + 'library/' + repoID + '/' + slug + '/'} className="text-dark text-decoration-underline">
<a href={siteRoot + 'library/' + repoID + '/'} className="text-dark text-decoration-underline">
{gettext('Go to Library')}
</a>
</div>
Expand Down
62 changes: 49 additions & 13 deletions frontend/src/pages/wikis/wikis.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,30 @@ class Wikis extends Component {
}

getWikis = () => {
let wikis = [];
wikiAPI.listWikis().then(res => {
wikis = wikis.concat(res.data.data);
wikis.map(wiki => {
return wiki['version'] = 'v1';
});
wikiAPI.listWikis2().then(res => {
let wikis2 = res.data.data;
wikis2.map(wiki => {
return wiki['version'] = 'v2';
});
this.setState({
loading: false,
wikis: wikis.concat(wikis2)
});
}).catch((error) => {
this.setState({
loading: false,
errorMsg: Utils.getErrorMsg(error, true) // true: show login tip if 403
});
});
this.setState({
loading: false,
wikis: res.data.data
wikis: wikis
});
}).catch((error) => {
this.setState({
Expand All @@ -61,9 +81,11 @@ class Wikis extends Component {
};

addWiki = (wikiName) => {
wikiAPI.addWiki(wikiName).then((res) => {
wikiAPI.addWiki2(wikiName).then((res) => {
let wikis = this.state.wikis.slice(0);
wikis.push(res.data);
let new_wiki = res.data;
new_wiki['version'] = 'v2';
wikis.push(new_wiki);
this.setState({ wikis });
}).catch((error) => {
if (error.response) {
Expand All @@ -74,17 +96,31 @@ class Wikis extends Component {
};

deleteWiki = (wiki) => {
wikiAPI.deleteWiki(wiki.id).then(() => {
let wikis = this.state.wikis.filter(item => {
return item.name !== wiki.name;
if (wiki.version === 'v1') {
wikiAPI.deleteWiki(wiki.id).then(() => {
let wikis = this.state.wikis.filter(item => {
return item.name !== wiki.name;
});
this.setState({wikis: wikis});
}).catch((error) => {
if(error.response) {
let errorMsg = error.response.data.error_msg;
toaster.danger(errorMsg);
}
});
this.setState({wikis: wikis});
}).catch((error) => {
if(error.response) {
let errorMsg = error.response.data.error_msg;
toaster.danger(errorMsg);
}
});
} else {
wikiAPI.deleteWiki2(wiki.id).then(() => {
let wikis = this.state.wikis.filter(item => {
return item.name !== wiki.name;
});
this.setState({wikis: wikis});
}).catch((error) => {
if(error.response) {
let errorMsg = error.response.data.error_msg;
toaster.danger(errorMsg);
}
});
}
};

render() {
Expand Down
73 changes: 67 additions & 6 deletions frontend/src/utils/wiki-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ class WikiAPI {
}


getWikiFileContent(slug, filePath) {
getWikiFileContent(wikiId, filePath) {
const path = encodeURIComponent(filePath);
const time = new Date().getTime();
const url = this.server + '/api/v2.1/wikis/' + encodeURIComponent(slug) + '/content/' + '?p=' + path + '&_=' + time;
const url = this.server + '/api/v2.1/wikis/' + wikiId + '/content/' + '?p=' + path + '&_=' + time;
return this.req.get(url);
}

Expand Down Expand Up @@ -104,16 +104,77 @@ class WikiAPI {
return this.req.delete(url);
}

updateWikiConfig(wikiId, wikiConfig) {
const url = this.server + '/api/v2.1/wiki-config/' + wikiId + '/';

// for wiki2
listWiki2Dir(wikiId, dirPath, withParents) {
const path = encodeURIComponent(dirPath);
let url = this.server + '/api/v2.1/wikis2/' + wikiId + '/dir/?p=' + path;
if (withParents) {
url = this.server + '/api/v2.1/wikis2/' + wikiId + '/dir/?p=' + path + '&with_parents=' + withParents;
}
return this.req.get(url);
}


getWiki2FileContent(wikiId, filePath) {
const path = encodeURIComponent(filePath);
const time = new Date().getTime();
const url = this.server + '/api/v2.1/wikis2/' + wikiId + '/content/' + '?p=' + path + '&_=' + time;
return this.req.get(url);
}


listWikis2(options) {
/*
* options: `{type: 'shared'}`, `{type: ['mine', 'shared', ...]}`
*/
let url = this.server + '/api/v2.1/wikis2/';
if (!options) {
// fetch all types of wikis
return this.req.get(url);
}
return this.req.get(url, {
params: options,
paramsSerializer: {
serialize: function(params) {
let list = [];
for (let key in params) {
if (Array.isArray(params[key])) {
for (let i = 0, len = params[key].length; i < len; i++) {
list.push(key + '=' + encodeURIComponent(params[key][i]));
}
} else {
list.push(key + '=' + encodeURIComponent(params[key]));
}
}
return list.join('&');
}
}
});
}

addWiki2(wikiName) {
const url = this.server + '/api/v2.1/wikis2/';
let form = new FormData();
form.append('name', wikiName);
return this._sendPostRequest(url, form);
}

deleteWiki2(wikiId) {
const url = this.server + '/api/v2.1/wikis2/' + wikiId + '/';
return this.req.delete(url);
}

updateWiki2Config(wikiId, wikiConfig) {
const url = this.server + '/api/v2.1/wiki2-config/' + wikiId + '/';
let params = {
wiki_config: wikiConfig
};
return this.req.put(url, params);
}

getWikiConfig(wikiId) {
const url = this.server + '/api/v2.1/wiki-config/' + wikiId + '/';
getWiki2Config(wikiId) {
const url = this.server + '/api/v2.1/wiki2-config/' + wikiId + '/';
return this.req.get(url);
}

Expand Down
Loading

0 comments on commit 15b63d5

Please sign in to comment.