feat: redirect + upload avatar store
This commit is contained in:
@ -2,15 +2,19 @@ import { create } from "zustand";
|
||||
import type { UserProfile } from "@/types";
|
||||
import {
|
||||
confirmEmailApi,
|
||||
finishVerificationApi,
|
||||
requestEmailOtpApi,
|
||||
type ConfirmEmailRequest,
|
||||
} from "@/api/verify";
|
||||
import { useAuth } from "./auth";
|
||||
import { uploadAvatarApi } from "@/api/avatar";
|
||||
|
||||
export type VerifyStep = "email" | "avatar" | "review";
|
||||
|
||||
export interface IVerifyState {
|
||||
step: VerifyStep | null;
|
||||
step: VerifyStep | null | false;
|
||||
|
||||
redirect: string | null;
|
||||
|
||||
loadStep: (profile: UserProfile) => void;
|
||||
|
||||
@ -20,14 +24,25 @@ export interface IVerifyState {
|
||||
|
||||
confirming: boolean;
|
||||
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) => ({
|
||||
step: null,
|
||||
redirect: null,
|
||||
|
||||
requesting: false,
|
||||
requested: false,
|
||||
confirming: false,
|
||||
uploading: false,
|
||||
verifying: false,
|
||||
|
||||
loadStep: (profile) => {
|
||||
if (!profile.email_verified) {
|
||||
@ -45,7 +60,7 @@ export const useVerify = create<IVerifyState>((set) => ({
|
||||
return;
|
||||
}
|
||||
|
||||
set({ step: null });
|
||||
set({ step: false });
|
||||
},
|
||||
|
||||
requestOTP: async () => {
|
||||
@ -73,4 +88,34 @@ export const useVerify = create<IVerifyState>((set) => ({
|
||||
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