Skip to content

Commit

Permalink
add indexer
Browse files Browse the repository at this point in the history
  • Loading branch information
Megha-Dev-19 committed Nov 15, 2024
1 parent 7573a28 commit d575f20
Show file tree
Hide file tree
Showing 9 changed files with 374 additions and 552 deletions.
3 changes: 2 additions & 1 deletion instances/treasury-templar.near/aliases.mainnet.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@
"REPL_RFP_INDEXER_QUERY_NAME": "polyprogrammist_near_devhub_ic_v1_rfp_snapshots",
"REPL_PROPOSAL_FEED_INDEXER_QUERY_NAME": "polyprogrammist_near_devhub_ic_v1_proposals_with_latest_snapshot",
"REPL_PROPOSAL_QUERY_NAME": "polyprogrammist_near_devhub_ic_v1_proposal_snapshots",
"REPL_INDEXER_HASURA_ROLE": "polyprogrammist_near"
"REPL_INDEXER_HASURA_ROLE": "polyprogrammist_near",
"REPL_CACHE_URL": "https://templar-cache-api-rs.fly.dev"
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,6 @@ const [selectedProposals, setSelectedProposals] = useState(linkedProposals);
const [proposalsOptions, setProposalsOptions] = useState([]);
const [searchProposalId, setSearchProposalId] = useState("");

const queryName = "${REPL_PROPOSAL_FEED_INDEXER_QUERY_NAME}";
const query = `query GetLatestSnapshot($offset: Int = 0, $limit: Int = 10, $where: ${queryName}_bool_exp = {}) {
${queryName}(
offset: $offset
limit: $limit
order_by: {proposal_id: desc}
where: $where
) {
name
proposal_id
}
}`;

