Skip to content

Commit

Permalink
Merge pull request #17 from abhik-wil/ret-ui
Browse files Browse the repository at this point in the history
Ret UI
  • Loading branch information
tanyamadaan authored Nov 17, 2023
2 parents 11c6ba0 + f068e5f commit 55acb81
Show file tree
Hide file tree
Showing 9 changed files with 14,779 additions and 479 deletions.
660 changes: 289 additions & 371 deletions index.html

Large diffs are not rendered by default.

88 changes: 88 additions & 0 deletions ui/api-contract.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// const {getFeatures} = require("./features")
function getStringAfterEquals(inputString) {
const index = inputString.indexOf("=");
if (index !== -1) {
return inputString.slice(index + 1).trim();
} else {
return "";
}
}

async function readBuildFile(branchName) {
if (!branchName) return;
const url = `https://api.github.com/repos/ONDC-Official/ONDC-LOG-Specifications/contents/ui/build.js?ref=${branchName}`;
const features = await getFeatures(branchName)

try {
const response = await fetch(url, {
headers: {
Authorization: "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",
},
});
const formattedResponse = await response?.json();
// reading data using github raw apis.
if(formattedResponse?.download_url){
setTimeout(async ()=>{
const rawResponse = await fetch(formattedResponse.download_url, {
// headers: {
// Authorization: "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",
// },
});
const formattedrawResponse = await rawResponse?.text();
build_spec = JSON.parse(getStringAfterEquals(formattedrawResponse));

onFirstLoad(build_spec,features);
},1200)
}

// let splitedText = atob(formattedResponse?.content);
// build_spec = JSON.parse(getStringAfterEquals(splitedText));
// onFirstLoad(build_spec);

} catch (error) {
console.log("Error fetching contract", error?.message || error);
//alert('Something went wrong, Please try again later')
}
}

async function fetchRequest(url){
try{
const response = await fetch(url, {
headers: {
Authorization: "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",
},
});
return await response?.json();
}catch{
console.log("Error fetching contract", error?.message || error);
}
}

async function loadContracts() {
//fetch branches & tags from repo
const BRANCHES_URL= "https://api.github.com/repos/ONDC-Official/ONDC-LOG-Specifications/branches";
const TAGS_URL= "https://api.github.com/repos/ONDC-Official/ONDC-LOG-Specifications/tags";

let response1, response2;
response1 = await fetchRequest(BRANCHES_URL)
response2 = await fetchRequest(TAGS_URL)
const response = [...response1,...response2]
const selectedOption = document.getElementById("contract-dropdown");
selectedOption.innerHTML = "";
response.forEach((flow) => {
var option = document.createElement("option");
option.text = flow.name;
selectedOption.add(option);
});
readBuildFile(response[0]?.name);
}

function upadteContract() {
const selectedOption = document.getElementById("contract-dropdown")?.value;
readBuildFile(selectedOption);
}


window.onload = function () {
loadContracts()
};
175 changes: 140 additions & 35 deletions ui/attribute.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,51 @@ var attributes;
function updateAttribute() {
var example_set = document.getElementById("attribute-dropdown");
var selectedValue = example_set.value;
document.querySelectorAll(".test").forEach((div) => div.remove());
document.querySelectorAll(".content").forEach((div) => div.remove());
addAttributeSets(selectedValue);
}

function updateSetsAttribute() {
var attributesDropDown = document.getElementById("attribute-dropdown");
var example_set = document.getElementById("attribute-sets-dropdown");
document.querySelectorAll(".test").forEach((div) => div.remove());
document.querySelectorAll(".content").forEach((div) => div.remove());
var selectedValue = example_set.value;
updateSets(attributesDropDown.value, selectedValue);
}

