Factor out get_cat

This commit is contained in:
Tom Russell 2018-11-30 09:26:28 +00:00
parent f3e09a283c
commit 446266b8b6
4 changed files with 19 additions and 34 deletions

View File

@ -6,6 +6,7 @@ import InfoBox from './info-box';
import Sidebar from './sidebar';
import Tooltip from './tooltip';
import { CloseIcon, SaveIcon } from './icons';
import { parseCategoryURL } from '../parse';
import CONFIG from './fields-config.json';
@ -25,7 +26,7 @@ const BuildingEdit = (props) => {
);
}
const cat = get_cat(props.match.url);
const cat = parseCategoryURL(props.match.url);
return (
<Sidebar
key={props.building_id}
@ -42,15 +43,6 @@ const BuildingEdit = (props) => {
);
}
function get_cat(url) {
if (url === "/") {
return "age"
}
const matches = /^\/(view|edit)\/([^\/.]+)/.exec(url);
const cat = (matches && matches.length > 2)? matches[2] : "age";
return cat;
}
class EditForm extends Component {
constructor(props) {
super(props);

View File

@ -5,6 +5,7 @@ import Sidebar from './sidebar';
import Tooltip from './tooltip';
import InfoBox from './info-box';
import { EditIcon } from './icons';
import { parseCategoryURL } from '../parse';
import CONFIG from './fields-config.json';
@ -20,7 +21,7 @@ const BuildingView = (props) => {
</Sidebar>
);
}
const cat = get_cat(props.match.url);
const cat = parseCategoryURL(props.match.url);
return (
<Sidebar title={`Data available for this building`} back={`/view/${cat}.html`}>
{
@ -53,16 +54,6 @@ const BuildingView = (props) => {
}
function get_cat(url) {
if (url === "/") {
return "age"
}
const matches = /^\/(view|edit)\/([^\/.]+)/.exec(url);
const cat = (matches && matches.length > 2)? matches[2] : "age";
return cat;
}
const DataSection = (props) => {
const match = props.cat === props.slug;
return (

View File

@ -4,6 +4,7 @@ import { Map, TileLayer, ZoomControl, AttributionControl } from 'react-leaflet-u
import '../../node_modules/leaflet/dist/leaflet.css'
import './map.css'
import ThemeSwitcher from './theme-switcher';
import { parseCategoryURL } from '../parse';
const OS_API_KEY = 'NVUxtY5r8eA6eIfwrPTAGKrAAsoeI9E9';
@ -29,7 +30,7 @@ class ColouringMap extends Component {
const mode = is_edit? 'edit': 'view';
const lat = e.latlng.lat
const lng = e.latlng.lng
const new_cat = get_cat(this.props.match.url);
const new_cat = parseCategoryURL(this.props.match.url);
const map_cat = new_cat || 'age';
fetch(
'/buildings/locate?lat='+lat+'&lng='+lng
@ -68,7 +69,7 @@ class ColouringMap extends Component {
// colour-data tiles
const is_building = /building/.test(this.props.match.url);
const cat = get_cat(this.props.match.url);
const cat = parseCategoryURL(this.props.match.url);
const tileset_by_cat = {
age: 'date_year',
size: 'size_storeys',
@ -118,13 +119,4 @@ class ColouringMap extends Component {
}
};
function get_cat(url) {
if (url === "/") {
return "age"
}
const matches = /^\/(view|edit)\/([^\/.]+)/.exec(url);
const cat = (matches && matches.length > 2)? matches[2] : "age";
return cat;
}
export default ColouringMap;

View File

@ -15,7 +15,7 @@ function strictParseInt(value) {
function parseBuildingURL(url){
const re = /\/building\/([^\/]+).html/;
const re = /\/building\/([^/]+).html/;
const matches = re.exec(url);
if (matches && matches.length >= 2) {
@ -24,4 +24,14 @@ function parseBuildingURL(url){
return undefined;
}
export { strictParseInt, parseBuildingURL };
function parseCategoryURL(url) {
const default_cat = 'age';
if (url === "/") {
return default_cat
}
const matches = /^\/(view|edit)\/([^/.]+)/.exec(url);
const cat = (matches && matches.length >= 3)? matches[2] : default_cat;
return cat;
}
export { strictParseInt, parseBuildingURL, parseCategoryURL };