ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

what i learned/TIL

[TIL] 2021/11/26 Fri -

zubetcha 2021. 11. 26. 23:05

 

 

 

 

 

 

Today I Learned

 

 

 

 

๐ŸŽ‰ ๊ฐœ์ธ ๊ณผ์ œ ๋! ๐ŸŽ‰

 

     

Main page

 

 

Add page

 

 

Detail page

 

 

 

๐Ÿ“Œ ๋‹จ์–ด ์นด๋“œ ์ฒดํฌ ์•„์ด์ฝ˜ ํด๋ฆญ ์‹œ ์ƒ‰ ๋ณ€๊ฒฝ ๊ธฐ๋Šฅ ์˜ค๋ฅ˜ ํ•ด๊ฒฐ

 

     ์–ด์ œ ๋งŒ๋“ค์—ˆ๋˜ checked ๊ธฐ๋Šฅ์ด ์•Œ๊ณ  ๋ณด๋‹ˆ ์ œ๋Œ€๋กœ ๋™์ž‘ํ•˜์ง€ ์•Š๊ณ  ์žˆ์—ˆ๋‹ค.

 

     ์›๋ž˜๋Š” ํด๋ฆญํ•˜๋ฉด Firestore์— ์žˆ๋Š” checked ์˜ ๊ฐ’์ด ๋ณ€๊ฒฝ์ด ๋˜๊ณ , 

     ๋ณ€๊ฒฝ๋œ ๋ฐ์ดํ„ฐ๋กœ ๋‹ค์‹œ State๋ฅผ ๊ต์ฒดํ•ด์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๊ตฌ๋…ํ•˜๋Š” ์ปดํฌ๋„ŒํŠธ์—๋„ ์—ฐ๋™์ด ๋˜์–ด์•ผ ํ•˜๋Š”๋ฐ

     ์•Œ๊ณ ๋ณด๋‹ˆ ๋ฏธ๋“ค์›จ์–ด ์ฝ”๋“œ๋ฅผ ์ž˜๋ชป ์ž‘์„ฑํ•ด์„œ ํด๋ฆญํ–ˆ์„ ๋•Œ Firestore์˜ checked ๊ฐ’์ด ๋ณ€๊ฒฝ๋˜์ง€ ์•Š๊ณ  ์žˆ์—ˆ๋‹ค.

 

     ๊ทธ๋ž˜์„œ ์ฒดํฌ ์•„์ด์ฝ˜์„ ํด๋ฆญํ•ด์„œ ์ƒ‰์„ ๋ณ€ํ•˜๊ฒŒ ํ•œ ํ›„ ์ƒˆ๋กœ๊ณ ์นจํ•˜๊ฑฐ๋‚˜ ๋‹ค๋ฅธ ํŽ˜์ด์ง€๋กœ ์ด๋™ํ•˜๊ณ  ๋‹ค์‹œ ๋Œ์•„์˜ค๋ฉด 

     ๋‹ค์‹œ checked์˜ ๊ฐ’์ด false๋กœ ๋ฐ”๋€Œ๊ณ , ์•„์ด์ฝ˜๋„ ๋‹ค์‹œ ํฐ ์ƒ‰์œผ๋กœ ๋˜์–ด ์žˆ์—ˆ๋‹ค.

 

     ์•Œ๊ณ ๋ณด๋‹ˆ ๋ฏธ๋“ค์›จ์–ด ํ•จ์ˆ˜์—์„œ checked ํ‚ค์˜ ๊ฒฝ๋กœ๋ฅผ ์ž˜๋ชป ์ ์—ˆ์—ˆ๋‹ค.

 

     ์•„๋ž˜์˜ ์ด์ „ checked์˜ ๊ฐ’์ด false ๋˜๋Š” true ์ธ์ง€์— ๋”ฐ๋ผ ๋‹ค์‹œ ๊ฐ’์„ ๋ณ€๊ฒฝํ•ด์ฃผ๋Š” ์‚ผํ•ญ์—ฐ์‚ฐ์ž์—์„œ

     ์กฐ๊ฑด๋ฌธ์„ ์•„๋ž˜ ์ฒ˜๋Ÿผ ์ž‘์„ฑํ•˜์ง€ ์•Š๊ณ  ๋ฐ”๋กœ docRef.checked == false? ๋กœ ์ž‘์„ฑํ–ˆ์—ˆ๊ธฐ ๋•Œ๋ฌธ์—

     ํ•ญ์ƒ ๊ฐ’์€ undefined ์—ฌ์„œ Firestore์— ๋ฐ์ดํ„ฐ ์—ฐ๋™์ด ์•ˆ ๋˜๊ณ  ์žˆ์—ˆ๋˜ ๊ฒƒ์ด์—ˆ๋‹ค.

 

     ๊ทธ๋ž˜์„œ ์•„๋ž˜์™€ ๊ฐ™์ด ๋‹ค์‹œ ์ด์ „์˜ checked ๊ฐ’์„ ํ™•์ธํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ๋‹ค์‹œ ์ž‘์„ฑํ•˜์˜€๋”๋‹ˆ 

     Firestore ์—๋„ ํด๋ฆญ ์‹œ checked์˜ ๋ณ€๊ฒฝ๋œ ๊ฐ’์ด ์ž˜ ์—ฐ๋™๋˜๊ณ  ํ™”๋ฉด์—์„œ๋„ ์•„์ด์ฝ˜์ด ์ž˜ ์œ ์ง€๋˜์—ˆ๋‹ค!

 

   

