Last active
February 16, 2020 17:35
-
-
Save jgaehring/8799af782404fdf8abbaa116b3e97614 to your computer and use it in GitHub Desktop.
Example usage of the new log formatting functions for farmOS-client
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import farmos from 'farmos'; | |
import farmLog from './farmLog'; | |
import defaultLogTypes from '../core/store/defaultLogTypes'; | |
// Initialize the formatting functions by passing in the log type schemas to `farmLog` | |
const { | |
createLog, | |
updateLog, | |
formatLogForServer, | |
mergeLogFromServer, | |
} = farmLog(defaultLogTypes); | |
// Initialize farmos.js. | |
const farm = farmos('test.farmos.net', 'admin', 'admin'); | |
// Create a brand new blank log. | |
const logA = createLog(); | |
// Update that log's name. | |
const logB = updateLog(logA, { name: 'This is the log name' }); | |
// Prepare that log for sending to the server. | |
const logC = formatLogForServer(logB); | |
// Start the process for connecting to the server... | |
farm.authenticate() | |
// Send logC to the server. | |
.then(token => farm.log.send(logC, token)) | |
.then((res) => { | |
// Modify logC locally, then fetch the log from the server again. | |
const localLog = updateLog(logC, { name: 'This is a new name' }); | |
farm.log.get(res.id) | |
.then((serverLog) => { | |
// Merge the changes to the local log with any changes that have occurred on the server, | |
// then log it to the console. | |
const mergedLog = mergeLogFromServer(serverLog, localLog); | |
console.log(mergedLog); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment