Skip to content

Commit

Permalink
linked dropdowns
Browse files Browse the repository at this point in the history
  • Loading branch information
Tguntenaar committed Dec 8, 2024
1 parent 9d94165 commit ae4d4d7
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 139 deletions.
Original file line number Diff line number Diff line change
@@ -1,28 +1,15 @@
const { fetchGraphQL } = VM.require(
`${REPL_INFRASTRUCTURE_COMMITTEE}/widget/core.common`
);

const { href } = VM.require(`${REPL_DEVHUB}/widget/core.lib.url`);
href || (href = () => {});

const { fetchCacheApi, searchCacheApi } = VM.require(
`${REPL_INFRASTRUCTURE_COMMITTEE}/widget/core.common`
);

const linkedProposals = props.linkedProposals;
const onChange = props.onChange;
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
}
}`;
const [textAfterHash, setTextAfterHash] = useState("");

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

function separateNumberAndText(str) {
const numberRegex = /\d+/;
function searchProposals(input) {
if (state.loading) return;
State.update({ loading: true });

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() };
}
}
searchCacheApi("proposals", input).then((result) => {
let proposalsData = result.body.records;

const buildWhereClause = () => {
let where = {};
const { number, text } = separateNumberAndText(searchProposalId);

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

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

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);
}
const data = [];
for (const prop of proposalsData) {
data.push({
label: "# " + prop.proposal_id + " : " + prop.name,
value: prop.proposal_id,
});
}
setProposalsOptions(data);
});
};
}

useEffect(() => {
fetchProposals();
}, [searchProposalId]);
if (textAfterHash.trim()) {
searchProposals(textAfterHash);
}
}, [textAfterHash]);

return (
<>
Expand Down Expand Up @@ -150,7 +96,7 @@ return (
defaultLabel: "Search proposals",
searchByValue: true,
onSearch: (value) => {
setSearchProposalId(value);
setTextAfterHash(value);
},
}}
/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
const { RFP_TIMELINE_STATUS, fetchGraphQL, parseJSON } = VM.require(
`${REPL_INFRASTRUCTURE_COMMITTEE}/widget/core.common`
) || { RFP_TIMELINE_STATUS: {}, parseJSON: () => {} };
const { RFP_TIMELINE_STATUS, parseJSON, fetchCacheApi, searchCacheApi } =
VM.require(`${REPL_INFRASTRUCTURE_COMMITTEE}/widget/core.common`) || {
RFP_TIMELINE_STATUS: {},
parseJSON: () => {},
fetchCacheApi: () => {},
searchCacheApi: () => {},
};
const { href } = VM.require(`${REPL_DEVHUB}/widget/core.lib.url`);
href || (href = () => {});

Expand All @@ -20,69 +24,14 @@ 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+/;

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 = () => {
// show only accepting submissions stage rfps
let where = {};
const { number, text } = separateNumberAndText(searchRFPId);

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") {
if (typeof searchCacheApi !== "function") {
return;
}
fetchGraphQL(query, "GetLatestSnapshot", variables).then(async (result) => {
searchCacheApi("rfps", searchRFPId).then(async (result) => {
if (result.status === 200) {
if (result.body.data) {
const rfpsData = result.body.data?.[queryName];
if (result.body.records) {
const rfpsData = result.body.records;
const data = [];
const acceptingData = [];
for (const prop of rfpsData) {
Expand Down

0 comments on commit ae4d4d7

Please sign in to comment.