Skip to content

Commit

Permalink
✨ feat: api호출 하여 받아온 데이터 가공
Browse files Browse the repository at this point in the history
  • Loading branch information
pinkdumbbel committed Jul 17, 2022
1 parent d0bedcb commit ae3bd3a
Show file tree
Hide file tree
Showing 9 changed files with 191 additions and 13 deletions.
9 changes: 9 additions & 0 deletions src/@types/api.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
declare module 'request' {
type ApiUrlType = '/media' | '/advertisements' | '/report';
interface ReportInterface {
dataType: 'Report' | 'Media';
imp: number;
click: number;
cost: number;
Expand Down Expand Up @@ -35,6 +36,7 @@ declare module 'request' {
}

interface MediaInterface {
dataType: 'Report' | 'Media';
channel: string;
date: string;

Expand All @@ -54,4 +56,11 @@ declare module 'request' {
reports: ReportInterface[];
media: MediaInterface[];
}

interface TotalData {
[date: string]: {
reports: ReportInterface[];
media: MediaInterface[];
};
}
}
8 changes: 7 additions & 1 deletion src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import React from 'react';
import React, { useEffect } from 'react';
import Router from '@/routes';
import { useTotalDataManagement } from './api/models/useTotalDataManagement';

function App() {
const { weekList, getTotalData } = useTotalDataManagement();
useEffect(() => {
console.log('Apprender');
}, []);

return <Router />;
}

Expand Down
15 changes: 9 additions & 6 deletions src/api/models/useTotalDataManagement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useState } from 'react';
import { TotalDataManagementInterface } from 'request';
import { apiRequest } from '../instance';
import createWeekList from '@/utils/createWeekList';
import createWeeklyList from '@/utils/createWeeklyList';

export const useTotalDataManagement = () => {
const [totalData, setTotalData] = useState<TotalDataManagementInterface>({
Expand All @@ -18,17 +19,19 @@ export const useTotalDataManagement = () => {
apiRequest.get('/media'),
]);

const reportsData = dateFormat(reportsResponse);
const mediaData = dateFormat(mediaResponse);
const reportsFormattedData = dateFormat(reportsResponse);
const MediaFormattedData = dateFormat(mediaResponse);

setTotalData({
reports: reportsData,
media: mediaData,
reports: reportsFormattedData,
media: MediaFormattedData,
});
setWeekList(createWeekList(reportsData, mediaData));

console.log(createWeeklyList(reportsFormattedData, MediaFormattedData));
setWeekList(createWeekList(reportsFormattedData, MediaFormattedData));
} catch (error) {
console.log(error);
alert('데이터를 불러오는데 실패 하였습니다. 관리자에게 문의하세요');
/* alert('데이터를 불러오는데 실패 하였습니다. 관리자에게 문의하세요'); */
}
};

Expand Down
1 change: 0 additions & 1 deletion src/components/DropDownMenu/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ const DropDownMenu = () => {
const clickedWeekList = (event: React.MouseEvent<HTMLElement>) => {
console.log(event.currentTarget.textContent);
};

return (
<PopupState variant="popover" popupId="demo-popup-menu">
{(popupState) => (
Expand Down
1 change: 0 additions & 1 deletion src/pages/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export { default as DashboardPage } from './DashboardPage';
export { default as ManagementPage } from './ManagementPage';
export { default as DrawerTest } from './DrawerTest';
2 changes: 1 addition & 1 deletion src/routes/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import { Route, Routes, Navigate } from 'react-router-dom';
import Layout from '@/components/Layout';
import { DashboardPage, ManagementPage, DrawerTest } from '@/pages';
import { DashboardPage, ManagementPage } from '@/pages';

const Router = () => {
return (
Expand Down
5 changes: 2 additions & 3 deletions src/utils/createWeekList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const getStartDateOfWeek = (date: string) => {
let dataFns = new Date(date);
let startDate = '';
if (isMonday(dataFns)) {
startDate = format(new Date(date), 'yyyy년MM월dd일');
return (startDate = format(new Date(date), 'yyyy년MM월dd일'));
} else {
startDate = format(previousMonday(new Date(date)), 'yyyy년MM월dd일');
}
Expand All @@ -29,7 +29,7 @@ const getEndDateOfWeek = (date: string) => {
return endDate;
};

const makeWeekList = (date: string, weekArray: Array<string>) => {
const makeWeekList = (date: string, weekArray: string[]) => {
const startDateOfWeek = getStartDateOfWeek(date);
const endDateOfWeek = getEndDateOfWeek(date);
const periodOfWeek = `${startDateOfWeek} ~ ${endDateOfWeek}`;
Expand All @@ -53,7 +53,6 @@ const createWeekList = (
makeWeekList(media.date, weekArray);
});

console.log(weekArray);
return weekArray;
};

Expand Down
162 changes: 162 additions & 0 deletions src/utils/createWeeklyList.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
import {
format,
isMonday,
previousMonday,
isSunday,
nextSunday,
getUnixTime,
} from 'date-fns';
import { MediaInterface, ReportInterface, TotalData } from 'request';

type CombineDataType = ReportInterface | MediaInterface;

const addTypeAndYearMonthDate = (
data: CombineDataType,
dataType: 'Report' | 'Media'
) => {
const date = new Date(data.date);
const yearMonthDate = format(date, 'yyyy년MM월dd일');
const monthDate = format(date, 'MM월dd일');

return { ...data, yearMonthDate, monthDate, dataType };
};

const sortData = (
combineDataA: CombineDataType,
combineDataB: CombineDataType
) => {
const unixTimeA = getUnixTime(new Date(combineDataA.date));
const unixTimeB = getUnixTime(new Date(combineDataB.date));

return unixTimeB - unixTimeA;
};

const combineData = (
reportData: ReportInterface[],
mediaData: MediaInterface[]
) => {
const combinedData: CombineDataType[] = [];
reportData.forEach((report) => {
combinedData.push(addTypeAndYearMonthDate(report, 'Report'));
});

mediaData.forEach((media) => {
combinedData.push(addTypeAndYearMonthDate(media, 'Media'));
});

combinedData.sort(sortData);

return combinedData;
};

const multiDimensionalUnique = (dateList: string[][]) => {
const uniques = [];
const itemsFound: { [key: string]: boolean } = {};

for (const date of dateList) {
const stringified = JSON.stringify(date);
if (itemsFound[stringified]) continue;
uniques.push(date);
itemsFound[stringified] = true;
}

return uniques;
};

const getStartDateOfWeek = (date: string) => {
let startDate = new Date(date);
if (isMonday(startDate))
return [
format(startDate, 'yyyy-MM-dd'),
format(startDate, 'yyyy년MM월dd일'),
];

return [
format(previousMonday(startDate), 'yyyy-MM-dd'),
format(previousMonday(startDate), 'yyyy년MM월dd일'),
];
};

const getEndDateOfWeek = (date: string) => {
let endDate = new Date(date);
if (isSunday(endDate))
return [format(endDate, 'yyyy-MM-dd'), format(endDate, 'yyyy년MM월dd일')];

return [
format(nextSunday(endDate), 'yyyy-MM-dd'),
format(nextSunday(endDate), 'yyyy년MM월dd일'),
];
};

const makeWeekList = (date: string) => {
const [startDate, startDateOfWeek] = getStartDateOfWeek(date);
const [endDate, endDateOfWeek] = getEndDateOfWeek(date);
const periodOfWeek = `${startDateOfWeek} ~ ${endDateOfWeek}`;

return { periodOfWeek, startDate, endDate };
};

const makeDeduplicatedDateList = (combinedData: CombineDataType[]) => {
const dateList: string[][] = [];
[...new Set(combinedData.map((data) => data.date))].forEach((data) => {
const { periodOfWeek, startDate, endDate } = makeWeekList(data);
dateList.push([periodOfWeek, startDate, endDate]);
});

return multiDimensionalUnique(dateList);
};

const checkBetweenStartAndEndDate = ({
date,
startDate,
endDate,
}: {
date: string;
startDate: string;
endDate: string;
}) => {
return (
getUnixTime(new Date(date)) >= getUnixTime(new Date(startDate)) &&
getUnixTime(new Date(date)) <= getUnixTime(new Date(endDate))
);
};

const setWeeklyData = (
dataList: string[][],
combinedData: CombineDataType[]
) => {
const weeklyData: TotalData = {};

dataList.forEach(([periodOfWeek, startDate, endDate]) => {
const reportData = combinedData.filter(
(data) =>
data.dataType === 'Report' &&
checkBetweenStartAndEndDate({ date: data.date, startDate, endDate })
);

const mediaData = combinedData.filter(
(data) =>
data.dataType === 'Media' &&
checkBetweenStartAndEndDate({ date: data.date, startDate, endDate })
);

weeklyData[periodOfWeek] = {
reports: reportData as ReportInterface[],
media: mediaData as MediaInterface[],
};
});

return weeklyData;
};

const createWeeklyList = (
reportData: ReportInterface[],
mediaData: MediaInterface[]
) => {
const combinedData = combineData(reportData, mediaData);
const dataList = makeDeduplicatedDateList(combinedData);

return setWeeklyData(dataList, combinedData);
};

export default createWeeklyList;
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"isolatedModules": true, //각 파일을 분리된 모듈로 트랜스파일
"baseUrl": "./", // Non-relativ 모듈 혹은 paths 옵션의 기준 디렉토리
"resolveJsonModule": true, // 타입스크립트에서 json모듈 import 되도록 허용
"downlevelIteration": true,
"paths": {
// baseUrl 옵션을 기준디렉토리로 불러올 모듈의 위치 설정이 가능
"@/*": ["src/*"]
Expand Down

0 comments on commit ae3bd3a

Please sign in to comment.