Skip to content

Commit

Permalink
fix<dust> : PmType 변경에 따른 미세먼지,초미세먼지 관련 오류 수정
Browse files Browse the repository at this point in the history
-  실시간 미세먼지 현황을 PmType으로 변경해줄 함수(getApNowPmType) 생성
-  getPmGrade 함수명을 getApFcstPmType으로 수정
- GRADE_ARRAY 삭제
  • Loading branch information
BadaHertz52 committed Nov 29, 2023
1 parent 1a9b99c commit 87da55c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 21 deletions.
2 changes: 0 additions & 2 deletions src/constants/weather.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,3 @@ export const PM_STATE = {
veryBad: { name: "매우 나쁨", color: "#ef5350" },
undefined: { name: "정보없음", color: "#6d6d6d" },
};

export const GRADE_ARRAY = Object.keys(PM_STATE) as PmType[];
49 changes: 30 additions & 19 deletions src/modules/api/forecast/dust.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
//미세먼지 관련 예보 수집
import {
AP_INFORMATION_API,
GRADE_ARRAY,
INQURY_AIR,
} from "../../../constants";
import { AP_INFORMATION_API, INQURY_AIR, PM_STATE } from "../../../constants";
import { WEST_AREA } from "../../../constants/areaCode";
import { SFGridItem } from "../../position";
import {
Expand All @@ -17,6 +13,20 @@ import { getAPIItems } from "../index";
import { PmType } from "../../weather";
import { changeSearchDate } from "./time";

const getApNowPmType = (pm: string): PmType => {
switch (Number(pm)) {
case 1:
return "good";
case 2:
return "common";
case 3:
return "bad";
case 4:
return "veryBad";
default:
return "common";
}
};
/**
* 실시간 미세먼지, 초미세먼지 등급을 반환하는 함수
* @param sidoName
Expand All @@ -39,8 +49,8 @@ export const getApNow = async (
stationName.includes(i.stationName)
)[0];
const pm: PmGrade = {
pm10Grade: GRADE_ARRAY[Number(targetItem.pm10Grade) - 1],
pm25Grade: GRADE_ARRAY[Number(targetItem.pm25Grade) - 1],
pm10Grade: getApNowPmType(targetItem.pm10Grade),
pm25Grade: getApNowPmType(targetItem.pm25Grade),
};

return pm;
Expand All @@ -63,11 +73,9 @@ export const findApFcstArea = (
case "강원":
if (pt2 == null || !WEST_AREA.includes(pt2)) {
// 강원도영동

return "영동";
} else {
//강원도영서

return "영서";
}
case "경기":
Expand Down Expand Up @@ -115,7 +123,7 @@ export const findApFcstArea = (
}
};

const getPmGrade = (
const getApFcstPmType = (
pmData: string,
apFcstArea: ApFcstAreaCode,
sidoName: ApiAreaCode
Expand All @@ -124,13 +132,16 @@ const getPmGrade = (
const sliced1 = pmData.slice(indexOfSido + sidoName.length + 1);
const indexOfComma = sliced1.indexOf(",");
const sliced2 = sliced1.slice(0, indexOfComma);
const grade = GRADE_ARRAY.map((g: PmType) => {
if (g !== null && sliced2.includes(g)) {
return g;
} else {
return null;
}
}).filter((i: PmType) => i !== null)[0];
const grade = Object.entries(PM_STATE)
.map((g) => {
const [key, value] = g;
if (sliced2.includes(value.name)) {
return key as PmType;
} else {
return null;
}
})
.filter((i: PmType) => i !== null)[0];
return grade;
};

Expand Down Expand Up @@ -168,8 +179,8 @@ export const getApFcst = async (
(i: ApFcstItem) => i.informCode === "PM25"
)[0].informGrade;

const pm10Grade = getPmGrade(pm10Fcst, apFcstArea, sidoName);
const pm25Grade = getPmGrade(pm25Fcst, apFcstArea, sidoName);
const pm10Grade = getApFcstPmType(pm10Fcst, apFcstArea, sidoName);
const pm25Grade = getApFcstPmType(pm25Fcst, apFcstArea, sidoName);
const pmData: PmGrade = {
pm10Grade: pm10Grade,
pm25Grade: pm25Grade,
Expand Down

0 comments on commit 87da55c

Please sign in to comment.