pleroma/src/actions/import-data.ts

97 lines
3.1 KiB
TypeScript
Raw Normal View History

import { defineMessages } from 'react-intl';
2022-12-20 07:47:46 -08:00
import toast from 'soapbox/toast';
2022-01-10 14:01:24 -08:00
import api from '../api';
import type { RootState } from 'soapbox/store';
const IMPORT_FOLLOWS_REQUEST = 'IMPORT_FOLLOWS_REQUEST';
const IMPORT_FOLLOWS_SUCCESS = 'IMPORT_FOLLOWS_SUCCESS';
const IMPORT_FOLLOWS_FAIL = 'IMPORT_FOLLOWS_FAIL';
const IMPORT_BLOCKS_REQUEST = 'IMPORT_BLOCKS_REQUEST';
const IMPORT_BLOCKS_SUCCESS = 'IMPORT_BLOCKS_SUCCESS';
const IMPORT_BLOCKS_FAIL = 'IMPORT_BLOCKS_FAIL';
2020-09-27 10:24:38 -07:00
const IMPORT_MUTES_REQUEST = 'IMPORT_MUTES_REQUEST';
const IMPORT_MUTES_SUCCESS = 'IMPORT_MUTES_SUCCESS';
const IMPORT_MUTES_FAIL = 'IMPORT_MUTES_FAIL';
2020-09-27 10:24:38 -07:00
type ImportDataActions = {
type: typeof IMPORT_FOLLOWS_REQUEST
| typeof IMPORT_FOLLOWS_SUCCESS
| typeof IMPORT_FOLLOWS_FAIL
| typeof IMPORT_BLOCKS_REQUEST
| typeof IMPORT_BLOCKS_SUCCESS
| typeof IMPORT_BLOCKS_FAIL
| typeof IMPORT_MUTES_REQUEST
| typeof IMPORT_MUTES_SUCCESS
| typeof IMPORT_MUTES_FAIL;
error?: any;
config?: string;
2022-12-20 09:45:46 -08:00
}
const messages = defineMessages({
blocksSuccess: { id: 'import_data.success.blocks', defaultMessage: 'Blocks imported successfully' },
followersSuccess: { id: 'import_data.success.followers', defaultMessage: 'Followers imported successfully' },
mutesSuccess: { id: 'import_data.success.mutes', defaultMessage: 'Mutes imported successfully' },
});
const importFollows = (params: FormData) =>
(dispatch: React.Dispatch<ImportDataActions>, getState: () => RootState) => {
dispatch({ type: IMPORT_FOLLOWS_REQUEST });
return api(getState)('/api/pleroma/follow_import', {
method: 'POST',
body: JSON.stringify(params),
}).then(response => {
toast.success(messages.followersSuccess);
dispatch({ type: IMPORT_FOLLOWS_SUCCESS, config: response.json });
}).catch(error => {
dispatch({ type: IMPORT_FOLLOWS_FAIL, error });
});
};
2020-09-27 10:24:38 -07:00
const importBlocks = (params: FormData) =>
(dispatch: React.Dispatch<ImportDataActions>, getState: () => RootState) => {
2020-09-27 10:24:38 -07:00
dispatch({ type: IMPORT_BLOCKS_REQUEST });
return api(getState)('/api/pleroma/blocks_import', {
method: 'POST',
body: JSON.stringify(params),
}).then(response => {
toast.success(messages.blocksSuccess);
dispatch({ type: IMPORT_BLOCKS_SUCCESS, config: response.json });
}).catch(error => {
dispatch({ type: IMPORT_BLOCKS_FAIL, error });
});
2020-09-27 10:24:38 -07:00
};
const importMutes = (params: FormData) =>
(dispatch: React.Dispatch<ImportDataActions>, getState: () => RootState) => {
2020-09-27 10:24:38 -07:00
dispatch({ type: IMPORT_MUTES_REQUEST });
return api(getState)('/api/pleroma/mutes_import', {
method: 'POST',
body: JSON.stringify(params),
}).then(response => {
toast.success(messages.mutesSuccess);
dispatch({ type: IMPORT_MUTES_SUCCESS, config: response.json });
}).catch(error => {
dispatch({ type: IMPORT_MUTES_FAIL, error });
});
2020-09-27 10:24:38 -07:00
};
export {
IMPORT_FOLLOWS_REQUEST,
IMPORT_FOLLOWS_SUCCESS,
IMPORT_FOLLOWS_FAIL,
IMPORT_BLOCKS_REQUEST,
IMPORT_BLOCKS_SUCCESS,
IMPORT_BLOCKS_FAIL,
IMPORT_MUTES_REQUEST,
IMPORT_MUTES_SUCCESS,
IMPORT_MUTES_FAIL,
importFollows,
importBlocks,
importMutes,
};