2021-02-06 19:15:33 +00:00
|
|
|
import {
|
|
|
|
Api,
|
|
|
|
CalendarItem,
|
|
|
|
Classmate,
|
2021-10-05 15:44:14 +00:00
|
|
|
EtjanstChild,
|
2021-02-06 19:15:33 +00:00
|
|
|
MenuItem,
|
|
|
|
NewsItem,
|
|
|
|
Notification,
|
2021-02-06 20:06:49 +00:00
|
|
|
ScheduleItem,
|
2021-10-05 15:44:14 +00:00
|
|
|
Skola24Child,
|
2021-04-09 16:31:31 +00:00
|
|
|
TimetableEntry,
|
2021-10-05 15:44:14 +00:00
|
|
|
User,
|
|
|
|
} from '@skolplattformen/api-skolplattformen'
|
2021-02-06 19:15:33 +00:00
|
|
|
import { Action, Reducer } from 'redux'
|
|
|
|
|
2021-02-08 16:27:51 +00:00
|
|
|
export interface Reporter {
|
|
|
|
log: (message: string) => void
|
|
|
|
error: (error: Error, label?: string) => void
|
|
|
|
}
|
|
|
|
|
2021-02-06 19:15:33 +00:00
|
|
|
export interface IApiContext {
|
|
|
|
api: Api
|
|
|
|
storage: AsyncStorage
|
|
|
|
isLoggedIn: boolean
|
|
|
|
isFake: boolean
|
2021-02-08 16:27:51 +00:00
|
|
|
reporter: Reporter
|
2021-02-06 19:15:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export type EntityStatus = 'pending' | 'loading' | 'loaded' | 'error'
|
|
|
|
export interface EntityState<T> {
|
|
|
|
data: T
|
|
|
|
status: EntityStatus
|
|
|
|
error?: Error
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface ApiCall<T> {
|
|
|
|
(): Promise<T>
|
|
|
|
}
|
|
|
|
export interface ExtraActionProps<T> {
|
|
|
|
apiCall: ApiCall<T>
|
2021-02-08 16:27:51 +00:00
|
|
|
retries: number
|
2021-02-06 19:15:33 +00:00
|
|
|
key: string
|
|
|
|
defaultValue: T
|
|
|
|
getFromCache?: () => Promise<string | null>
|
|
|
|
saveToCache?: (value: string) => Promise<void>
|
|
|
|
}
|
2021-10-05 15:44:14 +00:00
|
|
|
export type EntityActionType =
|
|
|
|
| 'GET_FROM_API'
|
|
|
|
| 'RESULT_FROM_API'
|
|
|
|
| 'API_ERROR'
|
|
|
|
| 'GET_FROM_CACHE'
|
|
|
|
| 'RESULT_FROM_CACHE'
|
|
|
|
| 'STORE_IN_CACHE'
|
|
|
|
| 'CLEAR'
|
|
|
|
export type EntityName =
|
|
|
|
| 'USER'
|
|
|
|
| 'ETJANST_CHILDREN'
|
|
|
|
| 'SKOLA24_CHILDREN'
|
|
|
|
| 'CHILDREN'
|
|
|
|
| 'CALENDAR'
|
|
|
|
| 'CLASSMATES'
|
|
|
|
| 'MENU'
|
|
|
|
| 'NEWS'
|
|
|
|
| 'NEWS_DETAILS'
|
|
|
|
| 'NOTIFICATIONS'
|
|
|
|
| 'SCHEDULE'
|
|
|
|
| 'TIMETABLE'
|
|
|
|
| 'ALL'
|
2021-02-06 19:15:33 +00:00
|
|
|
export interface EntityAction<T> extends Action<EntityActionType> {
|
|
|
|
entity: EntityName
|
|
|
|
data?: T
|
|
|
|
error?: Error
|
|
|
|
extra?: ExtraActionProps<T>
|
|
|
|
}
|
|
|
|
export interface EntityMap<T> {
|
|
|
|
[key: string]: EntityState<T>
|
|
|
|
}
|
|
|
|
export type EntityReducer<T> = Reducer<EntityMap<T>, EntityAction<T>>
|
|
|
|
|
|
|
|
export interface EntityStoreRootState {
|
2021-04-09 16:31:31 +00:00
|
|
|
etjanstChildren: EntityMap<EtjanstChild[]>
|
|
|
|
skola24Children: EntityMap<Skola24Child[]>
|
2021-02-06 19:15:33 +00:00
|
|
|
user: EntityMap<User>
|
2021-10-05 15:44:14 +00:00
|
|
|
calendar: EntityMap<CalendarItem[]>
|
|
|
|
classmates: EntityMap<Classmate[]>
|
|
|
|
menu: EntityMap<MenuItem[]>
|
|
|
|
news: EntityMap<NewsItem[]>
|
|
|
|
newsDetails: EntityMap<NewsItem>
|
|
|
|
notifications: EntityMap<Notification[]>
|
|
|
|
schedule: EntityMap<ScheduleItem[]>
|
|
|
|
timetable: EntityMap<TimetableEntry[]>
|
2021-02-06 19:15:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface EntityHookResult<T> extends EntityState<T> {
|
|
|
|
reload: () => void
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface AsyncStorage {
|
|
|
|
getItem(key: string): Promise<string | null>
|
|
|
|
setItem(key: string, value: string): Promise<void>
|
|
|
|
}
|