25-03-11
๐ Daily Reportโ
๐ ํ๋ก์ ํธ ์ํฉโ
์ปจ๋์ ์ด ์์ข์์ง๊ณ ์๋ค. ๋ง๊ฐ์ด 2์ฃผ๋ ์๋จ์๋๋ฐ ์ข์ ์ ํธ๋ ์๋๋ค.
git ๋ช ๋ น์ด๋ฅผ ์ด๋ฐ๊ตฌ๋ก ์น ์ ๋๋ก ์ฝ๊ฐ ๋ฉํ๋ค. ใ ใ ใ ใ ใ ใ ใ ใ ใ ใ 
๊ทธ๋๋ ๊ฐ์ข ์คํํฉ ๊ฝ์ผ๋๊น ๋ก์ง ์์ฑ์ ๋๊ณ ์์! ๐
๐ ํ์๊ฐ์ ์ ํจ์ฑ ๊ฒ์ฌโ
๊ฐ์ ์ ํจ์ฑ ๊ฒ์ฌ ์ง์ง ๋ณดํต์ด ์๋๋๋ผ.
DOM ์ปจํธ๋กค๊น์ง ํด์ ๊ทธ๋ฐ๊ฐ? ์ ๋ง ์๊ฐ ์ก์๋จน๋๊ฒ
๋๋จํ์ง ์์ ์์ํ ๋ก์ง์ ๋ญํฑ์ด(?)์ธ๋ฐ, JS๋ก view์์ ๊ฒ์ฌํ๋ค๊ณ ๋์ด ์๋๋ผ
์ง์ง ์ ๊ฒฝ์ ์ผ๋ค๋ฉด ๋ฐฑ ๋จ๊น์ง ๊ฒ์ฌํด์ผ ํ๋๊ฑฐ๋ผ
์ง์ฌ ํ์ฌ ์ํฉ์์ ๋ณดํต ๊ท์ฐฎ์ ์์ ์ด ์๋ ์๊ฐ ์๋ค;
...ํ๋จ์ ์ค๋๊น์ง ์ง ์ฝ๋์ธ๋ฐ ์ด๊ฑฐ 60ํผ๋ฐ์ ๊ตฌํ ์๋ ์ํ์ด๋ค.
const API_GATEWAY_HOST = "http://localhost:9093";
/* ๊ฐ์
 request ๊ฐ๋ฅํ ์ํ์ธ์ง check */