useEffect(() => {
if (JSON.stringify(linkedProposals) !== JSON.stringify(selectedProposals)) {
setSelectedProposals(linkedProposals);
Expand All @@ -36,69 +23,38 @@ useEffect(() => {
}
}, [selectedProposals]);

function separateNumberAndText(str) {
const numberRegex = /\d+/;

if (numberRegex.test(str)) {
const number = str.match(numberRegex)[0];
const text = str.replace(numberRegex, "").trim();
return { number: parseInt(number), text };
} else {
return { number: null, text: str.trim() };
}
}

const buildWhereClause = () => {
let where = {};
const { number, text } = separateNumberAndText(searchProposalId);
function searchProposals() {
const ENDPOINT = "${REPL_CACHE_URL}";

if (number) {
where = { proposal_id: { _eq: number }, ...where };
}

if (text) {
where = {
_or: [
{ name: { _iregex: `${text}` } },
{ summary: { _iregex: `${text}` } },
{ description: { _iregex: `${text}` } },
],
...where,
};
}
let searchInput = encodeURI(searchProposalId);
let searchUrl = searchInput
? `${ENDPOINT}/proposals/search/${searchInput}`
: `${ENDPOINT}/proposals`;

return where;
};

const fetchProposals = () => {
const FETCH_LIMIT = 30;
const variables = {
limit: FETCH_LIMIT,
offset: 0,
where: buildWhereClause(),
};
if (typeof fetchGraphQL !== "function") {
return;
}
fetchGraphQL(query, "GetLatestSnapshot", variables).then(async (result) => {
if (result.status === 200) {
if (result.body.data) {
const proposalsData = result.body.data?.[queryName];
const data = [];
for (const prop of proposalsData) {
data.push({
label: "# " + prop.proposal_id + " : " + prop.name,
value: prop.proposal_id,
});
}
setProposalsOptions(data);
return asyncFetch(searchUrl, {
method: "GET",
headers: {
accept: "application/json",
},
})
.then((result) => {
const proposalsData = result.body.records;
const data = [];
for (const prop of proposalsData) {
data.push({
label: "# " + prop.proposal_id + " : " + prop.name,
value: prop.proposal_id,
});
}
}
});
};
setProposalsOptions(data);
})
.catch((error) => {
console.log("Error searching cache api", error);
});
}

useEffect(() => {
fetchProposals();
searchProposals();
}, [searchProposalId]);

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,6 @@ const [allRfpOptions, setAllRfpOptions] = useState([]);
const [searchRFPId, setSearchRfpId] = useState("");
const [initialStateApplied, setInitialState] = useState(false);

const queryName = "${REPL_RFP_FEED_INDEXER_QUERY_NAME}";
const query = `query GetLatestSnapshot($offset: Int = 0, $limit: Int = 10, $where: ${queryName}_bool_exp = {}) {
${queryName}(
offset: $offset
limit: $limit
order_by: {rfp_id: desc}
where: $where
) {
name
rfp_id
timeline
}
}`;

function separateNumberAndText(str) {
const numberRegex = /\d+/;

Expand All @@ -46,69 +32,48 @@ function separateNumberAndText(str) {
}
}

const buildWhereClause = () => {
// show only accepting submissions stage rfps
let where = {};
const { number, text } = separateNumberAndText(searchRFPId);
function searchRfps() {
const ENDPOINT = "${REPL_CACHE_URL}";
let searchInput = encodeURI(searchRFPId);
let searchUrl = searchInput
? `${ENDPOINT}/rfps/search/${searchInput}`
: `${ENDPOINT}/rfps`;

if (number) {
where = { rfp_id: { _eq: number }, ...where };
}

if (text) {
where = {
_or: [
{ name: { _iregex: `${text}` } },
{ summary: { _iregex: `${text}` } },
{ description: { _iregex: `${text}` } },
],
...where,
};
}

return where;
};

const fetchRfps = () => {
const FETCH_LIMIT = 30;
const variables = {
limit: FETCH_LIMIT,
offset: 0,
where: buildWhereClause(),
};
if (typeof fetchGraphQL !== "function") {
return;
}
fetchGraphQL(query, "GetLatestSnapshot", variables).then(async (result) => {
if (result.status === 200) {
if (result.body.data) {
const rfpsData = result.body.data?.[queryName];
const data = [];
const acceptingData = [];
for (const prop of rfpsData) {
const timeline = parseJSON(prop.timeline);
const label = "# " + prop.rfp_id + " : " + prop.name;
const value = prop.rfp_id;
if (timeline.status === RFP_TIMELINE_STATUS.ACCEPTING_SUBMISSIONS) {
acceptingData.push({
label,
value,
});
}
data.push({
return asyncFetch(searchUrl, {
method: "GET",
headers: {
accept: "application/json",
},
})
.then((result) => {
const rfpsData = result.body.records;
const data = [];
const acceptingData = [];
for (const prop of rfpsData) {
const timeline = parseJSON(prop.timeline);
const label = "# " + prop.rfp_id + " : " + prop.name;
const value = prop.rfp_id;
if (timeline.status === RFP_TIMELINE_STATUS.ACCEPTING_SUBMISSIONS) {
acceptingData.push({
label,
value,
});
}
setAcceptingRfpsOption(acceptingData);
setAllRfpOptions(data);
data.push({
label,
value,
});
}
}
});
};
setAcceptingRfpsOption(acceptingData);
setAllRfpOptions(data);
})
.catch((error) => {
console.log("Error searching cache api", error);
});
}

useEffect(() => {
fetchRfps();
searchRfps();
}, [searchRFPId]);

useEffect(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ const fontFamily = props.fontFamily ?? "sans-serif";
const alignToolItems = props.alignToolItems ?? "right";
const placeholder = props.placeholder ?? "";
const showAccountAutoComplete = props.showAutoComplete ?? false;
const showProposalIdAutoComplete = props.showProposalIdAutoComplete ?? false;
const showRfpIdAutoComplete = props.showRfpIdAutoComplete ?? false;
const showProposalIdAutoComplete = false;
const showRfpIdAutoComplete = false;
const autoFocus = props.autoFocus ?? false;

const proposalQueryName = "${REPL_PROPOSAL_FEED_INDEXER_QUERY_NAME}";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -780,15 +780,15 @@ const onSubmit = ({ isDraft, isCancel }) => {
const body = {
proposal_body_version: "V1",
linked_rfp: linkedRfp?.value,
category: "Infrastructure Committee",
category: "Templar",
name: title,
description: description,
summary: summary,
linked_proposals: linkedProposalsIds,
requested_sponsorship_usd_amount: requestedSponsorshipAmount,
requested_sponsorship_paid_in_currency: requestedSponsorshipToken.value,
receiver_account: receiverAccount,
requested_sponsor: "infrastructure-committee.near",
requested_sponsor: "templar.sputnik-dao.near",
supervisor: supervisor,
timeline: isCancel
? {
Expand Down
Loading

0 comments on commit d575f20

Please sign in to comment.