2021-02-22 01:59:24 -05:00
|
|
|
import { useCallback, useEffect, useState } from 'react';
|
|
|
|
|
2021-02-24 02:48:09 -05:00
|
|
|
import { Building, BuildingAttributeVerificationCounts } from '../models/building';
|
2021-02-22 01:59:24 -05:00
|
|
|
import { apiGet } from '../apiHelpers';
|
|
|
|
|
|
|
|
export function useBuildingData(buildingId: number, preloadedData: Building): [Building, (updatedBuilding: Building) => void, () => void] {
|
|
|
|
const [buildingData, setBuildingData] = useState<Building>(preloadedData);
|
|
|
|
const [isOld, setIsOld] = useState<boolean>(preloadedData == undefined);
|
|
|
|
|
|
|
|
const fetchData = useCallback(async () => {
|
|
|
|
if(buildingId == undefined) {
|
|
|
|
setBuildingData(undefined);
|
|
|
|
setIsOld(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
const [building, buildingUprns] = await Promise.all([
|
|
|
|
apiGet(`/api/buildings/${buildingId}.json`),
|
|
|
|
apiGet(`/api/buildings/${buildingId}/uprns.json`)
|
|
|
|
]);
|
|
|
|
|
|
|
|
building.uprns = buildingUprns.uprns;
|
|
|
|
|
|
|
|
setBuildingData(building);
|
|
|
|
} catch(error) {
|
|
|
|
console.error(error);
|
|
|
|
// TODO: add UI for API errors
|
|
|
|
}
|
|
|
|
setIsOld(false);
|
|
|
|
}, [buildingId]);
|
|
|
|
|
2021-02-24 02:48:09 -05:00
|
|
|
const updateData = useCallback((building: Building) => {
|
|
|
|
if(building.verified == undefined) {
|
|
|
|
building.verified = {} as BuildingAttributeVerificationCounts;
|
|
|
|
}
|
|
|
|
setBuildingData(building);
|
|
|
|
}, []);
|
|
|
|
|
2021-02-22 01:59:24 -05:00
|
|
|
useEffect(() => {
|
|
|
|
return () => {
|
|
|
|
setIsOld(true);
|
|
|
|
};
|
|
|
|
}, [buildingId]);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if(isOld) {
|
|
|
|
fetchData();
|
|
|
|
}
|
|
|
|
}, [isOld]);
|
|
|
|
|
|
|
|
const reloadData = useCallback(() => setIsOld(true), []);
|
|
|
|
|
2021-02-24 02:48:09 -05:00
|
|
|
return [buildingData, updateData, reloadData];
|
2021-02-22 01:59:24 -05:00
|
|
|
}
|