Skip to content
This repository has been archived by the owner on Oct 23, 2023. It is now read-only.

Commit

Permalink
fix: ensure page property payload is not invalid
Browse files Browse the repository at this point in the history
  • Loading branch information
marionebl committed Sep 10, 2018
1 parent 5b34d1c commit be491b7
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 5 deletions.
11 changes: 11 additions & 0 deletions src/container/property-list/event-handler-property-view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,17 @@ export class EventHandlerPropertyView extends React.Component<EventHandlerProper

project.addElementAction(elementAction);
props.elementProperty.setValue(elementAction.getId());

const storePropertyId = elementAction.getStorePropertyId();
const storeProperty = storePropertyId
? userStore.getPropertyById(storePropertyId)
: undefined;

if (storeProperty && storeProperty.getType() === Types.UserStorePropertyType.Page) {
const page = props.store.getPages()[0];
elementAction.setPayload(page ? page.getId() : '');
}

props.store.commit();
}

Expand Down
4 changes: 2 additions & 2 deletions src/message/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export enum MessageType {
Undo = 'undo',
UpdatePatternLibraryRequest = 'update-pattern-library-request',
UpdatePatternLibraryResponse = 'update-pattern-library-response',
UserStoreChange = 'user-store-change',
ChangeUserStore = 'user-store-change',
SelectElement = 'select-element',
HighlightElement = 'highlight-element'
}
Expand Down Expand Up @@ -287,7 +287,7 @@ export type ChangeProject = Envelope<
>;

export type ChangeUserStore = Envelope<
MessageType.UserStoreChange,
MessageType.ChangeUserStore,
{ userStore: Types.SerializedUserStore }
>;

Expand Down
1 change: 0 additions & 1 deletion src/model/element-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ export class ElementAction {
return;
case Types.UserStoreActionType.Set: {
const storeProperty = this.userStore.getPropertyById(this.storePropertyId);

if (storeProperty) {
storeProperty.setPayload(this.payload);
}
Expand Down
3 changes: 3 additions & 0 deletions src/model/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ export class Project {
)
);

currentPageProperty.setProject(project);

return project;
}

Expand Down Expand Up @@ -163,6 +165,7 @@ export class Project {
project.addElementAction(ElementAction.from(elementAction, { userStore }));
});

userStore.getPageProperty().setProject(project);
return project;
}

Expand Down
13 changes: 13 additions & 0 deletions src/model/user-store-property/user-store-property.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as _ from 'lodash';
import * as Mobx from 'mobx';
import { Project } from '../project';
import * as Types from '../../types';

export interface UserStorePropertyInit {
Expand All @@ -19,6 +20,7 @@ export class UserStoreProperty {
@Mobx.observable private name: string;
@Mobx.observable private payload: string;
private type: UserStorePropertyType;
private project: Project;

public constructor(init: UserStorePropertyInit) {
this.id = init.id;
Expand Down Expand Up @@ -63,9 +65,20 @@ export class UserStoreProperty {

@Mobx.action
public setPayload(payload: string): void {
// Skip if we try to switch to a page that does not exist
if (this.getType() === Types.UserStorePropertyType.Page) {
if (this.project && !this.project.getPageById(payload)) {
return;
}
}

this.payload = payload;
}

public setProject(project: Project): void {
this.project = project;
}

public toJSON(): Types.SerializedUserStoreProperty {
return {
id: this.id,
Expand Down
6 changes: 5 additions & 1 deletion src/preview/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,10 @@ function main(): void {
});

store.setComponents(getComponents(store.getProject()));
break;
}
case Message.MessageType.ChangeUserStore: {
project.getUserStore().sync(message);
}
}
});
Expand Down Expand Up @@ -243,7 +247,7 @@ function main(): void {
sender.send({
id: uuid.v4(),
payload: { userStore: userStore.toJSON() },
type: Message.MessageType.UserStoreChange
type: Message.MessageType.ChangeUserStore
});
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/create-project-message-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export function createProjectMessageHandler({
app.setPane(message.payload.pane, message.payload.visible);
break;
}
case Message.MessageType.UserStoreChange: {
case Message.MessageType.ChangeUserStore: {
project.getUserStore().sync(message);
break;
}
Expand Down

0 comments on commit be491b7

Please sign in to comment.