From c0946af280438915a2ed3a91561aa2c738568373 Mon Sep 17 00:00:00 2001 From: hhjk00 Date: Fri, 10 Nov 2023 18:04:06 +0900 Subject: [PATCH] =?UTF-8?q?=EB=B9=84=ED=96=89=20=ED=86=B5=EA=B3=84=20api?= =?UTF-8?q?=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/modules/statistics/actions/index.ts | 35 +++++++++++++ src/modules/statistics/apis/index.ts | 24 +++++++++ src/modules/statistics/model/index.ts | 46 +++++++++++++++++ src/modules/statistics/reducers/index.ts | 23 +++++++++ src/modules/statistics/sagas/index.ts | 64 ++++++++++++++++++++++++ src/redux/reducers/rootReducer.ts | 6 ++- 6 files changed, 197 insertions(+), 1 deletion(-) create mode 100644 src/modules/statistics/actions/index.ts create mode 100644 src/modules/statistics/apis/index.ts create mode 100644 src/modules/statistics/model/index.ts create mode 100644 src/modules/statistics/reducers/index.ts create mode 100644 src/modules/statistics/sagas/index.ts diff --git a/src/modules/statistics/actions/index.ts b/src/modules/statistics/actions/index.ts new file mode 100644 index 00000000..1056475f --- /dev/null +++ b/src/modules/statistics/actions/index.ts @@ -0,0 +1,35 @@ +import { AxiosError } from 'axios'; +import { createAsyncAction, ActionType } from 'typesafe-actions'; +import { Flight, FlightSearch, FlightSearchRq } from '../model'; + +// 비행 통계 (비행시간, 비행거리, 비행횟수) +const FLIGHT_STCS_REQUEST = 'statistics/flight/FLIGHT_STCS_REQUEST'; +const FLIGHT_STCS_SUCCESS = 'statistics/flight/FLIGHT_STCS_SUCCESS'; +const FLIGHT_STCS_FAILURE = 'statistics/flight/FLIGHT_STCS_FAILURE'; + +// 비행 통계 카테고리별 검색 +const FLIGHT_STCS_SEARCH_REQUEST = + 'statistics/flight/FLIGHT_STCS_SEARCH_REQUEST'; +const FLIGHT_STCS_SEARCH_SUCCESS = + 'statistics/flight/FLIGHT_STCS_SEARCH_SUCCESS'; +const FLIGHT_STCS_SEARCH_FAILURE = + 'statistics/flight/FLIGHT_STCS_SEARCH_FAILURE'; + +export const FLIGHT_STCS = createAsyncAction( + FLIGHT_STCS_REQUEST, + FLIGHT_STCS_SUCCESS, + FLIGHT_STCS_FAILURE +)(); + +export const FLIGHT_STCS_SEARCH = createAsyncAction( + FLIGHT_STCS_SEARCH_REQUEST, + FLIGHT_STCS_SEARCH_SUCCESS, + FLIGHT_STCS_SEARCH_FAILURE +)(); + +const actions = { + FLIGHT_STCS, + FLIGHT_STCS_SEARCH +}; + +export type StcsAction = ActionType; diff --git a/src/modules/statistics/apis/index.ts b/src/modules/statistics/apis/index.ts new file mode 100644 index 00000000..bf088fd3 --- /dev/null +++ b/src/modules/statistics/apis/index.ts @@ -0,0 +1,24 @@ +import axios from '../../utils/customAxiosUtil'; +import qs from 'qs'; +import { FlightSearchRq } from '../model'; + +export const stcsAPI = { + flight: async () => { + return await axios.get('/api/main/dash/stcs/flight-static'); + }, + flightSearch: async (data: FlightSearchRq) => { + const { type } = data; + const params = {}; + Object.keys(data).forEach(i => { + if (data[i] && i !== 'type') { + params[`${i}`] = data[i]; + } + }); + const queryString = qs.stringify(params, { + addQueryPrefix: true, + arrayFormat: 'repeat' + }); + + return await axios.get(`api/main/dash/stcs/flight/${type}${queryString}`); + } +}; diff --git a/src/modules/statistics/model/index.ts b/src/modules/statistics/model/index.ts new file mode 100644 index 00000000..6ea0329c --- /dev/null +++ b/src/modules/statistics/model/index.ts @@ -0,0 +1,46 @@ +export interface IStcsState { + flight: Flight; + flightSearch: FlightSearch; +} + +export interface Flight { + count: number; + data: { + name: string; + year: string; + month: string; + day: string; + }[]; +} + +export interface FlightSearch { + count: number; + data: { + graphData: SearchData[]; + topData: SearchData[]; + }; +} +export interface SearchData { + name: string; + value: number; +} + +export interface FlightSearchRq { + cate: string; + date: string; + type: string; +} + +export const initialState = { + flight: { + count: 0, + data: [] + }, + flightSearch: { + count: 0, + data: { + graphData: [], + topData: [] + } + } +}; diff --git a/src/modules/statistics/reducers/index.ts b/src/modules/statistics/reducers/index.ts new file mode 100644 index 00000000..fa2e6493 --- /dev/null +++ b/src/modules/statistics/reducers/index.ts @@ -0,0 +1,23 @@ +import { createReducer } from 'typesafe-actions'; +import produce from 'immer'; +import * as Actions from '../actions'; +import { IStcsState, initialState } from '../model'; + +export const statisticsReducer = createReducer( + initialState +) + // 비행 통계 (비행시간, 비행거리, 비행횟수) + .handleAction(Actions.FLIGHT_STCS.success, (state, action) => + produce(state, draft => { + const data = action.payload; + draft.flight = data || state.flight; + }) + ) + + // 비행 통계 카테고리별 검색 + .handleAction(Actions.FLIGHT_STCS_SEARCH.success, (state, action) => + produce(state, draft => { + const data = action.payload; + draft.flightSearch = data || state.flightSearch; + }) + ); diff --git a/src/modules/statistics/sagas/index.ts b/src/modules/statistics/sagas/index.ts new file mode 100644 index 00000000..eb616a0c --- /dev/null +++ b/src/modules/statistics/sagas/index.ts @@ -0,0 +1,64 @@ +import { call, put, takeEvery } from '@redux-saga/core/effects'; +import * as MessageActions from '../../comn/message/actions/comnMessageAction'; +import * as Actions from '../actions'; +import * as Apis from '../apis'; +import { ActionType } from 'typesafe-actions'; + +function* flightStcsSaga( + action: ActionType +) { + try { + const payload = action.payload; + const res = yield call(Apis.stcsAPI.flight); + const { data, errorCode } = res; + + if (errorCode) { + // 오류메시지 호출 + yield put( + MessageActions.IS_ERROR({ + errorCode: errorCode, + errorMessage: '처리중 오류가 발생하였습니다', + isHistoryBack: false, + isRefresh: false + }) + ); + + return; + } + yield put(Actions.FLIGHT_STCS.success(data)); + } catch (error) { + yield put(Actions.FLIGHT_STCS.failure(error)); + } +} + +function* flightStcsSearchSaga( + action: ActionType +) { + try { + const payload = action.payload; + const res = yield call(Apis.stcsAPI.flightSearch, payload); + const { data, errorCode } = res; + + if (errorCode) { + // 오류메시지 호출 + yield put( + MessageActions.IS_ERROR({ + errorCode: errorCode, + errorMessage: '처리중 오류가 발생하였습니다', + isHistoryBack: false, + isRefresh: false + }) + ); + + return; + } + yield put(Actions.FLIGHT_STCS_SEARCH.success(data)); + } catch (error) { + yield put(Actions.FLIGHT_STCS_SEARCH.failure(error)); + } +} + +export function* statisticsSaga() { + yield takeEvery(Actions.FLIGHT_STCS.request, flightStcsSaga); + yield takeEvery(Actions.FLIGHT_STCS_SEARCH.request, flightStcsSearchSaga); +} diff --git a/src/redux/reducers/rootReducer.ts b/src/redux/reducers/rootReducer.ts index 3a913c97..192fe933 100644 --- a/src/redux/reducers/rootReducer.ts +++ b/src/redux/reducers/rootReducer.ts @@ -50,6 +50,8 @@ import { faqSaga } from '../../modules/cstmrService/faq/sagas'; import { faqReducer } from '../../modules/cstmrService/faq/reducers'; import { qnaSaga } from '../../modules/cstmrService/inquiry/sagas'; import { qnaReducer } from '../../modules/cstmrService/inquiry/reducers'; +import { statisticsSaga } from '../../modules/statistics/sagas'; +import { statisticsReducer } from '../../modules/statistics/reducers'; export interface StoreState { controlGpState: ControlGpState; @@ -69,6 +71,7 @@ export function* saga() { yield all([fork(laancSaga)]); yield all([fork(faqSaga)]); yield all([fork(qnaSaga)]); + yield all([fork(statisticsSaga)]); } const rootReducer = combineReducers({ @@ -100,7 +103,8 @@ const rootReducer = combineReducers({ flightState: flightReducer, findState: findAccountReducer, faqState: faqReducer, - qnaState: qnaReducer + qnaState: qnaReducer, + statisticsState: statisticsReducer }); export default rootReducer;