Skip to content
Snippets Groups Projects
Commit 31533d77 authored by Wilson Chua's avatar Wilson Chua :rice_ball:
Browse files

ModuleResources: Connect folder download endpoints

parent 81d7bded
No related branches found
No related tags found
No related merge requests found
import React from "react"; import React from "react";
import { Folder } from "../index";
import SelectionView, { import SelectionView, {
SelectionProps, SelectionProps,
} from "components/molecules/SelectionView"; } from "components/molecules/SelectionView";
...@@ -6,18 +7,17 @@ import FoldersRow from "components/molecules/FoldersRow"; ...@@ -6,18 +7,17 @@ import FoldersRow from "components/molecules/FoldersRow";
import { useHistory, useLocation } from "react-router-dom"; import { useHistory, useLocation } from "react-router-dom";
export interface FoldersViewProps { export interface FoldersViewProps {
folders: { folders: Folder[];
title: string;
id: number;
}[];
scope: string; scope: string;
searchText: string; searchText: string;
handleFolderDownload: (ids: number[]) => void;
} }
const FoldersView: React.FC<FoldersViewProps> = ({ const FoldersView: React.FC<FoldersViewProps> = ({
folders, folders,
scope, scope,
searchText, searchText,
handleFolderDownload
}) => { }) => {
let history = useHistory(); let history = useHistory();
let location = useLocation(); let location = useLocation();
...@@ -31,7 +31,7 @@ const FoldersView: React.FC<FoldersViewProps> = ({ ...@@ -31,7 +31,7 @@ const FoldersView: React.FC<FoldersViewProps> = ({
return ( return (
<SelectionView <SelectionView
heading="Folders" heading="Folders"
onDownloadClick={() => {}} onDownloadClick={handleFolderDownload}
onItemClick={handleFolderClick} onItemClick={handleFolderClick}
selectionItems={folders} selectionItems={folders}
render={(select: SelectionProps) => <FoldersRow select={select} />} render={(select: SelectionProps) => <FoldersRow select={select} />}
......
import React from "react"; import React from "react";
import { Resource } from "../index"; import { Resource, Folder } from "../index";
import SelectionView, { import SelectionView, {
SelectionProps, SelectionProps,
} from "components/molecules/SelectionView"; } from "components/molecules/SelectionView";
...@@ -7,10 +7,7 @@ import CategoryList from "components/molecules/CategoryList"; ...@@ -7,10 +7,7 @@ import CategoryList from "components/molecules/CategoryList";
import CategoryHeader from "components/molecules/CategoryHeader"; import CategoryHeader from "components/molecules/CategoryHeader";
export interface ListViewProps { export interface ListViewProps {
folders: { folders: Folder[];
title: string;
id: number;
}[];
resources: Resource[]; resources: Resource[];
searchText: string; searchText: string;
onDownloadClick: (identifiers: number[]) => void; onDownloadClick: (identifiers: number[]) => void;
......
...@@ -16,6 +16,11 @@ export interface Resource { ...@@ -16,6 +16,11 @@ export interface Resource {
id: number; id: number;
} }
export interface Folder {
title: string;
id: number;
}
export interface ResourcesProps { export interface ResourcesProps {
year: string; year: string;
moduleID: string; moduleID: string;
...@@ -77,6 +82,13 @@ class ModuleResources extends React.Component<ResourcesProps, ResourceState> { ...@@ -77,6 +82,13 @@ class ModuleResources extends React.Component<ResourcesProps, ResourceState> {
}); });
} }
// Gets the unique categories/folders that have been assigned for the resources
folders(): Folder[] {
return Array.from(new Set<string>(
this.state.resources.map((res: Resource) => res.folder))
).map((title, id) => ({ title: title, id: id }));
}
handleFileDownload(indices: number[]) { handleFileDownload(indices: number[]) {
if (indices.length === 1) { if (indices.length === 1) {
// Only one file to download, call single file endpoint // Only one file to download, call single file endpoint
...@@ -94,6 +106,20 @@ class ModuleResources extends React.Component<ResourcesProps, ResourceState> { ...@@ -94,6 +106,20 @@ class ModuleResources extends React.Component<ResourcesProps, ResourceState> {
} }
} }
handleFolderDownload(ids: number[]) {
let categories = this.folders().filter(folder => folder.id in ids)
.map(folder => folder.title);
if (categories.length === 1) {
this.handleSectionDownload(categories[0]);
} else {
// No endpoint for multiple category download, reuse zipped selection instead
let resourceIds = this.state.resources
.filter(resource => resource.folder in categories)
.map(resource => resource.id);
this.handleFileDownload(resourceIds);
}
}
handleSectionDownload(category: string) { handleSectionDownload(category: string) {
download(api.MATERIALS_ZIPPED, methods.GET, category + ".zip", { download(api.MATERIALS_ZIPPED, methods.GET, category + ".zip", {
year: this.props.year, year: this.props.year,
...@@ -176,58 +202,48 @@ class ModuleResources extends React.Component<ResourcesProps, ResourceState> { ...@@ -176,58 +202,48 @@ class ModuleResources extends React.Component<ResourcesProps, ResourceState> {
render() { render() {
let scope = this.props.scope || ""; let scope = this.props.scope || "";
let folders: { title: string; id: number }[] = Array.from(
new Set<string>(this.state.resources.map((res: Resource) => res.folder))
).map((title: string, id: number) => ({
title: title,
id: id,
}));
const view = () => { const view = () => {
switch (this.state.view) { switch(this.state.view) {
case "folder": case "folder": return (
return ( <>
<> <FoldersView
<FoldersView folders={this.folders()}
folders={folders} scope={scope}
scope={scope} searchText={this.state.searchText}
searchText={this.state.searchText} handleFolderDownload={(ids) => this.handleFolderDownload(ids)}
/> />
<CurrentDirectoryView <CurrentDirectoryView
resources={this.state.resources} resources={this.state.resources}
scope={scope} scope={scope}
searchText={this.state.searchText} searchText={this.state.searchText}
onDownloadClick={(ids) => this.handleFileDownload(ids)} onDownloadClick={(ids) => this.handleFileDownload(ids)}
onItemClick={(id) => this.handleFileClick(id)} onItemClick={(id) => this.handleFileClick(id)}
includeInSearchResult={this.includeInSearchResult} includeInSearchResult={this.includeInSearchResult}
/> />
<QuickAccessView <QuickAccessView
resources={this.state.resources} resources={this.state.resources}
scope={scope} scope={scope}
searchText={this.state.searchText} searchText={this.state.searchText}
onDownloadClick={(ids) => this.handleFileDownload(ids)} onDownloadClick={(ids) => this.handleFileDownload(ids)}
onItemClick={(id) => this.handleFileClick(id)} onItemClick={(id) => this.handleFileClick(id)}
/> />
</> </>
); );
case "list": case "list": return (
return ( <>
<> <ListView
<ListView folders={this.folders()}
folders={folders} resources={this.state.resources}
resources={this.state.resources} searchText={this.state.searchText}
searchText={this.state.searchText} onDownloadClick={(ids) => this.handleFileDownload(ids)}
onDownloadClick={(ids) => this.handleFileDownload(ids)} onSectionDownloadClick={(category) => this.handleSectionDownload(category)}
onSectionDownloadClick={(category) => onItemClick={(id) => this.handleFileClick(id)}
this.handleSectionDownload(category) includeInSearchResult={this.includeInSearchResult}
} />
onItemClick={(id) => this.handleFileClick(id)} </>
includeInSearchResult={this.includeInSearchResult} );
/>
</>
);
} }
}; };
return ( return (
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment