Skip to content

Commit

Permalink
Switch to async/await syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
bame-da committed Nov 27, 2019
1 parent d4e7607 commit 0ba6b65
Show file tree
Hide file tree
Showing 12 changed files with 518 additions and 437 deletions.
5 changes: 5 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@
}
},
"plugins": [
["@babel/plugin-transform-runtime",
{
"regenerator": true
}
],
"@babel/plugin-syntax-dynamic-import",
"@babel/plugin-syntax-import-meta",
"@babel/plugin-proposal-class-properties",
Expand Down
47 changes: 38 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
"@babel/plugin-proposal-throw-expressions": "^7.2.0",
"@babel/plugin-syntax-dynamic-import": "^7.2.0",
"@babel/plugin-syntax-import-meta": "^7.2.0",
"@babel/plugin-transform-runtime": "^7.6.2",
"@babel/plugin-transform-runtime": "^7.7.4",
"@babel/preset-env": "^7.6.2",
"@babel/preset-react": "^7.6.2",
"autoprefixer": "^8.6.5",
Expand Down
11 changes: 6 additions & 5 deletions src/app/components/Upgrade/Upgrade.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import { connect } from "react-redux";
import { upgrade } from "../../middleware/persistMiddleware"
import "./Upgrade.scss";

const confirmUpgrade = user => {
upgrade(user)
.then(() => location.reload())
.catch(err => {
const confirmUpgrade = async user => {
try{
await upgrade(user);
location.reload();
} catch (err) {
console.log(err);
})
}
}

const skipUpgrade = dispatch => {
Expand Down
139 changes: 73 additions & 66 deletions src/app/middleware/ledgerUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,21 @@ export const rootErr = err => {
return err;
}

export const processResponse = response => {
if(!response.ok) {
return response.text().then(body => {
export const processResponse = async response => {
try{
if(!response.ok) {
const body = await response.text();
throw new Error(`Bad response from ledger: ${response.status} ${response.statusText} ${body}`);
});
}
const json = await response.json();
return json["result"];
} catch (err) {
throw new NestedError("Error processing response", err);
}
return response.json().then(response => response["result"]);
}

export const callAPI = (url, token, method, body) => {
export const callAPI = async (url, token, method, body) => {
try {
return fetch(
url,
{
Expand All @@ -37,38 +42,46 @@ export const callAPI = (url, token, method, body) => {
"method": method,
"mode":"cors"
}
).catch(err => {
);
} catch(err) {
throw new NestedError("Error fetching" + url + " with token " + token + ", method " + method + ", body " + JSON.stringify(body) + ": ", err);
});
};
}

export const create = (ledgerUrl, jwt, templateId, argument) => callAPI (
ledgerUrl + "command/create",
jwt,
"POST",
{
templateId,
argument
}
)
.then(processResponse)
.catch(err => {
throw new NestedError(`Error creating ${JSON.stringify({ templateId, argument })}`, err);
});
const callAndProcessAPI = async (url, token, method, body) => {
try {
const response = await callAPI(url, token, method, body);
return processResponse(response);
} catch (err) {
throw err;
}
}

export const search = (ledgerUrl, jwt, templateId, filter) => callAPI(
ledgerUrl + "contracts/search",
jwt,
"POST",
{
"%templates": [templateId]
}
)
.then(processResponse)
.then(response => response.filter(filter))
.catch(err => {
export const create = async (ledgerUrl, jwt, templateId, argument) => callAndProcessAPI (
ledgerUrl + "command/create",
jwt,
"POST",
{
templateId,
argument
}
);

export const search = async (ledgerUrl, jwt, templateId, filter) => {
try {
const response = await callAndProcessAPI(
ledgerUrl + "contracts/search",
jwt,
"POST",
{
"%templates": [templateId]
}
)
return response.filter(filter);
} catch(err) {
throw new NestedError(`Error fetching ${JSON.stringify(templateId)} contracts: `, err);
});
}
}

const dataTemplates = [
["User", "Profile"],
Expand All @@ -83,18 +96,14 @@ const versionedTempates = dataTemplates.flatMap(t =>
"moduleName" : `${v}.${t[0]}`
})))

export const loadAll = (ledgerUrl, jwt) => callAPI(
export const loadAll = async (ledgerUrl, jwt) => callAndProcessAPI(
ledgerUrl + "contracts/search",
jwt,
"POST",
{
"%templates": versionedTempates
}
)
.then(processResponse)
.catch(err => {
throw new NestedError(`Error fetching all contracts: `, err);
});
);

const templateModule = c => c.templateId instanceof Object
? c.templateId.moduleName
Expand Down Expand Up @@ -131,31 +140,33 @@ const filterGroupAndVersion = (party, cs) => {
return ctMap
}

export const loadState = (ledgerUrl, jwt, party = null) => loadAll(ledgerUrl, jwt)
.then(contracts => {
const contractMap = filterGroupAndVersion(party, contracts);

const boardsById = mapBy("_id")(contractMap["Board"]["Data"]);
const listsById = mapBy("_id")(contractMap["Board"]["CardList"]);
const cardsById = mapBy("_id")(contractMap["Board"]["Card"]);
const users = (contractMap["User"]["Profile"]);
users.sort((a,b) => (a.displayName > b.displayName) ? 1 : ((b.displayName > a.displayName) ? -1 : 0));
const boardUsersById = mapBy("boardId")(contractMap["Rules"]["Board"]);

return {
boardsById,
listsById,
cardsById,
users,
boardUsersById
}
})
.catch(err => {
export const loadState = async (ledgerUrl, jwt, party = null) => {
try {
const contracts = await loadAll(ledgerUrl, jwt);

const contractMap = filterGroupAndVersion(party, contracts);

const boardsById = mapBy("_id")(contractMap["Board"]["Data"]);
const listsById = mapBy("_id")(contractMap["Board"]["CardList"]);
const cardsById = mapBy("_id")(contractMap["Board"]["Card"]);
const users = (contractMap["User"]["Profile"]);
users.sort((a,b) => (a.displayName > b.displayName) ? 1 : ((b.displayName > a.displayName) ? -1 : 0));
const boardUsersById = mapBy("boardId")(contractMap["Rules"]["Board"]);

return {
boardsById,
listsById,
cardsById,
users,
boardUsersById
}
} catch(err) {
throw new NestedError(`Error processing all contracts: `, err);
});
}
}


export const exercise = (ledgerUrl, jwt, templateId, contractId, choice, argument) => callAPI (
export const exercise = (ledgerUrl, jwt, templateId, contractId, choice, argument) => callAndProcessAPI (
ledgerUrl + "command/exercise",
jwt,
"POST",
Expand All @@ -165,8 +176,4 @@ export const exercise = (ledgerUrl, jwt, templateId, contractId, choice, argumen
contractId,
argument
}
)
.then(processResponse)
.catch(err => {
throw new NestedError(`Error exercising ${JSON.stringify({ contractId, choice, templateId, argument })}`, err);
});
);
Loading

0 comments on commit 0ba6b65

Please sign in to comment.