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

Templates for Relay frontend #43

Closed
wants to merge 18 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
109 changes: 109 additions & 0 deletions packages/generator/src/frontend/add/templates/Add.js.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import React, { Component, PropTypes } from 'react';
import Relay from 'react-relay';
import RelayStore from '../../RelayStore';
import { withRouter } from 'react-router';

import <%= name %>AddMutation from './<%= name %>AddMutation';

import Form from '../common/Form';

class <%= name %>Add extends Component {
static contextTypes = {
showSnackbar: PropTypes.func,
};

fields = [
{
name: 'id',
placeholder: 'ID',
required: true,
},
// TODO - add ObjectType fields here
];

onSubmit = (data) => {
const mutation = new <%= name %>AddMutation({
viewer: this.props.viewer,
...data,
});

RelayStore.commitUpdate(mutation, {
onSuccess: ({ <%= name %>Add }) => {
this.context.showSnackbar({
message: '<%= rawName %> created successfully!',
});

this.props.router.push(`/<%= pluralName %>/view/${<%= name %>Add.<%= rawName %>Edge.node.id}`);
},
onFailure: (failureResponse) => {
this.context.showSnackbar({
message: 'There was an error while trying to create a <%= rawName %>.',
});

console.log('FAIL', failureResponse);
},
});
};

render() {
return (
<div>
<h1 style={styles.title}>
New <%= name %>
</h1>
<Form
fields={this.fields}
onSubmit={this.onSubmit}
/>
</div>
);
}
}

const styles = {
form: {
backgroundColor: 'white',
boxShadow: 'rgba(0, 0, 0, 0.056863) 0px 7px 8px, rgba(0, 0, 0, 0.227451) 0px 0px 0px',
borderWidth: 1,
borderStyle: 'solid',
borderColor: '#E7ECEA',
padding: 20,
paddingTop: 50,
},
formContainer: {
display: 'flex',
flexWrap: 'wrap',
},
title: {
fontSize: 25,
fontWeight: 300,
},
actionsContainer: {
display: 'flex',
justifyContent: 'flex-end',
marginTop: 5,
paddingRight: 8,
borderTopStyle: 'solid',
borderTopWidth: 1,
paddingTop: 15,
borderColor: '#ECECEC',
},
formField: {
marginRight: 10,
flex: '1 0 47%',
},
selectField: {
marginRight: 10,
flex: '1 0 48%',
},
};

