1+ // @ts -ignore - no types available
12import normalize from "json-api-normalizer" ;
23import { saveGeostore } from "./geostores" ;
34import { toastr } from "react-redux-toastr" ;
45import omit from "lodash/omit" ;
56import { areaService } from "services/area" ;
67import { utilsService } from "services/utils" ;
8+ import { AppDispatch , RootState } from "store" ;
9+ import { TGetTeamResponse } from "services/teams" ;
710
811// Actions
912const SET_AREA = "areas/SET_AREA" ;
1013const SET_AREAS = "areas/SET_AREAS" ;
14+ const SET_AREA_TEAMS = "areas/SET_AREA_TEAMS" ;
1115const SET_COUNTRIES = "areas/SET_COUNTRIES" ;
1216const SET_LOADING_AREAS = "areas/SET_LOADING_AREAS" ;
1317const SET_SAVING_AREA = "areas/SET_SAVING_AREA" ;
1418const SET_EDITING_AREA = "areas/SET_EDITING_AREA" ;
1519const SET_AREA_DELETED = "areas/SET_AREA_DELETED" ;
1620
21+ export type TAreasState = {
22+ ids : string [ ] ;
23+ data : any ;
24+ countries : any [ ] ;
25+ areaTeams : TGetTeamResponse [ ] ;
26+ loading : boolean ;
27+ saving : boolean ;
28+ editing : boolean ;
29+ error : boolean ;
30+ } ;
31+
32+ export type TReducerActions =
33+ | { type : typeof SET_AREA ; payload : any }
34+ | { type : typeof SET_AREAS ; payload : any }
35+ | { type : typeof SET_AREA_TEAMS ; payload : { areaTeams : TAreasState [ "areaTeams" ] } }
36+ | { type : typeof SET_COUNTRIES ; payload : any }
37+ | { type : typeof SET_LOADING_AREAS ; payload : any }
38+ | { type : typeof SET_SAVING_AREA ; payload : any }
39+ | { type : typeof SET_EDITING_AREA ; payload : any }
40+ | { type : typeof SET_AREA_DELETED ; payload : any } ;
41+
1742// Reducer
18- const initialState = {
43+ const initialState : TAreasState = {
1944 ids : [ ] ,
2045 data : { } ,
2146 countries : [ ] ,
47+ areaTeams : [ ] ,
2248 loading : true ,
2349 saving : false ,
2450 editing : false ,
2551 error : false
2652} ;
2753
28- export default function reducer ( state = initialState , action ) {
54+ export default function reducer ( state = initialState , action : TReducerActions ) : TAreasState {
2955 switch ( action . type ) {
3056 case SET_AREA : {
3157 const area = action . payload . area ;
58+ //@ts -ignore - TODO: Figure out typescript error
3259 if ( state . ids . indexOf ( ...Object . keys ( area ) ) > - 1 ) {
3360 return {
3461 ...state ,
@@ -47,6 +74,9 @@ export default function reducer(state = initialState, action) {
4774 if ( areas ) return Object . assign ( { } , state , { ids : Object . keys ( areas ) , data : areas } ) ;
4875 return state ;
4976 }
77+ case SET_AREA_TEAMS : {
78+ return Object . assign ( { } , state , { areaTeams : action . payload } ) ;
79+ }
5080 case SET_AREA_DELETED : {
5181 const areaId = action . payload ;
5282 const ids = state . ids . filter ( id => id !== areaId ) ;
@@ -67,8 +97,8 @@ export default function reducer(state = initialState, action) {
6797}
6898
6999// Action Creators
70- export function getArea ( id ) {
71- return ( dispatch , state ) => {
100+ export function getArea ( id : string ) {
101+ return ( dispatch : AppDispatch , state : ( ) => RootState ) => {
72102 dispatch ( {
73103 type : SET_LOADING_AREAS ,
74104 payload : true
@@ -100,8 +130,8 @@ export function getArea(id) {
100130 } ;
101131}
102132
103- export function deleteArea ( areaId ) {
104- return ( dispatch , state ) => {
133+ export function deleteArea ( areaId : string ) {
134+ return ( dispatch : AppDispatch , state : ( ) => RootState ) => {
105135 areaService . token = state ( ) . user . token ;
106136 areaService
107137 . deleteArea ( areaId )
@@ -118,7 +148,7 @@ export function deleteArea(areaId) {
118148}
119149
120150export function getAreas ( ) {
121- return ( dispatch , state ) => {
151+ return ( dispatch : AppDispatch , state : ( ) => RootState ) => {
122152 dispatch ( {
123153 type : SET_LOADING_AREAS ,
124154 payload : true
@@ -148,8 +178,25 @@ export function getAreas() {
148178 } ;
149179}
150180
181+ export function getAreaTeams ( areaId : string ) {
182+ return ( dispatch : AppDispatch , state : ( ) => RootState ) => {
183+ return areaService
184+ . getAreaTeams ( areaId )
185+ . then ( data => {
186+ dispatch ( {
187+ type : SET_AREA_TEAMS ,
188+ payload : data
189+ } ) ;
190+ return data ;
191+ } )
192+ . catch ( error => {
193+ toastr . error ( "Unable to load area teams" , error ) ;
194+ } ) ;
195+ } ;
196+ }
197+
151198export function getCountries ( ) {
152- return ( dispatch , state ) => {
199+ return ( dispatch : AppDispatch ) => {
153200 return utilsService
154201 . getCountries ( )
155202 . then ( data => {
@@ -165,8 +212,8 @@ export function getCountries() {
165212}
166213
167214// POST name, geostore ID
168- export function saveArea ( area , node , method ) {
169- return async ( dispatch , state ) => {
215+ export function saveArea ( area : any , node : HTMLElement , method : string ) {
216+ return async ( dispatch : AppDispatch , state : ( ) => RootState ) => {
170217 dispatch ( {
171218 type : SET_SAVING_AREA ,
172219 payload : {
@@ -204,35 +251,35 @@ export function saveArea(area, node, method) {
204251}
205252
206253// async save geostore then area
207- export function saveAreaWithGeostore ( area , node , method ) {
208- return async ( dispatch , state ) => {
254+ export function saveAreaWithGeostore ( area : any , node : HTMLElement , method : string ) {
255+ return async ( dispatch : AppDispatch ) => {
209256 const geostore = await dispatch ( saveGeostore ( area . geojson ) ) ;
210257 const geostoreId = Object . keys ( geostore ) [ 0 ] ;
211258 const areaWithGeostore = { ...area , geostore : geostoreId } ;
212259 await dispatch ( saveArea ( areaWithGeostore , node , method ) ) ;
213260 } ;
214261}
215262
216- export function setEditing ( bool ) {
217- return async dispatch => {
263+ export function setEditing ( bool : boolean ) {
264+ return async ( dispatch : AppDispatch ) => {
218265 await dispatch ( {
219266 type : SET_EDITING_AREA ,
220267 payload : bool
221268 } ) ;
222269 } ;
223270}
224271
225- export function setSaving ( payload ) {
226- return dispatch => {
272+ export function setSaving ( payload : boolean ) {
273+ return ( dispatch : AppDispatch ) => {
227274 dispatch ( {
228275 type : SET_SAVING_AREA ,
229276 payload : payload
230277 } ) ;
231278 } ;
232279}
233280
234- export function setLoading ( bool ) {
235- return dispatch => {
281+ export function setLoading ( bool : boolean ) {
282+ return ( dispatch : AppDispatch ) => {
236283 dispatch ( {
237284 type : SET_LOADING_AREAS ,
238285 payload : bool
0 commit comments