Skip to content
This repository has been archived by the owner on Aug 19, 2022. It is now read-only.

Commit

Permalink
feat(#69): be able to rename dirs
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefanie S committed Jun 13, 2020
1 parent 0bc6952 commit a10f442
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 23 deletions.
4 changes: 2 additions & 2 deletions server/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,5 @@
api.add_resource(GitMove, '/git/mv')

if __name__ == '__main__':
# app.run(port=port, debug=True) # FIXME
serve(app, listen= host + ":" + port)
app.run(port=port, debug=True) # FIXME
# serve(app, listen= host + ":" + port)
13 changes: 8 additions & 5 deletions server/gitMv.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,17 @@ def get(self):
abort(501, 'not implemented yet')

def put(self):
#TODO BE ABLE TO HANDLE DIRS
doc_name = request.form['doc_name']
doc_name = None
new_name = None
doc_path = request.form['doc_path']
new_name = request.form['new_doc_name']
new_doc_path = request.form['new_doc_path']
project_dir = request.form['project_dir']
msg = request.form['msg']
self.logger.logDebug('params', doc_name, doc_path, new_name, new_doc_path, msg)
try:
doc_name = request.form['doc_name']
new_name = request.form['new_doc_name']
except:
pass

if doc_name and not new_name:
abort(400, 'Got no new name, cannot move')
Expand All @@ -40,7 +43,7 @@ def put(self):

if doc_path == new_doc_path and doc_name == new_name:
self.logger.logDebug('path and name are identical to new path and name, return 400')
abort(400, 'New path and name are identical to existing path and name. Do nothing.')
abort(400, 'New path and name are identical to existing path and name.')

if doc_path == new_doc_path and doc_name.lower() == new_name.lower():
self.logger.logInfo('Rename is only changing case sensitivity. Doing extra commit to prevent trouble.', doc_name, '-->', new_name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import { RenameItemDialogComponent } from './../../renameItemDialog/renameItemDi
import { DocumentService } from 'src/app/services/document.service';
import { DirectoryStore } from './../../../stores/directory.store';
import { DocumentStore } from '../../../stores/document.store';
import { Subscription } from 'rxjs';
import { Subscription, of } from 'rxjs';
import { Component, OnInit, OnDestroy, Input, EventEmitter, Output } from '@angular/core';
import { FlatTreeControl } from '@angular/cdk/tree';
import { MatTreeFlatDataSource, MatTreeFlattener } from '@angular/material/tree';
import { MatDialog } from '@angular/material/dialog';
import { flatMap } from 'rxjs/operators';
import { flatMap, filter } from 'rxjs/operators';

interface ExplorerNode {
expandable: boolean;
Expand Down Expand Up @@ -87,24 +87,32 @@ export class DocumentTreeComponent implements OnInit, OnDestroy {
}

renameDir(node) {
// todo
console.warn('rename dir not implemented yet', node);
this.renameItem(node, 'dir');
}

renameFile(node: ExplorerNode) {
this.renameItem(node, 'file');
}

renameItem(node, typeOfItem: 'dir' | 'file') {
const dialogRef = this.dialog.open(RenameItemDialogComponent, {
data: { oldName: this.stripFileEndingPipe.transform(node.name) },
});

this.subscription.add(
dialogRef.afterClosed().subscribe(newName => {
if (!newName) return;
this.documentService
.moveDocument(node.path, node.name, newName)
.pipe(
flatMap(_ => this.directoryService.getTree())
)
.subscribe();
})
dialogRef
.afterClosed()
.pipe(
filter(res => res),
flatMap(newName => {
let moveObs = of({});
if (typeOfItem === 'dir') moveObs = this.directoryService.moveDirectory(node.path, node.name, newName);
if (typeOfItem === 'file') moveObs = this.documentService.moveDocument(node.path, node.name, newName);
return moveObs;
}),
flatMap(_ => this.directoryService.getTree())
)
.subscribe()
);
}

Expand Down
44 changes: 41 additions & 3 deletions src/app/services/directory.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { LinkService } from 'src/app/services/link.service';
// Copyright (c) 2020 s-blu
//
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
Expand All @@ -11,7 +12,8 @@ import { HttpClient, HttpHeaders } from '@angular/common/http';
import { ApiService } from './api.service';
import { Injectable, OnDestroy } from '@angular/core';
import { sanitizeName } from '../utils/name.util';
import { Subscription } from 'rxjs';
import { Subscription, of } from 'rxjs';
import { translate } from '@ngneat/transloco';

@Injectable({
providedIn: 'root',
Expand All @@ -24,8 +26,10 @@ export class DirectoryService implements OnDestroy {
private api: ApiService,
private httpClient: HttpClient,
private projectStore: ProjectStore,
private directoryStore: DirectoryStore
private directoryStore: DirectoryStore,
private linkService: LinkService
) {}

ngOnDestroy() {
this.subscription.unsubscribe();
}
Expand All @@ -43,6 +47,40 @@ export class DirectoryService implements OnDestroy {
.pipe(catchError(err => this.api.handleHttpError(err)));
}

public moveDirectory(path: string, name: string, newName: string, movedPath?: string) {
console.log('moveDir', path, name, newName);
if (!newName) {
console.error('moveDirectory got called without a new name. do nothing.');
return;
}
newName = sanitizeName(newName);
const oldPath = path + '/' + name;
const newPath = movedPath ? `${movedPath}/${newName}` : `${path}/${newName}`;

let msg;
if (movedPath) {
msg = translate('git.message.move', { name: oldPath, newName: newPath });
} else {
msg = translate('git.message.rename', { oldName: oldPath, newName: newPath });
}

const formdata = new FormData();
formdata.append('doc_path', oldPath);
formdata.append('new_doc_path', newPath);
formdata.append('msg', msg);

const httpHeaders = new HttpHeaders();
httpHeaders.append('Content-Type', 'multipart/form-data');
return this.projectStore.project$.pipe(
flatMap(project => {
formdata.append('project_dir', project);
return this.linkService.moveLinkDestinations(project, oldPath, newPath);
}),
flatMap(_ => this.httpClient.put(this.api.getGitMoveRoute(), formdata, { headers: httpHeaders })),
catchError(err => this.api.handleHttpError(err))
);
}

public getTree(params?) {
const parameter: any = {
params: {
Expand Down
19 changes: 19 additions & 0 deletions src/app/services/link.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,25 @@ export class LinkService {
return this.getLinksForProject(project, findLinkWithId);
}

moveLinkDestinations(project: string, oldPath: string, newPath: string) {
console.log('moveLinkDestinations', project, oldPath, newPath)
if (!project || !oldPath || !newPath) {
console.error('linkService -> moveLinkDestinations was called with invalid data. Aborting.');
return;
}

const replaceNameAndPathForLinks = links => {
const affectedLinks = links.filter(l => l.path.startsWith(oldPath));

for (const link of affectedLinks) {
link.path = link.path.replace(oldPath, newPath);
}
return this.saveLinksToServer(project, links);
};

return this.getLinksForProject(project, replaceNameAndPathForLinks);
}

moveLinkDestination(project: string, oldName: string, oldPath: string, newName: string, newPath: string) {
if (!project || !oldName || !oldPath) {
console.error('linkService -> moveLinkDestination was called with invalid data. Aborting.');
Expand Down

0 comments on commit a10f442

Please sign in to comment.