Skip to content
This repository was archived by the owner on Apr 5, 2024. It is now read-only.

Commit 9193a78

Browse files
qvalentinGimleux
andauthored
Feature/sort file view (#28) (#29)
* Added Sort function for File View * Connected SortFiles logic to UI * Fixed warning * Fixed some old code smells * Very important feature Co-authored-by: Gimleux <[email protected]>
1 parent 1fc8c87 commit 9193a78

File tree

9 files changed

+94
-16
lines changed

9 files changed

+94
-16
lines changed

webapp_frontend/src/background/api/auth.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {UserState} from "../redux/actions/userTypes";
77
import store from "../redux/store";
88
import {addAccessToken, addRefreshToken, checkedCookies, removeTokens} from "../redux/actions/tokens";
99
import {addUser} from "../redux/actions/user";
10-
import {AccessToken, RemoveTokens, TokensState} from "../redux/actions/tokenTypes";
10+
import {AccessToken} from "../redux/actions/tokenTypes";
1111
import {deleteCookie, getCookie, setCookie} from "../methods/cookies";
1212

1313

@@ -71,7 +71,7 @@ console.log("[Auth] loginWithUsernameAndPassword")
7171
export const getAccessTokenWithRefreshToken = () => {
7272
console.log("getAccessTokenWithRefreshToken")
7373

74-
let refreshToken: string|null = (store.getState().tokens as TokensState).refreshToken;
74+
let refreshToken: string|null = (store.getState().tokens).refreshToken;
7575

7676
let config = {
7777
headers: {
@@ -92,7 +92,7 @@ export const getAccessTokenWithRefreshToken = () => {
9292

9393
})
9494
.catch(((error) => {
95-
store.dispatch(removeTokens()as RemoveTokens);
95+
store.dispatch(removeTokens());
9696

9797
console.log(error)
9898
//you probably want to notify the user, maybe with a toast or similar
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// `keyof any` is short for "string | number | symbol"
2+
// since an object key can be any of those types, our key can too
3+
// in TS 3.0+, putting just "string" raises an error
4+
// since an object key can be any of those types, our key can too
5+
// in TS 3.0+, putting just "string" raises an error
6+
// https://dev.to/kingdaro/indexing-objects-in-typescript-1cgi
7+
export function hasKey<O>(obj: O, key: keyof any): key is keyof O {
8+
return key in obj
9+
}
File renamed without changes.

webapp_frontend/src/components/App.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ const connector = connect(mapState, mapDispatch)
3535
type PropsFromRedux = ConnectedProps<typeof connector>
3636

3737
// this defines the component props and also adds the redux imported props
38-
type Props = PropsFromRedux & {}
38+
type Props = PropsFromRedux
3939

4040
function App(props: Props): ReactElement {
4141

webapp_frontend/src/components/basicElements/PermanentAssets.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import React, {ReactElement} from "react";
33
export default function PermanentAssets(): ReactElement {
44
return (
55
<div>
6-
<audio id={"audio_viking"} preload={"metadata"}>
6+
<audio id={"audio_viking"} preload={"metadata"} loop>
77
<source src="/assets/audio/2017-04-01_-_Viking_Ship_-_David_Fesliyan.mp3"/>
88
</audio>
99
</div>

webapp_frontend/src/components/pages/filesytem/FileList.tsx

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import FileListFolder from "./FileListFolder";
77
import FileListFile from "./FileListFile";
88
import {FilesBreadcrumb} from "./FilesBreadcrumb";
99
import {filesBaseUrl} from "./Filesystem";
10+
import {sortObjectsInArrayByProperty} from "./sortFilesAndFolders";
1011

1112

1213
type Props = {}
@@ -19,6 +20,8 @@ export default function FileList(props: Props): ReactElement {
1920
const [files, setFiles] = useState<File[] | null>(null)
2021
const [folders, setFolders] = useState<Folder[] | null>(null)
2122
const [error, setError] = useState<string>("");
23+
const [sortedBy, setSortedBy] = useState<keyof File | keyof Folder | null>(null)
24+
const [sortIncreasing, setSortIncreasing] = useState<boolean>(false)
2225

2326

2427
console.log("[FileList path]" + path)
@@ -33,8 +36,8 @@ export default function FileList(props: Props): ReactElement {
3336
setError("")
3437
}
3538
)
36-
.catch(error => {
37-
setError(error.response?.data.message)
39+
.catch(err => {
40+
setError(err.response?.data.message)
3841
setFiles([])
3942
setFolders([])
4043
});
@@ -46,7 +49,27 @@ export default function FileList(props: Props): ReactElement {
4649

4750
}, [path, location]);
4851

49-
52+
function handleSortClick(property: keyof File | keyof Folder) {
53+
if (sortedBy === property) setSortIncreasing(!sortIncreasing);
54+
else {
55+
setSortedBy(property);
56+
setSortIncreasing(true)
57+
}
58+
setFolders(sortObjectsInArrayByProperty(folders, property, sortIncreasing));
59+
setFiles(sortObjectsInArrayByProperty(files, property, sortIncreasing));
60+
}
61+
62+
// console.log("--------------------------------------------------------------------------------------")
63+
// console.log(folders)
64+
// let foldersa = folders ? [...folders] : []
65+
// let filesa = files ? [...files] : []
66+
// let sortedFoldersa = sortObjectsInArrayByProperty(foldersa, "name")
67+
// let sortedFilesa = sortObjectsInArrayByProperty(filesa, "name")
68+
// console.log(sortedFoldersa)
69+
// console.log("---------")
70+
// console.log(filesa)
71+
// console.log()
72+
// console.log("--------------------------------------------------------------------------------------")
5073

5174

5275
return (
@@ -56,13 +79,13 @@ export default function FileList(props: Props): ReactElement {
5679
<Col xs={1}> <Form.Group controlId="formBasicCheckbox">
5780
<Form.Check type="checkbox" onChange={() => console.log(`selected all files` /*TODO*/)}/>
5881
</Form.Group></Col>
59-
<Col xs={1}>{"Type"}</Col>
82+
<Col xs={1} onClick={() => handleSortClick("type")}>{"Type"}</Col>
6083
<Col xs={1}>{}</Col>
6184
<Col xs={1}>{"Share"}</Col>
62-
<Col xs={3}>{"Name"}</Col>
63-
<Col xs={3}>{"Owner"}</Col>
64-
<Col xs={1}>{"Last changes"}</Col>
65-
<Col xs={1}>{"Size"}</Col>
85+
<Col xs={3} onClick={() => handleSortClick("name")}>{"Name"}</Col>
86+
<Col xs={3} onClick={() => handleSortClick("createdByUserId")}>{"Owner"}</Col>
87+
<Col xs={1} onClick={() => handleSortClick("lastUpdated")}>{"Last changes"}</Col>
88+
<Col xs={1} onClick={() => handleSortClick("size")}>{"Size"}</Col>
6689
</Row>
6790
<hr/>
6891
<Row>

webapp_frontend/src/components/pages/filesytem/FileListItem.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@ export interface FileListEntity {
3434
type: string;
3535
isFolder: boolean
3636
path?: string
37-
38-
3937
}
4038

4139
export default function FileListItem(props: Props): ReactElement {

webapp_frontend/src/components/pages/filesytem/FilesBreadcrumb.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export function FilesBreadcrumb(props: Props): ReactElement {
1414
return (
1515
<Breadcrumb>
1616
<Link className={'breadcrumb-item active'} to={filesBaseUrl + '/'} onClick={() => props.setPath("/")}>root </Link>
17-
{props.path.split('/').filter((s: String) => s).map((folder: string, i: number) => {
17+
{props.path.split('/').filter((s: string) => s).map((folder: string, i: number) => {
1818
return (<Link className={'breadcrumb-item active'} to={filesBaseUrl + props.path.split('/').slice(0, i + 2).join("/")}
1919
onClick={() => props.setPath(props.path.split('/').slice(0, i + 2).join("/"))} key={i}>{folder} </Link>)
2020
})}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import {Folder, PermissionSet} from "../../../background/api/filesystemTypes";
2+
3+
function mergeObjectArraysByProperty(leftArray: any, rightArray: any, propertyName: keyof File | keyof Folder, sortIncreasing: boolean): File[] & Folder[] {
4+
let arr: any = []
5+
// Break out of loop if any one of the array gets empty
6+
while (leftArray.length && rightArray.length) {
7+
// Pick the smaller among the smallest element of left and right sub arrays
8+
9+
if (rightArray[0] === undefined || rightArray[0][propertyName] === undefined || !rightArray[0].hasOwnProperty(propertyName)) {
10+
let help: File | Folder | undefined = leftArray.shift();
11+
if (help) arr.push(help)
12+
continue;
13+
} else if (leftArray[0] === undefined || leftArray[0][propertyName] === undefined || !leftArray[0].hasOwnProperty(propertyName)) {
14+
let help: (File | Folder) | undefined = rightArray.shift();
15+
if (help) arr.push(help)
16+
continue;
17+
}
18+
19+
//at this point both elements should have property
20+
let firstLeftElement: string | number | boolean | PermissionSet | undefined = leftArray[0][propertyName];
21+
let firstRightElement: string | number | boolean | PermissionSet | undefined = rightArray[0][propertyName];
22+
if (typeof firstLeftElement === "string") firstLeftElement = firstLeftElement.toLowerCase();
23+
if (typeof firstRightElement === "string") firstRightElement = firstRightElement.toLowerCase();
24+
if (
25+
(firstLeftElement !== undefined && firstRightElement !== undefined && firstLeftElement <= firstRightElement && sortIncreasing)
26+
) {
27+
let help: (File | Folder) | undefined = leftArray.shift();
28+
if (help) arr.push(help)
29+
} else {
30+
let help: (File | Folder) | undefined = rightArray.shift();
31+
if (help) arr.push(help)
32+
}
33+
}
34+
35+
// Concatenating leftover elements
36+
return [...arr, ...leftArray, ...rightArray]
37+
}
38+
39+
export function sortObjectsInArrayByProperty(originalArray: any, propertyName: keyof File | keyof Folder, sortIncreasing: boolean): any {
40+
const array = [...originalArray]
41+
if (!array || array.length <= 1) return array ?? [];
42+
43+
const half = array.length / 2
44+
const left = array.splice(0, half)
45+
46+
47+
return mergeObjectArraysByProperty(sortObjectsInArrayByProperty(left, propertyName, sortIncreasing), sortObjectsInArrayByProperty(array, propertyName, sortIncreasing), propertyName, sortIncreasing);
48+
}

0 commit comments

Comments
 (0)