function updateL1Attribute() {
var attributesDropDown = document.getElementById("attribute-dropdown");
var example_set = document.getElementById("attribute-sets-dropdown");
var l1Dropdown = document.getElementById("attribute-l1-dropdown");
document.querySelectorAll(".content").forEach((div) => div.remove());
var object =
attributes[attributesDropDown.value]?.attribute_set[example_set.value];
for (const each of l1Dropdown.value.split(".")) {
object = object[each];
}
console.log("Object to render", object);
object = removeParentAttributes(object);
if (
"required" in object[Object.keys(object).filter((e) => e !== "parent")[0]]
)
flattenObject(object, null, null, object?.required_attributes);
}

function removeParentAttributes(obj) {
var newObj = {};
for (const key in obj) {
if (
!["parent", "usage", "description", "reference", "required"].includes(
key
)
&&
!("parent" in obj[key])
)
newObj = { ...newObj, [key]: { ...obj[key] } };
}
return newObj;
}

function loadAttributes(data) {
attributes = data;
var attributesDropDown = document.getElementById("attribute-dropdown");
Expand All @@ -30,9 +63,29 @@ function loadAttributes(data) {
}

function updateSets(value, option) {
const object = attributes[value]?.attribute_set;
console.log('object[option]',object[option]["required_attributes"]);
flattenObject(object[option],null,null,object[option]?.required_attributes);
const object = attributes[value]?.attribute_set[option];

var l1DropDown = document.getElementById("attribute-l1-dropdown");
l1DropDown.innerHTML = "";

const paths = generatePaths(object);

paths.forEach(function (key) {
var option = document.createElement("option");
option.text = key;
l1DropDown.add(option);
});

const firstKey = paths[0];
var keyDetail = object;
for (const each of firstKey.split(".")) {
keyDetail = keyDetail[each];
}

console.log("Object to render", keyDetail);

// if ("required" in keyDetail)
flattenObject(keyDetail, null, null, keyDetail?.required_attributes);
}

function addAttributeSets(option) {
Expand All @@ -46,47 +99,99 @@ function addAttributeSets(option) {
setsDropDown.add(option);
});

const firstKey = Object.keys(object)[0];
const keyDetail = object[firstKey];
console.log('object',object,keyDetail,keyDetail?.required_attributes );
const requiredAttr = 'required_attributes' in keyDetail
console.log('requiredAttr', requiredAttr)
flattenObject(keyDetail,null,null,keyDetail?.required_attributes);
var l1DropDown = document.getElementById("attribute-l1-dropdown");
l1DropDown.innerHTML = "";

const paths = generatePaths(object[Object.keys(object)[0]]);
console.log(
"PATH from addAttributeSets",
paths,
object[Object.keys(object)[0]]
);

paths.forEach(function (key) {
var option = document.createElement("option");
option.text = key;
l1DropDown.add(option);
});

const firstKey = paths[0];
var keyDetail = object[Object.keys(object)[0]];
for (const each of firstKey.split(".")) {
keyDetail = keyDetail[each];
}

console.log("Object to render", keyDetail);

if (
"required" in
keyDetail[Object.keys(keyDetail).filter((e) => e !== "parent")[0]]
)
flattenObject(keyDetail, null, null, keyDetail?.required_attributes);
}

