Last active
October 15, 2019 06:20
-
-
Save amorfati0310/be2ed6a88b82510aa807852e4b4d9ff6 to your computer and use it in GitHub Desktop.
calendar.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default { | |
namespaced: true, | |
state: { | |
dateSet: [], // 캘린더 날짜 비교를 위한 배열 | |
updatedDateId: null, | |
}, | |
mutations: { | |
updateDateSet(state, data) { | |
state.updatedDateId = data.id; | |
switch (state.dateSet.length) { | |
case 0: { | |
state.dateSe = state.dateSet.concat(data); | |
break; | |
} | |
case 1: { | |
if (data.id === state.dateSet[0].id){ | |
state.dateSet = [data]; | |
} else { | |
state.dateSe = state.dateSet.concat(data); | |
} | |
break; | |
} | |
case 2: { | |
state.dateSet = state.dateSet.map(date => { | |
return date.id === data.id ? data : date; | |
}); | |
break; | |
} | |
default: return state.dateSet; | |
} | |
} | |
}, | |
actions: { | |
updateDataSetAction({ commit }, payload) { | |
commit('updateDateSet', payload); | |
} | |
}, | |
getters: { | |
isValidDateSetGap: state => { | |
const sorted = [...state.dateSet].sort((a, b) => a.id - b.id); | |
return sorted.length === 2 ? sorted[1].date - sorted[0].date >= 0 : true; | |
}, | |
dateErrorMessage: (state, getters) => { | |
const hasSet = state.dateSet && state.dateSet.length === 2; | |
if (hasSet) { | |
if (!getters.isValidDateSetGap) { | |
const sorted = [...state.dateSet].sort((a, b) => a.id - b.id); | |
const lastDate = sorted[1]; | |
const startDate = sorted[0]; | |
const isLastSelected = state.updatedDateId === sorted[1].id; | |
if (isLastSelected) { | |
return `${lastDate.title} 기한는(은) ${startDate.title} 날짜 이후로 설정해주세요.`; | |
} else { | |
return `${startDate.title} 기한는(은) ${lastDate.title} 날짜 이전로 설정해주세요.`; | |
} | |
} | |
} else { | |
return ''; | |
} | |
} | |
}, | |
} |
state.dateSet.push(data);
=> state.dateSet = [...state.dataSet, data];
요새는 push는 잘 안 쓰는? 추세로 알고 있습니다.
감사합니다 ~~ :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
state.dateSet.push(data);
=> state.dateSet = [...state.dataSet, data];
요새는 push는 잘 안 쓰는? 추세로 알고 있습니다.