export const checkVocaFB = (voca_id) => {
    return async function (dispatch, getState) {
        const docRef = doc(db, 'voca', voca_id)
        
        const pre_voca_list = getState().voca.list;
        const voca_index = pre_voca_list.findIndex((vc) => {
            return vc.id === voca_id;
        });
        const pre_checked = pre_voca_list[voca_index].checked;

        pre_checked == false ? await updateDoc(docRef, {checked: true})
        : await updateDoc(docRef, {checked: false});

        const voca_list = getState().voca.list;
        const voca_checked = voca_list[voca_index].checked;

        dispatch(checkVoca(voca_index, voca_checked));
    }
}

 

 

 

๐Ÿ“Œ input ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ ์ถ”๊ฐ€

 

   ๊ทธ๋ฆฌ๊ณ  input ์˜ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ๋„ ์ถ”๊ฐ€ํ–ˆ๋‹ค.

   

   ์–ด์ฐจํ”ผ ๋ฐ์ดํ„ฐ ํƒ€์ž…์€ String์ด๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ„๋‹จํ•˜๊ฒŒ

 

   1) ์ž…๋ ฅ ๊ธ€์ž ์ˆ˜ ์ œํ•œ

   2) ๋‹ค์„ฏ ๊ฐœ์˜ ์ž…๋ ฅ๋ž€ ์ค‘ ํ•˜๋‚˜๋ผ๋„ ๋น„์–ด ์žˆ์œผ๋ฉด ์ถ”๊ฐ€ ๋˜๋Š” ์ˆ˜์ •์ด ๋˜์ง€ ์•Š๋„๋ก ํ•˜์˜€๋‹ค.

 

 

    const updateVocaList = () => {
        window.alert('๋‹จ์–ด๋ฅผ ์ˆ˜์ •ํ•˜์‹œ๊ฒ ์Šต๋‹ˆ๋‹ค?');
        let wordValue = word.current.value;
        let pronunciationValue = pronunciation.current.value;
        let meaningValue = meaning.current.value;
        let exampleValue = example.current.value;
        let translateValue = translate.current.value;

        if ((wordValue && pronunciationValue && meaningValue &&
            exampleValue && translateValue) !== '') {
            dispatch(updateVocaFB({
                word: wordValue,
                pronunciation: pronunciationValue,
                meaning: meaningValue,
                example: exampleValue,
                translate: translateValue,
                checked: current_voca.checked,
            }, current_voca.id));
            history.push("/");
        } else {
            window.alert('Please, fill in all blanks.');
            return false;
        }
    };

    return (
        <Container>
            <UpdateBox>
                <label>word</label>
                <TextArea type="text" ref={word} maxLength='14' 
                defaultValue={current_voca ? current_voca.word : ""} />
                <label>pronunciation</label>
                <TextArea type="text" ref={pronunciation} maxLength='20' 
                defaultValue={current_voca ? current_voca.pronunciation : ""} />
                <label>meaning</label>
                <TextArea type="text" ref={meaning} maxLength='20' 
                defaultValue={current_voca ? current_voca.meaning : ""} />
                <label>example sentence</label>
                <TextArea type="text" ref={example} maxLength='20' 
                defaultValue={current_voca ? current_voca.example : ""} />
                <label>translation</label>
                <TextArea type="text" ref={translate} maxLength='20' 
                defaultValue={current_voca ? current_voca.translate : ""} />
                <ChangeVocaButton
                    onClick={() => {
                        updateVocaList();
                    }}
                >CHANGE<br />VOCA</ChangeVocaButton>
            </UpdateBox>
        </Container>
    )
}

 

 

 

๐Ÿ“Œ ํŽ˜์ด์ง€ ๋กœ๋”ฉ ์ค‘ ๋ณด์—ฌ์ง€๋Š” ์Šคํ”ผ๋„ˆ ์ถ”๊ฐ€

 

   ๊ฐ•์˜ ๋ณด๋ฉด์„œ ์Šคํ”ผ๋„ˆ ํ™”๋ฉด์„ ๋งŒ๋“ค์–ด์„œ ์ถ”๊ฐ€ํ–ˆ๋‹ค.

   ๊ธฐ๋ณธ ๋ฐฐ๊ฒฝ์ƒ‰์ด ์–ด๋‘์šด ์ƒ‰์ด๋ผ์„œ ๋‹ฌ ์•„์ด์ฝ˜์œผ๋กœ ๋งŒ๋“ค์—ˆ๋‹ค.

 

 

