Skip to content

Instantly share code, notes, and snippets.

@burakcan
Created August 31, 2016 10:53
Show Gist options
  • Save burakcan/365833f1fbabd98166f2fa8b98f0862f to your computer and use it in GitHub Desktop.
Save burakcan/365833f1fbabd98166f2fa8b98f0862f to your computer and use it in GitHub Desktop.
import { takeLatest } from 'redux-saga';
import { take, actionChannel, select, call, put } from 'redux-saga/effects';
import authorizedFetch from 'common/utils/fetch/authorizedFetchSaga';
import { getAdAccount } from 'facebook/Campaigns/Manage/reducer/campaign';
import { getAdsets } from 'facebook/Campaigns/Manage/reducer/adsets';
import { FETCH_ADSETS_SUCCESS } from 'facebook/Campaigns/Manage/actions/adsets';
import { FETCH_CAMPAIGN_SUCCESS } from 'facebook/Campaigns/Manage/actions/campaign';
import {
FETCH_TARGETING_INFO,
createFetchTargetingInfoSuccess,
createFetchTargetingInfoError,
} from '../actions';
export function *fetchTargetingInfo() {
const [adAccount, adsets] = [
yield select(getAdAccount),
yield select(getAdsets),
];
const specs = adsets.map(adset => adset.targeting);
function *handleSuccess(result) {
const adsetIdMap =
result.specs
.reduce((prev, curr, index) => ({
...prev,
[adsets[index].id]: curr,
}), {});
yield put(
createFetchTargetingInfoSuccess(adsetIdMap)
);
}
yield call(
authorizedFetch, [
`adAccounts/${adAccount.id}/targetinginfo`, {
method: 'POST',
body: JSON.stringify({
params: 'sentence,reach,bid',
specs,
}),
},
],
error => put(createFetchTargetingInfoError(error)),
result => call(handleSuccess, result)
);
}
export default function *() {
const channel = yield actionChannel(FETCH_TARGETING_INFO);
yield [ // wait for these
take(FETCH_CAMPAIGN_SUCCESS),
take(FETCH_ADSETS_SUCCESS),
];
yield takeLatest(channel, fetchTargetingInfo);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment