-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: cache elevation data in client redux slice
- Loading branch information
1 parent
1495280
commit faa15a9
Showing
8 changed files
with
158 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* Copyright © 2024 Technology Matters | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published | ||
* by the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see https://www.gnu.org/licenses/. | ||
*/ | ||
|
||
import {useEffect} from 'react'; | ||
|
||
import {Coords} from 'terraso-client-shared/types'; | ||
|
||
import {selectElevation} from 'terraso-mobile-client/model/elevation/elevationSelectors'; | ||
import {fetchElevation} from 'terraso-mobile-client/model/elevation/elevationSlice'; | ||
import {useDispatch, useSelector} from 'terraso-mobile-client/store'; | ||
|
||
export const useElevationData = (coords: Coords): number | undefined => { | ||
const dispatch = useDispatch(); | ||
const elevation = useSelector(selectElevation(coords)); | ||
|
||
useEffect(() => { | ||
if (!elevation) { | ||
dispatch(fetchElevation(coords)); | ||
} | ||
}, [dispatch, elevation, coords]); | ||
|
||
return elevation?.value; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* | ||
* Copyright © 2024 Technology Matters | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published | ||
* by the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see https://www.gnu.org/licenses/. | ||
*/ | ||
|
||
import {Coords} from 'terraso-client-shared/types'; | ||
|
||
import {elevationKey} from 'terraso-mobile-client/model/elevation/elevationFunctions'; | ||
import {AppState} from 'terraso-mobile-client/store'; | ||
|
||
export const selectElevation = (coords: Coords) => (state: AppState) => | ||
state.elevation.elevation[elevationKey(coords)]; |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* Copyright © 2024 Technology Matters | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published | ||
* by the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see https://www.gnu.org/licenses/. | ||
*/ | ||
|
||
import {createAsyncThunk, createSlice} from '@reduxjs/toolkit'; | ||
|
||
import {Coords} from 'terraso-client-shared/types'; | ||
|
||
import {elevationKey} from 'terraso-mobile-client/model/elevation/elevationFunctions'; | ||
import {getElevation} from 'terraso-mobile-client/model/elevation/elevationService'; | ||
import { | ||
ElevationKey, | ||
ElevationRecord, | ||
} from 'terraso-mobile-client/model/elevation/elevationTypes'; | ||
|
||
export type ElevationState = { | ||
elevation: Record<ElevationKey, ElevationRecord>; | ||
}; | ||
|
||
const initialState: ElevationState = { | ||
elevation: {}, | ||
}; | ||
|
||
const {reducer} = createSlice({ | ||
name: 'elevation', | ||
initialState, | ||
reducers: {}, | ||
extraReducers(builder) { | ||
builder | ||
.addCase(fetchElevation.pending, (state, action) => { | ||
state.elevation[elevationKey(action.meta.arg)] = {fetching: true}; | ||
}) | ||
.addCase(fetchElevation.rejected, (state, action) => { | ||
state.elevation[elevationKey(action.meta.arg)] = {fetching: false}; | ||
}) | ||
.addCase(fetchElevation.fulfilled, (state, action) => { | ||
state.elevation[elevationKey(action.meta.arg)] = { | ||
fetching: false, | ||
value: action.payload, | ||
}; | ||
}); | ||
}, | ||
}); | ||
|
||
const fetchElevation = createAsyncThunk( | ||
'elevation/fetchElevation', | ||
async (coords: Coords) => { | ||
return getElevation(coords.latitude, coords.longitude); | ||
}, | ||
); | ||
|
||
export {fetchElevation, reducer}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/* | ||
* Copyright © 2024 Technology Matters | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published | ||
* by the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see https://www.gnu.org/licenses/. | ||
*/ | ||
|
||
export type ElevationKey = `(${number}, ${number})`; | ||
export type ElevationRecord = { | ||
value?: number; | ||
fetching: boolean; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters