|
@@ -1,5 +1,6 @@
|
|
|
-/** @module config/state */
|
|
|
-import { call, put, takeEvery, select } from 'redux-saga/effects'
|
|
|
+/** @module swisstennis/state */
|
|
|
+import { call, put, takeEvery } from 'redux-saga/effects'
|
|
|
+import { SZTM_API } from '../config/static'
|
|
|
|
|
|
/**
|
|
|
* state.js
|
|
@@ -9,162 +10,217 @@ import { call, put, takeEvery, select } from 'redux-saga/effects'
|
|
|
|
|
|
/** actionTypes define what actions are handeled by the reducer. */
|
|
|
export const actions = {
|
|
|
- configClearForm: () => {
|
|
|
+ fileListRequest: () => {
|
|
|
return {
|
|
|
- type: 'CONFIG/CLEAR_FORM'
|
|
|
+ type: 'SWISSTENNIS/FILE_LIST_REQUEST',
|
|
|
}
|
|
|
},
|
|
|
- configLoadForm: (key) => {
|
|
|
+ fileListSuccess: data => {
|
|
|
return {
|
|
|
- type: 'CONFIG/LOAD_FORM',
|
|
|
+ type: 'SWISSTENNIS/FILE_LIST_SUCCESS',
|
|
|
+ data
|
|
|
+ }
|
|
|
+ },
|
|
|
+ fileListFailure: error => {
|
|
|
+ return {
|
|
|
+ type: 'SWISSTENNIS/FILE_LIST_FAILURE',
|
|
|
+ error
|
|
|
+ }
|
|
|
+ },
|
|
|
+ loginRequest: () => {
|
|
|
+ return {
|
|
|
+ type: 'SWISSTENNIS/LOGIN_REQUEST'
|
|
|
+ }
|
|
|
+ },
|
|
|
+ loginSuccess: (key) => {
|
|
|
+ return {
|
|
|
+ type: 'SWISSTENNIS/LOGIN_SUCCESS',
|
|
|
key
|
|
|
}
|
|
|
},
|
|
|
- configChangeForm: (data) => {
|
|
|
+ loginFailure: (data) => {
|
|
|
+ return {
|
|
|
+ type: 'SWISSTENNIS/LOGIN_FAILURE',
|
|
|
+ data
|
|
|
+ }
|
|
|
+ },
|
|
|
+ getPlayerListRequest: () => {
|
|
|
+ return {
|
|
|
+ type: 'SWISSTENNIS/GET_PLAYERLIST_REQUEST'
|
|
|
+ }
|
|
|
+ },
|
|
|
+ getPlayerListSuccess: (data) => {
|
|
|
+ return {
|
|
|
+ type: 'SWISSTENNIS/GET_PLAYERLIST_SUCCESS',
|
|
|
+ data
|
|
|
+ }
|
|
|
+ },
|
|
|
+ getPlayerListFailure: error => {
|
|
|
+ return {
|
|
|
+ type: 'SWISSTENNIS/GET_PLAYERLIST_FAILURE',
|
|
|
+ error
|
|
|
+ }
|
|
|
+ },
|
|
|
+ downloadPlayerListRequest: () => {
|
|
|
return {
|
|
|
- type: 'CONFIG/CHANGE_FORM',
|
|
|
+ type: 'SWISSTENNIS/DOWNLOAD_PLAYERLIST_REQUEST'
|
|
|
+ }
|
|
|
+ },
|
|
|
+ downloadPlayerListSuccess: (data) => {
|
|
|
+ return {
|
|
|
+ type: 'SWISSTENNIS/DOWNLOAD_PLAYERLIST_SUCCESS',
|
|
|
data
|
|
|
}
|
|
|
},
|
|
|
- configGetRequest: () => {
|
|
|
+ downloadPlayerListFailure: error => {
|
|
|
return {
|
|
|
- type: 'CONFIG/GET_REQUEST'
|
|
|
+ type: 'SWISSTENNIS/DOWNLOAD_PLAYERLIST_FAILURE',
|
|
|
+ error
|
|
|
}
|
|
|
},
|
|
|
- configGetSuccess: (data) => {
|
|
|
+ getCalendarRequest: (data) => {
|
|
|
return {
|
|
|
- type: 'CONFIG/GET_SUCCESS',
|
|
|
+ type: 'SWISSTENNIS/GET_CALENDAR_REQUEST',
|
|
|
data
|
|
|
}
|
|
|
},
|
|
|
- configGetFailure: error => {
|
|
|
+ getCalendarSuccess: (data) => {
|
|
|
return {
|
|
|
- type: 'CONFIG/GET_FAILURE',
|
|
|
+ type: 'SWISSTENNIS/GET_CALENDAR_SUCCESS',
|
|
|
+ data
|
|
|
+ }
|
|
|
+ },
|
|
|
+ getCalendarFailure: error => {
|
|
|
+ return {
|
|
|
+ type: 'SWISSTENNIS/GET_CALENDAR_FAILURE',
|
|
|
error
|
|
|
}
|
|
|
},
|
|
|
- configAddRequest: (data) => {
|
|
|
+ downloadCalendarRequest: (data) => {
|
|
|
return {
|
|
|
- type: 'CONFIG/ADD_REQUEST',
|
|
|
+ type: 'SWISSTENNIS/DOWNLOAD_CALENDAR_REQUEST',
|
|
|
data
|
|
|
}
|
|
|
},
|
|
|
- configAddSuccess: (data) => {
|
|
|
+ downloadCalendarSuccess: (data) => {
|
|
|
return {
|
|
|
- type: 'CONFIG/ADD_SUCCESS',
|
|
|
+ type: 'SWISSTENNIS/DOWNLOAD_CALENDAR_SUCCESS',
|
|
|
data
|
|
|
}
|
|
|
},
|
|
|
- configAddFailure: error => {
|
|
|
+ downloadCalendarFailure: error => {
|
|
|
return {
|
|
|
- type: 'CONFIG/ADD_FAILURE',
|
|
|
+ type: 'SWISSTENNIS/DOWNLOAD_CALENDAR_FAILURE',
|
|
|
error
|
|
|
}
|
|
|
},
|
|
|
- configEditRequest: (data) => {
|
|
|
+ parsePlayerListRequest: (data) => {
|
|
|
return {
|
|
|
- type: 'CONFIG/EDIT_REQUEST',
|
|
|
+ type: 'SWISSTENNIS/PARSE_PLAYERLIST_REQUEST',
|
|
|
data
|
|
|
}
|
|
|
},
|
|
|
- configEditSuccess: (data) => {
|
|
|
+ parsePlayerListSuccess: (data) => {
|
|
|
return {
|
|
|
- type: 'CONFIG/EDIT_SUCCESS',
|
|
|
+ type: 'SWISSTENNIS/PARSE_PLAYERLIST_SUCCESS',
|
|
|
data
|
|
|
}
|
|
|
},
|
|
|
- configEditFailure: error => {
|
|
|
+ parsePlayerListFailure: error => {
|
|
|
return {
|
|
|
- type: 'CONFIG/EDIT_FAILURE',
|
|
|
+ type: 'SWISSTENNIS/PARSE_PLAYERLIST_FAILURE',
|
|
|
error
|
|
|
}
|
|
|
},
|
|
|
- configDeleteRequest: (key) => {
|
|
|
+ parseCalendarRequest: (key) => {
|
|
|
return {
|
|
|
- type: 'CONFIG/DELETE_REQUEST',
|
|
|
+ type: 'SWISSTENNIS/PARSE_CALENDAR_REQUEST',
|
|
|
key
|
|
|
}
|
|
|
},
|
|
|
- configDeleteSuccess: (data) => {
|
|
|
+ parseCalendarSuccess: (data) => {
|
|
|
return {
|
|
|
- type: 'CONFIG/DELETE_SUCCESS',
|
|
|
+ type: 'SWISSTENNIS/PARSE_CALENDAR_SUCCESS',
|
|
|
data
|
|
|
}
|
|
|
},
|
|
|
- configDeleteFailure: error => {
|
|
|
+ parseCalendarFailure: error => {
|
|
|
return {
|
|
|
- type: 'CONFIG/DELETE_FAILURE',
|
|
|
+ type: 'SWISSTENNIS/PARSE_CALENDAR_FAILURE',
|
|
|
error
|
|
|
}
|
|
|
},
|
|
|
}
|
|
|
console.log('State actions', actions)
|
|
|
|
|
|
-const emptyForm = {
|
|
|
- _id: null,
|
|
|
- key: '',
|
|
|
- description: '',
|
|
|
- value: ''
|
|
|
-}
|
|
|
-
|
|
|
/** state definition */
|
|
|
export const state = {
|
|
|
- configs: [],
|
|
|
- configForm: emptyForm,
|
|
|
- getRequested: false,
|
|
|
- addRequested: false,
|
|
|
- editRequested: false,
|
|
|
- deleteRequested: false
|
|
|
+ files: [],
|
|
|
+ fileListStatus: 'uninitialized',
|
|
|
+ calendars: [],
|
|
|
+ calendarsStatus: 'uninitialized',
|
|
|
+ playerLists: [],
|
|
|
+ playerListsStatus: 'uninitialized',
|
|
|
+ transferStarted: false,
|
|
|
+ loginState: false,
|
|
|
}
|
|
|
console.log('State state', state)
|
|
|
|
|
|
/** reducer is called by the redux dispatcher and handles all component actions */
|
|
|
export function reducer (state = [], action) {
|
|
|
switch (action.type) {
|
|
|
- case 'CONFIG/CLEAR_FORM':
|
|
|
- return { ...state, configForm: emptyForm }
|
|
|
- case 'CONFIG/LOAD_FORM':
|
|
|
- return { ...state, configForm: { ...state.configs[action.key] } }
|
|
|
- case 'CONFIG/CHANGE_FORM':
|
|
|
- return { ...state, configForm: { ...action.data } }
|
|
|
- case 'CONFIG/GET_SUCCESS':
|
|
|
- return { ...state, configs: action.data.config, getRequested: false }
|
|
|
- case 'CONFIG/GET_FAILURE':
|
|
|
- return { ...state, getRequested: false, alert: action.error }
|
|
|
- case 'CONFIG/ADD_REQUEST':
|
|
|
- return { ...state, addRequested: true }
|
|
|
- case 'CONFIG/ADD_SUCCESS':
|
|
|
- console.log(action)
|
|
|
- return { ...state, configs: [ ...state.configs, action.data ], addRequested: false }
|
|
|
- case 'CONFIG/ADD_FAILURE':
|
|
|
- return { ...state, addRequested: false, alert: action.error }
|
|
|
- case 'CONFIG/EDIT_REQUEST':
|
|
|
- return { ...state, editRequested: true }
|
|
|
- case 'CONFIG/EDIT_SUCCESS':
|
|
|
- const nextConfigs = state.configs.map(config => {
|
|
|
- return config._id == action.data._id ? action.data : config
|
|
|
- })
|
|
|
- return { ...state, configs: nextConfigs, editRequested: false }
|
|
|
- case 'CONFIG/EDIT_FAILURE':
|
|
|
- return { ...state, editRequested: false, alert: action.error }
|
|
|
- case 'CONFIG/DELETE_REQUEST':
|
|
|
- return { ...state, deleteRequested: true }
|
|
|
- case 'CONFIG/DELETE_SUCCESS':
|
|
|
- const newConfigs = [ ...state.configs.slice(0, action.data), ...state.configs.slice(action.data + 1) ]
|
|
|
- delete newConfigs[action.data.key]
|
|
|
- return { ...state, configs: newConfigs, deleteRequested: false }
|
|
|
- case 'CONFIG/DELETE_FAILURE':
|
|
|
- return { ...state, deleteRequested: false, alert: action.error }
|
|
|
+ case 'SWISSTENNIS/FILE_LIST_REQUEST':
|
|
|
+ return { ...state, fileListStatus: 'loading' }
|
|
|
+ case 'SWISSTENNIS/FILE_LIST_SUCCESS':
|
|
|
+ return { ...state, fileListStatus: 'loaded', files: action.data }
|
|
|
+ case 'SWISSTENNIS/FILE_LIST_FAILURE':
|
|
|
+ return { ...state, fileListStatus: 'failed' }
|
|
|
+ case 'SWISSTENNIS/GET_CALENDAR_REQUEST':
|
|
|
+ return { ...state, calendarsStatus: 'loading' }
|
|
|
+ case 'SWISSTENNIS/GET_CALENDAR_SUCCESS':
|
|
|
+ return { ...state, calendarsStatus: 'loaded', calendars: action.data }
|
|
|
+ case 'SWISSTENNIS/GET_CALENDAR_FAILURE':
|
|
|
+ return { ...state, calendarsStatus: 'failed' }
|
|
|
+ case 'SWISSTENNIS/GET_PLAYERLIST_REQUEST':
|
|
|
+ return { ...state, playerListsStatus: 'loading' }
|
|
|
+ case 'SWISSTENNIS/GET_PLAYERLIST_SUCCESS':
|
|
|
+ return { ...state, playerListsStatus: 'loaded', playerLists: action.data }
|
|
|
+ case 'SWISSTENNIS/GET_PLAYERLIST_FAILURE':
|
|
|
+ return { ...state, playerListsStatus: 'failed' }
|
|
|
default:
|
|
|
return state
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-function * getConfig (action) {
|
|
|
+function * fileList (action) {
|
|
|
+ try {
|
|
|
+ const token = localStorage.getItem('accessToken')
|
|
|
+ console.log('File list requested', action, token)
|
|
|
+ const response = yield call(fetch, `${SZTM_API}/api/swisstennis/files`, {
|
|
|
+ method: 'GET',
|
|
|
+ headers: {
|
|
|
+ 'Content-Type': 'application/json',
|
|
|
+ 'x-access-token': token
|
|
|
+ }
|
|
|
+ })
|
|
|
+ if (response.status != 200) {
|
|
|
+ console.log(response)
|
|
|
+ throw Error(response.status)
|
|
|
+ } else {
|
|
|
+ const responseJson = yield response.json()
|
|
|
+ yield put(actions.fileListSuccess(responseJson.fileList))
|
|
|
+ }
|
|
|
+ } catch (error) {
|
|
|
+ console.log('Config failure!', actions.fileListFailure(error))
|
|
|
+ yield put(actions.fileListFailure(error))
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+function * login (action) {
|
|
|
try {
|
|
|
const token = localStorage.getItem('accessToken')
|
|
|
console.log('Get config requested', action, token)
|
|
|
- const response = yield call(fetch, 'http://localhost:3002/api/config', {
|
|
|
+ const response = yield call(fetch, `${SZTM_API}/api/config`, {
|
|
|
method: 'GET',
|
|
|
headers: {
|
|
|
'Content-Type': 'application/json',
|
|
@@ -184,36 +240,58 @@ function * getConfig (action) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-function * addConfig (action) {
|
|
|
+function * getCalendars (action) {
|
|
|
try {
|
|
|
const token = localStorage.getItem('accessToken')
|
|
|
- console.log('Add config requested', action, token)
|
|
|
- const response = yield call(fetch, 'http://localhost:3002/api/config', {
|
|
|
- method: 'POST',
|
|
|
+ console.log('Get config requested', action, token)
|
|
|
+ const response = yield call(fetch, `${SZTM_API}/api/swisstennis/calendar`, {
|
|
|
+ method: 'GET',
|
|
|
headers: {
|
|
|
'Content-Type': 'application/json',
|
|
|
'x-access-token': token
|
|
|
- },
|
|
|
- body: JSON.stringify(action.data)
|
|
|
+ }
|
|
|
})
|
|
|
if (response.status != 200) {
|
|
|
throw Error(response.status)
|
|
|
} else {
|
|
|
- yield put(actions.configAddSuccess(action.data))
|
|
|
- yield put(actions.configGetRequest())
|
|
|
+ const responseJson = yield response.json()
|
|
|
+ yield put(actions.getCalendarSuccess(responseJson.calendars))
|
|
|
}
|
|
|
} catch (error) {
|
|
|
- console.log('Config failure!', actions.configAddFailure(error))
|
|
|
- yield put(actions.configAddFailure(error))
|
|
|
+ console.log('Config failure!', actions.getCalendarFailure(error))
|
|
|
+ yield put(actions.getCalendarFailure(error))
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-function * editConfig (action) {
|
|
|
+function * getPlayerLists (action) {
|
|
|
try {
|
|
|
const token = localStorage.getItem('accessToken')
|
|
|
- console.log('Edit config requested', action, token)
|
|
|
- const response = yield call(fetch, 'http://localhost:3002/api/config', {
|
|
|
- method: 'PUT',
|
|
|
+ console.log('Get config requested', action, token)
|
|
|
+ const response = yield call(fetch, `${SZTM_API}/api/swisstennis/playerList`, {
|
|
|
+ method: 'GET',
|
|
|
+ headers: {
|
|
|
+ 'Content-Type': 'application/json',
|
|
|
+ 'x-access-token': token
|
|
|
+ }
|
|
|
+ })
|
|
|
+ if (response.status != 200) {
|
|
|
+ throw Error(response.status)
|
|
|
+ } else {
|
|
|
+ const responseJson = yield response.json()
|
|
|
+ yield put(actions.getPlayerListSuccess(responseJson.playerLists))
|
|
|
+ }
|
|
|
+ } catch (error) {
|
|
|
+ console.log('Config failure!', actions.getPlayerListFailure(error))
|
|
|
+ yield put(actions.getPlayerListFailure(error))
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+function * addConfig (action) {
|
|
|
+ try {
|
|
|
+ const token = localStorage.getItem('accessToken')
|
|
|
+ console.log('Add config requested', action, token)
|
|
|
+ const response = yield call(fetch, `${SZTM_API}/api/config`, {
|
|
|
+ method: 'POST',
|
|
|
headers: {
|
|
|
'Content-Type': 'application/json',
|
|
|
'x-access-token': token
|
|
@@ -223,45 +301,43 @@ function * editConfig (action) {
|
|
|
if (response.status != 200) {
|
|
|
throw Error(response.status)
|
|
|
} else {
|
|
|
- yield put(actions.configEditSuccess(action.data))
|
|
|
+ yield put(actions.configAddSuccess(action.data))
|
|
|
+ yield put(actions.configGetRequest())
|
|
|
}
|
|
|
} catch (error) {
|
|
|
- console.log('Config failure!', actions.configEditFailure(error))
|
|
|
- yield put(actions.configEditFailure(error))
|
|
|
+ console.log('Config failure!', actions.configAddFailure(error))
|
|
|
+ yield put(actions.configAddFailure(error))
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-function * deleteConfig (action) {
|
|
|
+function * editConfig (action) {
|
|
|
try {
|
|
|
const token = localStorage.getItem('accessToken')
|
|
|
- const state = yield select()
|
|
|
- const { configs } = state.config
|
|
|
- console.log('Delete config requested', action, token, state, configs)
|
|
|
- const key = configs[action.key].key
|
|
|
- const response = yield call(fetch, 'http://localhost:3002/api/config', {
|
|
|
- method: 'DELETE',
|
|
|
+ console.log('Edit config requested', action, token)
|
|
|
+ const response = yield call(fetch, `${SZTM_API}/api/config`, {
|
|
|
+ method: 'PUT',
|
|
|
headers: {
|
|
|
'Content-Type': 'application/json',
|
|
|
'x-access-token': token
|
|
|
},
|
|
|
- body: JSON.stringify({ key })
|
|
|
+ body: JSON.stringify(action.data)
|
|
|
})
|
|
|
if (response.status != 200) {
|
|
|
throw Error(response.status)
|
|
|
} else {
|
|
|
- yield put(actions.configDeleteSuccess(action.key))
|
|
|
+ yield put(actions.configEditSuccess(action.data))
|
|
|
}
|
|
|
} catch (error) {
|
|
|
- console.log('Config failure!', actions.configDeleteFailure(error))
|
|
|
- yield put(actions.configDeleteFailure(error))
|
|
|
+ console.log('Config failure!', actions.configEditFailure(error))
|
|
|
+ yield put(actions.configEditFailure(error))
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/** sagas are asynchronous workers (JS generators) to handle the state. */
|
|
|
export function * saga () {
|
|
|
console.log('Config saga started.')
|
|
|
- yield takeEvery('CONFIG/GET_REQUEST', getConfig)
|
|
|
- yield takeEvery('CONFIG/ADD_REQUEST', addConfig)
|
|
|
- yield takeEvery('CONFIG/EDIT_REQUEST', editConfig)
|
|
|
- yield takeEvery('CONFIG/DELETE_REQUEST', deleteConfig)
|
|
|
+ yield takeEvery('SWISSTENNIS/FILE_LIST_REQUEST', fileList)
|
|
|
+ yield takeEvery('SWISSTENNIS/GET_CALENDAR_REQUEST', getCalendars)
|
|
|
+ yield takeEvery('SWISSTENNIS/GET_PLAYERLIST_REQUEST', getPlayerLists)
|
|
|
+ yield takeEvery('SWISSTENNIS/LOGIN_REQUEST', login)
|
|
|
}
|