export default Relay.createContainer(withRouter(<%= name %>Add), {
fragments: {
viewer: () => Relay.QL`
fragment on Viewer {
${<%= name %>AddMutation.getFragment('viewer')}
}
`,
},
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import Relay from 'react-relay';

export default class <%= name %>AddMutation extends Relay.Mutation {
static fragments = {
viewer: () => Relay.QL`
fragment on Viewer {
id
}
`,
};

getMutation() {
return Relay.QL`mutation {
<%= name %>Add
}`;
}

getVariables() {
const {
id
// TODO - add mutation input fields here
} = this.props;

return {
id
// TODO - add mutation input fields here
};
}

getFatQuery() {
return Relay.QL`
fragment on <%= name %>AddPayload {
<%= rawName %>Edge
viewer {
<%= pluralName %>
}
}
`;
}

getConfigs() {
return [
{
type: 'RANGE_ADD',
parentName: 'viewer',
parentID: this.props.viewer.id,
connectionName: '<%= pluralName %>',
edgeName: '<%= rawName %>Edge',
rangeBehaviors: {
'': 'prepend',
},
},
{
type: 'REQUIRED_CHILDREN',
children: [Relay.QL`
fragment on <%= name %>AddPayload {
<%= rawName %>Edge
}
`],
},
];
}
}
106 changes: 106 additions & 0 deletions packages/generator/src/frontend/edit/templates/Edit.js.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import React, { Component, PropTypes } from 'react';
import Relay from 'react-relay';
import RelayStore from '../../../RelayStore';
import { withRouter } from 'react-router';

import <%= name %>EditMutation from './<%= name %>EditMutation.js';

import Form from '../../common/Form';

class <%= name %>Edit extends Component {
static contextTypes = {
showSnackbar: PropTypes.func,
};

fields = [
{
name: 'id',
placeholder: 'ID',
required: true,
},
// TODO - add ObjectType fields here
];

onSubmit = (data) => {
const { company } = this.props;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be const { <%= camelCasedName %> } = this.props;.


const mutation = new <%= rawName %>EditMutation({
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be name instead of rawName.

...data,
});

RelayStore.commitUpdate(mutation, {
onSuccess: () => {
this.context.showSnackbar({
message: '<%= name %> edited successfully!',
});

this.props.router.goBack();
},
onFailure: (failureResponse) => {
this.context.showSnackbar({
message: 'There was an error while trying to edit this <%= rawName %>.',
});

console.log('FAIL', failureResponse);
},
});
};

render() {
const { <%= rawName %> } = this.props;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<%= camelCasedName %> instead of rawName.


return (
<Form
fields={this.fields}
onSubmit={this.onSubmit}
value={<%= rawName %>}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<%= camelCasedName %> instead of rawName.

/>
);
}
}

const styles = {
formContainer: {
display: 'flex',
flexWrap: 'wrap',
paddingTop: 30,
paddingLeft: 10,
},
actionsContainer: {
display: 'flex',
justifyContent: 'flex-end',
marginTop: 5,
paddingRight: 8,
borderTopStyle: 'solid',
borderTopWidth: 1,
paddingTop: 15,
borderColor: '#ECECEC',
},
formField: {
marginRight: 10,
flex: '1 0 47%',
},
selectField: {
marginRight: 10,
flex: '1 0 48%',
},
};

export default Relay.createContainer(withRouter(<%= name %>Edit), {
initialVariables: {
id: null,
},
fragments: {
<%= rawName %>: () => Relay.QL`
fragment on <%= name %> {
id
${<%= name %>EditMutation.getFragment('<%= rawName %>')}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<%= camelCasedName %> instead of rawName.

}
`,
viewer: () => Relay.QL`
fragment on Viewer {
id
}
`,
},
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import Relay from 'react-relay';

export default class <%= name %>EditMutation extends Relay.Mutation {
static fragments = {
<%= rawName %>: () => Relay.QL`
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<%= camelCasedName %> instead of rawName.

fragment on <%= name %> {
id
}
`,
};

getMutation() {
return Relay.QL`mutation {
<%= name %>Edit
}`;
}

getVariables() {
const {
<%= rawName %>: {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<%= camelCasedName %> instead of rawName.

id,
},
// Todo add more mutation input fields here
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use // TODO: instead of // Todo.

} = this.props;

return {
id,
// Todo add more mutation input fields here
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use // TODO: instead of // Todo.

};
}

getFatQuery() {
return Relay.QL`
fragment on FlightEditPayload {
<%= rawName %>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<%= camelCasedName %> instead of rawName.

}
`;
}

getConfigs() {
return [{
type: 'FIELDS_CHANGE',
fieldIDs: {
<%= rawName %>: this.props.<%= rawName %>.id,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<%= camelCasedName %> instead of rawName.

},
}];
}
}
62 changes: 62 additions & 0 deletions packages/generator/src/frontend/list/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import Generator from 'yeoman-generator';
import pluralize from 'pluralize';
import {
getMongooseModelSchema,
getConfigDir,
getRelativeConfigDir,
camelCaseText,
uppercaseFirstLetter,
} from '../../utils';

class ListGenerator extends Generator {
constructor(args, options) {
super(args, options);

this.argument('name', {
type: String,
required: true,
});

// TODO read schema.json

this.destinationDir = getConfigDir('list');
}

_getConfigDirectories() {
return getRelativeConfigDir('loader', ['model', 'connection']);
}

generateList() {
// const schema = this.options.model ?
// getMongooseModelSchema(this.options.model, true)
// : null;

const name = uppercaseFirstLetter(this.options.name);

const templatePath = this.templatePath('List.js.template');

// const templatePath = schema ?
// this.templatePath('LoaderWithSchema.js.template')
// : this.templatePath('Loader.js.template');
//
// const directories = this._getConfigDirectories();

const pluralName = pluralize(this.options.name);

const destinationPath = this.destinationPath(`${this.destinationDir}/${name}List.js`);
const templateVars = {
name,
rawName: this.options.name,
pluralName,
pluralCamelCaseName: camelCaseText(pluralName),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a camelCaseName without plural here to be used on the fields.

};

this.fs.copyTpl(templatePath, destinationPath, templateVars);
}

end() {
this.log('🔥 List created!');
}
}

module.exports = ListGenerator;
Loading