Skip to content

Commit

Permalink
refactor: 코드 이뿌게 이뿌게
Browse files Browse the repository at this point in the history
  • Loading branch information
potados99 committed Feb 17, 2022
1 parent 763e57e commit af0abbd
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 41 deletions.
9 changes: 0 additions & 9 deletions src/features/checkin/CheckIn.vue
Original file line number Diff line number Diff line change
Expand Up @@ -142,15 +142,6 @@ export default {
width: 100%;
}
.overlay-top {
display: flex;
flex-direction: row;
justify-content: center;
align-items: center;
padding: 5px 8px 0;
font-size: 12px;
}
.overlay-section-container {
display: flex;
flex: 1;
Expand Down
19 changes: 16 additions & 3 deletions src/features/checkin/CheckInRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,33 @@ import http from '@/core/request/http';
class CheckInRepository {
private previousEventSource?: EventSource = undefined;

/**
* 특정 식당에 대해 체크인 Context를 관찰합니다.
*
* @param cafeteriaId 관찰할 식당의 식별자.
* @param onContext 새 context 도착시 실행할 콜백.
*/
listenForContext(cafeteriaId: number, onContext: (context: Context) => void) {
this.previousEventSource?.close();

const eventSource = new EventSource(config.api.endpoints.checkInContext(cafeteriaId), {withCredentials: true});

eventSource.addEventListener('context', event => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
onContext(Context.fromResponse(JSON.parse(event.data)));
const context = Context.fromResponse(JSON.parse(event['data']));

onContext(context);
});

this.previousEventSource = eventSource;
}

/**
* 체크인 요청을 날립니다.
*
* @param ticket 예약 티켓.
* @param cafeteriaId 요청을 보내는 현재 위치한 식당의 식별자.
* @param gracefulInTime "시간 봐줘" 모드.
*/
async checkIn(ticket: string, cafeteriaId: number, gracefulInTime: boolean) {
await http.post(config.api.endpoints.checkIn, {ticket, cafeteriaId, gracefulInTime});
}
Expand Down
2 changes: 1 addition & 1 deletion src/features/checkin/Context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export default class Context {
return Object.assign(new Context(), properties);
}

static fromResponse(raw: Record<string, any>) {
static fromResponse(raw: Record<string, never>) {
const {capacity, expected, actual, total, timeSlotStart, timeSlotEnd} = raw;

return Context.of({
Expand Down
4 changes: 4 additions & 0 deletions src/features/checkin/mixins/CafeteriaSelectionMixin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ export default Vue.extend({
this.selectionDialog = false;
},

/**
* 서버로부터 예약을 지원하는 식당 목록을 불러온 다음,
* 현재 선택된 식당을 복구(또는 초기화)합니다.
*/
async fetchCafeteria() {
const allCafeteria = await Cafeteria.find();
const cafeteriaSupportingBooking = allCafeteria.filter(c => c.supportBooking);
Expand Down
6 changes: 6 additions & 0 deletions src/features/checkin/mixins/CheckInContextMixin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ export default Vue.extend({
},

watch: {
/**
* selectedCafeteria가 변할 때마다,
* 해당 식당에 대한 Context를 구독하는 새로운 이벤트 소스를 등록합니다.
*
* @param selected 선택된 식당.
*/
selectedCafeteria(selected: Cafeteria) {
console.log(`카페테리아 ${selected.id}의 Context를 관찰하는 이벤트 소스 등록!`);

Expand Down
82 changes: 57 additions & 25 deletions src/features/checkin/mixins/CheckInRequestMixin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ export default Vue.extend({
* 서버와 통신하고, 결과를 반환만 하면 나머지는 다른 친구들이 다 해줍니다.
* 네트워크 타고 처리 끝났으면 최대한 신속히 반환합니다.
*
* @param ticket
* @param gracefulInTime
* @param ticket 예약 티켓.
* @param gracefulInTime "시간 조금 다른건 봐줘" 모드.
*/
async tryCheckInAndGetError(ticket: string, gracefulInTime: boolean): Promise<HttpError | undefined> {
try {
Expand All @@ -87,40 +87,72 @@ export default Vue.extend({
this.checkInLoading = false;
},

/**
* 반환된 결과를 가지고 사용자에게 표시하거나 추가 조치를 취합니다.
* @param error (존재할 경우) 발생한 에러.
* @param ticket 요청에 포함된 티켓.
*/
async dealWithResult(error: HttpError | undefined, ticket: string) {
if (error == null) {
this.showSuccessAndAutoDismiss();
await this.showSuccessAndAutoDismiss();
} else {
if (error.error === 'check_in_not_in_time') {
const allow = await this.$dialog.showAndWait(BigDialog, {
level: 2,
title: '⚠️ 앗 잠시만요...!',
text: `${error.message} 체크인을 진행할까요?`,
positiveButtonText: '진행',
neutralButtonText: '중단',
showClose: false,
});

if (allow === true) {
await this.requestCheckIn(ticket, true);
}
} else {
await this.$dialog.showAndWait(BigDialog, {
level: 3,
title: '체크인을 진행할 수 없습니다.',
text: `${error.message}`,
neutralButtonText: '닫기',
showClose: false,
});
switch (error.error) {
case 'check_in_not_in_time':
await this.checkInNotInTime(error, ticket);
break;

case 'check_in_not_in_place':
await this.checkInNotInPlace(error);
break;

default:
await this.checkInUnavailable(error);
break;
}
}
},

showSuccessAndAutoDismiss() {
async showSuccessAndAutoDismiss() {
this.checkInSuccess = true;

setTimeout(() => {
this.checkInSuccess = null;
}, 750);
},

async checkInNotInTime(error: HttpError, ticket: string) {
const allow = await this.$dialog.showAndWait(BigDialog, {
level: 2,
title: '⚠️ 앗 잠시만요...!',
text: `${error.message} 체크인을 진행할까요?`,
positiveButtonText: '진행',
neutralButtonText: '중단',
showClose: false,
});

if (allow === true) {
await this.requestCheckIn(ticket, true);
}
},

async checkInNotInPlace(error: HttpError) {
await this.$dialog.showAndWait(BigDialog, {
level: 3,
title: '다른 식당의 예약입니다.',
text: `${error.message}`,
neutralButtonText: '닫기',
showClose: false,
});
},

async checkInUnavailable(error: HttpError) {
await this.$dialog.showAndWait(BigDialog, {
level: 3,
title: '체크인을 진행할 수 없습니다.',
text: `${error.message}`,
neutralButtonText: '닫기',
showClose: false,
});
},
},
});
2 changes: 1 addition & 1 deletion src/features/checkin/mixins/ScannerBaseMixin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export default Vue.extend({
} catch (e) {
this.scannerError = e;

console.error(e);
console.log(`스캐너 에러 발생: ${e}`);
}
},

Expand Down
14 changes: 12 additions & 2 deletions src/utils/audio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@ export async function unlockAudio() {
sound.src =
'data:audio/mpeg;base64,SUQzBAAAAAABEVRYWFgAAAAtAAADY29tbWVudABCaWdTb3VuZEJhbmsuY29tIC8gTGFTb25vdGhlcXVlLm9yZwBURU5DAAAAHQAAA1N3aXRjaCBQbHVzIMKpIE5DSCBTb2Z0d2FyZQBUSVQyAAAABgAAAzIyMzUAVFNTRQAAAA8AAANMYXZmNTcuODMuMTAwAAAAAAAAAAAAAAD/80DEAAAAA0gAAAAATEFNRTMuMTAwVVVVVVVVVVVVVUxBTUUzLjEwMFVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVf/zQsRbAAADSAAAAABVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVf/zQMSkAAADSAAAAABVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV';

await sound.play();
try {
await sound.play();
} catch (e) {
console.log(`오디오 언락 중 에러 발생: ${e}`);
}

sound.pause();
sound.currentTime = 0;

Expand All @@ -40,5 +45,10 @@ document.body.addEventListener('touchstart', unlockAudio);

export async function playSound(src: string) {
sound.src = src;
await sound.play();

try {
await sound.play();
} catch (e) {
console.log(`오디오 재생 중 에러 발생: ${e}`);
}
}

0 comments on commit af0abbd

Please sign in to comment.