server.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import api from '../api';
  2. import { importFetchedAccount } from './importer';
  3. export const SERVER_FETCH_REQUEST = 'Server_FETCH_REQUEST';
  4. export const SERVER_FETCH_SUCCESS = 'Server_FETCH_SUCCESS';
  5. export const SERVER_FETCH_FAIL = 'Server_FETCH_FAIL';
  6. export const EXTENDED_DESCRIPTION_REQUEST = 'EXTENDED_DESCRIPTION_REQUEST';
  7. export const EXTENDED_DESCRIPTION_SUCCESS = 'EXTENDED_DESCRIPTION_SUCCESS';
  8. export const EXTENDED_DESCRIPTION_FAIL = 'EXTENDED_DESCRIPTION_FAIL';
  9. export const SERVER_DOMAIN_BLOCKS_FETCH_REQUEST = 'SERVER_DOMAIN_BLOCKS_FETCH_REQUEST';
  10. export const SERVER_DOMAIN_BLOCKS_FETCH_SUCCESS = 'SERVER_DOMAIN_BLOCKS_FETCH_SUCCESS';
  11. export const SERVER_DOMAIN_BLOCKS_FETCH_FAIL = 'SERVER_DOMAIN_BLOCKS_FETCH_FAIL';
  12. export const fetchServer = () => (dispatch, getState) => {
  13. dispatch(fetchServerRequest());
  14. api(getState)
  15. .get('/api/v2/instance').then(({ data }) => {
  16. if (data.contact.account) dispatch(importFetchedAccount(data.contact.account));
  17. dispatch(fetchServerSuccess(data));
  18. }).catch(err => dispatch(fetchServerFail(err)));
  19. };
  20. const fetchServerRequest = () => ({
  21. type: SERVER_FETCH_REQUEST,
  22. });
  23. const fetchServerSuccess = server => ({
  24. type: SERVER_FETCH_SUCCESS,
  25. server,
  26. });
  27. const fetchServerFail = error => ({
  28. type: SERVER_FETCH_FAIL,
  29. error,
  30. });
  31. export const fetchExtendedDescription = () => (dispatch, getState) => {
  32. dispatch(fetchExtendedDescriptionRequest());
  33. api(getState)
  34. .get('/api/v1/instance/extended_description')
  35. .then(({ data }) => dispatch(fetchExtendedDescriptionSuccess(data)))
  36. .catch(err => dispatch(fetchExtendedDescriptionFail(err)));
  37. };
  38. const fetchExtendedDescriptionRequest = () => ({
  39. type: EXTENDED_DESCRIPTION_REQUEST,
  40. });
  41. const fetchExtendedDescriptionSuccess = description => ({
  42. type: EXTENDED_DESCRIPTION_SUCCESS,
  43. description,
  44. });
  45. const fetchExtendedDescriptionFail = error => ({
  46. type: EXTENDED_DESCRIPTION_FAIL,
  47. error,
  48. });
  49. export const fetchDomainBlocks = () => (dispatch, getState) => {
  50. dispatch(fetchDomainBlocksRequest());
  51. api(getState)
  52. .get('/api/v1/instance/domain_blocks')
  53. .then(({ data }) => dispatch(fetchDomainBlocksSuccess(true, data)))
  54. .catch(err => {
  55. if (err.response.status === 404) {
  56. dispatch(fetchDomainBlocksSuccess(false, []));
  57. } else {
  58. dispatch(fetchDomainBlocksFail(err));
  59. }
  60. });
  61. };
  62. const fetchDomainBlocksRequest = () => ({
  63. type: SERVER_DOMAIN_BLOCKS_FETCH_REQUEST,
  64. });
  65. const fetchDomainBlocksSuccess = (isAvailable, blocks) => ({
  66. type: SERVER_DOMAIN_BLOCKS_FETCH_SUCCESS,
  67. isAvailable,
  68. blocks,
  69. });
  70. const fetchDomainBlocksFail = error => ({
  71. type: SERVER_DOMAIN_BLOCKS_FETCH_FAIL,
  72. error,
  73. });