Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enh: allow storing and accessing scoped variables from activities #322

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .github/workflows/build_deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ jobs:
steps:

- name: Setup Node
uses: actions/setup-node@v1
uses: actions/setup-node@v4
with:
node-version: '12.x'
node-version: '14.x'

# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v2
- uses: actions/checkout@v4

# Runs a set of commands using the runners shell
- name: Run a multi-line script
Expand All @@ -35,7 +35,7 @@ jobs:
touch dist/.nojekyll

- name: Deploy 🚀
uses: JamesIves/github-pages-deploy-action@releases/v3
uses: JamesIves/github-pages-deploy-action@v4
if: github.ref == 'refs/heads/master'
with:
ACCESS_TOKEN: ${{ secrets.ACCESS_TOKEN }}
Expand Down
30 changes: 8 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,33 +152,19 @@ need to [fork this repository](https://help.github.com/en/github/getting-started
and [clone it](https://help.github.com/en/github/creating-cloning-and-archiving-repositories/cloning-a-repository)
on your machine.

To then run the app locally, you will have to install javascript `node.js`. A
good way to do this, is to install [node version manager](https://github.com/nvm-sh/nvm)
(NVM) to help you deal with different version of `node.js`.

If you are running linux, you can install NVM by typing:

```
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash
```

Then close your terminal and reopen it then run the following to install the
version 9 of `node.js`
This app uses an older version of node, hence the easiest option is to use a
Docker setup.

```
nvm install node
nvm install 9
```shell
docker run -it --rm -p 8080:8080 -v $(pwd):/src --entrypoint bash \
--platform linux/amd64 node:14.21.3-buster-slim
```

You should then be able to use node to the following:
In the container terminal

### Install the dependencies
``` bash
```shell
cd /src
npm install
```

### Serve the app locally
``` bash
npm run serve
```

Expand Down
54 changes: 27 additions & 27 deletions package-lock.json

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

59 changes: 36 additions & 23 deletions src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -366,35 +366,47 @@ export default {
_.map(keys, (k) => {
// grab the value of the key from responseMapper
let val = responseMapper[k].val;
if (val !== 'skipped' && val !== 'dontknow') {
if (_.isString(val)) {
val = `'${val}'`; // put the string in quotes
}
if (_.isArray(val)) {
val = `[${val}]`; // put braces for array
if (val !== undefined) {
if (val !== 'skipped' && val !== 'dontknow') {
if (_.isString(val)) {
val = `'${val}'`; // put the string in quotes
}
if (_.isArray(val)) {
val = `[${val}]`; // put braces for array
}
output = output.replaceAll(new RegExp(`\\b${k}\\b` || `\\b${k}\\.`, 'g'), val);
} else {
output = output.replaceAll(new RegExp(`\\b${k}\\b`, 'g'), 0);
}
output = output.replace(new RegExp(`\\b${k}\\b` || `\\b${k}\\.`), val);
} else {
output = output.replace(new RegExp(`\\b${k}\\b`), 0);
}
});
return safeEval(output);
},
responseMapper(index, responses) {
let keyArr;
responseMapper(index, responses, responseMap) {
let keyArr = [];
// a variable map is defined! great
if (this.schema['http://schema.repronim.org/addProperties']) {
const vmap = this.schema['http://schema.repronim.org/addProperties'];
keyArr = _.map(vmap, (v) => {
const key = v['http://schema.repronim.org/isAbout'][0]['@id'];
const qId = v['http://schema.repronim.org/variableName'][0]['@value'];
const rp = _.filter(responses, r => key in r);
let val = rp[0];
if (rp[0]) {
val = rp[0][key];
}
return { key, val, qId };
});
Object.entries(vmap).forEach(
// eslint-disable-next-line no-unused-vars
([unused, v]) => {
const key = v['http://schema.repronim.org/isAbout'][0]['@id'];
const qId = v['http://schema.repronim.org/variableName'][0]['@value'];
if (key in responseMap) {
Object.entries(responseMap[key]).forEach(
([key1, value1]) => {
const joined_key = ''.concat(qId,'.',key1);
keyArr.push({ qId: joined_key, val: value1['val'], key: value1['ref'] });
});
}
const rp = _.filter(responses, r => key in r);
let val = rp[0];
if (rp[0]) {
val = rp[0][key];
}
keyArr.push({ key, val, qId });
}
);
if (this.$store.getters.getQueryParameters) {
const q = this.$store.getters.getQueryParameters;
Object.entries(q).forEach(
Expand Down Expand Up @@ -433,6 +445,7 @@ export default {
}
},
async computeVisibilityCondition(cond, index) {
console.log('computeVisibilityCondition', cond, index);
if (_.isObject(cond)) {
const request = {
method: cond.method,
Expand All @@ -452,13 +465,13 @@ export default {
// default to false
this.visibility[index] = false;
}
// console.log('making request', request, 'cache', this.cache);
console.log('making request', request, 'cache', this.cache);
const resp = await axios(request);
// this.visibility[index] = resp.data;
this.cache[cacheKey] = resp.data.qualified;
return resp.data.qualified;
} else if (_.isString(cond)) {
const responseMapper = this.responseMapper(index, this.$store.state.responses);
const responseMapper = this.responseMapper(index, this.$store.state.responses, this.$store.state.responseMap);
const v = this.evaluateString(cond, responseMapper);
// this.visibilty[index] = v;
return v;
Expand Down
12 changes: 10 additions & 2 deletions src/components/Survey/Survey.vue
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
import SurveyItem from '../SurveyItem/';
import Loader from '../Loader/';
import JSZip from "jszip";
import {saveAs} from "file-saver";
//import {saveAs} from "file-saver";
import config from "../../config";
import axios from "axios";

Expand Down Expand Up @@ -251,7 +251,8 @@
let exportVal = val;
let usedList = [];
let isAboutUrl = itemUrl;
if (val.constructor === Object && !val.hasOwnProperty('unitCode')) { // to find sub-activities; condition might need to be changed
// eslint-disable-next-line no-prototype-builtins
if (_.isObject(val) && !val.hasOwnProperty('unitCode')) { // to find sub-activities; condition might need to be changed
const sectionItemKey = Object.keys(val)[0];
const sectionItemValue = Object.values(val)[0];
exportVal = sectionItemValue;
Expand Down Expand Up @@ -368,6 +369,12 @@
});

}
const respMapper = {};
_.map(keyArr, (a) => {
respMapper[a.qId] = { val: a.val, ref: a.key };
});
// Store the response variables in the state
this.$store.state.responseMap[this.activity['@id']] = respMapper;
if (this.$store.getters.getQueryParameters) {
const q = this.$store.getters.getQueryParameters;
Object.entries(q).forEach(
Expand Down Expand Up @@ -495,6 +502,7 @@
'Content-Type': 'multipart/form-data'
};
try {
// eslint-disable-next-line no-unused-vars
const res = await axios.post(`${config.backendServer}/submit`, formData, config1);
// console.log(530, 'SUCCESS!!', formData, res.status);
} catch (e) {
Expand Down
2 changes: 2 additions & 0 deletions src/store/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const state = {
schema: {},
progress: [],
responses: [],
responseMap: {}, // Stores the responses indexed by variable name
exportResponses: [],
scores: {},
participantId: '',
Expand Down Expand Up @@ -94,6 +95,7 @@ const mutations = {
// console.log(67, response.data);
const ctx = response.data['@context'];
// const ctx = _.filter(response.data['@context'], c => c.includes('contexts/generic'));
// eslint-disable-next-line no-unused-vars
axios.get(ctx).then((resp) => {
// console.log(68, resp.data);
// state.termUrl = resp.data['@context'].reproterms;
Expand Down