UNPKG

2.16 kBJavaScriptView Raw
1import { useEventEmitter } from './event-emitter.js';
2import { STEP_PREPARE, STEP_READY, STEP_RUN } from './constant.js';
3
4const eventEmitter = useEventEmitter();
5
6eventEmitter.on('update:user-info', (userInfo) => {
7 localStore.collector && localStore.collector.updateUserInfo({ ...userInfo });
8});
9
10const stepMapper = {
11 STEP_PREPARE,
12 STEP_READY,
13 STEP_RUN
14};
15
16// 공통 데이터 제어
17const localStore = {
18 currentStepIndex: 0, // 0 : 사용자 정보 입력, 1: 시험 대기, 2: 시험 중
19 steps: [STEP_PREPARE, STEP_READY, STEP_RUN],
20 communicator: null,
21 collector: null,
22 userData: null,
23 apiBaseUrl: ''
24};
25
26function init() {
27 if (window.nhnCDSDK) {
28 localStore.communicator = new window.nhnCDSDK.Communicator();
29 }
30 showCurrentStep();
31}
32
33function showCurrentStep() {
34 [...document.querySelectorAll('.step')].forEach((step) => {
35 if (stepMapper[step.dataset.step] === localStore.steps[localStore.currentStepIndex]) {
36 step.style.display = 'block';
37 } else {
38 step.style.display = 'none';
39 }
40 });
41}
42
43const store = {
44 getApiBaseUrl() {
45 return localStore.apiBaseUrl;
46 },
47 setApiBaseUrl(apiBaseUrl) {
48 localStore.apiBaseUrl = apiBaseUrl;
49 },
50 getCommunicator() {
51 return localStore.communicator;
52 },
53 getCollector() {
54 return localStore.collector;
55 },
56 setCollector(collector) {
57 localStore.collector = collector;
58 },
59 setUserData(userData) {
60 localStore.userData = userData;
61 },
62 getUserData() {
63 return localStore.userData;
64 },
65 nextStep() {
66 if (localStore.currentStepIndex >= localStore.steps.length - 1) {
67 return;
68 }
69 localStore.currentStepIndex++;
70 eventEmitter.fire('change:step', localStore.steps[localStore.currentStepIndex]);
71 showCurrentStep();
72 },
73 prevStep() {
74 if (localStore.currentStepIndex <= 0) {
75 return;
76 }
77 localStore.currentStepIndex--;
78 eventEmitter.fire('change:step', localStore.steps[localStore.currentStepIndex]);
79 showCurrentStep();
80 if (localStore.currentStepIndex <= 0) {
81 this.userData = null;
82 }
83 }
84};
85
86init();
87
88export function useGlobalStore() {
89 return store;
90}
91
92window.store = store;