|
| 1 | +/** |
| 2 | + * Internal dependencies |
| 3 | + */ |
| 4 | +import Dispatcher from 'dispatcher'; |
| 5 | +import wpcom from 'lib/wp'; |
| 6 | +import { |
| 7 | + DNS_ADD, |
| 8 | + DNS_ADD_COMPLETED, |
| 9 | + DNS_ADD_FAILED, |
| 10 | + DNS_APPLY_TEMPLATE_COMPLETED, |
| 11 | + DNS_DELETE, |
| 12 | + DNS_DELETE_COMPLETED, |
| 13 | + DNS_DELETE_FAILED, |
| 14 | + DNS_FETCH, |
| 15 | + DNS_FETCH_COMPLETED, |
| 16 | + DNS_FETCH_FAILED, |
| 17 | +} from './action-types'; |
| 18 | +import DnsStore from './store'; |
| 19 | +import { isBeingProcessed } from '.'; |
| 20 | + |
| 21 | +export function fetchDns( domainName ) { |
| 22 | + const dns = DnsStore.getByDomainName( domainName ); |
| 23 | + |
| 24 | + if ( dns.isFetching || dns.hasLoadedFromServer ) { |
| 25 | + return; |
| 26 | + } |
| 27 | + |
| 28 | + Dispatcher.handleViewAction( { |
| 29 | + type: DNS_FETCH, |
| 30 | + domainName, |
| 31 | + } ); |
| 32 | + |
| 33 | + wpcom.undocumented().fetchDns( domainName, ( error, data ) => { |
| 34 | + if ( ! error ) { |
| 35 | + Dispatcher.handleServerAction( { |
| 36 | + type: DNS_FETCH_COMPLETED, |
| 37 | + records: data && data.records, |
| 38 | + domainName, |
| 39 | + } ); |
| 40 | + } else { |
| 41 | + Dispatcher.handleServerAction( { |
| 42 | + type: DNS_FETCH_FAILED, |
| 43 | + domainName, |
| 44 | + } ); |
| 45 | + } |
| 46 | + } ); |
| 47 | +} |
| 48 | + |
| 49 | +export function addDns( domainName, record, onComplete ) { |
| 50 | + Dispatcher.handleServerAction( { |
| 51 | + type: DNS_ADD, |
| 52 | + domainName, |
| 53 | + record, |
| 54 | + } ); |
| 55 | + |
| 56 | + const dns = DnsStore.getByDomainName( domainName ); |
| 57 | + |
| 58 | + wpcom.undocumented().updateDns( domainName, dns.records, error => { |
| 59 | + const type = ! error ? DNS_ADD_COMPLETED : DNS_ADD_FAILED; |
| 60 | + Dispatcher.handleServerAction( { |
| 61 | + type, |
| 62 | + domainName, |
| 63 | + record, |
| 64 | + } ); |
| 65 | + |
| 66 | + onComplete( error ); |
| 67 | + } ); |
| 68 | +} |
| 69 | + |
| 70 | +export function deleteDns( domainName, record, onComplete ) { |
| 71 | + if ( isBeingProcessed( record ) ) { |
| 72 | + return; |
| 73 | + } |
| 74 | + |
| 75 | + Dispatcher.handleServerAction( { |
| 76 | + type: DNS_DELETE, |
| 77 | + domainName, |
| 78 | + record, |
| 79 | + } ); |
| 80 | + |
| 81 | + const dns = DnsStore.getByDomainName( domainName ); |
| 82 | + |
| 83 | + wpcom.undocumented().updateDns( domainName, dns.records, error => { |
| 84 | + const type = ! error ? DNS_DELETE_COMPLETED : DNS_DELETE_FAILED; |
| 85 | + |
| 86 | + Dispatcher.handleServerAction( { |
| 87 | + type, |
| 88 | + domainName, |
| 89 | + record, |
| 90 | + } ); |
| 91 | + |
| 92 | + onComplete( error ); |
| 93 | + } ); |
| 94 | +} |
| 95 | + |
| 96 | +export function applyDnsTemplate( domainName, provider, service, variables, onComplete ) { |
| 97 | + wpcom |
| 98 | + .undocumented() |
| 99 | + .applyDnsTemplate( domainName, provider, service, variables, ( error, data ) => { |
| 100 | + if ( ! error ) { |
| 101 | + Dispatcher.handleServerAction( { |
| 102 | + type: DNS_APPLY_TEMPLATE_COMPLETED, |
| 103 | + records: data && data.records, |
| 104 | + domainName, |
| 105 | + } ); |
| 106 | + } |
| 107 | + onComplete( error ); |
| 108 | + } ); |
| 109 | +} |
0 commit comments