let CHECK_EMAIL = false;
let CHECK_PW = false;
let CHECK_PHONE = false;
let CHECK_NAME = false;
let CHECK_GENDER = false;
let CHECK_BIRTH = false;
/* ๋ค๋ก ๊ฐ๊ธฐ, ์์ฑ ์ทจ์ ๋ฒํผ ํด๋ฆญ์ ์๋ํ๋ ํจ์ */
function goBack() {
    const ok = confirm("๊ฐ์
์ ์ทจ์ํ๊ณ  ๋ค๋ก ๊ฐ์๊ฒ ์ต๋๊น?");
    if (ok) history.back();
}
/* ๊ฐ์
ํ๊ธฐ ๋ฒํผ ํด๋ฆญ์ ์๋ํ๋ ํจ์ */
function SignUp() {
    let requestBody = {
        member_id: memberId,
        member_pw: memberPw
    };
}
/*****************************************************************
    [๋ฒํผ ์ด๋ฒคํธ ํจ์]
    ์ด๋ฉ์ผ ์ธ์ฆ ๋ฒํผ ํด๋ฆญ ์ ์๋
*/
async function handleEmailCodeButtonClicked() {
    const email = document.getElementById('email-input').value;
    console.log('email input:', email);
    // ์ด๋ฉ์ผ ํฌ๋งท ๊ฒ์ฌ
    if (checkEmailInput(email) === false) {
        alert("์ด๋ฉ์ผ ํ์์ด ์ฌ๋ฐ๋ฅด์ง ์์ต๋๋ค.");
        return;
    }
    // ์ธ์ฆ์ฝ๋ ๋ฐ์ก (์ฌ๊ธฐ์  await ํ์ง ์...๋๊ฒ ์ข์๋ฐ ๋ฐ์๋ ์ผ๋จ ํด๋์)
    await sendEmailCode();
    // ์ด๋ฉ์ผ input readonly ์ฒ๋ฆฌ + ์ด๋ฉ์ผ ์ธ์ฆ ๋ฒํผ ๋นํ์ฑํ
    let emailInput = document.getElementById('email-input');
    let emailCheckButton = document.getElementById('email-check-button');
    emailInput.readOnly = true;
    emailCheckButton.innerText = "์ธ์ฆ ์ด๊ธฐํ";
    emailCheckButton.classList.add('active');
    emailCheckButton.onclick = resetEmailVerification; // onclick ํจ์๋ฅผ ์ธ์ฆ ์ด๊ธฐํ ํจ์๋ก ๋ณ๊ฒฝ
    // global check ๋ณ์
    CHECK_EMAIL = true;
    // ์ด๋ฉ์ผ input ํ๋จ์ ์ธ์ฆ์ฝ๋ ์
๋ ฅ input๊ณผ [์ธ์ฆ ํ์ธ] ๋ฒํผ ํ์
    if (!document.getElementById('email-verification-box')) { // ํ์ฌ DOM์ ์์ผ๋ฉด
        let newChildDiv = document.createElement('div');
        newChildDiv.classList.add("group-content-box");
        newChildDiv.id = 'email-verification-box';
        let childInnerHtml = `
            <div class="group-content-label"></div>
            <input id="email-code-input" type="text" placeholder="5๋ถ ๋ด์ ๋ฐ์ก๋ ์ฝ๋๋ฅผ ์
๋ ฅํด์ฃผ์ธ์." />
            <button class="signup-check-button" id="email-code-check-button" onclick="validateEmailCode()">์ธ์ฆ ํ์ธ</button>
        `;
        newChildDiv.innerHTML = childInnerHtml;
        let signupGroup = document.getElementById('signup-group-top');
        signupGroup.insertBefore(newChildDiv, signupGroup.children[2]);
    }
}
// API ํธ์ถํด์ ์ธ์ฆ ์ฝ๋ ๋ณด๋ด๋ณด๊ณ , ์คํจํ๋ฉด view ๋๋๋ฆฌ๋ ํจ์
async function sendEmailCode() {
    try {
        await getRequest(API_GATEWAY_HOST + "/v2/auth/email/code?to=" + email);
    } catch (error) {
        // error๋ getRequest ์์์ logging ๋์์.
        // ์ธ์ฆ ์ฝ๋ ๋ฐ์ก ์คํจํ์ผ๋ฏ๋ก view ๋๋ ค๋๊ธฐ
        resetEmailVerification();
    }
}
// ์ด๋ฉ์ผ ํฌ๋งท ๊ฒ์ฌ ํจ์
function checkEmailInput(email) {
    const emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
    return emailPattern.test(email);
}
// ์ด๋ฉ์ผ input readonly ํ๊ณ  + check button ๋๋ ค๋๋ ํจ์
function resetEmailVerification() {
    let emailInput = document.getElementById('email-input');
    let emailCheckButton = document.getElementById('email-check-button');
    let boxDiv = document.getElementById('email-verification-box');
    // global check ๋ณ์
    CHECK_EMAIL = false;
    emailInput.readOnly = false;
    emailCheckButton.innerText = "์ด๋ฉ์ผ ์ธ์ฆ";
    emailCheckButton.classList.remove('active');
    emailCheckButton.onclick = handleEmailCodeButtonClicked;
    boxDiv.remove(); // ์ธ์ฆ ์ฝ๋ ์์ญ ์ ๊ฑฐ
}
/*****************************************************************
    [๋ฒํผ ์ด๋ฒคํธ ํจ์]
    ์ด๋ฉ์ผ ์ธ์ฆ ํ์ธ ๋ฒํผ ํด๋ฆญ ์ ์๋
*/
async function validateEmailCode() {
    // ์
๋ ฅ๋ ์ฝ๋ ๊ฐ ๊ฐ์ ธ์ค๊ธฐ
    const input = document.getElementById('email-code-input');
    const code = input.value;
    // ์ธ์ฆ ํ์ธ
    try {
        await getRequest(API_GATEWAY_HOST + "/v2/auth/email/code?to=" + code);
        let button = document.getElementById('email-code-check-button');
        button.innerText = "์ธ์ฆ ์๋ฃ";
        button.classList.add("disable");
        button.onclick = null;
    } catch (error) {
        console.error(error);
        alert(error.message);
        // readonly ํ๊ณ  ์ธ์ฆ๋ฒํผ ๋๋ ค๋๊ธฐ
        resetEmailVerification();
    }
}
/*****************************************************************
    [๋ฒํผ ์ด๋ฒคํธ ํจ์]
    ํด๋ํฐ ์ธ์ฆ ๋ฒํผ ํด๋ฆญ ์ ์๋
*/
async function handlePhoneCodeButtonClicked() {
    const phonePrefix = document.getElementById('phone-prefix')
    const phoneInput = document.getElementById('phone-input')
    const phone = phonePrefix.value + phoneInput.value;
    console.log('phone prefix + input:', phone);
    // ์ธ์ฆ์ฝ๋ ๋ฐ์ก (์ฌ๊ธฐ๋ await ํ์ง ์๋๊ฒ ์ข์๋ฐ ๋ฐ์๋ ์ผ๋จ ํด๋์)
    await sendPhoneCode();
    // prefix disable & input readonly ์ฒ๋ฆฌ + ํด๋ํฐ ์ธ์ฆ ๋ฒํผ ๋นํ์ฑํ
    let phoneCheckButton = document.getElementById('phone-check-button');
    phonePrefix.disabled = true;
    phoneInput.readOnly = true;
    phoneCheckButton.innerText = "์ธ์ฆ ์ด๊ธฐํ";
    phoneCheckButton.classList.add('active');
    phoneCheckButton.onclick = resetPhoneVerification; // onclick ํจ์๋ฅผ ์ธ์ฆ ์ด๊ธฐํ ํจ์๋ก ๋ณ๊ฒฝ
    // global check ๋ณ์
    CHECK_PHONE = true;
    // ํด๋ํฐ ๋ฒํธ input ํ๋จ์ ์ธ์ฆ์ฝ๋ ์
๋ ฅ input๊ณผ [์ธ์ฆ ํ์ธ] ๋ฒํผ ํ์
    if (!document.getElementById('phone-verification-box')) { // ํ์ฌ DOM์ ์์ผ๋ฉด
        let newChildDiv = document.createElement('div');
        newChildDiv.classList.add("group-content-box");
        newChildDiv.id = 'phone-verification-box';
        let childInnerHtml = `
            <div class="group-content-label"></div>
            <input id="phone-code-input" type="text" placeholder="30๋ถ ๋ด์ ๋ฐ์ก๋ ์ฝ๋๋ฅผ ์
๋ ฅํด์ฃผ์ธ์." />
            <button class="signup-check-button" id="phone-code-check-button" onclick="validatePhoneCode()">์ธ์ฆ ํ์ธ</button>
        `;
        newChildDiv.innerHTML = childInnerHtml;
        let signupGroup = document.getElementById('signup-group-bottom');
        signupGroup.insertBefore(newChildDiv, signupGroup.children[2]);
    }
}
// API ํธ์ถํด์ ์ธ์ฆ ์ฝ๋ ๋ณด๋ด๋ณด๊ณ , ์คํจํ๋ฉด view ๋๋๋ฆฌ๋ ํจ์
async function sendPhoneCode(phone) {
    try {
        await getRequest(API_GATEWAY_HOST + "/v2/auth/phone/code?to=" + phone);
    } catch (error) {
        // error๋ getRequest ์์์ logging ๋์์.
        // ์ธ์ฆ ์ฝ๋ ๋ฐ์ก ์คํจํ์ผ๋ฏ๋ก view ๋๋ ค๋๊ธฐ
        resetPhoneVerification();
    }
}
/*****************************************************************
    [๋ฒํผ ์ด๋ฒคํธ ํจ์]
    ํด๋ํฐ ์ธ์ฆ ํ์ธ ๋ฒํผ ํด๋ฆญ ์ ์๋
*/
async function validatePhoneCode() {
    // ์
๋ ฅ๋ ์ฝ๋ ๊ฐ ๊ฐ์ ธ์ค๊ธฐ
    const code = document.getElementById('phone-code-input').value;
    // ์ธ์ฆ ํ์ธ
    await getRequest(API_GATEWAY_HOST + "/v2/auth/phone/code?to=" + code);
}
function resetPhoneVerification() {
    let phonePrefix = document.getElementById('phone-prefix');
    let phoneInput = document.getElementById('phone-input');
    let phonelCheckButton = document.getElementById('phone-check-button');
    let boxDiv = document.getElementById('phone-verification-box');
    // global check ๋ณ์
    CHECK_PHONE = false;
    phonePrefix.disabled = false;
    phoneInput.readOnly = false;
    phonelCheckButton.innerText = "ํด๋ํฐ ์ธ์ฆ";
    phonelCheckButton.classList.remove('active');
    phonelCheckButton.onclick = handlePhoneCodeButtonClicked;
    boxDiv.remove(); // ์ธ์ฆ ์ฝ๋ ์์ญ ์ ๊ฑฐ
}
/*****************************************************************
    [์
๋ ฅ ์ด๋ฒคํธ ํจ์]
    input ํ์ดํ ์ ์๋
*/
function checkEmailInputEmpty(event) {
    // ์ด๋ฉ์ผ input ๊ณต๋ ์ฌ๋ถ ๊ฒ์ฌ
    let button = document.getElementById('email-check-button');
    if (event.value.length < 1) {
        button.classList.add('disable');
        button.onclick = null;
    }
    else {
        button.classList.remove('disable');
        button.onclick = sendEmailCode;
    }
}
function checkPasswordValid(event) {
    // 8์ ์ด์ ์ซ์/๋์๋ฌธ์/ํน์๋ฌธ์ ๋ชจ๋ ํฌํจ ์กฐ๊ฑด์ ๋ถํฉํ๋์ง ๊ฒ์ฌ
    let pwInput = event.value;
    const passwordPattern = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[!@#$%^&*()_+={}\[\]:;"'<>,.?/-]).{8,}$/;
    let pwReInput = document.getElementById('pw-re-input');
    let pwCheckDiv = document.getElementById('pw-check');
    if (passwordPattern.test(pwInput)) {
        pwReInput.placeholder = '๋น๋ฐ๋ฒํธ๋ฅผ ์ฌ์
๋ ฅํด์ฃผ์ธ์.';
        pwReInput.readOnly = false;
        pwReInput.oninput = checkPasswordMatch;
        pwCheckDiv.style.visibility = 'unset';
    }
    else {
        pwReInput.placeholder = '8์ ์ด์ ์ซ์/๋์๋ฌธ์/ํน์๋ฌธ์๋ฅผ ๋ชจ๋ ํฌํจํด์ผ ํฉ๋๋ค.';
        pwReInput.readOnly = true;
        pwReInput.oninput = null;
        pwCheckDiv.style.visibility = 'hidden';
    }
}
function checkPasswordMatch(event) {
    // ๋น๋ฐ๋ฒํธ ์ฌ์
๋ ฅ ๊ฐ์ด ๊ธฐ์กด๊ณผ ๋์ผํ์ง ๊ฒ์ฌ
    let pwInput = document.getElementById('pw-input').value;
    let pwReInput = event.target.value;
    let pwCheckDiv = document.getElementById('pw-check');
    console.log(pwInput, pwReInput)
    if (pwInput === pwReInput) {
        pwCheckDiv.classList.add('ok');
        pwCheckDiv.innerText = '๋น๋ฐ๋ฒํธ ์ผ์น';
        // global check ๋ณ์
        CHECK_PW = true;
    }
    else {
        pwCheckDiv.classList.remove('ok');
        pwCheckDiv.innerText = '๋น๋ฐ๋ฒํธ ๋ถ์ผ์น';
        // global check ๋ณ์
        CHECK_PW = false;
    }
}
function checkPhoneValid(event) {
    // 8์๋ฆฌ ์ซ์ ์ฌ๋ถ ๊ฒ์ฌ
    const digitPattern = /^\d{8}$/;
    const button = document.getElementById('phone-check-button');
    
    if (digitPattern.test(event.value)) {
        button.classList.remove('disable');
        button.onclick = handlePhoneCodeButtonClicked;
    }
    else {
        button.classList.add('disable');
        button.onclick = null;
    }
}
/* axios request */
async function getRequest(endpoint) {
    try {
        const response = await axios.get(endpoint);
        console.log(response);
        return response;
    } catch (error) {
        console.error(error);
        alert("์๋ฒ์์ ํต์ ์ ์คํจํ์ต๋๋ค.");
        throw error;
    }
}
async function postRequest(endpoint, requestBody) {
    try {
        const response = await axios.post(endpoint, requestBody);
        console.log(response);
        return response;
    } catch (error) {
        console.error(error);
        alert("์๋ฒ์์ ํต์ ์ ์คํจํ์ต๋๋ค.");
    }
}