import { Constant, Constants, Option } from "./types"; export default class SelectedDataProcessor { /** * selectedList, storedList를 인자로 받아 { label: string, value: string }[]를 return한다. * * selectedList의 length가 0이면 빈배열을 return한다. * [], [{ label: "아우토크립트", value: "123n4-2kweb" },{ label: "전체", value: "ALL" }] -> [] * selectedList의 length가 1이고 label이 전체가 아닐 경우 value를 array에 담아 return한다. * [{ label: "아우토크립트", value: "234-12b3-2" }], [{ label: "아우토크립트", value: "123n4-2kweb" },{ label: "전체", value: "ALL" }] -> ["234-12b3-2"] * selectedList의 length가 1이고 0번째 인덱스에 value가 ALL일 경우 sotredList에서 value를 array로 만들어 return한다. * [{ label: "전체", value: "ALL" }], [{ label: "아우토크립트", value: "123n4-2kweb" },{ label: "전체", value: "ALL" }] -> ["123n4-2kweb", "ALL"] * selectedList에 length가 2 이상일 경우 selectedList의 value들을 array로 만들어 return한다. * [ { label: "전체", value: "ALL" }, { label: "이지차저", value: "kkkkk" },], [ { label: "이지차저", value: "kkkkk" },{ label: "아우토크립트", value: "123n4-2kweb" },{ label: "전체", value: "ALL" }] * -> ["ALL", "kkkkk"] * * @public * @static * @param selectedList * @param storedList * @return array */ static convertOperatingInstitutionList( selectedList: { label: string; value: string }[], storedList: { label: string; value: string }[] ): any[] { if (0 === selectedList.length) { return []; } if (1 === selectedList.length) { return "ALL" === selectedList[0].value ? storedList.map((item: { label: string; value: string }) => item.value) : [selectedList[0].value]; } else { return selectedList.map( (item: { label: string; value: string }) => item.value ); } } /** * * data를 인자로 넘기면 description을 label로 index를 value로 하여 array에 담아 return한다 * * { option1: { index: 1, text: "Text 1", description: "Description 1" },option2: { index: 2, text: "Text 2", description: "Description 2" }, option3: { index: 3, text: "Text 3", description: "Description 3" },} * -> [{ label: "Description 1", value: 1 }, { label: "Description 2", value: 2 }, { label: "Description 3", value: 3 }] * * @static * @public * @param data Constants * @return array */ static convertSelectOptions(data: Constants): Option[] { return Object.values(data).map((obj: Constant) => ({ label: obj["description"], value: obj["index"], })); } /** * data로 넘긴 object의 property 중 labelKey와 valueKey에 넘겨진 인자에 따라 label과 value의 값을 넣어 return한다. * * { option1: { name: "Label 1", id: "value 1" }, option2: { name: "Label 2", id: "value 2" }, option3: { name: "Label 3", id: "value 3" }} * -> [{ label: "Label 1",value: "value 1",}, {label: "Label 2",value: "value 2",},{label: "Label 3",value: "value 3",}] * * @static * @public * @param data * @param labelKey * @param valueKey * @return array */ static convertSelectOptionsWithKeyValue( data: any, labelKey: string, valueKey: string ): Option[] { return Object.values(data).map( (obj: any): Option => ({ label: obj[labelKey], value: obj[valueKey], }) ); } /** * * ALL을 선택했을 때 필드에 저장한다. * * newValue의 value중 ALL이 있고 isList가 true이면 operatingInstitutionIdList 필드에 [{label: '전체', value: 'ALL'}]을 인자로 넣어 실행한다. * [{ label: "아우토크립트", value: "234-12b3-2" }], [ { label: "아우토크립트", value: "" },{ label: "전체", value: "ALL" }], true * -> "operatingInstitutionIdList" 필드에 [{ label: "전체", value: "ALL" }] 저장 * oldValue가 전체인데 newValue에 새로운 data가 들어올 경우 새로운 데이터를 저장하고 전체는 삭제한다. isList가 false이면 operatingInstitution필드에 저장한다. * [{ label: "전체", value: "ALL" }], [ { label: "아우토크립트", value: "" },{ label: "전체", value: "ALL" }], false * -> "operatingInstitution" 필드에 [{ label: "아우토크립트", value: "" }]를 저장 * newValue에 ALL이 없을 경우 빈 스트링을 return한다 * [{ label: "전체", value: "ALL" }], [] -> return [] * * @static * @public * @return array * @param oldValue * @param newValue * @param setFieldValue * @param isList */ static handleSelectAll( oldValue: any[], newValue: any[], setFieldValue: (field: string, value: any) => void, isList?: boolean ) { if (oldValue && 0 !== oldValue?.length) { const oldValueHasAll = oldValue.find((obj: any) => obj.value === "ALL"); const newValueHasAll = newValue.find((obj: any) => obj.value === "ALL"); if (!newValueHasAll) { // 새로운 값에 all이 없을 때 return ""; } else { if (!oldValueHasAll) { // 이전 값이 all이 없을 때 setFieldValue( isList ? "operatingInstitutionIdList" : "operatingInstitution", [{ label: "전체", value: "ALL" }] ); } if (oldValueHasAll) { // 이전 값이 all이 있을 때(all 이 아닌 다른 값이 선택됐을 때) const allIndex = newValue.findIndex((obj: any) => { return obj.value === "ALL"; }); newValue.splice(allIndex, 1); setFieldValue( isList ? "operatingInstitutionIdList" : "operatingInstitution", newValue ); } } } } /** * arr의 인자값으로 넘어간 배열에서 'ALL'인 것들은 찾아 삭제한 후 배열을 return한다. * * [ { label: "아우토크립트", value: "" },{ label: "전체", value: "ALL" }] -> [ { label: "아우토크립트", value: "" }] * * @static * @public * @return array * @param arr */ static removeAllOptionInList(arr?: Array) { if (arr) { const index = arr.findIndex((obj: any) => obj === "ALL"); arr.splice(index, 1); } return arr; } /** * newValue가 빈배열 경우 전체를 담아 return한다. * [{ label: "전체", value: "ALL" }], [] -> "operatingInstitution"필드에 [{ label: "전체", value: "ALL" }]를 set한다. * oldValue에는 ALL이 없고 newValue에는 ALL이 있을 경우 operatingInstitution필드에 ALL을 set한다. * [{ label: "차밥스", value: "ekekek-111" }], [{ label: "전체", value: "ALL" }] -> "operatingInstitution"필드에 [{ label: "전체", value: "ALL" }]를 set한다. * oldValue에 ALL이 있고 newValue에 ALL과 새로운 데이터가 있다면 새로운 데이터를 operatingInstitution필드에 set한다. * [{ label: "전체", value: "ALL" }], [ { label: "전체", value: "ALL" },{ label: "차밥스", value: "ekekek-111" }] -> "operatingInstitution"필드에 [{ label: "차밥스", value: "ekekek-111" }]를 set한다. * * @public * @return array * @param oldValue 이전에 선택한 value array * @param newValue 새로 선택한 value array * @param setFieldValue form의 field의 value를 set할 수 있는 함수 * @param allValue label이 '전체'일때 value값 */ static selectAll( oldValue: any[], newValue: any[], setFieldValue: (field: string, value: any) => void, allValue = "ALL" ) { if (oldValue && 0 !== oldValue?.length) { const oldValueHasAll = oldValue.find( (obj: any) => obj.value === "ALL" || obj.value === null ); const newValueHasAll = newValue.find( (obj: any) => obj.value === "ALL" || obj.value === null ); if (!newValueHasAll) { // 새로운 값에 all이 없을 때 if (!newValue.length) { setFieldValue("operatingInstitution", [ { label: "전체", value: allValue }, ]); } } else { if (!oldValueHasAll) { // 이전 값이 all이 없을 때 setFieldValue("operatingInstitution", [ { label: "전체", value: allValue }, ]); } if (oldValueHasAll) { // 이전 값이 all이 있을 때(all 이 아닌 다른 값이 선택됐을 때) const allIndex = newValue.findIndex((obj: any) => { return obj.value === "ALL" || obj.value === null; }); newValue.splice(allIndex, 1); setFieldValue("operatingInstitution", newValue); } } } } }