Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add in getRecords() function to httpModule #72

Merged
merged 3 commits into from
Dec 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ module.exports = {
'/login/',
'/restws/',
'/log',
'/log.json',
'/taxonomy_term.json',
'/taxonomy_vocabulary.json',
'/farm_asset.json'
],
target: 'http://localhost:80',
changeOrigin: true,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,4 @@
"ios"
]
}
}
}
85 changes: 74 additions & 11 deletions src/data/httpModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,35 @@ export default {
actions: {

// SEND RECORDS TO SERVER
// EXPERIMENTAL: GETS AREAS AND OUTPUTS TO CONSOLE
pushToServer({ commit, rootState }, payload) {
const storage = window.localStorage;
const storedUrl = storage.getItem('url');
const storedToken = storage.getItem('token');

function handleSyncResponse(response, index) {
commit('updateLogs', {
indices: [index],
mapper(log) {
return logFactory({
...log,
id: response.id,
wasPushedToServer: true,
remoteUri: response.uri,
});
},
});
// EXPERIMENTAL BLOCK 1
// Get records when sync is complete
// Display the response in the console.
// getRecords requires the params URL, RESOURCE
// RESOURCE can be 'farm_asset' 'taxonomy_term' 'taxonomy_vocabulary' or 'log'
getRecords(storedUrl, 'taxonomy_vocabulary') // eslint-disable-line no-use-before-define
.then(
// END BLOCK 1
commit('updateLogs', {
indices: [index],
mapper(log) {
return logFactory({
...log,
id: response.id,
wasPushedToServer: true,
remoteUri: response.uri,
});
},
}),
// EXPERIMENTAL BLOCK 2
).catch(err => console.log('GET ERROR: ', err));
// END BLOCK 2
}

function handleSyncError(error, index) {
Expand Down Expand Up @@ -100,3 +112,54 @@ function pushRecord(url, token, log) {
}).then(resolve).catch(reject);
});
}

// EXPERIMENTAL
// Executes AJAX to get records from server
function getRecords(farmosUrl, recordClass) {
const logUrl = `${farmosUrl}/${recordClass}.json`;
const requestHeaders = {
'Content-Type': 'application/json',
};
console.log(`GETTING RECORDS FROM URL : ${logUrl}`);
return new Promise((resolve, reject) => {
fetch(logUrl, {
method: 'GET',
mode: 'cors',
headers: requestHeaders,
credentials: 'include',
}).then((response) => {
console.log('RESPONSE TO GET REQUEST RECEIVED!');
if (!response.ok) {
throw response;
}
return response.json();
}).then(
// Log response to the terminal
(response) => {
// When making a call to taxonomy_vocabulary I want to return only
// the VID for the farm_areas category
// getArea Returns an array consisting of '' or the vid\
function getArea(term) {
let VID = '';
if (term.machine_name === 'farm_areas') {
VID = term.vid;
}
return VID;
}
if (recordClass === 'taxonomy_vocabulary') {
console.log('DISPLAYING THE VID FOR AREA TERMS');
// Extracts single numerical value from the returned array
const areaVid = response.list.map(getArea).filter(
element => element !== '',
)[0];
console.log(areaVid);
} else {
// When making a call to log or taxonomy_term I will display all values received
console.log('DISPLAYING REQUESTED VALUES');
console.log(response);
}
},

).catch(reject);
});
}