frontend-rw #1
3 changed files with 22 additions and 211 deletions
|
@ -5,19 +5,7 @@ import { normalizeAccount } from 'pl-fe/normalizers/account';
|
|||
import toast from 'pl-fe/toast';
|
||||
|
||||
import type { Account, PaginatedResponse } from 'pl-api';
|
||||
import type { RootState } from 'pl-fe/store';
|
||||
|
||||
const EXPORT_FOLLOWS_REQUEST = 'EXPORT_FOLLOWS_REQUEST' as const;
|
||||
const EXPORT_FOLLOWS_SUCCESS = 'EXPORT_FOLLOWS_SUCCESS' as const;
|
||||
const EXPORT_FOLLOWS_FAIL = 'EXPORT_FOLLOWS_FAIL' as const;
|
||||
|
||||
const EXPORT_BLOCKS_REQUEST = 'EXPORT_BLOCKS_REQUEST' as const;
|
||||
const EXPORT_BLOCKS_SUCCESS = 'EXPORT_BLOCKS_SUCCESS' as const;
|
||||
const EXPORT_BLOCKS_FAIL = 'EXPORT_BLOCKS_FAIL' as const;
|
||||
|
||||
const EXPORT_MUTES_REQUEST = 'EXPORT_MUTES_REQUEST' as const;
|
||||
const EXPORT_MUTES_SUCCESS = 'EXPORT_MUTES_SUCCESS' as const;
|
||||
const EXPORT_MUTES_FAIL = 'EXPORT_MUTES_FAIL' as const;
|
||||
import type { AppDispatch, RootState } from 'pl-fe/store';
|
||||
|
||||
const messages = defineMessages({
|
||||
blocksSuccess: { id: 'export_data.success.blocks', defaultMessage: 'Blocks exported successfully' },
|
||||
|
@ -25,20 +13,6 @@ const messages = defineMessages({
|
|||
mutesSuccess: { id: 'export_data.success.mutes', defaultMessage: 'Mutes exported successfully' },
|
||||
});
|
||||
|
||||
type ExportDataAction = {
|
||||
type: typeof EXPORT_FOLLOWS_REQUEST
|
||||
| typeof EXPORT_BLOCKS_REQUEST
|
||||
| typeof EXPORT_MUTES_REQUEST
|
||||
| typeof EXPORT_FOLLOWS_SUCCESS
|
||||
| typeof EXPORT_BLOCKS_SUCCESS
|
||||
| typeof EXPORT_MUTES_SUCCESS;
|
||||
} | {
|
||||
type: typeof EXPORT_FOLLOWS_FAIL
|
||||
| typeof EXPORT_BLOCKS_FAIL
|
||||
| typeof EXPORT_MUTES_FAIL;
|
||||
error?: unknown;
|
||||
}
|
||||
|
||||
const fileExport = (content: string, fileName: string) => {
|
||||
const fileToDownload = document.createElement('a');
|
||||
|
||||
|
@ -62,8 +36,7 @@ const listAccounts = async (response: PaginatedResponse<Account>) => {
|
|||
return Array.from(new Set(accounts));
|
||||
};
|
||||
|
||||
const exportFollows = () => async (dispatch: React.Dispatch<ExportDataAction>, getState: () => RootState) => {
|
||||
dispatch({ type: EXPORT_FOLLOWS_REQUEST });
|
||||
const exportFollows = () => async (_dispatch: AppDispatch, getState: () => RootState) => {
|
||||
const me = getState().me;
|
||||
if (!me) return;
|
||||
|
||||
|
@ -75,52 +48,31 @@ const exportFollows = () => async (dispatch: React.Dispatch<ExportDataAction>, g
|
|||
fileExport(followings.join('\n'), 'export_followings.csv');
|
||||
|
||||
toast.success(messages.followersSuccess);
|
||||
dispatch({ type: EXPORT_FOLLOWS_SUCCESS });
|
||||
}).catch(error => {
|
||||
dispatch({ type: EXPORT_FOLLOWS_FAIL, error });
|
||||
});
|
||||
};
|
||||
|
||||
const exportBlocks = () => (dispatch: React.Dispatch<ExportDataAction>, getState: () => RootState) => {
|
||||
dispatch({ type: EXPORT_BLOCKS_REQUEST });
|
||||
const exportBlocks = () => (_dispatch: AppDispatch, getState: () => RootState) => {
|
||||
return getClient(getState()).filtering.getBlocks({ limit: 40 })
|
||||
.then(listAccounts)
|
||||
.then((blocks) => {
|
||||
fileExport(blocks.join('\n'), 'export_block.csv');
|
||||
|
||||
toast.success(messages.blocksSuccess);
|
||||
dispatch({ type: EXPORT_BLOCKS_SUCCESS });
|
||||
}).catch(error => {
|
||||
dispatch({ type: EXPORT_BLOCKS_FAIL, error });
|
||||
});
|
||||
};
|
||||
|
||||
const exportMutes = () => (dispatch: React.Dispatch<ExportDataAction>, getState: () => RootState) => {
|
||||
dispatch({ type: EXPORT_MUTES_REQUEST });
|
||||
const exportMutes = () => (_dispatch: AppDispatch, getState: () => RootState) => {
|
||||
return getClient(getState()).filtering.getMutes({ limit: 40 })
|
||||
.then(listAccounts)
|
||||
.then((mutes) => {
|
||||
fileExport(mutes.join('\n'), 'export_mutes.csv');
|
||||
|
||||
toast.success(messages.mutesSuccess);
|
||||
dispatch({ type: EXPORT_MUTES_SUCCESS });
|
||||
}).catch(error => {
|
||||
dispatch({ type: EXPORT_MUTES_FAIL, error });
|
||||
});
|
||||
};
|
||||
|
||||
export {
|
||||
EXPORT_FOLLOWS_REQUEST,
|
||||
EXPORT_FOLLOWS_SUCCESS,
|
||||
EXPORT_FOLLOWS_FAIL,
|
||||
EXPORT_BLOCKS_REQUEST,
|
||||
EXPORT_BLOCKS_SUCCESS,
|
||||
EXPORT_BLOCKS_FAIL,
|
||||
EXPORT_MUTES_REQUEST,
|
||||
EXPORT_MUTES_SUCCESS,
|
||||
EXPORT_MUTES_FAIL,
|
||||
exportFollows,
|
||||
exportBlocks,
|
||||
exportMutes,
|
||||
type ExportDataAction,
|
||||
};
|
||||
|
|
|
@ -8,25 +8,7 @@ import { getClient } from '../api';
|
|||
import type { Filter, FilterContext } from 'pl-api';
|
||||
import type { AppDispatch, RootState } from 'pl-fe/store';
|
||||
|
||||
const FILTERS_FETCH_REQUEST = 'FILTERS_FETCH_REQUEST' as const;
|
||||
const FILTERS_FETCH_SUCCESS = 'FILTERS_FETCH_SUCCESS' as const;
|
||||
const FILTERS_FETCH_FAIL = 'FILTERS_FETCH_FAIL' as const;
|
||||
|
||||
const FILTER_FETCH_REQUEST = 'FILTER_FETCH_REQUEST' as const;
|
||||
const FILTER_FETCH_SUCCESS = 'FILTER_FETCH_SUCCESS' as const;
|
||||
const FILTER_FETCH_FAIL = 'FILTER_FETCH_FAIL' as const;
|
||||
|
||||
const FILTERS_CREATE_REQUEST = 'FILTERS_CREATE_REQUEST' as const;
|
||||
const FILTERS_CREATE_SUCCESS = 'FILTERS_CREATE_SUCCESS' as const;
|
||||
const FILTERS_CREATE_FAIL = 'FILTERS_CREATE_FAIL' as const;
|
||||
|
||||
const FILTERS_UPDATE_REQUEST = 'FILTERS_UPDATE_REQUEST' as const;
|
||||
const FILTERS_UPDATE_SUCCESS = 'FILTERS_UPDATE_SUCCESS' as const;
|
||||
const FILTERS_UPDATE_FAIL = 'FILTERS_UPDATE_FAIL' as const;
|
||||
|
||||
const FILTERS_DELETE_REQUEST = 'FILTERS_DELETE_REQUEST' as const;
|
||||
const FILTERS_DELETE_SUCCESS = 'FILTERS_DELETE_SUCCESS' as const;
|
||||
const FILTERS_DELETE_FAIL = 'FILTERS_DELETE_FAIL' as const;
|
||||
|
||||
const messages = defineMessages({
|
||||
added: { id: 'filters.added', defaultMessage: 'Filter added.' },
|
||||
|
@ -39,130 +21,59 @@ const fetchFilters = () =>
|
|||
(dispatch: AppDispatch, getState: () => RootState) => {
|
||||
if (!isLoggedIn(getState)) return;
|
||||
|
||||
dispatch<FiltersAction>({
|
||||
type: FILTERS_FETCH_REQUEST,
|
||||
});
|
||||
|
||||
return getClient(getState).filtering.getFilters()
|
||||
.then((data) => dispatch<FiltersAction>({
|
||||
type: FILTERS_FETCH_SUCCESS,
|
||||
.then((data) => ({
|
||||
filters: data,
|
||||
}))
|
||||
.catch(error => dispatch<FiltersAction>({
|
||||
type: FILTERS_FETCH_FAIL,
|
||||
.catch(error => ({
|
||||
error,
|
||||
skipAlert: true,
|
||||
}));
|
||||
};
|
||||
|
||||
const fetchFilter = (filterId: string) =>
|
||||
(dispatch: AppDispatch, getState: () => RootState) => {
|
||||
dispatch<FiltersAction>({ type: FILTER_FETCH_REQUEST });
|
||||
|
||||
return getClient(getState).filtering.getFilter(filterId)
|
||||
.then((data) => {
|
||||
dispatch<FiltersAction>({
|
||||
type: FILTER_FETCH_SUCCESS,
|
||||
filter: data,
|
||||
});
|
||||
|
||||
return data;
|
||||
})
|
||||
.catch(error => {
|
||||
dispatch<FiltersAction>({
|
||||
type: FILTER_FETCH_FAIL,
|
||||
error,
|
||||
skipAlert: true,
|
||||
});
|
||||
});
|
||||
};
|
||||
(dispatch: AppDispatch, getState: () => RootState) =>
|
||||
getClient(getState).filtering.getFilter(filterId);
|
||||
|
||||
const createFilter = (title: string, expires_in: number | undefined, context: Array<FilterContext>, hide: boolean, keywords_attributes: FilterKeywords) =>
|
||||
(dispatch: AppDispatch, getState: () => RootState) => {
|
||||
dispatch<FiltersAction>({ type: FILTERS_CREATE_REQUEST });
|
||||
|
||||
return getClient(getState).filtering.createFilter({
|
||||
(dispatch: AppDispatch, getState: () => RootState) =>
|
||||
getClient(getState).filtering.createFilter({
|
||||
title,
|
||||
context,
|
||||
filter_action: hide ? 'hide' : 'warn',
|
||||
expires_in,
|
||||
keywords_attributes,
|
||||
}).then(response => {
|
||||
dispatch<FiltersAction>({ type: FILTERS_CREATE_SUCCESS, filter: response });
|
||||
toast.success(messages.added);
|
||||
|
||||
return response;
|
||||
}).catch(error => {
|
||||
dispatch<FiltersAction>({ type: FILTERS_CREATE_FAIL, error });
|
||||
});
|
||||
};
|
||||
|
||||
const updateFilter = (filterId: string, title: string, expires_in: number | undefined, context: Array<FilterContext>, hide: boolean, keywords_attributes: FilterKeywords) =>
|
||||
(dispatch: AppDispatch, getState: () => RootState) => {
|
||||
dispatch<FiltersAction>({ type: FILTERS_UPDATE_REQUEST });
|
||||
|
||||
return getClient(getState).filtering.updateFilter(filterId, {
|
||||
(dispatch: AppDispatch, getState: () => RootState) =>
|
||||
getClient(getState).filtering.updateFilter(filterId, {
|
||||
title,
|
||||
context,
|
||||
filter_action: hide ? 'hide' : 'warn',
|
||||
expires_in,
|
||||
keywords_attributes,
|
||||
}).then(response => {
|
||||
dispatch<FiltersAction>({ type: FILTERS_UPDATE_SUCCESS, filter: response });
|
||||
toast.success(messages.added);
|
||||
|
||||
return response;
|
||||
}).catch(error => {
|
||||
dispatch<FiltersAction>({ type: FILTERS_UPDATE_FAIL, filterId, error });
|
||||
});
|
||||
};
|
||||
|
||||
const deleteFilter = (filterId: string) =>
|
||||
(dispatch: AppDispatch, getState: () => RootState) => {
|
||||
dispatch<FiltersAction>({ type: FILTERS_DELETE_REQUEST });
|
||||
return getClient(getState).filtering.deleteFilter(filterId).then(response => {
|
||||
dispatch<FiltersAction>({ type: FILTERS_DELETE_SUCCESS, filterId });
|
||||
(dispatch: AppDispatch, getState: () => RootState) =>
|
||||
getClient(getState).filtering.deleteFilter(filterId).then(response => {
|
||||
toast.success(messages.removed);
|
||||
|
||||
return response;
|
||||
}).catch(error => {
|
||||
dispatch<FiltersAction>({ type: FILTERS_DELETE_FAIL, filterId, error });
|
||||
});
|
||||
};
|
||||
|
||||
type FiltersAction =
|
||||
| { type: typeof FILTERS_FETCH_REQUEST }
|
||||
| { type: typeof FILTERS_FETCH_SUCCESS; filters: Array<Filter> }
|
||||
| { type: typeof FILTERS_FETCH_FAIL; error: unknown; skipAlert: true }
|
||||
| { type: typeof FILTER_FETCH_REQUEST }
|
||||
| { type: typeof FILTER_FETCH_SUCCESS; filter: Filter }
|
||||
| { type: typeof FILTER_FETCH_FAIL; error: unknown; skipAlert: true }
|
||||
| { type: typeof FILTERS_CREATE_REQUEST }
|
||||
| { type: typeof FILTERS_CREATE_SUCCESS; filter: Filter }
|
||||
| { type: typeof FILTERS_CREATE_FAIL; error: unknown }
|
||||
| { type: typeof FILTERS_UPDATE_REQUEST }
|
||||
| { type: typeof FILTERS_UPDATE_SUCCESS; filter: Filter }
|
||||
| { type: typeof FILTERS_UPDATE_FAIL; filterId: string; error: unknown }
|
||||
| { type: typeof FILTERS_DELETE_REQUEST }
|
||||
| { type: typeof FILTERS_DELETE_SUCCESS; filterId: string }
|
||||
| { type: typeof FILTERS_DELETE_FAIL; filterId: string; error: unknown }
|
||||
type FiltersAction = { type: typeof FILTERS_FETCH_SUCCESS; filters: Array<Filter> };
|
||||
|
||||
export {
|
||||
FILTERS_FETCH_REQUEST,
|
||||
FILTERS_FETCH_SUCCESS,
|
||||
FILTERS_FETCH_FAIL,
|
||||
FILTER_FETCH_REQUEST,
|
||||
FILTER_FETCH_SUCCESS,
|
||||
FILTER_FETCH_FAIL,
|
||||
FILTERS_CREATE_REQUEST,
|
||||
FILTERS_CREATE_SUCCESS,
|
||||
FILTERS_CREATE_FAIL,
|
||||
FILTERS_UPDATE_REQUEST,
|
||||
FILTERS_UPDATE_SUCCESS,
|
||||
FILTERS_UPDATE_FAIL,
|
||||
FILTERS_DELETE_REQUEST,
|
||||
FILTERS_DELETE_SUCCESS,
|
||||
FILTERS_DELETE_FAIL,
|
||||
fetchFilters,
|
||||
fetchFilter,
|
||||
createFilter,
|
||||
|
|
|
@ -4,35 +4,7 @@ import toast from 'pl-fe/toast';
|
|||
|
||||
import { getClient } from '../api';
|
||||
|
||||
import type { RootState } from 'pl-fe/store';
|
||||
|
||||
const IMPORT_FOLLOWS_REQUEST = 'IMPORT_FOLLOWS_REQUEST' as const;
|
||||
const IMPORT_FOLLOWS_SUCCESS = 'IMPORT_FOLLOWS_SUCCESS' as const;
|
||||
const IMPORT_FOLLOWS_FAIL = 'IMPORT_FOLLOWS_FAIL' as const;
|
||||
|
||||
const IMPORT_BLOCKS_REQUEST = 'IMPORT_BLOCKS_REQUEST' as const;
|
||||
const IMPORT_BLOCKS_SUCCESS = 'IMPORT_BLOCKS_SUCCESS' as const;
|
||||
const IMPORT_BLOCKS_FAIL = 'IMPORT_BLOCKS_FAIL' as const;
|
||||
|
||||
const IMPORT_MUTES_REQUEST = 'IMPORT_MUTES_REQUEST' as const;
|
||||
const IMPORT_MUTES_SUCCESS = 'IMPORT_MUTES_SUCCESS' as const;
|
||||
const IMPORT_MUTES_FAIL = 'IMPORT_MUTES_FAIL' as const;
|
||||
|
||||
type ImportDataActions = {
|
||||
type: typeof IMPORT_FOLLOWS_REQUEST
|
||||
| typeof IMPORT_BLOCKS_REQUEST
|
||||
| typeof IMPORT_MUTES_REQUEST;
|
||||
} | {
|
||||
type: typeof IMPORT_FOLLOWS_SUCCESS
|
||||
| typeof IMPORT_BLOCKS_SUCCESS
|
||||
| typeof IMPORT_MUTES_SUCCESS;
|
||||
response?: string;
|
||||
} | {
|
||||
type: | typeof IMPORT_FOLLOWS_FAIL
|
||||
| typeof IMPORT_BLOCKS_FAIL
|
||||
| typeof IMPORT_MUTES_FAIL;
|
||||
error?: unknown;
|
||||
}
|
||||
import type { AppDispatch, RootState } from 'pl-fe/store';
|
||||
|
||||
const messages = defineMessages({
|
||||
blocksSuccess: { id: 'import_data.success.blocks', defaultMessage: 'Blocks imported successfully' },
|
||||
|
@ -41,48 +13,24 @@ const messages = defineMessages({
|
|||
});
|
||||
|
||||
const importFollows = (list: File | string, overwrite?: boolean) =>
|
||||
(dispatch: React.Dispatch<ImportDataActions>, getState: () => RootState) => {
|
||||
dispatch({ type: IMPORT_FOLLOWS_REQUEST });
|
||||
return getClient(getState).settings.importFollows(list, overwrite ? 'overwrite' : 'merge').then(response => {
|
||||
(dispatch: AppDispatch, getState: () => RootState) =>
|
||||
getClient(getState).settings.importFollows(list, overwrite ? 'overwrite' : 'merge').then(response => {
|
||||
toast.success(messages.followersSuccess);
|
||||
dispatch({ type: IMPORT_FOLLOWS_SUCCESS, response });
|
||||
}).catch(error => {
|
||||
dispatch({ type: IMPORT_FOLLOWS_FAIL, error });
|
||||
});
|
||||
};
|
||||
|
||||
const importBlocks = (list: File | string, overwrite?: boolean) =>
|
||||
(dispatch: React.Dispatch<ImportDataActions>, getState: () => RootState) => {
|
||||
dispatch({ type: IMPORT_BLOCKS_REQUEST });
|
||||
return getClient(getState).settings.importBlocks(list, overwrite ? 'overwrite' : 'merge').then(response => {
|
||||
(dispatch: AppDispatch, getState: () => RootState) =>
|
||||
getClient(getState).settings.importBlocks(list, overwrite ? 'overwrite' : 'merge').then(response => {
|
||||
toast.success(messages.blocksSuccess);
|
||||
dispatch({ type: IMPORT_BLOCKS_SUCCESS, response });
|
||||
}).catch(error => {
|
||||
dispatch({ type: IMPORT_BLOCKS_FAIL, error });
|
||||
});
|
||||
};
|
||||
|
||||
const importMutes = (list: File | string) =>
|
||||
(dispatch: React.Dispatch<ImportDataActions>, getState: () => RootState) => {
|
||||
dispatch({ type: IMPORT_MUTES_REQUEST });
|
||||
return getClient(getState).settings.importMutes(list).then(response => {
|
||||
(dispatch: AppDispatch, getState: () => RootState) =>
|
||||
getClient(getState).settings.importMutes(list).then(response => {
|
||||
toast.success(messages.mutesSuccess);
|
||||
dispatch({ type: IMPORT_MUTES_SUCCESS, response });
|
||||
}).catch(error => {
|
||||
dispatch({ type: IMPORT_MUTES_FAIL, error });
|
||||
});
|
||||
};
|
||||
|
||||
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,
|
||||
|
|
Loading…
Reference in a new issue