Skip to content

Commit

Permalink
Enable collaboration if lastPersonalDataLabel exists.
Browse files Browse the repository at this point in the history
  • Loading branch information
lublagg committed Sep 4, 2024
1 parent 90aff1c commit 729ed74
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 56 deletions.
101 changes: 53 additions & 48 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { DB } from "./lib/db";
import { DBSharedTable } from "./lib/db-types";
import { DataContext, CodapItem, CodapRequest } from "./lib/types";
import { kInitialDimensions, kPluginName, kSharedDimensions, kVersion, kNewDataContextTitle,
kNewSharedTable, kShareIdLength } from "./constants";
kShareIdLength } from "./constants";
import { IState } from "./types";
import { FirstPage } from "./ui-pages/first-page";
import { JoinAndMergeTable } from "./ui-pages/join-and-merge-table";
Expand Down Expand Up @@ -85,12 +85,17 @@ export default class App extends Component {
const handleDataLabelChange = (event: ChangeEvent<HTMLInputElement>) => this.handleDataLabelChange(event);
const joinShare = () => this.joinShare();
const handleDataContextChange = (event: ChangeEvent<HTMLSelectElement>) => this.handleDataContextChange(event);
const initiateShare = () => this.initiateShare();
const initiateShare = (selectedContext?: string) => {
if (selectedContext) {
this.setState({ selectedDataContext: selectedContext });
}
this.initiateShare()
};

const availableContextOptions = availableDataContexts.map((dc: DataContext) =>
<option key={dc.name} value={dc.name}>{dc.title}</option>
);
const selectedContextOption = selectedDataContext || lastSelectedDataContext || kNewSharedTable;
const selectedContextOption = selectedDataContext || lastSelectedDataContext || availableDataContexts[0]?.name;

if (showFirstStep) {
return (
Expand All @@ -109,45 +114,45 @@ export default class App extends Component {
/>
)
} else if (joinAndMergeTable) {
return (
<JoinAndMergeTable
joinShareId={this.state.joinShareId}
personalDataLabel={this.state.personalDataLabel}
lastPersonalDataLabel={this.state.lastPersonalDataLabel}
handleJoinShareIdChange={handleJoinShareIdChange}
handleDataLabelChange={handleDataLabelChange}
handleDataContextChange={handleDataContextChange}
joinShare={joinShare}
updateState={setState}
selectedContextOption={selectedContextOption}
availableContextOptions={availableContextOptions}
/>
)
} else if (joinWithoutMerging) {
return (
<JoinWithoutMerging
joinShareId={this.state.joinShareId}
personalDataLabel={this.state.personalDataLabel}
lastPersonalDataLabel={this.state.lastPersonalDataLabel}
handleJoinShareIdChange={handleJoinShareIdChange}
handleDataLabelChange={handleDataLabelChange}
joinShare={joinShare}
updateState={setState}
/>
)
} else if (shareExistingTable) {
return (
<ShareExistingTable
selectedContextOption={selectedContextOption}
availableContextOptions={availableContextOptions}
personalDataLabel={this.state.personalDataLabel}
lastPersonalDataLabel={this.state.lastPersonalDataLabel}
handleDataContextChange={handleDataContextChange}
handleDataLabelChange={handleDataLabelChange}
initiateShare={initiateShare}
updateState={setState}
return (
<JoinAndMergeTable
joinShareId={this.state.joinShareId}
personalDataLabel={this.state.personalDataLabel}
lastPersonalDataLabel={this.state.lastPersonalDataLabel}
handleJoinShareIdChange={handleJoinShareIdChange}
handleDataLabelChange={handleDataLabelChange}
handleDataContextChange={handleDataContextChange}
joinShare={joinShare}
updateState={setState}
selectedContextOption={selectedContextOption}
availableContextOptions={availableContextOptions}
/>
)
)
} else if (joinWithoutMerging) {
return (
<JoinWithoutMerging
joinShareId={this.state.joinShareId}
personalDataLabel={this.state.personalDataLabel}
lastPersonalDataLabel={this.state.lastPersonalDataLabel}
handleJoinShareIdChange={handleJoinShareIdChange}
handleDataLabelChange={handleDataLabelChange}
joinShare={joinShare}
updateState={setState}
/>
)
} else if (shareExistingTable) {
return (
<ShareExistingTable
selectedContextOption={selectedContextOption}
availableContextOptions={availableContextOptions}
personalDataLabel={this.state.personalDataLabel}
lastPersonalDataLabel={this.state.lastPersonalDataLabel}
handleDataContextChange={handleDataContextChange}
handleDataLabelChange={handleDataLabelChange}
initiateShare={initiateShare}
updateState={setState}
/>
)
} else if (createNewTable) {
return (
<ShareNewTable
Expand Down Expand Up @@ -378,7 +383,8 @@ export default class App extends Component {

joinShare = async () => {
await this.updatePersonalDataLabelAndKey();
const {joinShareId: shareId, personalDataKey, personalDataLabel, selectedDataContext } = this.state;
const {joinShareId: shareId, personalDataKey, personalDataLabel, selectedDataContext,
joinAndMergeTable } = this.state;

this.setState({ isInProcessOfSharing: true });
try {
Expand All @@ -393,8 +399,9 @@ export default class App extends Component {
let ownDataContextName;
if (sharedContextData) {
const { dataContext: sharedDataContext, itemData } = sharedContextData;
const existingDataContext = selectedDataContext && (selectedDataContext !== kNewSharedTable) &&
await Codap.getDataContext(selectedDataContext);

const existingDataContext = joinAndMergeTable && selectedDataContext &&
await Codap.getDataContext(selectedDataContext);

if (!existingDataContext) {
const newDataContext = sharedDataContext &&
Expand All @@ -406,8 +413,7 @@ export default class App extends Component {
} else {
throw new Error("failed to create data context");
}
}
else {
} else {
ownDataContextName = selectedDataContext;
await Codap.addNewCollaborationCollections(selectedDataContext, personalDataKey, personalDataLabel, false);
await Codap.syncDataContexts(selectedDataContext, sharedDataContext, true);
Expand All @@ -424,8 +430,7 @@ export default class App extends Component {
if (!itemData?.[personalDataKey]) {
Codap.configureUserCase(ownDataContextName, personalDataKey, personalDataLabel, true);
}
}
else {
} else {
Codap.moveUserItemsToLast(selectedDataContext, personalDataKey);
this.writeUserItems(selectedDataContext, personalDataKey);
}
Expand Down
2 changes: 1 addition & 1 deletion src/ui-pages/join-and-merge-table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export const JoinAndMergeTable = (props: JoinAndMergeTableProps) => {
{BACK}
</button>
<button
disabled={!selectedContextOption || !personalDataLabel}
disabled={!selectedContextOption || !joinShareId || (!personalDataLabel && !lastPersonalDataLabel)}
onClick={joinShare}>{BEGIN_COLLABORATION}
</button>
</div>
Expand Down
6 changes: 5 additions & 1 deletion src/ui-pages/join-without-merging.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ export const JoinWithoutMerging = (props: JoinWithoutMergingProps) => {
onClick={() => updateState({ joinWithoutMerging: false })}>
{BACK}
</button>
<button disabled={!joinShareId || !personalDataLabel} onClick={joinShare}>{BEGIN_COLLABORATION}</button>
<button
disabled={!joinShareId || (!personalDataLabel && !lastPersonalDataLabel)}
onClick={joinShare}>
{BEGIN_COLLABORATION}
</button>
</div>
</div>
)
Expand Down
8 changes: 4 additions & 4 deletions src/ui-pages/share-existing-table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ interface ShareExistingTableProps {
lastPersonalDataLabel: string;
handleDataContextChange: (event: React.ChangeEvent<HTMLSelectElement>) => void;
handleDataLabelChange: (event: React.ChangeEvent<HTMLInputElement>) => void;
initiateShare: () => void;
initiateShare: (selectedContextOption?: string) => void;
updateState: (state: Partial<IState>) => void;
}

export const ShareExistingTable = (props: ShareExistingTableProps) => {
const { selectedContextOption, availableContextOptions, personalDataLabel,
lastPersonalDataLabel, handleDataContextChange, handleDataLabelChange,
initiateShare, updateState } = props;
return (
return (
<div className="form-container">
<div className="select-stack">
<div>{SELECT_TABLE_TO_SHARE}</div>
Expand All @@ -37,8 +37,8 @@ export const ShareExistingTable = (props: ShareExistingTableProps) => {
{BACK}
</button>
<button
disabled={!selectedContextOption || !personalDataLabel}
onClick={initiateShare}>{BEGIN_COLLABORATION}
disabled={!selectedContextOption || (!personalDataLabel && !lastPersonalDataLabel)}
onClick={() => initiateShare(selectedContextOption)}>{BEGIN_COLLABORATION}
</button>
</div>
</div>
Expand Down
9 changes: 7 additions & 2 deletions src/ui-pages/share-new-table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ interface ShareNewTableProps {
personalDataLabel: string;
lastPersonalDataLabel: string;
handleDataLabelChange: (event: React.ChangeEvent<HTMLInputElement>) => void;
initiateShare: () => void;
initiateShare: (selectedContextOption?: string) => void;
}

export const ShareNewTable = (props: ShareNewTableProps) => {
Expand All @@ -34,7 +34,12 @@ export const ShareNewTable = (props: ShareNewTableProps) => {
onClick={() => updateState({ createNewTable: false })}>
{BACK}
</button>
<button disabled={!personalDataLabel || !newTableName} onClick={initiateShare}>{BEGIN_COLLABORATION}</button>
<button
disabled={(!personalDataLabel && !lastPersonalDataLabel) || !newTableName}
onClick={() => initiateShare()}
>
{BEGIN_COLLABORATION}
</button>
</div>
</div>
)
Expand Down

0 comments on commit 729ed74

Please sign in to comment.