Skip to content

Commit

Permalink
feat(project manager): add jump to project manager
Browse files Browse the repository at this point in the history
  • Loading branch information
MatsJohansen87 committed Apr 22, 2024
1 parent 959fdb1 commit 80f1a60
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 41 deletions.
3 changes: 2 additions & 1 deletion packages/demo/public/options.json
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@
]
},
"negotiateOptions": {
"negotiatorURL": "http://localhost:8097/create-query-and-design-project",
"newProjectUrl": "http://localhost:8097/create-query-and-design-project",
"editProjectUrl": "http://localhost:8097/edit-project",
"siteMapping": [
{
"site": "Dresden",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,12 @@
export let title: string = "Negotiate with biobanks";
let disabled: boolean = false;
/**
* TODO: implement negotiator connection
*/
</script>
<button
part={`lens-negotiate-button lens-negotiate-button-${
disabled ? "disabled" : "active"
}`}
on:click={() => negotiate($negotiateStore, $queryBase64Store)}
on:click={() => negotiate($negotiateStore)}
disabled={disabled}
>
<div part="lens-negotiate-button-title">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@
);
}
};
$: console.log($responseStore);
/**
* sort tableRowData alphanumerically by the given column
Expand Down
87 changes: 55 additions & 32 deletions packages/lib/src/stores/negotiate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,20 @@ export const getHumanReadableQuery = (): string => {
let negotiateOptions: any = {}
const siteCollectionMap: Map<string,string> = new Map()

const urlParams: URLSearchParams = new URLSearchParams(window.location.search);
const collectionParams: string | null = urlParams.get("collections");

lensOptions.subscribe((options: any) => {
if (!options) return

negotiateOptions = options.negotiateOptions
options.negotiateOptions.siteMapping.forEach(({ site, collection }) => {
siteCollectionMap.set(site, collection)
if (collectionParams !== null && collectionParams.split(',').includes(collection)) {
negotiateStore.update((value) => {
return [...value, site]
})
}
})
})

Expand All @@ -134,11 +142,11 @@ export const getCollections = (sitesToNegotiate: string[]): Collection[] => {
let siteCollections: Collection[] = []

sitesToNegotiate.forEach((site: string) => {
let collectionId: string = "couldn't map site in search UI"
let collectionId: string = ""

// TODO: Why is site id mapped to Uppercase?
if (siteCollectionMap.has(site) && siteCollectionMap.get(site) !== '') {
collectionId = siteCollectionMap.get(site)
collectionId = siteCollectionMap.get(site) || ""
}

const siteId: string = site.split(':collection:')[0]
Expand All @@ -165,17 +173,20 @@ export const getCollections = (sitesToNegotiate: string[]): Collection[] => {
// })
// )

siteCollections.push({
siteId,
site,
collectionId,
/**
* TODO: add the local redirect uri here
*/
localRedirectUri: 'some uri'
})
if(collectionId !== "") {
siteCollections.push({
siteId,
site,
collectionId,
/**
* TODO: add the local redirect uri here
*/
localRedirectUri: 'some uri'
})
}
})

console.log('getCollections -> return siteCollections', siteCollections);
return siteCollections
}

