2024-03-27 14:52:57 -07:00
|
|
|
import { v4 as uuid } from 'uuid';
|
|
|
|
|
2024-04-02 14:32:12 -07:00
|
|
|
import { makeGetAccount } from 'soapbox/selectors';
|
2024-03-27 14:52:57 -07:00
|
|
|
import KVStore from 'soapbox/storage/kv-store';
|
|
|
|
|
|
|
|
import type { AppDispatch, RootState } from 'soapbox/store';
|
|
|
|
|
|
|
|
const DRAFT_STATUSES_FETCH_SUCCESS = 'DRAFT_STATUSES_FETCH_SUCCESS';
|
|
|
|
|
|
|
|
const PERSIST_DRAFT_STATUS = 'PERSIST_DRAFT_STATUS';
|
|
|
|
const CANCEL_DRAFT_STATUS = 'DELETE_DRAFT_STATUS';
|
|
|
|
|
2024-04-02 14:32:12 -07:00
|
|
|
const getAccount = makeGetAccount();
|
|
|
|
|
2024-03-27 14:52:57 -07:00
|
|
|
const fetchDraftStatuses = () =>
|
2024-04-02 14:32:12 -07:00
|
|
|
(dispatch: AppDispatch, getState: () => RootState) => {
|
|
|
|
const state = getState();
|
|
|
|
const accountUrl = getAccount(state, state.me as string)!.url;
|
|
|
|
|
|
|
|
return KVStore.getItem(`drafts:${accountUrl}`).then((statuses) => {
|
2024-03-27 14:52:57 -07:00
|
|
|
dispatch({
|
|
|
|
type: DRAFT_STATUSES_FETCH_SUCCESS,
|
|
|
|
statuses,
|
|
|
|
});
|
|
|
|
}).catch(() => {});
|
2024-04-02 14:32:12 -07:00
|
|
|
};
|
2024-03-27 14:52:57 -07:00
|
|
|
|
|
|
|
const saveDraftStatus = (composeId: string) =>
|
|
|
|
(dispatch: AppDispatch, getState: () => RootState) => {
|
|
|
|
const state = getState();
|
2024-04-02 14:32:12 -07:00
|
|
|
const accountUrl = getAccount(state, state.me as string)!.url;
|
2024-03-27 14:52:57 -07:00
|
|
|
|
|
|
|
const compose = state.compose.get(composeId)!;
|
|
|
|
|
|
|
|
const draft = {
|
|
|
|
...compose.toJS(),
|
|
|
|
draft_id: compose.draft_id || uuid(),
|
|
|
|
};
|
|
|
|
|
|
|
|
dispatch({
|
|
|
|
type: PERSIST_DRAFT_STATUS,
|
|
|
|
status: draft,
|
2024-04-02 14:32:12 -07:00
|
|
|
accountUrl,
|
2024-03-27 14:52:57 -07:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const cancelDraftStatus = (id: string) =>
|
2024-04-02 14:32:12 -07:00
|
|
|
(dispatch: AppDispatch, getState: () => RootState) => {
|
|
|
|
const state = getState();
|
|
|
|
const accountUrl = getAccount(state, state.me as string)!.url;
|
|
|
|
|
2024-03-27 14:52:57 -07:00
|
|
|
dispatch({
|
|
|
|
type: CANCEL_DRAFT_STATUS,
|
|
|
|
id,
|
2024-04-02 14:32:12 -07:00
|
|
|
accountUrl,
|
2024-03-27 14:52:57 -07:00
|
|
|
});
|
2024-04-02 14:32:12 -07:00
|
|
|
};
|
2024-03-27 14:52:57 -07:00
|
|
|
|
|
|
|
export {
|
|
|
|
DRAFT_STATUSES_FETCH_SUCCESS,
|
|
|
|
PERSIST_DRAFT_STATUS,
|
|
|
|
CANCEL_DRAFT_STATUS,
|
|
|
|
fetchDraftStatuses,
|
|
|
|
saveDraftStatus,
|
|
|
|
cancelDraftStatus,
|
|
|
|
};
|