Skip to content

Commit

Permalink
Return special attributes to normal when unsharing a table.
Browse files Browse the repository at this point in the history
  • Loading branch information
tealefristoe committed Sep 19, 2024
1 parent b09d7df commit 7f6cc40
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 19 deletions.
6 changes: 3 additions & 3 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ export default class App extends Component {
const shareId = randomize("a0", kShareIdLength, { exclude: "0oOiIlL1" });
this.setState({shareId});
database.createSharedTable(shareId, personalDataKey);
Codap.configureForSharing(dataContextName, this.state.id, true);
Codap.configureForSharing(dataContextName, this.state.id, this.state.personalDataKey, true);

const updatedNewContext = await Codap.getDataContext(dataContextName);
await this.writeDataContext(updatedNewContext);
Expand Down Expand Up @@ -422,7 +422,7 @@ export default class App extends Component {

await this.writeDataContext(selectedDataContext);
}
Codap.configureForSharing(ownDataContextName, this.state.id, true);
Codap.configureForSharing(ownDataContextName, this.state.id, personalDataKey, true);

// listeners must be added after data context is configured
database.installUserItemListeners();
Expand Down Expand Up @@ -453,7 +453,7 @@ export default class App extends Component {
};

leaveShare = () => {
Codap.configureForSharing(this.state.selectedDataContext, this.state.id, false);
Codap.configureForSharing(this.state.selectedDataContext, this.state.id, this.state.personalDataKey, false);
this.setState({
shareId: null,
personalDataLabel: "",
Expand Down
68 changes: 52 additions & 16 deletions src/lib/codap-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as randomize from "randomatic";
import codapInterface, { IConfig } from "./CodapInterface";
import {
Attribute, Collection, DataContext, DataContextCreation, CodapItem, CodapItemValue, CodapRequest,
CodapRequestResponse, CodapItemValues, CodapRequestHandler
CodapRequestResponse, CodapItemValues, CodapRequestHandler, CodapRequests
} from "./types";

export interface ISaveState {
Expand Down Expand Up @@ -31,17 +31,52 @@ const kShareLabelName = "shareName";
const kCollaboratorKey = "__collaborator__";
const kEditableAttrName = "__editable__";

function editableAttributeSpec(personalDataKey: string) {
function shareAttributeSpec(editable = false) {
return {
name: kShareLabelName,
editable,
renameable: editable,
deleteable: editable
};
}

function collaboratorAttributeSpec(editable = false) {
return {
name: kCollaboratorKey,
editable,
renameable: editable,
deleteable: editable,
hidden: true
};
}

function editableAttributeSpec(personalDataKey: string, editable = false) {
return {
name: kEditableAttrName,
formula: `${kCollaboratorKey}="${personalDataKey}"`,
editable: false,
renameable: false,
deleteable: false,
editable,
renameable: editable,
deleteable: editable,
hidden: true
};
}

function updateAttributeCommands(dataContextName: string, personalDataKey: string, editable = false) {
return [{
action: "update",
resource: attributeResource(dataContextName, "Collaborators", kCollaboratorKey),
values: collaboratorAttributeSpec(editable)
}, {
action: "update",
resource: attributeResource(dataContextName, "Collaborators", kShareLabelName),
values: shareAttributeSpec(editable),
}, {
action: "update",
resource: attributeResource(dataContextName, "Collaborators", kEditableAttrName),
values: editableAttributeSpec(personalDataKey, editable)
}];
}

export class CodapHelper {

static async initializePlugin(pluginName: string, version: string,
Expand Down Expand Up @@ -352,19 +387,15 @@ export class CodapHelper {
pluralCase: "names"
},
attrs: [
{name: kShareLabelName, editable: false, renameable: false, deleteable: false},
{name: kCollaboratorKey, editable: false, renameable: false, deleteable: false, hidden: true},
shareAttributeSpec(),
collaboratorAttributeSpec(),
editableAttributeSpec(personalDataKey)
]
});
}
else {
// if we already have the Collaborators collection, update the __editable__ attribute
await codapInterface.sendRequest({
action: "update",
resource: attributeResource(dataContextName, "Collaborators", kEditableAttrName),
values: editableAttributeSpec(personalDataKey)
});
// if we already have the Collaborators collection, update the collaborator, share, and __editable__ attributes
await codapInterface.sendRequest(updateAttributeCommands(dataContextName, personalDataKey));
}

if (addEmptyDataCollection) {
Expand Down Expand Up @@ -565,8 +596,10 @@ export class CodapHelper {
});
}

static configureForSharing(dataContextName: string, controllerId: string, isSharing: boolean) {
codapInterface.sendRequest([
static configureForSharing(
dataContextName: string, controllerId: string, personalDataKey: string, isSharing: boolean
) {
const commands: CodapRequests = [
{
action: "update",
resource: dataContextResource(dataContextName),
Expand All @@ -583,7 +616,10 @@ export class CodapHelper {
respectEditableItemAttribute: isSharing
}
}
]);
];
// If we're unsharing, make the special attributes editable
if (!isSharing) commands.push(...updateAttributeCommands(dataContextName, personalDataKey, true));
codapInterface.sendRequest(commands);
}

static async getDataContext(dataContextName: string): Promise<DataContext | null> {
Expand Down

0 comments on commit 7f6cc40

Please sign in to comment.