Vue PWA mylog Source Code

UserMyLogsView.vue

그랜파 개발자 2024. 12. 8. 03:55

UserMyLogsView.vue

<!-- src/views/UserMyLogsView.vue -->
<template>
  <v-container>
    <v-row>
      <v-col>
        <v-card-title style="font-size:1em" v-if=author> {{ getMylogName }} </v-card-title>   
      </v-col>
    </v-row>

    <v-row class="mt-n5">
      <v-col>
        <v-select class="mt-2" v-model="selectedCategory" :items="myCategories" 
                  label="카테고리를 선택하세요" outlined dense @change="onItemChange">
        </v-select>
        <span style="text-align: right">
          <v-btn text @click="viewByDateDesc"> 최신순 </v-btn> 
          <v-btn text @click="viewByDateAsc"> 오래된순 </v-btn> 
          <v-btn text @click="viewByViews"> 조회순 </v-btn> 
        </span>
      </v-col>
    </v-row>

    <v-row v-if="userMylogs.length > 0">
      <v-col v-for="mylog in userMylogs" :key="mylog.id" @click="goToMylogDetail(mylog.id)" cols="12">
        <v-card>
          <!--카테고리-->
          <v-card-text class="mb-n5" v-if="mylog.category" style="font-size:1em">
            {{ mylog.category }}
          </v-card-text>
          <v-card-text  class="mb-n5" v-else style="font-size:1em">
            카테고리 없음
          </v-card-text>

          <v-card-title style="font-size:1em">{{ mylog.title }}</v-card-title>
          <!-- eslint-disable -->
          <v-card-text style="font-size:1em" class="mt-n2 mb-n6" v-html="sanitizeContent(mylog.content)"></v-card-text>
          <!-- eslint-enable -->
          <v-card-subtitle>
            <span v-if="mylog.commentCount> 0" style="cursor: pointer">댓글 {{mylog.commentCount }}&nbsp;</span> 
            {{ mylog.userName }} . {{ formatDate(mylog.createdAt) }} 
            <span v-if="mylog.views > 0" > ({{ mylog.views }})</span>
          </v-card-subtitle>
        </v-card>
      </v-col>
    </v-row>
    
    <div class="text-center">
      <v-progress-circular v-if="loading" indeterminate></v-progress-circular>
    </div> 

  </v-container>
</template>

<script>
import { mapActions, mapState, mapGetters } from "vuex";
import sanitizeHtml from 'sanitize-html';

export default {
  data() {
    return {
      author: null, // 저자의 이름을 가져오기 위해
      myCategories: [],
      selectedCategory: null, // 선택된 카테고리
    };
  },
  computed: {
    ...mapGetters('mylogs',['userMylogs', 'loading']),
    ...mapState('mylogs', ['categories']),
    getMylogName() {
      return this.author.mylogname;
    }
  },
  methods: {
    ...mapActions('mylogs', ['fetchUserMylogs','fetchUserMylogsOrderByDateAsc', 'fetchUserMylogsOrderByViews','fetchCategories']),
    goToMylogDetail(mylogId) {
      this.$router.push({ name: 'mylog', params: { id: mylogId } });
    },
    viewByDateDesc() { 
      const userId = this.author.id;
      //this.fetchUserMylogs(authorId); // 본인 작성 글 가져오기, 생성일 순
      if(this.selectedCategory == "전체 보기")
        this.fetchUserMylogs({userId, category:null});
      else
        this.fetchUserMylogs({userId, category: this.selectedCategory}); 
    },
    viewByDateAsc() { 
      const userId = this.author.id;
      //this.fetchUserMylogsOrderByCreated(authorId); // 본인 작성 글 가져오기, 생성일 순
      if(this.selectedCategory == "전체 보기")
        this.fetchUserMylogsOrderByDateAsc({userId, category:null});
      else
        this.fetchUserMylogsOrderByDateAsc({userId, category: this.selectedCategory});   
    },
    viewByViews() { 
      const userId = this.author.id;
      //this.fetchUserMylogsOrderByViews(authorId); // 본인 작성 글 가져오기, 조회순
      if(this.selectedCategory == "전체 보기")
        this.fetchUserMylogsOrderByViews({userId, category:null});
      else
        this.fetchUserMylogsOrderByViews({userId, category: this.selectedCategory});  
    },
    // content를 안전한 html로 바꿔준다.
    sanitizeContent(content) {
      return sanitizeHtml(content.replace(/\n/g, '<br>'), {
        allowedTags: ['b', 'i', 'em', 'strong', 'p', 'br'],
        allowedAttributes: {}
      });
    },
    formatDate(date) {
      if (date && date.toDate) {
        return date.toDate().toISOString().replace('T', ' ').substring(0, 16);
      }
      return '';
    },
    onItemChange(category) {
      //console.log("선택값이 변경되었습니다:", category);
      //const userId = this.$store.state.auth.user.id;
      const userId = this.author.id;
      if(category == "전체 보기")
        this.fetchUserMylogs({userId, category:null});
      else
        this.fetchUserMylogs({userId, category}); 
    },
  },
  mounted() {    
    const userId = this.$route.params.userId;
    
    // 전체 회원 정보는 이미 읽어 두었다.
    const users = this.$store.state.auth.users;
    this.author = users.find(user => user.id === userId);

    this.fetchCategories(this.author.id); // Firestore에서 카테고리 데이터 가져오기
    // 카테고리 이름만 가져온다.
    //console.log(this.categories);
    this.myCategories = this.categories.map(category => category.name);
    this.myCategories.unshift('카테고리 없음');
    this.myCategories.unshift('전체 보기');    

    this.fetchUserMylogs(this.author.id); // 저자의 마이로그 목록을 가져오기
  },
};
</script>

'Vue PWA mylog Source Code' 카테고리의 다른 글

Vue PWA mylog Source 코드는?  (1) 2024.12.08
WriteMyLogView.vue  (0) 2024.12.08
SubscriptionsView.vue  (1) 2024.12.07
SearchMyLogsView.vue  (2) 2024.12.06
RegisterView.vue  (0) 2024.12.05