Skip to content

Commit

Permalink
Merge pull request #701 from materialsproject/add-structure-and-attac…
Browse files Browse the repository at this point in the history
…hments-fields-react

add structure and attachments fields (react)
  • Loading branch information
yang-ruoxi authored Nov 21, 2023
2 parents 5f4d805 + d33f240 commit 5e1a4e5
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,14 @@ export const SearchUIContextProvider: React.FC<SearchState> = ({
return initFilterGroups(props.filterGroups);
}, []);
const columns = useMemo(() => {
return initColumns(props.columns, props.disableRichColumnHeaders);
return initColumns(props.columns, props.apiEndpoint, props.disableRichColumnHeaders);
}, []);
const [state, setState] = useState<SearchState>({
...propsWithoutChildren,
filterGroups,
columns
});

/**
* These default param values are added to the API query if no value is present
* for that param. These params are omitted from the URL if they are equal to the
Expand Down
3 changes: 2 additions & 1 deletion src/components/data-display/SearchUI/types.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ export enum ColumnFormat {
RADIO = 'RADIO',
EMAIL = 'EMAIL',
TAG = 'TAG',
DICT = 'DICT'
DICT = 'DICT',
CONTRIBS_FILES_DOWNLOAD = 'CONTRIBS_FILES_DOWNLOAD'
}

/**
Expand Down
51 changes: 49 additions & 2 deletions src/utils/table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { spaceGroups } from '../constants/spaceGroups';
import { joinUrl } from './navigation';
import { FaEnvelope } from 'react-icons/fa';
import { FaDownload } from 'react-icons/fa';
import axios from 'axios';

const emptyCellPlaceholder = '-';

Expand Down Expand Up @@ -48,7 +49,11 @@ export const getRowValueFromSelectorString = (selector: string, row: any) => {
* FIXED_DECIMAL and SIGNIFICANT_FIGURES both expect another column property "formatArg"
* that will specify how many decimals or figures to apply to the format.
*/
export const initColumns = (columns: Column[], disableRichColumnHeaders?: boolean): Column[] => {
export const initColumns = (
columns: Column[],
apiEndpoint: string,
disableRichColumnHeaders?: boolean
): Column[] => {
return columns.map((c) => {
/** Make columns sortable by default */
c.sortable = c.sortable !== undefined ? c.sortable : true;
Expand Down Expand Up @@ -281,7 +286,6 @@ export const initColumns = (columns: Column[], disableRichColumnHeaders?: boolea
);
};
return c;

case ColumnFormat.DICT:
c.cell = (row: any) => {
const rowValue = getRowValueFromSelectorString(c.selector, row);
Expand Down Expand Up @@ -322,6 +326,49 @@ export const initColumns = (columns: Column[], disableRichColumnHeaders?: boolea
}
};
return c;
case ColumnFormat.CONTRIBS_FILES_DOWNLOAD:
c.cell = (row: any) => {
if (hasFormatOptions && c.formatOptions) {
if (c.formatOptions === 'structures' && row.structures) {
let out: any[] = [];
for (let sitem of row.structures) {
out.push(
<span key={Math.random()}>
<span className="tag">{sitem.name}</span>
<a
href={
apiEndpoint.replace('contribs-api', 'contribs') + 'component/' + sitem.id
}
>
<FaDownload className="mr-1" />
</a>
</span>
);
}
return <span>{out}</span>;
} else if (c.formatOptions === 'attachments' && row.attachments) {
let out: any[] = [];
for (let aitem of row.attachments) {
out.push(
<span key={Math.random()}>
<span className="tag">{aitem.name}</span>
<a
href={
apiEndpoint.replace('contribs-api', 'contribs') + 'component/' + aitem.id
}
>
<FaDownload className="mr-1" />
</a>
</span>
);
}
return <span>{out}</span>;
} else {
return null;
}
} else return null;
};
return c;
default:
c.format = (row: any) => {
const rowValue = getRowValueFromSelectorString(c.selector, row);
Expand Down

0 comments on commit 5e1a4e5

Please sign in to comment.