Skip to content

Commit

Permalink
Merge pull request #96 from concord-consortium/188115683-remove-unnec…
Browse files Browse the repository at this point in the history
…essary-placeholder

188115683 Remove Placeholder Item When Possible
  • Loading branch information
tealefristoe authored Aug 15, 2024
2 parents 55f5fa8 + 41f73dd commit 0935ad0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 21 deletions.
7 changes: 7 additions & 0 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,13 @@ export default class App extends Component {

async writeUserItems(selectedDataContext: string, personalDataKey: string) {
const items = await Codap.getItemsOfCollaborator(selectedDataContext, personalDataKey);

// Remove the first item if it has no values, i.e. the placeholder item for this user
if (items.length > 1 && Codap.isValuelessUserItem(items[0])) {
await Codap.removeItems(selectedDataContext, [items[0]]);
items.shift();
}

// write non-empty user items to firebase
database.writeUserItems(personalDataKey, items.filter(item => !Codap.isEmptyUserItem(item)));
return items;
Expand Down
38 changes: 17 additions & 21 deletions src/lib/codap-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ export class CodapHelper {
action: "get",
resource: collaboratorsResource(dataContextName, `caseSearch[${kCollaboratorKey}==${personalDataKey}]`)
});
userCaseId = userCase?.values?.[0]?.id;
userCaseId = userCase.values?.[0]?.id;
if (!userCaseId) {
// update existing items with current user information
unsharedChanges = await this.configureUnsharedCases(dataContextName, personalDataKey, personalDataLabel);
Expand Down Expand Up @@ -333,22 +333,7 @@ export class CodapHelper {
static async addNewCollaborationCollections(dataContextName: string, personalDataKey: string,
personalDataLabel: string, addEmptyDataCollection: boolean) {

const collections: Collection[] = [
{
name: "Collaborators",
title: "List of collaborators",
parent: "_root_",
labels: {
singleCase: "name",
pluralCase: "names"
},
attrs: [
{name: kShareLabelName, editable: false, renameable: false, deleteable: false},
{name: kCollaboratorKey, editable: false, renameable: false, deleteable: false, hidden: true},
editableAttributeSpec(personalDataKey)
]
}
];
const collections: Collection[] = [];

const collaboratorsCollection = await codapInterface.sendRequest({
action: "get",
Expand All @@ -366,8 +351,8 @@ export class CodapHelper {
pluralCase: "names"
},
attrs: [
{name: "Name", editable: false, renameable: false, deleteable: false},
{name: kCollaboratorKey, editable: false, renameable: false, deleteable: false, hidden: false},
{name: kShareLabelName, editable: false, renameable: false, deleteable: false},
{name: kCollaboratorKey, editable: false, renameable: false, deleteable: false, hidden: true},
editableAttributeSpec(personalDataKey)
]
});
Expand Down Expand Up @@ -638,11 +623,22 @@ export class CodapHelper {
return res.success ? res.values : [];
}

static isEmptyUserItem(item: CodapItem) {
// Returns all values of the item except from the required sharing attributes
static getItemOtherValues(item: CodapItem) {
const { [kShareLabelName]: _shareLabel, [kCollaboratorKey]: _collaborator,
[kEditableAttrName]: _editable, ...others } = item.values;
return others;
}

static isEmptyUserItem(item: CodapItem) {
const values = this.getItemOtherValues(item);
// empty if there are no values besides the required sharing attributes
return Object.keys(others).length === 0;
return Object.keys(values).length === 0;
}

static isValuelessUserItem(item: CodapItem) {
const values = this.getItemOtherValues(item);
return !Object.keys(values).some(key => !!values[key]);
}

static async moveUserItemsToLast(dataContextName: string, personalDataKey: string) {
Expand Down

0 comments on commit 0935ad0

Please sign in to comment.