Improve history types, handling unknown category
This commit is contained in:
parent
70fa8725b4
commit
5e3ce365d5
@ -3,7 +3,7 @@ import { Link } from 'react-router-dom';
|
||||
|
||||
import './building-edit-summary.css';
|
||||
|
||||
import { dataFields } from '../../data_fields';
|
||||
import { Category, DataFieldDefinition, dataFields } from '../../data_fields';
|
||||
import { arrayToDictionary, parseDate } from '../../helpers';
|
||||
import { EditHistoryEntry } from '../../models/edit-history-entry';
|
||||
|
||||
@ -26,22 +26,26 @@ function formatDate(dt: Date) {
|
||||
});
|
||||
}
|
||||
|
||||
function enrichHistoryEntries(forwardPatch: object, reversePatch: object) {
|
||||
return Object
|
||||
.entries(forwardPatch)
|
||||
.map(([key, value]) => {
|
||||
const info = dataFields[key] as DataFieldDefinition;
|
||||
return {
|
||||
title: info.title || `Unknown field (${key})`,
|
||||
category: info.category || Category.Unknown,
|
||||
value: value,
|
||||
oldValue: reversePatch && reversePatch[key]
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
const BuildingEditSummary: React.FunctionComponent<BuildingEditSummaryProps> = ({
|
||||
historyEntry,
|
||||
showBuildingId = false,
|
||||
hyperlinkCategories = false
|
||||
}) => {
|
||||
const entriesWithMetadata = Object
|
||||
.entries(historyEntry.forward_patch)
|
||||
.map(([key, value]) => {
|
||||
const info = dataFields[key] || {};
|
||||
return {
|
||||
title: info.title || `Unknown field (${key})`,
|
||||
category: info.category || 'Unknown',
|
||||
value: value,
|
||||
oldValue: historyEntry.reverse_patch && historyEntry.reverse_patch[key]
|
||||
};
|
||||
});
|
||||
const entriesWithMetadata = enrichHistoryEntries(historyEntry.forward_patch, historyEntry.reverse_patch);
|
||||
const entriesByCategory = arrayToDictionary(entriesWithMetadata, x => x.category);
|
||||
|
||||
const categoryHyperlinkTemplate = hyperlinkCategories && historyEntry.building_id != undefined ?
|
||||
@ -61,7 +65,7 @@ const BuildingEditSummary: React.FunctionComponent<BuildingEditSummaryProps> = (
|
||||
{
|
||||
Object.entries(entriesByCategory).map(([category, fields]) =>
|
||||
<CategoryEditSummary
|
||||
category={category}
|
||||
category={category as keyof typeof Category} // https://github.com/microsoft/TypeScript/issues/14106
|
||||
fields={fields}
|
||||
hyperlinkCategory={hyperlinkCategories}
|
||||
hyperlinkTemplate={categoryHyperlinkTemplate}
|
||||
|
@ -8,7 +8,7 @@ import { categories, Category } from '../../data_fields';
|
||||
import { FieldEditSummary } from './field-edit-summary';
|
||||
|
||||
interface CategoryEditSummaryProps {
|
||||
category: string;
|
||||
category: keyof typeof Category; // https://github.com/microsoft/TypeScript/issues/14106
|
||||
fields: {
|
||||
title: string;
|
||||
value: any;
|
||||
@ -19,9 +19,7 @@ interface CategoryEditSummaryProps {
|
||||
}
|
||||
|
||||
const CategoryEditSummary : React.FunctionComponent<CategoryEditSummaryProps> = props => {
|
||||
const category: Category = Category[props.category];
|
||||
console.log(category);
|
||||
console.log(typeof(category));
|
||||
const category = Category[props.category];
|
||||
const categoryInfo = categories[category] || {name: undefined, slug: undefined};
|
||||
const categoryName = categoryInfo.name || 'Unknown category';
|
||||
const categorySlug = categoryInfo.slug || 'categories';
|
||||
|
@ -11,6 +11,8 @@ export enum Category {
|
||||
Community = 'Community',
|
||||
Planning = 'Planning',
|
||||
Like = 'Like',
|
||||
|
||||
Unknown = 'Unknown'
|
||||
}
|
||||
|
||||
export const categories = {
|
||||
@ -79,6 +81,18 @@ export const categoriesOrder: Category[] = [
|
||||
Category.Like,
|
||||
];
|
||||
|
||||
/**
|
||||
* This interface is used only in code which uses dataFields, not in the dataFields definition itself
|
||||
* Cannot make dataFields an indexed type ({[key: string]: DataFieldDefinition}),
|
||||
* because then we wouldn't have type-checking for whether a given key exists on dataFields,
|
||||
* e.g. dataFields.foo_bar would not be highlighted as an error.
|
||||
*/
|
||||
export interface DataFieldDefinition {
|
||||
category: Category;
|
||||
title: string;
|
||||
tooltip?: string;
|
||||
}
|
||||
|
||||
export const dataFields = {
|
||||
location_name: {
|
||||
category: Category.Location,
|
||||
|
Loading…
Reference in New Issue
Block a user