Vue PWA mylog Source Code

SubscriptionsView.vue

그랜파 개발자 2024. 12. 7. 04:30

SubscriptionsView.vue

<!-- src/views/SubscriptionsView.vue -->
<template>
  <v-container> 

    <v-row>
      <v-col><v-card-title style="font-size:1em"> 구독 </v-card-title></v-col>
    </v-row>

    <v-row class="mt-n5">
      <v-col 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> 
      </v-col>
    </v-row>

    <v-row v-if="subscribedMylogs.length > 0">
      <v-col v-for="mylog in subscribedMylogs" :key="mylog.id" @click="viewMylog(mylog.id)" cols="12">
        <v-card>
          <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, mapGetters } from "vuex";
import sanitizeHtml from 'sanitize-html';

export default {
  data() {
    return {
      user: null
    };
  },
  async created() {
    // 회원의 구독 중인 마이로그를 로드한다.
    this.user = this.$store.state.auth.user;
    this.fetchSubscribedMylogs(this.user.id);
  },
  computed: {
    ...mapGetters('mylogs',['subscribedMylogs', 'loading']), 
  },
  methods: {
    ...mapActions('mylogs', ['fetchSubscribedMylogs','fetchSubscribedMylogsOrderByCreated','fetchSubscribedMylogsOrderByViews']),
    viewMylog(mylogId) {
      this.$router.push({ name: 'MyLogView', params: { id: mylogId } });
    },
    // 최신순
    viewByDateDesc() { 
      const userId = this.$store.state.auth.user.id;
      this.fetchSubscribedMylogs(userId);   
    },
    // 오래된 순
    viewByDateAsc() { 
      const userId = this.$store.state.auth.user.id;
      this.fetchSubscribedMylogsOrderByCreated(userId);    
    },
    // 조회순
    viewByViews() { 
      const userId = this.$store.state.auth.user.id;
      this.fetchSubscribedMylogsOrderByViews(userId); 
    },
    // content를 안전한 html로 바꿔준다.
    sanitizeContent(content) {
      return sanitizeHtml(content.replace(/\n/g, '<br>'), {
        allowedTags: ['b', 'i', 'em', 'strong', 'p', 'br'],
        allowedAttributes: {}
      });
    },
    formatDate(date) {
      //console.log(date.toDate().toLocaleString());
      if (date && date.toDate) {
        return date.toDate().toISOString().replace('T', ' ').substring(0, 16);
        //return date.toDate().toISOString().split('T')[0];
        //return date.toDate().toLocaleString();
      }
      return '';
    },
  }
};
</script>

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

WriteMyLogView.vue  (0) 2024.12.08
UserMyLogsView.vue  (0) 2024.12.08
SearchMyLogsView.vue  (2) 2024.12.06
RegisterView.vue  (0) 2024.12.05
ReadersView.vue  (1) 2024.12.03