Spinner

 

 

 

 

์ด๋ฒˆ ์ฃผ๋ฅผ ์‹œ์ž‘ํ•  ๋•Œ๋งŒ ํ•ด๋„ '๊ฐœ์ธ ๊ณผ์ œ ๋นจ๋ฆฌ ๋๋‚ด๊ณ  ๋ถ€์กฑํ•œ ๊ณต๋ถ€ ํ•ด์•ผ์ง€!' ๋ผ๊ณ  ๋งˆ์Œ์„ ๋จน์—ˆ์—ˆ๋Š”๋ฐ,

์˜คํžˆ๋ ค ๊ทธ๋Ÿฐ ๋‹ค์ง์ด ๋‚˜๋ฅผ ๋” ์กฐ๊ธ‰ํ•˜๊ฒŒ ๋งŒ๋“œ๋Š” ๊ฒƒ ๊ฐ™์•˜๋‹ค.

 

๊ทธ๋ž˜์„œ ๋นจ๋ฆฌ ๋๋‚ด๋ ค๋Š” ์š•์‹ฌ๊ณผ ๋‹ค๋ฅธ ์‚ฌ๋žŒ๊ณผ ๋‚˜๋ฅผ ๋น„๊ตํ•˜๋ ค๊ณ  ํ•˜๋Š” ๋ฌด์˜์‹์ ์ธ ์Šต๊ด€๋“ค์„ ๋‚ด๋ ค๋†“์œผ๋ ค๊ณ  ๋งˆ์ธ๋“œ์…‹์„ ์—ด์‹ฌํžˆ ํ–ˆ๋‹ค.

 

์ƒ๊ฐํ•ด ๋ณด๋‹ˆ ๋‚˜๋Š” ์•„์ง ๋งŽ์€ ๋ถ€๋ถ„์—์„œ ๋ถ€์กฑํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ฐœ์ธ ๊ณผ์ œ ํ•˜๋‚˜๋ฅผ ํ•˜๋ฉด์„œ๋„

๋‚ด๊ฐ€ ๋ฐฐ์šธ ์ˆ˜ ์žˆ๋Š” ๊ฒƒ๋“ค์ด ์•„์ฃผ ๋งŽ์•˜๋Š”๋ฐ, ํ•˜๋งˆํ„ฐ๋ฉด ์ด๋Ÿฌํ•œ ๊นจ๋‹ฌ์Œ์„ ๋†“์น  ๋ป” ํ–ˆ๋‹ค.

 

๊ทธ๋ž˜์„œ ์ด๋ฒˆ ์ฃผ๋Š” ์ €๋ฒˆ ์ฃผ๋ณด๋‹ค๋Š” ์—ฌ์œ ๋กœ์šด ๋งˆ์Œ์„ ๊ฐ€์ง€๋ ค๊ณ  ๋…ธ๋ ฅํ•˜๊ณ ,

ํ•„์ˆ˜๋กœ ์š”๊ตฌํ•˜๋Š” ์‚ฌํ•ญ ์™ธ์—๋„ ๋” ๊ฐœ์„ ํ•  ์ˆ˜ ์žˆ๋Š” ๋ถ€๋ถ„์€ ์—†๋Š”์ง€ ๊ณ ๋ฏผํ•˜๋ฉด์„œ ๊ณผ์ œ๋ฅผ ์ง„ํ–‰ํ–ˆ๋‹ค.

 

๊ทธ๋ž˜์„œ ๊ฒฐ๊ณผ์ ์œผ๋กœ๋Š” ํ•„์ˆ˜๋กœ ์š”๊ตฌํ•˜๋Š” ๊ธฐ๋Šฅ๋ณด๋‹ค ์กฐ๊ธˆ ๋” ๋งŽ์€ ๊ฒƒ๋“ค์„ ๊ตฌํ˜„ํ–ˆ๋‹ค!

๊ทธ๋ž˜์„œ ์ด๋ฒˆ ์ฃผ ๊ฐœ์ธ ๊ณผ์ œ ๊ฒฐ๊ณผ๋ฌผ๋„ ์• ์ •์ด ๊ฐ„๋‹ค.

 

๋˜ ํ•˜๋‚˜์˜ ์ž‘๊ณ  ์†Œ์ค‘ํ•œ ํŽ˜์ด์ง€๊ฐ€ ์ƒ๊ฒจ๋‚ฌ๊ธฐ ๋•Œ๋ฌด๋„ค~

์ž์„ธํ•œ ๋‚ด์šฉ์€ ์ž‘์†ŒํŽ˜ ์นดํ…Œ๊ณ ๋ฆฌ์—์„œ ๋‹ค๋ฃฐ ์˜ˆ์ •์ด๋‹ค.

 

์˜ค๋Š˜์˜ TIL ๋!

 

 

 

 

 

๋งํฌ
ยซ   2024/11   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
Total
Today
Yesterday