nicolium: cleanup, migrations
Signed-off-by: nicole mikołajczyk <git@mkljczk.pl>
This commit is contained in:
@ -14,7 +14,7 @@ import Toggle from '@/components/ui/toggle';
|
||||
import SettingToggle from '@/features/settings/components/setting-toggle';
|
||||
import { useAppDispatch } from '@/hooks/use-app-dispatch';
|
||||
import { useOwnAccount } from '@/hooks/use-own-account';
|
||||
import { useUpdateCredentials } from '@/queries/accounts';
|
||||
import { useUpdateCredentials } from '@/queries/accounts/use-account-credentials';
|
||||
import { useSettings } from '@/stores/settings';
|
||||
|
||||
type FormData = {
|
||||
@ -34,6 +34,11 @@ const messages = defineMessages({
|
||||
defaultMessage: 'Play a sound when you receive a message',
|
||||
},
|
||||
submit: { id: 'chat.page_settings.submit', defaultMessage: 'Save' },
|
||||
success: {
|
||||
id: 'settings.messages.success',
|
||||
defaultMessage: 'Chat settings updated successfully',
|
||||
},
|
||||
fail: { id: 'settings.messages.fail', defaultMessage: 'Failed to update chat settings' },
|
||||
});
|
||||
|
||||
const ChatsPageSettings = () => {
|
||||
@ -55,7 +60,14 @@ const ChatsPageSettings = () => {
|
||||
const handleSubmit = (event: React.FormEvent) => {
|
||||
event.preventDefault();
|
||||
|
||||
updateCredentials.mutate(data);
|
||||
updateCredentials.mutate(data, {
|
||||
onSuccess: () => {
|
||||
toast.success(intl.formatMessage(messages.success));
|
||||
},
|
||||
onError: () => {
|
||||
toast.error(intl.formatMessage(messages.fail));
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
return (
|
||||
|
||||
@ -4,13 +4,19 @@ import { defineMessages, useIntl } from 'react-intl';
|
||||
import List, { ListItem } from '@/components/list';
|
||||
import Toggle from '@/components/ui/toggle';
|
||||
import { useOwnAccount } from '@/hooks/use-own-account';
|
||||
import { useUpdateCredentials } from '@/queries/accounts';
|
||||
import { useUpdateCredentials } from '@/queries/accounts/use-account-credentials';
|
||||
import toast from '@/toast';
|
||||
|
||||
const messages = defineMessages({
|
||||
label: {
|
||||
id: 'settings.messages.label',
|
||||
defaultMessage: 'Allow users to start a new chat with you',
|
||||
},
|
||||
success: {
|
||||
id: 'settings.messages.success',
|
||||
defaultMessage: 'Chat settings updated successfully',
|
||||
},
|
||||
fail: { id: 'settings.messages.fail', defaultMessage: 'Failed to update chat settings' },
|
||||
});
|
||||
|
||||
const MessagesSettings = () => {
|
||||
@ -19,7 +25,17 @@ const MessagesSettings = () => {
|
||||
const updateCredentials = useUpdateCredentials();
|
||||
|
||||
const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
|
||||
updateCredentials.mutate({ accepts_chat_messages: event.target.checked });
|
||||
updateCredentials.mutate(
|
||||
{ accepts_chat_messages: event.target.checked },
|
||||
{
|
||||
onSuccess: () => {
|
||||
toast.success(intl.formatMessage(messages.success));
|
||||
},
|
||||
onError: () => {
|
||||
toast.error(intl.formatMessage(messages.fail));
|
||||
},
|
||||
},
|
||||
);
|
||||
};
|
||||
|
||||
if (!account) {
|
||||
|
||||
@ -1,37 +0,0 @@
|
||||
import { useMutation } from '@tanstack/react-query';
|
||||
|
||||
import { patchMeSuccess } from '@/actions/me';
|
||||
import { useAppDispatch } from '@/hooks/use-app-dispatch';
|
||||
import { useClient } from '@/hooks/use-client';
|
||||
import toast from '@/toast';
|
||||
|
||||
type UpdateCredentialsData = {
|
||||
accepts_chat_messages?: boolean;
|
||||
};
|
||||
|
||||
const useUpdateCredentials = () => {
|
||||
// const { account } = useOwnAccount();
|
||||
const client = useClient();
|
||||
const dispatch = useAppDispatch();
|
||||
|
||||
return useMutation({
|
||||
mutationFn: (data: UpdateCredentialsData) => client.settings.updateCredentials(data),
|
||||
// TODO: What is it intended to do?
|
||||
// onMutate(variables) {
|
||||
// const cachedAccount = account;
|
||||
// dispatch(patchMeSuccess({ ...account, ...variables }));
|
||||
|
||||
// return { cachedAccount };
|
||||
// },
|
||||
onSuccess(response) {
|
||||
dispatch(patchMeSuccess(response));
|
||||
toast.success('Chat Settings updated successfully');
|
||||
},
|
||||
onError(_error, _variables, context: any) {
|
||||
toast.error('Chat Settings failed to update.');
|
||||
dispatch(patchMeSuccess(context.cachedAccount));
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
export { useUpdateCredentials };
|
||||
@ -0,0 +1,37 @@
|
||||
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
|
||||
|
||||
import { patchMeSuccess } from '@/actions/me';
|
||||
import { useCurrentAccount } from '@/contexts/current-account-context';
|
||||
import { useAppDispatch } from '@/hooks/use-app-dispatch';
|
||||
import { useClient } from '@/hooks/use-client';
|
||||
|
||||
import type { UpdateCredentialsParams } from 'pl-api';
|
||||
|
||||
const useCredentialAccount = () => {
|
||||
const client = useClient();
|
||||
const currentAccount = useCurrentAccount();
|
||||
|
||||
return useQuery({
|
||||
queryKey: [currentAccount, 'credentialAccount'],
|
||||
queryFn: () => client.settings.verifyCredentials(),
|
||||
enabled: currentAccount !== 'unauthenticated',
|
||||
});
|
||||
};
|
||||
|
||||
const useUpdateCredentials = () => {
|
||||
const client = useClient();
|
||||
const currentAccount = useCurrentAccount();
|
||||
const dispatch = useAppDispatch();
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
return useMutation({
|
||||
mutationKey: [currentAccount, 'credentialAccount'],
|
||||
mutationFn: (params: UpdateCredentialsParams) => client.settings.updateCredentials(params),
|
||||
onSuccess: (response) => {
|
||||
queryClient.setQueryData([currentAccount, 'credentialAccount'], response);
|
||||
dispatch(patchMeSuccess(response));
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
export { useCredentialAccount, useUpdateCredentials };
|
||||
@ -15,9 +15,7 @@ import type { Account, CredentialAccount } from 'pl-api';
|
||||
|
||||
interface AccountMeta {
|
||||
pleroma: Account['__meta']['pleroma'];
|
||||
pleromaSource: Account['__meta']['source'];
|
||||
source?: CredentialAccount['source'];
|
||||
role?: CredentialAccount['role'];
|
||||
}
|
||||
|
||||
type State = Immutable<Record<string, AccountMeta | undefined>>;
|
||||
@ -30,9 +28,7 @@ const importAccount = (state: State, account: CredentialAccount): State =>
|
||||
|
||||
draft[account.id] = {
|
||||
pleroma: account.__meta.pleroma ?? existing?.pleroma,
|
||||
pleromaSource: account.__meta.source ?? existing?.pleromaSource,
|
||||
source: account.source ?? existing?.source,
|
||||
role: account.role ?? existing?.role,
|
||||
};
|
||||
},
|
||||
{ enableAutoFreeze: true },
|
||||
|
||||
Reference in New Issue
Block a user