Expand All @@ -186,9 +197,11 @@ export const getCollections = (sitesToNegotiate: string[]): Collection[] => {
* redirects to negotiator
* @param sitesToNegotiate the sites to negotiate with
*/
export const negotiate = async (sitesToNegotiate: string[], queryBase64String: string) => {
export const negotiate = async (sitesToNegotiate: string[]) => {

//TODO: get auth token here

let sendableQuery: SendableQuery
let sendableQuery!: SendableQuery
queryStore.subscribe((value: QueryItem[][]) => {
const uuid = uuidv4()
sendableQuery = {
Expand All @@ -197,13 +210,19 @@ export const negotiate = async (sitesToNegotiate: string[], queryBase64String: s
}
})

let humanReadable: string = getHumanReadableQuery();
let collections: Collection[] = getCollections(sitesToNegotiate)
const queryBase64String: string = btoa(JSON.stringify(sendableQuery.query))
const humanReadable: string = getHumanReadableQuery();
const collections: Collection[] = getCollections(sitesToNegotiate)
// TODO: Implement proper configuration option for the switch between negotiator and project manager
let negotiatorResponse = (false)
? await sendRequestToNegotiator(sendableQuery, humanReadable, collections, queryBase64String)
: await sendRequestToProjectManager(sendableQuery, humanReadable, collections, queryBase64String)
window.location.href = negotiatorResponse.redirect_uri.toString()

const indexOfQuestionMark = negotiatorResponse.redirect_uri.toString().indexOf('?')
const subpage = "/project-view"
const negotiationURI = negotiatorResponse.redirect_uri.toString().slice(0, indexOfQuestionMark) + `${subpage}` + negotiatorResponse.redirect_uri.toString().slice(indexOfQuestionMark)

window.location.href = negotiationURI
}


Expand Down Expand Up @@ -251,22 +270,18 @@ async function sendRequestToNegotiator(sendableQuery: SendableQuery, humanReadab
* */
async function sendRequestToProjectManager(sendableQuery: SendableQuery, humanReadable: string, collections: Collection[], queryBase64String: string): Promise<any> {


const base64Query: string = btoa(JSON.stringify(sendableQuery.query))
const queryParam: string = (base64Query != "") ?
`?query=${base64Query}` : ""

const returnURL: string = `${window.location.protocol}//${window.location.host}/${queryParam}`;
console.log(returnURL)


console.log(collections)
const queryParam: string = (queryBase64String != "") ?
`&query=${queryBase64String}` : ""

const negotiationPartners = collections.map(collection => collection.collectionId.toLocaleLowerCase()).join(',')
console.log(negotiationPartners)
console.log(encodeURIComponent(returnURL))
const returnURL: string = `${window.location.protocol}//${window.location.host}/?collections=${negotiationPartners}${queryParam}`;
const urlParams: URLSearchParams = new URLSearchParams(window.location.search);
const projectCode: string | null = urlParams.get("project-code");
const projectCodeParam: string = projectCode ? `&project-code=${projectCode}` : "";
const negotiateUrl = projectCode ? negotiateOptions.editProjectUrl : negotiateOptions.newProjectUrl;

const response: Response = await fetch(
`${negotiateOptions.negotiatorURL}?explorer-ids=${negotiationPartners}&query-format=CQL_DATA&human-readable=${humanReadable}&explorer-url=${encodeURIComponent(returnURL)}`,
`${negotiateUrl}?explorer-ids=${negotiationPartners}&query-format=CQL_DATA&human-readable=${humanReadable}&explorer-url=${encodeURIComponent(returnURL)}${projectCodeParam}`,
{
method: "POST",
headers: {
Expand All @@ -276,8 +291,17 @@ async function sendRequestToProjectManager(sendableQuery: SendableQuery, humanRe
},
body: getCql()
}
);
return response.json();
).then(response => response.json());

/**
* replace query-code with project-code
* TODO: remove when backend bug is fixed
*/
if (response.redirect_uri) {
response.redirect_uri = response.redirect_uri.replace('query-code', 'project-code');
}

return response;
}

function getCql(): string {
Expand All @@ -289,6 +313,5 @@ function getCql(): string {
const measure = buildMeasure(library.url, currentMeasures.map(measureItem => measureItem.measure))
const query = {lang: "cql", lib: library, measure: measure};

console.log(btoa(decodeURI(JSON.stringify(query))))
return btoa(decodeURI(JSON.stringify(query)));
}
2 changes: 1 addition & 1 deletion packages/lib/src/stores/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const queryBase64Store = writable<string>("");
* when the url has a query as base64 string, this will be parsed and the queryStore will be updated
*/
const urlParams: URLSearchParams = new URLSearchParams(window.location.search);
const queryParam: string = urlParams.get("query");
const queryParam: string | null = urlParams.get("query");

if (queryParam !== null) {
const queryParamDecoded: QueryItem[][] = JSON.parse(atob(queryParam));
Expand Down

0 comments on commit 80f1a60

Please sign in to comment.