2022-09-30 10:01:49 -07:00
|
|
|
import { useMutation } from '@tanstack/react-query';
|
|
|
|
|
|
|
|
import { patchMeSuccess } from 'soapbox/actions/me';
|
|
|
|
import { useApi, useAppDispatch, useOwnAccount } from 'soapbox/hooks';
|
2022-12-20 07:47:46 -08:00
|
|
|
import toast from 'soapbox/toast';
|
2022-09-30 10:01:49 -07:00
|
|
|
|
2022-09-08 09:47:19 -07:00
|
|
|
export type IAccount = {
|
|
|
|
acct: string
|
|
|
|
avatar: string
|
|
|
|
avatar_static: string
|
|
|
|
bot: boolean
|
|
|
|
created_at: string
|
|
|
|
discoverable: boolean
|
|
|
|
display_name: string
|
|
|
|
followers_count: number
|
|
|
|
following_count: number
|
|
|
|
group: boolean
|
|
|
|
header: string
|
|
|
|
header_static: string
|
|
|
|
id: string
|
|
|
|
last_status_at: string
|
|
|
|
location: string
|
|
|
|
locked: boolean
|
|
|
|
note: string
|
|
|
|
statuses_count: number
|
|
|
|
url: string
|
|
|
|
username: string
|
|
|
|
verified: boolean
|
|
|
|
website: string
|
2022-09-30 10:01:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
type UpdateCredentialsData = {
|
2022-11-02 13:10:13 -07:00
|
|
|
accepts_chat_messages?: boolean
|
2022-09-30 10:01:49 -07:00
|
|
|
chats_onboarded?: boolean
|
|
|
|
}
|
|
|
|
|
|
|
|
const useUpdateCredentials = () => {
|
2023-06-25 10:35:09 -07:00
|
|
|
const { account } = useOwnAccount();
|
2022-09-30 10:01:49 -07:00
|
|
|
const api = useApi();
|
|
|
|
const dispatch = useAppDispatch();
|
|
|
|
|
|
|
|
return useMutation((data: UpdateCredentialsData) => api.patch('/api/v1/accounts/update_credentials', data), {
|
|
|
|
onMutate(variables) {
|
2023-06-20 12:24:39 -07:00
|
|
|
const cachedAccount = account;
|
|
|
|
dispatch(patchMeSuccess({ ...account, ...variables }));
|
2022-09-30 10:01:49 -07:00
|
|
|
|
|
|
|
return { cachedAccount };
|
|
|
|
},
|
|
|
|
onSuccess(response) {
|
|
|
|
dispatch(patchMeSuccess(response.data));
|
2022-12-20 07:47:46 -08:00
|
|
|
toast.success('Chat Settings updated successfully');
|
2022-09-30 10:01:49 -07:00
|
|
|
},
|
|
|
|
onError(_error, _variables, context: any) {
|
2022-12-20 07:47:46 -08:00
|
|
|
toast.error('Chat Settings failed to update.');
|
2022-09-30 10:01:49 -07:00
|
|
|
dispatch(patchMeSuccess(context.cachedAccount));
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
export { useUpdateCredentials };
|