function flattenObject(obj, prefix = "", result = {},requiredAttr) {
function generatePaths(o, root = "", result = [], itr = 0) {
var ok = Object.keys(o);
const filteredKeys = ok.filter(
(e) =>
!["required", "description", "usage", "reference", "parent"].includes(e)
);

if (ok.includes("parent") && o["parent"] === true)
for (const each of filteredKeys) {
var p = root.length > 0 ? root + "." + each : each;

generatePaths(o[each], p, result, itr + 1);
}
const addRoute = ["required", "description", "usage", "reference"].every(
(e) => ok.includes(e)
);
if (addRoute && root.length > 0)
var actualRoot = root.split(".").slice(0, -1).join(".");
if (actualRoot && !result.includes(actualRoot)) {
result.push(actualRoot);
}

return result;
}

function flattenObject(obj, prefix = "", result = {}, requiredAttr) {
if ("required" in obj) {
if(requiredAttr===undefined || requiredAttr.includes(prefix)){
var table = document.getElementById("tableset");
const newRow = document.createElement("tr");
newRow.classList.add("test");
newRow.style.wordBreak = "break-all";
const cell1 = document.createElement("td");
const cell2 = document.createElement("td");
const cell3 = document.createElement("td");
const cell4 = document.createElement("td");

cell1.textContent = prefix;
cell2.textContent = obj["required"];
cell3.textContent = obj["usage"];
cell4.textContent = obj["description"];

newRow.appendChild(cell1);
newRow.appendChild(cell2);
newRow.appendChild(cell3);
newRow.appendChild(cell4);

table.appendChild(newRow);
if (requiredAttr === undefined || requiredAttr.includes(prefix)) {
var table = document.getElementById("tableset");
const newRow = document.createElement("tr");
newRow.classList.add("content");
newRow.style.wordBreak = "break-all";
const cell1 = document.createElement("td");
const cell2 = document.createElement("td");
const cell3 = document.createElement("td");
const cell4 = document.createElement("td");

cell1.textContent = prefix;
cell2.textContent = obj["required"];
cell3.textContent = obj["usage"];
cell4.textContent = obj["description"];

newRow.appendChild(cell1);
newRow.appendChild(cell2);
newRow.appendChild(cell3);
newRow.appendChild(cell4);

table.appendChild(newRow);
}
return;
}
for (const key in obj) {
if (obj.hasOwnProperty(key) && key!=='required_attributes') {
if (
obj.hasOwnProperty(key) &&
key !== "required_attributes" &&
key !== "parent"
) {
const newKey = prefix ? prefix + "." + key : key;
if (Array.isArray(obj[key])) {
result[newKey] = obj[key];
} else if (typeof obj[key] === "object" && obj[key] !== null) {
flattenObject(obj[key], newKey, result,requiredAttr);
flattenObject(obj[key], newKey, result, requiredAttr);
} else {
result[newKey] = obj[key];
}
Expand Down
13,993 changes: 13,992 additions & 1 deletion ui/build.js

Large diffs are not rendered by default.

3 changes: 0 additions & 3 deletions ui/enum.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ function loadDropdown4() {
data.forEach(function (item) {
var option = document.createElement('option');
option.text = item["code"];
console.log("iten added", item.code)
dropdown4.add(option);
});
displayTable()
Expand Down Expand Up @@ -92,7 +91,6 @@ function displayTable() {
var selectedValue2 = dropdown2.value;
var selectedValue3 = dropdown4.value;

console.log("selectedValue3", selectedValue3)
// Get the table data
let data = getAttribute(parsedData[selectedValue1], selectedValue2.split("."))
var tableData = data.find(obj => {
Expand Down Expand Up @@ -127,7 +125,6 @@ function fetchData(url) {
function populateEnums(url) {
// Example usage: fetch YAML data from a URL
fetchData(url).then(data => {
console.log(data)
initSchema(data["x-enum"])
initTag(data["x-tags"])
})
Expand Down
3 changes: 3 additions & 0 deletions ui/examples.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,14 @@ function loadExample(data) {
}
function addExample(example_set) {
// let base = jsyaml.load(openApiYaml)
const matchText = 'form/'
let base = build_spec;
base["info"]["title"] = examples[example_set]["summary"]
base["info"]["description"] = examples[example_set]["description"]
let examplesList = examples[example_set]["example_set"]
for (var key in examplesList) {
//for forms
if(key.match(matchText)) continue;
var list = examplesList[key]["examples"];
base["paths"]["/" + key]["post"]["requestBody"]["content"]["application/json"]["examples"] = {};
for (var key2 in list) {
Expand Down
Loading

0 comments on commit 55acb81

Please sign in to comment.