์™ธ์ฃผ์ผ์ง€ 2 (html์— firestore์—ฐ๊ฒฐ, ํŽ˜์ด์ง€ ๊ฐ„ ๋ฐ์ดํ„ฐ ๋„˜๊ฒจ์ฃผ๊ธฐ, ui ๋ฟŒ๋ฆฌ๊ธฐ)

    ์˜์‹์˜ ํ๋ฆ„๋Œ€๋กœ ๋ฐฑ์„ ์—ฐ๊ฒฐํ•˜๋‹ค๋ณด๋‹ˆ๊นŒ ๋‚ด๊ฐ€ ์–ด๋–ป๊ฒŒ ํ–ˆ๋Š”์ง€ ๊ธฐ์–ต์ด ์•ˆ๋‚œ๋‹ค..

    ํฐ์ผ๋‚ฌ๋‹ค ์‹ถ์–ด์„œ ์ง€๊ธˆ์ด๋ผ๋„ ๊ธฐ๋ก ์‹œ์ž‘ํ•˜๊ธฐ

     

    https://codingapple.com/unit/firebase-installation-with-npm/

     

    Firebase ์„ค์น˜ ๋ฐฉ๋ฒ• ๋ช‡๊ฐ€์ง€ ์ •๋ฆฌ (์ŒฉHTML์‚ฌ์šฉ์‹œ & ๋ฆฌ์•กํŠธ์‚ฌ์šฉ์‹œ) - ์ฝ”๋”ฉ์• ํ”Œ ์˜จ๋ผ์ธ ๊ฐ•์ขŒ

    0:48 ๊ฐœ๋ฐœํ™˜๊ฒฝ์…‹ํŒ…๊ณผ ์„ค์น˜ํ• ๊ฑฐ ์žˆ์Œ 4:22 ์ž‘์—…ํด๋”๋งŒ๋“ค๊ณ  Firebase ํ”„๋กœ์ ํŠธ ๋งŒ๋“œ๋Š” ๋ฒ• 9:55 html ํŒŒ์ผ์— Firebase SDK ์„ค์น˜ํ•ด์•ผ Firebase ๋ฌธ๋ฒ• ์‚ฌ์šฉ๊ฐ€๋Šฅ (์ฐธ๊ณ ) Monterey ๋ฒ„์ „ os ์“ฐ๋Š” ๋งฅ๋ถ์€  firebase serve --port=90

    codingapple.com

     

    ์šฐ์„  ์ดˆ๊ธฐ ์„ค์ •์€ ์ด๊ฑฐ ๋ณด๊ณ  ํ–ˆ๋‹ค. 

     

     

    blog.html (๊ธ€ ๋ชฉ๋ก์ด ๋ณด์ด๋Š” ํŽ˜์ด์ง€) 

     

    ์Šคํฌ๋ฆฝํŠธ ๋ถ€๋ถ„

      const db = firebase.firestore(); //db์—ฐ๊ฒฐํ•˜๊ณ  
        console.log(db);
        db.collection('posts') //posts collection๊ฐ€์ ธ์™€์„œ ์ „์ฒด ๋‹ค ๋ฟŒ๋ ค์ฃผ๊ธฐ 
        .get().then((result) => {
          result.forEach((postInfo) => {
            console.log(postInfo);
           
            const convertDate = (date) => { //firestore์— timestamp๋กœ ์ €์žฅ๋œ ๋‚ ์งœ format
                // whatever formatting you want to do can be done here
                var d = date.toString()
                
                return d.substr(4, 11);
            }
            var date = convertDate(postInfo.data().uploadDate.toDate())
          
            const data = postInfo.data().id;
            const postTemplate = `
             <div class="col-xl-4 col-md-6">
                <div class="post-item position-relative h-100">
                  <div class="post-content d-flex flex-column">
                    <h3 class="post-title"><a class="goToPost" style="color: #364D59;" href="post.html?${data}">${postInfo.data().title}</a></h3>
                    <div class="meta d-flex align-items-center">
                      <div class="d-flex align-items-center">
                        <span>${postInfo.data().sub}</span> //๋ฐ์ดํ„ฐ ์ด๋Ÿฐ์‹์œผ๋กœ ๋ฟŒ๋ ค์คŒ 
                        
                        <!--์ƒ๋žต-->
            `   
            document.querySelector('.posts-list').insertAdjacentHTML('afterbegin', postTemplate); //์œ„์—์„œ ๋งŒ๋“  postTemplate์„ posts-list class์— ์—ฐ๊ฒฐํ•ด์„œ ๋„ฃ๊ธฐ! 
             
         
            const aTag = document.getElementsByClassName('goToPost')[0]; //className์œผ๋กœ ๊ฐ€์ ธ์˜ฌ๋•Œ ๋ฐฐ์—ดํ˜•์‹์ด๊ธฐ๋•Œ๋ฌธ์— [0] ๋ถ™์—ฌ์•ผ ์—๋Ÿฌ ์•ˆ๋œธ! 
            aTag.addEventListener('click', () => { //์ œ๋ชฉ ๋ˆ„๋ฅด๋ฉด ์ด๋™ํ•  ์ˆ˜ ์žˆ๋„๋ก ์ด๋ฒคํŠธ ์ถ”๊ฐ€
              location.href = `post.html?${data}`; //์•„์ด๋”” ๋„˜๊ธฐ๊ธฐ 
            },false);
          });
        });
    
      </script>

    html ๋ถ€๋ถ„

    <div class="row gy-4 posts-list"></div>

     

     

     

    ํฌ์ŠคํŠธ์˜ ๊ฒŒ์‹œ๊ธ€ ํƒ€์ดํ‹€์„ ํด๋ฆญํ•˜๋ฉด ๋‚˜์˜ค๋Š” post.html

    post detail ์ด ๋‚˜์˜ค๊ณ  ๋งํฌ ๋„˜์–ด์˜จ ์•„์ด๋””๋กœ ๋ฐ์ดํ„ฐ ๊ฐ€์ ธ์˜ด 

     

     

        const receivedData = location.href.split('?')[1]; //ํŒŒ์‹ฑ 
        console.log(receivedData);
        const db = firebase.firestore();
        console.log(db);
       
        db.collection('posts').doc(receivedData).get().then((postInfo) => {
          
     	//์•„์ด๋””๋กœ ๋ฐ์ดํ„ฐ ๊ฐ€์ ธ์˜ค๊ธฐ 
           
            const convertDate = (date) => {
                // whatever formatting you want to do can be done here
                var d = date.toString()
                
                return d.substr(4, 11);
            }
            var date = convertDate(postInfo.data().uploadDate.toDate())
          
            const data = postInfo.data().id;
            const postTemplate = `
            <article class="blog-details">
              <h2 class="title">${postInfo.data().title}</h2>
    
              <div class="meta-top">
                <ul>
                  <li class="d-flex align-items-center"><i class="bi bi-clock"></i> ${date}</li>
                </ul>
                        `;
            document.querySelector('.blogPost').insertAdjacentHTML('afterbegin', postTemplate);
    
        });

     

    ๋งํฌ๋Š” ์ด๋ ‡๊ฒŒ

    ๋ฐ˜์‘ํ˜•

    ๋Œ“๊ธ€