Newer
Older
taehui / taehui-fe / src / avatar / setAvatarStore.ts
@Taehui Taehui on 17 Mar 1 KB 2024-03-17 오후 2:12
import { wwwAPI } from "@/utilities/wwwAPI";
import { getMillis } from "taehui-ts/date";

export default function setAvatarStore() {
  return {
    totem: "",
    taehuiAvatarID: "",
    taehuiAvatarName: "",
    taehuiLevel: 0,
    taehuiFax: "",
    taehuiAvatarIntro: "",

    setSession({
      totem,
      avatarID,
      avatarName,
      level,
      fax,
      avatarIntro,
    }: {
      totem?: string;
      avatarID?: string;
      avatarName?: string;
      level?: number;
      fax?: string;
      avatarIntro?: string;
    }) {
      if (totem) {
        this.totem = totem;
      }
      if (avatarID) {
        this.taehuiAvatarID = avatarID;
      }
      if (avatarName) {
        this.taehuiAvatarName = avatarName;
      }
      if (level) {
        this.taehuiLevel = level;
      }
      if (fax) {
        this.taehuiFax = fax;
      }
      if (avatarIntro) {
        this.taehuiAvatarIntro = avatarIntro;
      }
    },

    saveTotem() {
      window.sessionStorage.setItem("totem", this.totem);
    },

    async loadTotem() {
      const totem = window.sessionStorage.getItem("totem");
      if (totem) {
        const { status, data } = await wwwAPI.patch("/avatar/totem", null, {
          headers: { millis: getMillis(), totem },
        });
        if (status === 201) {
          this.setSession({ totem, ...data });
        }
      }
    },

    get isSU() {
      return this.taehuiLevel === 2;
    },
  };
}