export const updateUserProfile = createAsyncThunk(
"user/updateUserProfile",
async (formData, thunkAPI) => {
const { firstName, lastName } = formData
console.log(auth.currentUser)
try {
// update firebase auth
if (firstName) {
if (auth.currentUser.displayName !== `${firstName} ${lastName}`) {
await updateProfile(auth.currentUser, {
displayName: `${firstName} ${lastName}`,
})
}
} else {
if (auth.currentUser.displayName !== lastName) {
await updateProfile(auth.currentUser, {
displayName: lastName,
})
}
}
// update firestore
let formCopy = { ...formData }
formCopy.timestamp = serverTimestamp()
delete formCopy.password
const docRef = doc(db, "user", auth.currentUser.uid)
console.log(auth.currentUser)
await updateDoc(docRef, formCopy)
delete formCopy.timestamp
return formCopy
} catch (err) {
return thunkAPI.rejectWithValue(
`Could not update the profile. ${err.message}`
)
}
}
)