/** * WordPress dependencies */ import apiFetch from '@safe-wordpress/api-fetch'; import { select, dispatch } from '@safe-wordpress/data'; /** * External dependencies */ import { isEqual } from 'lodash'; import { store as NC_DATA } from '@nelio-content/data'; import type { Uuid } from '@nelio-content/types'; /** * Internal dependencies */ import { store as NC_PROFILE_SETTINGS } from '../../store'; import { showErrorNotice } from '@nelio-content/utils'; export async function saveProfileSettings(): Promise< void > { const profileId = select( NC_PROFILE_SETTINGS ).getEditingProfileId(); if ( ! profileId ) { return; } const profile = select( NC_DATA ).getSocialProfile( profileId ); if ( ! profile ) { return; } const isSaving = select( NC_PROFILE_SETTINGS ).isSavingProfileSettings(); if ( isSaving ) { return; } const { markAsSavingProfileSettings } = dispatch( NC_PROFILE_SETTINGS ); await markAsSavingProfileSettings( true ); try { const settings = select( NC_PROFILE_SETTINGS ).getEditingProfileSettings(); const siteId = select( NC_DATA ).getSiteId(); const apiRoot = select( NC_DATA ).getApiRoot(); const token = select( NC_DATA ).getAuthenticationToken(); await apiFetch( { url: `${ apiRoot }/site/${ siteId }/profile/${ profileId }`, method: 'PUT', credentials: 'omit', mode: 'cors', headers: { Authorization: `Bearer ${ token }`, }, data: settings, } ); await dispatch( NC_DATA ).receiveSocialProfiles( { ...profile, ...settings, } ); } catch ( e ) { await showErrorNotice( e ); } await markAsSavingProfileSettings( false ); } export async function saveProfileLabels( labelProfiles: ReadonlyArray< { readonly label: string; readonly profileIds: ReadonlyArray< Uuid >; } > ): Promise< void > { const profiles = select( NC_DATA ).getSocialProfiles() ?? []; const siteId = select( NC_DATA ).getSiteId(); const apiRoot = select( NC_DATA ).getApiRoot(); const token = select( NC_DATA ).getAuthenticationToken(); const getLabels = ( profileId: Uuid, currentLabels: ReadonlyArray< string > ) => { const selectedLabels = labelProfiles .filter( ( { profileIds } ) => profileIds.includes( profileId ) ) .map( ( { label } ) => label ); const keptLabels = currentLabels.filter( ( label ) => selectedLabels.includes( label ) ); const addedLabels = selectedLabels.filter( ( label ) => ! keptLabels.includes( label ) ); return [ ...keptLabels, ...addedLabels ]; }; try { const updates = profiles .map( ( profile ) => ( { profile, labels: getLabels( profile.id, profile.labels ?? [] ), } ) ) .filter( ( { profile, labels } ) => ! isEqual( profile.labels ?? [], labels ) ); await Promise.all( updates.map( ( { profile, labels } ) => apiFetch( { url: `${ apiRoot }/site/${ siteId }/profile/${ profile.id }`, method: 'PUT', credentials: 'omit', mode: 'cors', headers: { Authorization: `Bearer ${ token }`, }, data: { labels }, } ) ) ); if ( updates.length ) { await dispatch( NC_DATA ).receiveSocialProfiles( updates.map( ( { profile, labels } ) => ( { ...profile, labels, } ) ) ); } } catch ( e ) { await showErrorNotice( e ); throw e; } }