Skip to content

Custom Alert ๊ตฌํ˜„

Jin Young Park edited this page Dec 20, 2020 · 2 revisions

Custom Alert ๊ตฌํ˜„

API ์š”์ฒญ ํ›„ ๊ฒฐ๊ณผ์— ๋Œ€ํ•œ ํ™•์ธ, ์‚ฌ์šฉ์ž์˜ UX์— ๋„์›€์„ ์ฃผ๋Š” ์•Œ๋ฆผ ์ฐฝ์„ ๊ตฌํ˜„ํ•˜๊ธฐ ์œ„ํ•ด ๊ณตํ†ต ์ปดํฌ๋„ŒํŠธ๋กœ Alert ์ปดํฌ๋„ŒํŠธ๋ฅผ ๊ตฌํ˜„ํ–ˆ์Šต๋‹ˆ๋‹ค. ๋ธŒ๋ผ์šฐ์ €์—์„œ ์ œ๊ณตํ•˜๋Š” alert ๊ธฐ๋Šฅ์€ ํด๋ฆญ์„ ํ•ด์•ผ ์‚ฌ๋ผ์ง€๋Š” ๋ถˆํŽธํ•จ, ์„œ๋น„์Šค UI์™€ ๋งž์ง€ ์•Š๋Š”๋‹ค๋Š” ํŒ๋‹จ์— ํ™”๋ฉด ์ค‘์•™ ํ•˜๋‹จ์— ์ƒ์„ฑ๋˜์—ˆ๋‹ค๊ฐ€ 3์ดˆ ๋’ค์— ์ž๋™์œผ๋กœ ์‚ฌ๋ผ์ง€๋Š” Custom Alert์„ ๊ตฌํ˜„ํ–ˆ์Šต๋‹ˆ๋‹ค.

Alert

ํ…œํ”Œ๋ฆฟ

vuetify์˜ v-alert๋ฅผ ํ™œ์šฉํ•˜์—ฌ ์Šคํƒ€์ผ๋ง์„ ์ง„ํ–‰ํ–ˆ์Šต๋‹ˆ๋‹ค.

type, color, text์™€ ๊ฐ™์€ props๋กœ ์ปค์Šคํ…€ํ•œ alert๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. type์€ ์•„๋ž˜์™€ ๊ฐ™์ด success, info, warning, error ์˜ ๋„ค๊ฐ€์ง€ ์†์„ฑ์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค. vuetify theme์„ ์ปค์Šคํ„ฐ๋งˆ์ด์ง•ํ•˜์—ฌ ๊ฐ type์˜ color๋ฅผ ๋ฐ”๊ฟ”์ค„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

image-20201220145808778

์‹ค์ œ ๊ตฌํ˜„ํ•œ ํ…œํ”Œ๋ฆฟ ์ฝ”๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. store์—์„œ alert์— ๋Œ€ํ•œ ์ƒํƒœ๊ฐ’์„ ๊ฐ€์ ธ์˜จ ๋’ค ๊ด€๋ จ๋œ props์— ํ• ๋‹นํ•ด์ค๋‹ˆ๋‹ค.

<template>
  <div class="alert">
    <v-alert
      v-if="alert.message"
      :value="!!alert.message"
      :type="alert.type"
      :color="alert.type === 'success' ? 'whaleGreen' : null"
      dismissible
      @input="CLEAR_ALERT()"
      >{{ alert.message }}</v-alert
    >
  </div>
</template>

store๋ฅผ ์ด์šฉํ•œ ์ƒํƒœ๊ด€๋ฆฌ

๋‹ค์–‘ํ•œ ํ™”๋ฉด๊ณผ ์ด๋ฒคํŠธ์—์„œ alert๋ฅผ ๊ฐ™์ด ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด store์—์„œ alert์˜ ์ƒํƒœ์™€ action์„ ๊ด€๋ฆฌํ•˜๊ธฐ๋กœ ํ–ˆ์Šต๋‹ˆ๋‹ค. state๋กœ type๊ณผ message๋ฅผ ๊ฐ€์ง„ alert๊ฐ์ฒด๋ฅผ ์„ ์–ธํ•˜๊ณ , ๊ด€๋ จ๋œ action์„ ์ง€์ •ํ•ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

import router from "@/router";

const state = {
  alert: {
    message: "",
    type: "",
  },
};

const mutations = {
  SET_ERROR_ALERT(state, { data, status }) {
    if (status === 401) {
      state.alert = { message: "์„ธ์…˜์ด ๋งŒ๋ฃŒ๋˜์—ˆ์Šต๋‹ˆ๋‹ค", type: "error" };
      router.replace("/login").catch(() => {});
      return;
    } else {
      state.alert = { message: data.message, type: "error" };
    }
  },
  CLEAR_ALERT(state) {
    state.alert = { message: "", type: "" };
  },
  SET_SUCCESS_ALERT(state, message) {
    state.alert = { message, type: "success" };
  },
};

export default {
  state,
  mutations,
};

๋˜ํ•œ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์—…๋ฐ์ดํŠธ๋˜๊ธฐ ์ด์ „์ธ beforeUpdate() ๋ผ์ดํ”„์‚ฌ์ดํด์—์„œ 3์ดˆ๋’ค์— state๊ฐ’์„ ์ดˆ๊ธฐํ™”ํ•ด์ฃผ๋Š” ๋กœ์ง์„ ๊ตฌํ˜„ํ–ˆ์Šต๋‹ˆ๋‹ค.

beforeUpdate() {
  if (this.alert.message) {
    setTimeout(() => this.CLEAR_ALERT(), 3000);
  }
}

API ํ˜ธ์ถœ์‹œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ง€์ •๋˜์–ด์žˆ๋Š” mutation์„ commit์œผ๋กœ ํ˜ธ์ถœํ•˜๋ฉด ๊ฐ„๋‹จํ•˜๊ฒŒ alert์ฐฝ์„ ๋„์šธ ์ˆ˜ ์žˆ๋Š” ๊ตฌ์กฐ์ž…๋‹ˆ๋‹ค.

async addComment({ commit, dispatch }, comment) {
    try {
        await commentAPI.createComment(comment);
        await dispatch("fetchComments", comment.taskId);

        commit("SET_SUCCESS_ALERT", "๋Œ“๊ธ€์ด ์ƒ์„ฑ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.");
    } catch (err) {
        commit("SET_ERROR_ALERT", err.response);
    }
},
Clone this wiki locally