Home Reference Source

application/services/version-service.js

  1. import Immutable from 'immutable';
  2.  
  3. import alt from './../alt';
  4. import Alt from 'alt';
  5. import BaseService from './base-service';
  6. import {ErrorService} from './error-service';
  7.  
  8. import {parseSrn} from './../util/strings';
  9. import {info} from './../util/console';
  10.  
  11. /**
  12. * Interact with version API service
  13. */
  14. class VersionService extends BaseService {
  15.  
  16. updateVersion(version) {
  17. return version;
  18. }
  19.  
  20. updateVersions(version) {
  21. return version;
  22. }
  23.  
  24. /**
  25. * Fetch single version
  26. * @param {string} id UUID
  27. * @param {object} opts Hash of query options
  28. * @param {Boolean} forceUpdate If true (default) force update to version store, otherwise, don't
  29. */
  30. fetchOne(content_id, version_id, opts, forceUpdate = true) {
  31. if (forceUpdate === true) {
  32. this.updateVersion([]);
  33. }
  34. return (dispatch) => {
  35. return this.get('/v3/version/' + content_id + '/' + version_id, opts)
  36. .then((data) => {
  37. info('FetchOne returned', data);
  38. this.updateVersion(data.first());
  39. });
  40. };
  41. }
  42.  
  43. /**
  44. * Fetch all versions for given UUID
  45. * @param {string} srn valid content SRN
  46. * @param {object} opts hash of query options
  47. */
  48. fetch(srn = false, opts = {}) {
  49. if (!opts.content && srn) {
  50. opts.content = srn;
  51. }
  52. if (!opts.content) {
  53. throw 'SRN is required';
  54. }
  55. return (dispatch) => {
  56. return this.get('/v3/version', opts)
  57. .then((data) => {
  58. info('Fetch returned', data);
  59. this.updateVersions(data.get('items'))
  60. });
  61. };
  62. }
  63. }
  64.  
  65. const service = alt.createActions(VersionService);
  66.  
  67. /**
  68. * Versions Flux store. Tracks
  69. * <ul>
  70. * <li>version</li>
  71. * <li>versions</li>
  72. * </ul>
  73. */
  74. class VersionsStore {
  75. constructor() {
  76. this.state = {
  77. 'version': Immutable.Map(),
  78. 'versions': Immutable.Map()
  79. };
  80.  
  81. this.bindListeners({
  82. 'handleUpdateVersion': service.UPDATE_VERSION,
  83. 'handleUpdateVersions': service.UPDATE_VERSIONS
  84. });
  85. }
  86.  
  87. handleUpdateVersion(version) {
  88. this.setState({'version': version});
  89. }
  90.  
  91. handleUpdateVersions(version) {
  92. this.setState({'versions': version});
  93. }
  94. }
  95.  
  96. const store = alt.createStore(VersionsStore, 'VersionsStore');
  97.  
  98. /**
  99. * Instace wrapper if you don't or can't use the singleton action and classes.
  100. *
  101. * Actions and store are both under the 'versions' key.
  102. */
  103. class VersionFlux extends Alt {
  104. constructor(config = {}) {
  105. super(config);
  106.  
  107. this.addActions('versions', VersionService);
  108. this.addStore('versions', VersionsStore);
  109. }
  110. }
  111.  
  112. export {VersionFlux as default, service as VersionService, store as VersionsStore};