@@ -9,7 +9,6 @@ import { toast } from "sonner";
99import type { saveSettingsInput } from "@/schema/profile" ;
1010import { saveSettingsSchema } from "@/schema/profile" ;
1111
12- import { uploadFile } from "@/utils/s3helpers" ;
1312import type { user } from "@/server/db/schema" ;
1413import { Button } from "@/components/ui-components/button" ;
1514import { Loader2 } from "lucide-react" ;
@@ -25,6 +24,7 @@ import { Textarea } from "@/components/ui-components/textarea";
2524import { Switch } from "@/components/ui-components/switch" ;
2625import { Divider } from "@/components/ui-components/divider" ;
2726import { Text } from "@/components/ui-components/text" ;
27+ import { uploadToUrl } from "@/utils/fileUpload" ;
2828
2929type User = Pick <
3030 typeof user . $inferSelect ,
@@ -75,10 +75,10 @@ const Settings = ({ profile }: { profile: User }) => {
7575 } ) ;
7676
7777 const { mutate, isError, isSuccess } = api . profile . edit . useMutation ( ) ;
78- const { mutate : getUploadUrl } = api . profile . getUploadUrl . useMutation ( ) ;
79- const { mutate : updateUserPhotoUrl } =
78+ const { mutateAsync : getUploadUrl } = api . profile . getUploadUrl . useMutation ( ) ;
79+ const { mutateAsync : updateUserPhotoUrl } =
8080 api . profile . updateProfilePhotoUrl . useMutation ( ) ;
81- const { mutate : updateEmail } = api . profile . updateEmail . useMutation ( ) ;
81+ const { mutateAsync : updateEmail } = api . profile . updateEmail . useMutation ( ) ;
8282
8383 const fileInputRef = useRef < HTMLInputElement > ( null ) ;
8484
@@ -104,49 +104,35 @@ const Settings = ({ profile }: { profile: User }) => {
104104 mutate ( { ...values , newsletter : weeklyNewsletter , emailNotifications } ) ;
105105 } ;
106106
107- const uploadToUrl = async ( signedUrl : string , file : File ) => {
108- setProfilePhoto ( { status : "loading" , url : "" } ) ;
109-
110- if ( ! file ) {
111- setProfilePhoto ( { status : "error" , url : "" } ) ;
112- toast . error ( "Invalid file upload." ) ;
113- return ;
114- }
115-
116- const response = await uploadFile ( signedUrl , file ) ;
117- const { fileLocation } = response ;
118- await updateUserPhotoUrl ( {
119- url : fileLocation ,
120- } ) ;
121-
122- return fileLocation ;
123- } ;
124-
125107 const imageChange = async ( e : React . ChangeEvent < HTMLInputElement > ) => {
126108 if ( e . target . files && e . target . files . length > 0 ) {
127109 const file = e . target . files [ 0 ] ;
128110 const { size, type } = file ;
129111
112+ setProfilePhoto ( { status : "loading" , url : "" } ) ;
113+
130114 await getUploadUrl (
131115 { size, type } ,
132116 {
133117 onError ( error ) {
134- if ( error ) return toast . error ( error . message ) ;
135- return toast . error (
136- "Something went wrong uploading the photo, please retry." ,
118+ toast . error (
119+ error . message ||
120+ "Something went wrong uploading the photo, please retry." ,
137121 ) ;
122+ setProfilePhoto ( { status : "error" , url : "" } ) ;
138123 } ,
139124 async onSuccess ( signedUrl ) {
140- const url = await uploadToUrl ( signedUrl , file ) ;
141- if ( ! url ) {
142- return toast . error (
143- "Something went wrong uploading the photo, please retry." ,
144- ) ;
125+ const { status, fileLocation } = await uploadToUrl ( {
126+ signedUrl,
127+ file,
128+ updateUserPhotoUrl,
129+ } ) ;
130+
131+ if ( status === "success" && fileLocation ) {
132+ setProfilePhoto ( { status : "success" , url : fileLocation } ) ;
133+ } else {
134+ setProfilePhoto ( { status : "error" , url : "" } ) ;
145135 }
146- setProfilePhoto ( { status : "success" , url } ) ;
147- toast . success (
148- "Profile photo successfully updated. This may take a few minutes to update around the site." ,
149- ) ;
150136 } ,
151137 } ,
152138 ) ;
0 commit comments