{"version":3,"sources":["https://lively-kernel.org/lively4/lively4-jsx/src/client/reactive/components/rewritten/conduit/src/stores/articleStore.js"],"names":["_","agent","LIMIT","ArticleStore","constructor","loadArticles","articles","values","clear","setPredicate","predicate","stringify","$req","articlesCount","fromPairs","map","article","ceil","loadArticle","slug","acceptCached","has","resolve","makeFavorite","unmakeFavorite","err","createArticle","updateArticle","data","deleteArticle","unset"],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;AAEOA,O;;AACAC,W;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAEP,YAAMC,QAAQ,EAAd;;;;;;;;;;;;;;;;;AAEA,YAAMC,YAAN,CAAmB;;AAEjBC,sBAAc;AACZ,+CAAwB,EAAxB;AACA,wCAAiB,EAAjB;AACA,mCAAY,CAAZ;AACA,mCAAM;AAAM;AAAN,WAAN;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,2BAAgC;AAAM,uGAAKC,YAAL;AAAN,WAAhC;AACA,mCAAM;AAAM;AAAN,WAAN;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,2BAAqC;AAAM,uGAAKA,YAAL;AAAN,WAArC;AACD;;AAED,YAAIC,QAAJ,GAAe;AACb,uFAAO,0EAAEC,MAAF,YAAS,IAAT,sBAAP;AACD;;AAEDC,gBAAQ;AACN,+CAAwB,EAAxB;AACA,mCAAY,CAAZ;AACD;;AAEDC,qBAAaC,SAAb,EAAwB;AAAA;;AACtB,cAAI,mJAAKC,SAAL,yFAAeD,SAAf,qFAA8B,sEAAKC,SAAL,YAAe,IAAf,eAA9B,CAAJ;AAAkE;AAAlE,WADsB;AAEtB,eAAKH,KAAL;AACA,gIAAiBE,SAAjB;AACD;;AAEDE,eAAO;AACL,cAAI,qDAAuB,MAA3B;AACE,+HAAOX,KAAP,oCAA2B,IAA3B,0FAAsCC,KAAtC;AADF,WAEA,IAAI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;;;;;;;;;;;;;yBAqHJ,iGAAItB,YAAJ,G","file":"articleStore.js","sourcesContent":["'enable aexpr';\n\nimport _ from 'src/external/lodash/lodash.js'\nimport agent from 'src/client/reactive/components/rewritten/conduit/src/agent.js';\n\nconst LIMIT = 10;\n\nclass ArticleStore {\n\n  constructor() {\n    this.articlesRegistry = {};\n    this.predicate = {};\n    this.page = 1;\n    aexpr(() => this.page).onChange(() => this.loadArticles());\n    aexpr(() => this.predicate).onChange(() => this.loadArticles());\n  }\n  \n  get articles() {\n    return _.values(this.articlesRegistry);\n  }\n\n  clear() {\n    this.articlesRegistry = {};\n    this.page = 1;\n  }\n\n  setPredicate(predicate) {\n    if (JSON.stringify(predicate) === JSON.stringify(this.predicate)) return;\n    this.clear();\n    this.predicate = predicate;\n  }\n\n  $req() {\n    if (this.predicate.tab === 'feed') \n      return agent.Articles.feed(this.page, LIMIT);\n    if (this.predicate.tab === 'tag') \n      return agent.Articles.byTag(this.predicate.tag, this.page, LIMIT);\n    if (this.predicate.target === 'author' && this.predicate.tab === 'favorites') \n      return agent.Articles.favoritedBy(this.predicate.favoritedBy, this.page, LIMIT);\n    if (this.predicate.target === 'author' && this.predicate.tab === 'author') \n      return agent.Articles.byAuthor(this.predicate.author, this.page, LIMIT);\n    return agent.Articles.all(this.page, LIMIT);\n  }\n\n  loadArticles() {\n    this.isLoading = true;\n    return this.$req()\n      .then(({ articles, articlesCount }) => {\n        this.articlesRegistry = _.fromPairs(\n          _.map(articles, article => [article.slug, article]));\n        this.totalPagesCount = Math.ceil(articlesCount / LIMIT);\n      })\n      .finally(() => \n        { this.isLoading = false; });\n  }\n\n  loadArticle(slug, { acceptCached = false } = {}) {\n    if (acceptCached && _.has(this.articlesRegistry, slug))\n      return Promise.resolve(this.articlesRegistry[slug]);\n    this.isLoading = true;\n    return agent.Articles.get(slug)\n      .then(({ article }) => {\n        this.articlesRegistry[slug] = article;\n        return article;\n      })\n      .finally(() => \n        { this.isLoading = false; });\n  }\n\n  makeFavorite(slug) {\n    const article = this.articlesRegistry[slug];\n    if (article && !article.favorited) {\n      article.favorited = true;\n      article.favoritesCount++;\n      return agent.Articles.favorite(slug)\n        .catch(() => {\n          article.favorited = false;\n          article.favoritesCount--;\n        });\n    }\n    return Promise.resolve();\n  }\n\n  unmakeFavorite(slug) {\n    const article = this.articlesRegistry[slug];\n    if (article && article.favorited) {\n      article.favorited = false;\n      article.favoritesCount--;\n      return agent.Articles.unfavorite(slug)\n        .catch(err => {\n          article.favorited = true;\n          article.favoritesCount++;\n          throw err;\n        });\n    }\n    return Promise.resolve();\n  }\n\n  createArticle(article) {\n    return agent.Articles.create(article)\n      .then(({ article }) => {\n        this.articlesRegistry[article.slug] = article;\n        return article;\n      });\n  }\n\n  updateArticle(data) {\n    return agent.Articles.update(data)\n      .then(({ article }) => {\n        this.articlesRegistry[article.slug] = article;\n        return article;\n      });\n  }\n\n  deleteArticle(slug) {\n    _.unset(this.articlesRegistry, slug);\n    return agent.Articles.del(slug)\n      .catch(err => { \n      this.loadArticles(); \n      throw err; \n    });\n  }\n}\n\nexport default new ArticleStore();"]}