feat: redirect + upload avatar store
This commit is contained in:
@ -2,15 +2,19 @@ import { create } from "zustand";
|
|||||||
import type { UserProfile } from "@/types";
|
import type { UserProfile } from "@/types";
|
||||||
import {
|
import {
|
||||||
confirmEmailApi,
|
confirmEmailApi,
|
||||||
|
finishVerificationApi,
|
||||||
requestEmailOtpApi,
|
requestEmailOtpApi,
|
||||||
type ConfirmEmailRequest,
|
type ConfirmEmailRequest,
|
||||||
} from "@/api/verify";
|
} from "@/api/verify";
|
||||||
import { useAuth } from "./auth";
|
import { useAuth } from "./auth";
|
||||||
|
import { uploadAvatarApi } from "@/api/avatar";
|
||||||
|
|
||||||
export type VerifyStep = "email" | "avatar" | "review";
|
export type VerifyStep = "email" | "avatar" | "review";
|
||||||
|
|
||||||
export interface IVerifyState {
|
export interface IVerifyState {
|
||||||
step: VerifyStep | null;
|
step: VerifyStep | null | false;
|
||||||
|
|
||||||
|
redirect: string | null;
|
||||||
|
|
||||||
loadStep: (profile: UserProfile) => void;
|
loadStep: (profile: UserProfile) => void;
|
||||||
|
|
||||||
@ -20,14 +24,25 @@ export interface IVerifyState {
|
|||||||
|
|
||||||
confirming: boolean;
|
confirming: boolean;
|
||||||
confirmOTP: (req: ConfirmEmailRequest) => Promise<void>;
|
confirmOTP: (req: ConfirmEmailRequest) => Promise<void>;
|
||||||
|
|
||||||
|
uploading: boolean;
|
||||||
|
uploadAvatar: (image: File) => Promise<void>;
|
||||||
|
|
||||||
|
verifying: boolean;
|
||||||
|
verify: () => Promise<void>;
|
||||||
|
|
||||||
|
setRedirect: (redirect: string) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const useVerify = create<IVerifyState>((set) => ({
|
export const useVerify = create<IVerifyState>((set) => ({
|
||||||
step: null,
|
step: null,
|
||||||
|
redirect: null,
|
||||||
|
|
||||||
requesting: false,
|
requesting: false,
|
||||||
requested: false,
|
requested: false,
|
||||||
confirming: false,
|
confirming: false,
|
||||||
|
uploading: false,
|
||||||
|
verifying: false,
|
||||||
|
|
||||||
loadStep: (profile) => {
|
loadStep: (profile) => {
|
||||||
if (!profile.email_verified) {
|
if (!profile.email_verified) {
|
||||||
@ -45,7 +60,7 @@ export const useVerify = create<IVerifyState>((set) => ({
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
set({ step: null });
|
set({ step: false });
|
||||||
},
|
},
|
||||||
|
|
||||||
requestOTP: async () => {
|
requestOTP: async () => {
|
||||||
@ -73,4 +88,34 @@ export const useVerify = create<IVerifyState>((set) => ({
|
|||||||
set({ confirming: false });
|
set({ confirming: false });
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
uploadAvatar: async (image) => {
|
||||||
|
set({ uploading: true });
|
||||||
|
|
||||||
|
try {
|
||||||
|
await uploadAvatarApi(image);
|
||||||
|
await useAuth.getState().authenticate();
|
||||||
|
} catch (err) {
|
||||||
|
console.log("ERR: Failed to request OTP:", err);
|
||||||
|
} finally {
|
||||||
|
set({ uploading: false });
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
setRedirect: (redirect) => {
|
||||||
|
set({ redirect });
|
||||||
|
},
|
||||||
|
|
||||||
|
verify: async () => {
|
||||||
|
set({ verifying: true });
|
||||||
|
|
||||||
|
try {
|
||||||
|
await finishVerificationApi();
|
||||||
|
useAuth.getState().authenticate();
|
||||||
|
} catch (err) {
|
||||||
|
console.log("ERR: Failed to finish verification:", err);
|
||||||
|
} finally {
|
||||||
|
set({ verifying: false });
|
||||||
|
}
|
||||||
|
},
|
||||||
}));
|
}));
|
||||||
|
Reference in New Issue
Block a user