gab-social/app/javascript/gabsocial/actions/bookmarks.js

270 lines
8.3 KiB
JavaScript
Raw Permalink Normal View History

import api, { getLinks } from '../api'
import { importFetchedStatuses } from './importer'
import { me } from '../initial_state'
export const BOOKMARKED_STATUSES_FETCH_REQUEST = 'BOOKMARKED_STATUSES_FETCH_REQUEST'
export const BOOKMARKED_STATUSES_FETCH_SUCCESS = 'BOOKMARKED_STATUSES_FETCH_SUCCESS'
export const BOOKMARKED_STATUSES_FETCH_FAIL = 'BOOKMARKED_STATUSES_FETCH_FAIL'
export const BOOKMARKED_STATUSES_EXPAND_REQUEST = 'BOOKMARKED_STATUSES_EXPAND_REQUEST'
export const BOOKMARKED_STATUSES_EXPAND_SUCCESS = 'BOOKMARKED_STATUSES_EXPAND_SUCCESS'
export const BOOKMARKED_STATUSES_EXPAND_FAIL = 'BOOKMARKED_STATUSES_EXPAND_FAIL'
2020-12-16 00:31:30 +00:00
//
export const BOOKMARK_COLLECTIONS_FETCH_REQUEST = 'BOOKMARK_COLLECTIONS_FETCH_REQUEST'
export const BOOKMARK_COLLECTIONS_FETCH_SUCCESS = 'BOOKMARK_COLLECTIONS_FETCH_SUCCESS'
export const BOOKMARK_COLLECTIONS_FETCH_FAIL = 'BOOKMARK_COLLECTIONS_FETCH_FAIL'
export const BOOKMARK_COLLECTIONS_CREATE_REQUEST = 'BOOKMARK_COLLECTIONS_CREATE_REQUEST'
export const BOOKMARK_COLLECTIONS_CREATE_SUCCESS = 'BOOKMARK_COLLECTIONS_CREATE_SUCCESS'
export const BOOKMARK_COLLECTIONS_CREATE_FAIL = 'BOOKMARK_COLLECTIONS_CREATE_FAIL'
export const BOOKMARK_COLLECTIONS_REMOVE_REQUEST = 'BOOKMARK_COLLECTIONS_REMOVE_REQUEST'
export const BOOKMARK_COLLECTIONS_REMOVE_SUCCESS = 'BOOKMARK_COLLECTIONS_REMOVE_SUCCESS'
export const BOOKMARK_COLLECTIONS_REMOVE_FAIL = 'BOOKMARK_COLLECTIONS_REMOVE_FAIL'
2020-12-16 07:39:07 +00:00
//
export const UPDATE_BOOKMARK_COLLECTION_FAIL = 'UPDATE_BOOKMARK_COLLECTION_FAIL'
export const UPDATE_BOOKMARK_COLLECTION_REQUEST = 'UPDATE_BOOKMARK_COLLECTION_REQUEST'
export const UPDATE_BOOKMARK_COLLECTION_SUCCESS = 'UPDATE_BOOKMARK_COLLECTION_SUCCESS'
export const UPDATE_BOOKMARK_COLLECTION_STATUS_FAIL = 'UPDATE_BOOKMARK_COLLECTION_STATUS_FAIL'
export const UPDATE_BOOKMARK_COLLECTION_STATUS_REQUEST = 'UPDATE_BOOKMARK_COLLECTION_STATUS_REQUEST'
export const UPDATE_BOOKMARK_COLLECTION_STATUS_SUCCESS = 'UPDATE_BOOKMARK_COLLECTION_STATUS_SUCCESS'
2020-11-15 18:48:32 +00:00
/**
*
*/
2020-12-16 07:39:07 +00:00
export const fetchBookmarkedStatuses = (bookmarkCollectionId) => (dispatch, getState) => {
2020-11-15 18:48:32 +00:00
if (!me) return
if (getState().getIn(['status_lists', 'bookmarks', bookmarkCollectionId, 'isLoading'])) {
2020-11-15 18:48:32 +00:00
return
}
2020-12-16 07:39:07 +00:00
dispatch(fetchBookmarkedStatusesRequest(bookmarkCollectionId))
api(getState).get(`/api/v1/bookmark_collections/${bookmarkCollectionId}/bookmarks`).then((response) => {
2020-11-15 18:48:32 +00:00
const next = getLinks(response).refs.find(link => link.rel === 'next')
dispatch(importFetchedStatuses(response.data))
2020-12-16 07:39:07 +00:00
dispatch(fetchBookmarkedStatusesSuccess(response.data, bookmarkCollectionId, next ? next.uri : null))
2020-12-16 00:31:30 +00:00
}).catch((error) => {
2020-12-16 07:39:07 +00:00
dispatch(fetchBookmarkedStatusesFail(bookmarkCollectionId, error))
2020-11-15 18:48:32 +00:00
})
}
2020-12-16 07:39:07 +00:00
const fetchBookmarkedStatusesRequest = (bookmarkCollectionId) => ({
2020-11-15 18:48:32 +00:00
type: BOOKMARKED_STATUSES_FETCH_REQUEST,
bookmarkCollectionId,
2020-11-15 18:48:32 +00:00
})
2020-12-16 07:39:07 +00:00
const fetchBookmarkedStatusesSuccess = (statuses, bookmarkCollectionId, next) => ({
2020-11-15 18:48:32 +00:00
type: BOOKMARKED_STATUSES_FETCH_SUCCESS,
2020-12-16 07:39:07 +00:00
bookmarkCollectionId,
2020-11-15 18:48:32 +00:00
statuses,
next,
})
2020-12-16 07:39:07 +00:00
const fetchBookmarkedStatusesFail = (bookmarkCollectionId, error) => ({
2020-11-15 18:48:32 +00:00
type: BOOKMARKED_STATUSES_FETCH_FAIL,
showToast: true,
2020-12-16 07:39:07 +00:00
bookmarkCollectionId,
2020-11-15 18:48:32 +00:00
error,
})
/**
*
*/
2020-12-16 07:39:07 +00:00
export const expandBookmarkedStatuses = (bookmarkCollectionId) => (dispatch, getState) => {
2020-11-15 18:48:32 +00:00
if (!me) return
2020-12-16 07:39:07 +00:00
const url = getState().getIn(['status_lists', 'bookmarks', bookmarkCollectionId, 'next'], null)
2020-11-15 18:48:32 +00:00
2020-12-16 07:39:07 +00:00
if (url === null || getState().getIn(['status_lists', 'bookmarks', bookmarkCollectionId, 'isLoading'])) {
2020-11-15 18:48:32 +00:00
return
}
2020-12-16 07:39:07 +00:00
dispatch(expandBookmarkedStatusesRequest(bookmarkCollectionId))
2020-12-16 00:31:30 +00:00
api(getState).get(url).then((response) => {
2020-11-15 18:48:32 +00:00
const next = getLinks(response).refs.find(link => link.rel === 'next')
dispatch(importFetchedStatuses(response.data))
2020-12-16 07:39:07 +00:00
dispatch(expandBookmarkedStatusesSuccess(response.data, bookmarkCollectionId, next ? next.uri : null))
2020-12-16 00:31:30 +00:00
}).catch((error) => {
2020-12-16 07:39:07 +00:00
dispatch(expandBookmarkedStatusesFail(bookmarkCollectionId, error))
2020-11-15 18:48:32 +00:00
})
}
2020-12-16 07:39:07 +00:00
const expandBookmarkedStatusesRequest = (bookmarkCollectionId) => ({
2020-11-15 18:48:32 +00:00
type: BOOKMARKED_STATUSES_EXPAND_REQUEST,
bookmarkCollectionId,
2020-11-15 18:48:32 +00:00
})
2020-12-16 07:39:07 +00:00
const expandBookmarkedStatusesSuccess = (statuses, bookmarkCollectionId, next) => ({
2020-11-15 18:48:32 +00:00
type: BOOKMARKED_STATUSES_EXPAND_SUCCESS,
bookmarkCollectionId,
2020-11-15 18:48:32 +00:00
statuses,
next,
})
2020-12-16 07:39:07 +00:00
const expandBookmarkedStatusesFail = (bookmarkCollectionId, error) => ({
2020-11-15 18:48:32 +00:00
type: BOOKMARKED_STATUSES_EXPAND_FAIL,
showToast: true,
2020-12-16 07:39:07 +00:00
bookmarkCollectionId,
2020-11-15 18:48:32 +00:00
error,
})
2020-12-16 00:31:30 +00:00
/**
*
*/
export const fetchBookmarkCollections = () => (dispatch, getState) => {
if (!me) return
if (getState().getIn(['bookmark_collections', 'isLoading'])) return
dispatch(fetchBookmarkCollectionsRequest())
api(getState).get('/api/v1/bookmark_collections').then((response) => {
dispatch(fetchBookmarkCollectionsSuccess(response.data))
}).catch((error) => {
dispatch(fetchBookmarkCollectionsFail(error))
})
}
const fetchBookmarkCollectionsRequest = () => ({
type: BOOKMARK_COLLECTIONS_FETCH_REQUEST,
})
const fetchBookmarkCollectionsSuccess = (bookmarkCollections) => ({
type: BOOKMARK_COLLECTIONS_FETCH_SUCCESS,
bookmarkCollections,
})
const fetchBookmarkCollectionsFail = (error) => ({
type: BOOKMARK_COLLECTIONS_FETCH_FAIL,
showToast: true,
error,
})
/**
*
*/
export const createBookmarkCollection = (title) => (dispatch, getState) => {
if (!me || !title) return
dispatch(createBookmarkCollectionRequest())
api(getState).post('/api/v1/bookmark_collections', { title }).then((response) => {
dispatch(createBookmarkCollectionSuccess(response.data))
}).catch((error) => {
dispatch(createBookmarkCollectionFail(error))
})
}
const createBookmarkCollectionRequest = () => ({
type: BOOKMARK_COLLECTIONS_CREATE_REQUEST,
})
const createBookmarkCollectionSuccess = (bookmarkCollection) => ({
type: BOOKMARK_COLLECTIONS_CREATE_SUCCESS,
bookmarkCollection,
})
const createBookmarkCollectionFail = (error) => ({
type: BOOKMARK_COLLECTIONS_CREATE_FAIL,
showToast: true,
error,
})
/**
*
*/
export const removeBookmarkCollection = (bookmarkCollectionId) => (dispatch, getState) => {
if (!me || !bookmarkCollectionId) return
dispatch(removeBookmarkCollectionRequest(bookmarkCollectionId))
api(getState).delete(`/api/v1/bookmark_collection/${bookmarkCollectionId}`).then((response) => {
dispatch(removeBookmarkCollectionSuccess(response.data))
}).catch((error) => {
dispatch(removeBookmarkCollectionFail(error))
})
}
const removeBookmarkCollectionRequest = (bookmarkCollectionId) => ({
type: BOOKMARK_COLLECTIONS_REMOVE_REQUEST,
2020-12-16 00:31:30 +00:00
bookmarkCollectionId,
})
const removeBookmarkCollectionSuccess = () => ({
type: BOOKMARK_COLLECTIONS_REMOVE_SUCCESS,
2020-12-16 00:31:30 +00:00
})
const removeBookmarkCollectionFail = (error) => ({
type: BOOKMARK_COLLECTIONS_REMOVE_FAIL,
2020-12-16 00:31:30 +00:00
showToast: true,
error,
2020-12-16 07:39:07 +00:00
})
/**
*
*/
export const updateBookmarkCollection = (bookmarkCollectionId, title) => (dispatch, getState) => {
if (!me || !statusId) return
dispatch(updateBookmarkCollectionRequest())
api(getState).put('/api/v1/bookmark_collections', { title }).then((response) => {
2020-12-16 07:39:07 +00:00
dispatch(updateBookmarkCollectionSuccess(response.data))
}).catch((error) => {
dispatch(updateBookmarkCollectionFail(error))
})
}
const updateBookmarkCollectionRequest = () => ({
type: UPDATE_BOOKMARK_COLLECTION_REQUEST,
})
const updateBookmarkCollectionSuccess = (bookmarkCollection) => ({
type: UPDATE_BOOKMARK_COLLECTION_SUCCESS,
bookmarkCollection,
})
const updateBookmarkCollectionFail = (error) => ({
type: UPDATE_BOOKMARK_COLLECTION_FAIL,
showToast: true,
error,
})
/**
*
*/
export const updateBookmarkCollectionStatus = (statusId, bookmarkCollectionId) => (dispatch, getState) => {
if (!me || !statusId) return
dispatch(updateBookmarkCollectionStatusRequest())
api(getState).post(`/api/v1/bookmark_collections/${bookmarkCollectionId}/update_status`, { statusId }).then((response) => {
dispatch(importFetchedStatuses([response.data]))
dispatch(updateBookmarkCollectionStatusSuccess())
2020-12-16 07:39:07 +00:00
}).catch((error) => {
dispatch(updateBookmarkCollectionStatusFail(error))
})
}
const updateBookmarkCollectionStatusRequest = () => ({
type: UPDATE_BOOKMARK_COLLECTION_STATUS_REQUEST,
})
const updateBookmarkCollectionStatusSuccess = () => ({
2020-12-16 07:39:07 +00:00
type: UPDATE_BOOKMARK_COLLECTION_STATUS_SUCCESS,
})
const updateBookmarkCollectionStatusFail = (error) => ({
type: UPDATE_BOOKMARK_COLLECTION_STATUS_FAIL,
showToast: true,
error,
})