|
| 1 | +import { select } from '@wordpress/data'; |
| 2 | +import { SOCIAL_STORE_ID } from '../../social-store'; |
| 3 | +import { |
| 4 | + fetchJetpackSettings, |
| 5 | + updateJetpackSettings as updateJetpackSettingsControl, |
| 6 | +} from '../controls'; |
| 7 | +import { setJetpackSettings } from './jetpack-settings'; |
| 8 | + |
| 9 | +/** |
| 10 | + * Yield actions to update settings |
| 11 | + * |
| 12 | + * @param {object} settings - settings to apply. |
| 13 | + * @yields {object} - an action object. |
| 14 | + * @returns {object} - an action object. |
| 15 | + */ |
| 16 | +export function* updateSocialNotesSettings( settings ) { |
| 17 | + try { |
| 18 | + yield setUpdatingSocialNotesSettings(); |
| 19 | + yield setJetpackSettings( settings ); |
| 20 | + yield updateJetpackSettingsControl( settings ); |
| 21 | + const updatedSettings = yield fetchJetpackSettings(); |
| 22 | + yield setJetpackSettings( updatedSettings ); |
| 23 | + return true; |
| 24 | + } catch ( e ) { |
| 25 | + const oldSettings = select( SOCIAL_STORE_ID ).getSocialNotesSettings(); |
| 26 | + yield setJetpackSettings( oldSettings ); |
| 27 | + return false; |
| 28 | + } finally { |
| 29 | + yield setUpdatingSocialNotesSettingsDone(); |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +/** |
| 34 | + * Yield actions to refresh settings |
| 35 | + * |
| 36 | + * @yields {object} - an action object. |
| 37 | + * @returns {object} - an action object. |
| 38 | + */ |
| 39 | +export function* refreshSocialNotesSettings() { |
| 40 | + try { |
| 41 | + yield setUpdatingSocialNotesSettings(); |
| 42 | + const updatedSettings = yield fetchJetpackSettings(); |
| 43 | + yield setJetpackSettings( updatedSettings ); |
| 44 | + return true; |
| 45 | + } catch ( e ) { |
| 46 | + return false; |
| 47 | + } finally { |
| 48 | + yield setUpdatingSocialNotesSettingsDone(); |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +/** |
| 53 | + * Set state updating action |
| 54 | + * |
| 55 | + * @returns {object} - an action object. |
| 56 | + */ |
| 57 | +export function setUpdatingSocialNotesSettings() { |
| 58 | + return setJetpackSettings( { social_notes_is_updating: true } ); |
| 59 | +} |
| 60 | + |
| 61 | +/** |
| 62 | + * Set state updating finished |
| 63 | + * |
| 64 | + * @returns {object} - an action object. |
| 65 | + */ |
| 66 | +export function setUpdatingSocialNotesSettingsDone() { |
| 67 | + return setJetpackSettings( { social_notes_is_updating: false } ); |
| 68 | +} |
| 69 | + |
| 70 | +export default { |
| 71 | + updateSocialNotesSettings, |
| 72 | + refreshSocialNotesSettings, |
| 73 | +}; |
0 commit comments