restrictedWords.js

From disqus.com, 1 Year ago, written in JavaScript, viewed 3 times. This paste is a reply to aet.js from disqus.com - view diff
URL https://pastebin.freepbx.org/view/8d1559bb Embed
Download Paste or View Raw
  1. import _ from 'underscore';
  2. import $ from 'jquery';
  3.  
  4. import { restrictedWords as actionTypes } from 'action-types';
  5.  
  6. import api from 'core/api';
  7.  
  8.  
  9. const MAX_ENTRY_LENGTH = 200;
  10. const MIN_ENTRY_LENGTH = 3;
  11.  
  12. const updateBlacklist = function (opts) {
  13.     const deferred = $.Deferred();
  14.     if (!opts.forum || !opts.entries) {
  15.         deferred.reject();
  16.     } else if (opts.entries.length === 0) {
  17.         deferred.resolve();
  18.     } else {
  19.         const error = opts.checkErrors && opts.checkErrors(opts.entries);
  20.         if (error) {
  21.             deferred.reject(error);
  22.         }
  23.         else {
  24.             return api.call(opts.endpoint, {
  25.                 data: {
  26.                     forum: opts.forum,
  27.                     word: opts.entries,
  28.                 },
  29.                 method: 'POST',
  30.             });
  31.         }
  32.     }
  33.     return deferred.promise();
  34. };
  35.  
  36. const addToBlacklist = function (forum, entries) {
  37.     return updateBlacklist({
  38.         forum,
  39.         entries,
  40.         endpoint: 'blacklists/add',
  41.         checkErrors: entryArray => {
  42.             for (let i = 0; i < entryArray.length; i++) {
  43.                 if (entryArray[i].length < MIN_ENTRY_LENGTH)
  44.                     return new Error(`Blacklist word ${entryArray[i]} is too short. Only words of at least 3 characters can be added to the blacklist.`);
  45.                 if (entryArray[i].length > MAX_ENTRY_LENGTH)
  46.                     return new Error(`Blacklist word ${entryArray[i]} is too long. Only words of at most 200 characters can be added to the blacklist.`);
  47.             }
  48.         },
  49.     });
  50. };
  51.  
  52. const removeFromBlacklist = function (forum, entries) {
  53.     return updateBlacklist({
  54.         forum,
  55.         entries,
  56.         endpoint: 'blacklists/remove',
  57.     });
  58. };
  59.  
  60. const save = forum => (dispatch, getState) => {
  61.     const state = getState().restrictedWords;
  62.     const shortname = forum;
  63.  
  64.     const initialWords = state.wordArr;
  65.     const initialWordsObj = initialWords.reduce((reducer, value) => {
  66.         reducer[value] = true;
  67.         return reducer;
  68.     }, {});
  69.  
  70.     const words = _.uniq(_.escape(state.words.trim()).split(/\s*,\s*/));
  71.     const wordsObj = words.reduce((reducer, value) => {
  72.         reducer[value] = true;
  73.         return reducer;
  74.     }, {});
  75.  
  76.     const toAdd = words.filter(item => item && !initialWordsObj[item]);
  77.     const toRemove = initialWords.filter(item => item && !wordsObj[item]);
  78.  
  79.     dispatch({ type: actionTypes.saving });
  80.  
  81.     removeFromBlacklist(shortname, toRemove)
  82.     .then(addToBlacklist.bind(this, shortname, toAdd))
  83.     .then(() => {
  84.         dispatch({ type: actionTypes.saved, entries: words });
  85.     })
  86.     .fail(error => {
  87.         const message = error.length ? error[0].message : error.message;
  88.         dispatch({
  89.             type: actionTypes.saveFailed,
  90.             message: message || 'An unknown error occurred trying to save your restricted words.',
  91.         });
  92.     });
  93. };
  94.  
  95. const fetch = forum => dispatch => {
  96.     dispatch({ type: actionTypes.fetching });
  97.  
  98.     api.call('blacklists/list', {
  99.         data: {
  100.             forum,
  101.             type: 'word',
  102.             limit: 0,
  103.         },
  104.         success: data => {
  105.             dispatch({
  106.                 type: actionTypes.update,
  107.                 entries: data.response.map(wordObj => wordObj.value),
  108.             });
  109.         },
  110.         error: () => {
  111.             dispatch({
  112.                 type: actionTypes.fetchFailed,
  113.                 message: 'An unknown error occurred trying to fetch your restricted words.',
  114.             });
  115.         },
  116.     });
  117. };
  118.  
  119. export default {
  120.     fetch,
  121.     save,
  122.     edit: words => ({
  123.         type: actionTypes.edit,
  124.         words,
  125.     }),
  126.     clearError: () => ({ type: actionTypes.clearError }),
  127. };
  128.  
  129.  
  130.  
  131. // WEBPACK FOOTER //
  132. // ./src/js/actions/restrictedWords.js

Replies to restrictedWords.js rss

Title Name Language When
deactivation.js disqus.com javascript 1 Year ago.

Reply to "restrictedWords.js"

Here you can reply to the paste above