├── .github └── PULL_REQUEST_TEMPLATE.md ├── .gitignore ├── LICENSE ├── NOTICE ├── README.md ├── media ├── AllEvents.png └── CreateEvent.png ├── package.json ├── public ├── favicon.ico ├── index.html └── manifest.json └── src ├── App.css ├── App.js ├── App.test.js ├── Components ├── AllEvents.js ├── DateTimePickerCustomInput.js ├── EventComments.js ├── NewComment.js ├── NewEvent.js └── ViewEvent.js ├── GraphQL ├── MutationCommentOnEvent.js ├── MutationCreateEvent.js ├── MutationDeleteEvent.js ├── QueryAllEvents.js ├── QueryGetEvent.js └── SubsriptionEventComments.js ├── Utils.js ├── aws-exports.js ├── index.css ├── index.js └── registerServiceWorker.js /.github/PULL_REQUEST_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | *Issue #, if available:* 2 | 3 | *Description of changes:* 4 | 5 | 6 | By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license. 7 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/ignore-files/ for more about ignoring files. 2 | 3 | # dependencies 4 | /node_modules 5 | 6 | # testing 7 | /coverage 8 | 9 | # production 10 | /build 11 | 12 | # misc 13 | .DS_Store 14 | .env.local 15 | .env.development.local 16 | .env.test.local 17 | .env.production.local 18 | 19 | npm-debug.log* 20 | yarn-debug.log* 21 | yarn-error.log* 22 | 23 | # AppSync 24 | AppSync.js 25 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright [yyyy] [name of copyright owner] 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | -------------------------------------------------------------------------------- /NOTICE: -------------------------------------------------------------------------------- 1 | AWS Mobile Appsync Events Starter React 2 | Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved. 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Introduction 2 | 3 | This is a Starter React application for using the Sample app in the AWS AppSync console when building your GraphQL API. The Sample app creates a GraphQL schema and provisions Amazon DynamoDB resources, then connects them appropriately with Resolvers. The application demonstrates GraphQL Mutations, Queries and Subscriptions using AWS AppSync. You can use this for learning purposes or adapt either the application or the GraphQL Schema to meet your needs. 4 | 5 | ![EventDetails](media/AllEvents.png) 6 | ![EventDetails](media/CreateEvent.png) 7 | 8 | ## Features 9 | 10 | - GraphQL Mutations 11 | - Create new events 12 | - Create comments on existing events 13 | 14 | - GraphQL Queries 15 | - Get all events 16 | - Get an event by Id 17 | 18 | - GraphQL Subscriptions 19 | - Real time updates for comments on an event 20 | 21 | - Authorization 22 | - The app uses API Key as the authorization mechanism 23 | 24 | ## AWS Setup 25 | 26 | 1. Navigate to the AWS AppSync console using the URL: http://console.aws.amazon.com/appsync/home 27 | 28 | 2. Click on `Create API` and select the `Event App` under the `sample project` in the bottom pane, and select `Start`. Enter a API name of your choice. Click `Create`. 29 | 30 | 31 | ## React Setup 32 | 33 | First, clone this repo: 34 | 35 | ``` 36 | git clone https://github.com/aws-samples/aws-mobile-appsync-events-starter-react.git 37 | cd ./aws-mobile-appsync-events-starter-react 38 | ``` 39 | 40 | Wait until the progress bar at the top has completed deploying your resources. Then from the integration page of your GraphQL API (you can click the name you entered in the left hand navigation). 41 | 42 | On this same page, select `JavaScript` at the bottom to download your `aws-exports.js` configuration file by clicking the **Download Config** button. Replace the `aws-exports.js` file in the root of your app with the file you just downloaded. 43 | 44 | Install dependencies 45 | ``` 46 | npm install 47 | ``` 48 | 49 | Start the application: 50 | ``` 51 | npm run start 52 | ``` 53 | 54 | ## Application Walkthrough 55 | 56 | ### ./src/App.js 57 | 58 | - Sets up the application navigation between screens using `BrowserRouter` from React Router. 59 | - Configures the `AWSAppSyncClient` using an API Key. This can be confugured to use Amazon Cognito Identity or Amazon Cognito User Pools as well. 60 | 61 | 62 | ### ./Components/AllEvents.js 63 | 64 | - Uses Higher Order Components for making GraphQL calls to AWS AppSync. 65 | - View to display all the events from `./GraphQL/QueryAllEvents.js` 66 | - Allows you to delete individual events. This will use `./GraphQL/MutationDeleteEvent.js` 67 | 68 | ### ./Components/NewEvent.js 69 | 70 | - Uses Higher Order Components for making GraphQL calls to AWS AppSync. 71 | - View to create a new event using `./GraphQL/MutationCreateEvent.js` 72 | 73 | ### ./Components/ViewEvent.js and EventComment.js 74 | 75 | - Uses Higher Order Components for making GraphQL calls to AWS AppSync. 76 | - `ViewEvent` gets all the comments for a specific event when page loads with a GraphQL query defined in `./GraphQL/QueryGetEvent` 77 | - Once the page loads, `EventComments` sets up a GraphQL subscription using `./GraphQL/SubscriptionEventComments` to display any new comments on an event in realtime. 78 | 79 | ### ./GraphQL Directory 80 | 81 | - Contains GraphQL queries and mutations for interacting with AWS AppSync. 82 | 83 | -------------------------------------------------------------------------------- /media/AllEvents.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/amazon-archives/aws-mobile-appsync-events-starter-react/994dd6269d67723bad1923ee35789c211244c1b3/media/AllEvents.png -------------------------------------------------------------------------------- /media/CreateEvent.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/amazon-archives/aws-mobile-appsync-events-starter-react/994dd6269d67723bad1923ee35789c211244c1b3/media/CreateEvent.png -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "aws-mobile-appsync-events-starter-react", 3 | "version": "0.1.0", 4 | "private": true, 5 | "dependencies": { 6 | "aws-appsync": "^3.0.1", 7 | "aws-appsync-react": "^3.0.1", 8 | "graphql": "^0.11.7", 9 | "graphql-tag": "^2.5.0", 10 | "moment": "^2.19.2", 11 | "react": "^16.1.1", 12 | "react-apollo": "^2.0.1", 13 | "react-datepicker": "^0.61.0", 14 | "react-dom": "^16.4.2", 15 | "react-router-dom": "^4.2.2", 16 | "react-scripts": "^3.4.0", 17 | "semantic-ui-css": "^2.2.12", 18 | "uuid": "^3.1.0" 19 | }, 20 | "scripts": { 21 | "start": "react-scripts start", 22 | "build": "react-scripts build", 23 | "test": "react-scripts test --env=jsdom", 24 | "eject": "react-scripts eject" 25 | }, 26 | "browserslist": { 27 | "production": [ 28 | ">0.2%", 29 | "not dead", 30 | "not op_mini all" 31 | ], 32 | "development": [ 33 | "last 1 chrome version", 34 | "last 1 firefox version", 35 | "last 1 safari version" 36 | ] 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/amazon-archives/aws-mobile-appsync-events-starter-react/994dd6269d67723bad1923ee35789c211244c1b3/public/favicon.ico -------------------------------------------------------------------------------- /public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 11 | 12 | 13 | 22 | React App 23 | 24 | 25 | 28 |
29 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /public/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "short_name": "React App", 3 | "name": "Create React App Sample", 4 | "icons": [ 5 | { 6 | "src": "favicon.ico", 7 | "sizes": "64x64 32x32 24x24 16x16", 8 | "type": "image/x-icon" 9 | } 10 | ], 11 | "start_url": "./index.html", 12 | "display": "standalone", 13 | "theme_color": "#000000", 14 | "background_color": "#ffffff" 15 | } 16 | -------------------------------------------------------------------------------- /src/App.css: -------------------------------------------------------------------------------- 1 | .new-event.content { 2 | align-items: center; 3 | justify-content: center; 4 | display: flex; 5 | flex-direction: column; 6 | } 7 | .react-datepicker__time-container .react-datepicker__time .react-datepicker__time-box ul.react-datepicker__time-list { 8 | padding-left: 0; 9 | padding-right: 0; 10 | } 11 | 12 | .react-datepicker__time-container .react-datepicker__time .react-datepicker__time-box { 13 | width: 80px; 14 | } 15 | -------------------------------------------------------------------------------- /src/App.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { BrowserRouter as Router, Route } from 'react-router-dom'; 3 | import "semantic-ui-css/semantic.min.css"; 4 | import 'react-datepicker/dist/react-datepicker.css'; 5 | 6 | import appSyncConfig from "./aws-exports"; 7 | import { ApolloProvider } from "react-apollo"; 8 | import AWSAppSyncClient, { defaultDataIdFromObject } from "aws-appsync"; 9 | import { Rehydrated } from "aws-appsync-react"; 10 | 11 | import './App.css'; 12 | import AllEvents from './Components/AllEvents'; 13 | import NewEvent from './Components/NewEvent'; 14 | import ViewEvent from './Components/ViewEvent'; 15 | 16 | const Home = () => ( 17 |
18 | 19 |
20 | ); 21 | 22 | const App = () => ( 23 | 24 |
25 | 26 | 27 | 28 |
29 |
30 | ); 31 | 32 | const client = new AWSAppSyncClient({ 33 | url: appSyncConfig.aws_appsync_graphqlEndpoint, 34 | region: appSyncConfig.aws_appsync_region, 35 | auth: { 36 | type: appSyncConfig.aws_appsync_authenticationType, 37 | apiKey: appSyncConfig.aws_appsync_apiKey, 38 | }, 39 | cacheOptions: { 40 | dataIdFromObject: (obj) => { 41 | let id = defaultDataIdFromObject(obj); 42 | 43 | if (!id) { 44 | const { __typename: typename } = obj; 45 | switch (typename) { 46 | case 'Comment': 47 | return `${typename}:${obj.commentId}`; 48 | default: 49 | return id; 50 | } 51 | } 52 | 53 | return id; 54 | } 55 | } 56 | }); 57 | 58 | const WithProvider = () => ( 59 | 60 | 61 | 62 | 63 | 64 | ); 65 | 66 | export default WithProvider; 67 | -------------------------------------------------------------------------------- /src/App.test.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ReactDOM from 'react-dom'; 3 | import App from './App'; 4 | 5 | it('renders without crashing', () => { 6 | const div = document.createElement('div'); 7 | ReactDOM.render(, div); 8 | }); 9 | -------------------------------------------------------------------------------- /src/Components/AllEvents.js: -------------------------------------------------------------------------------- 1 | import React, { Component } from "react"; 2 | import { Link } from "react-router-dom"; 3 | 4 | import { graphql, compose, withApollo } from "react-apollo"; 5 | import QueryAllEvents from "../GraphQL/QueryAllEvents"; 6 | import MutationDeleteEvent from "../GraphQL/MutationDeleteEvent"; 7 | 8 | import moment from "moment"; 9 | 10 | class AllEvents extends Component { 11 | 12 | state = { 13 | busy: false, 14 | } 15 | 16 | static defaultProps = { 17 | events: [], 18 | deleteEvent: () => null, 19 | } 20 | 21 | async handleDeleteClick(event, e) { 22 | e.preventDefault(); 23 | 24 | if (window.confirm(`Are you sure you want to delete event ${event.id}`)) { 25 | const { deleteEvent } = this.props; 26 | 27 | await deleteEvent(event); 28 | } 29 | } 30 | 31 | handleSync = async () => { 32 | const { client } = this.props; 33 | const query = QueryAllEvents; 34 | 35 | this.setState({ busy: true }); 36 | 37 | await client.query({ 38 | query, 39 | fetchPolicy: 'network-only', 40 | }); 41 | 42 | this.setState({ busy: false }); 43 | } 44 | 45 | renderEvent = (event) => ( 46 | 47 |
48 |
{event.name}
49 |
50 |
51 |

{moment(event.when).format('LL')}

52 |

{moment(event.when).format('LT')}

53 |

{event.where}

54 |
55 |
56 |
{event.description}
57 |
58 |
59 | {event.comments.items.length} comments 60 |
61 | 65 | 66 | ); 67 | 68 | render() { 69 | const { busy } = this.state; 70 | const { events } = this.props; 71 | 72 | return ( 73 |
74 |
75 |

All Events

76 | 80 |
81 |
82 |
83 | 84 | 85 |

Create new event

86 | 87 |
88 | {[].concat(events).sort((a, b) => a.when.localeCompare(b.when)).map(this.renderEvent)} 89 |
90 |
91 | ); 92 | } 93 | 94 | } 95 | 96 | export default withApollo(compose( 97 | graphql( 98 | QueryAllEvents, 99 | { 100 | options: { 101 | fetchPolicy: 'cache-first', 102 | }, 103 | props: ({ data: { listEvents = { items: [] } } }) => ({ 104 | events: listEvents.items 105 | }) 106 | } 107 | ), 108 | graphql( 109 | MutationDeleteEvent, 110 | { 111 | options: { 112 | update: (proxy, { data: { deleteEvent } }) => { 113 | const query = QueryAllEvents; 114 | const data = proxy.readQuery({ query }); 115 | 116 | data.listEvents.items = data.listEvents.items.filter(event => event.id !== deleteEvent.id); 117 | 118 | proxy.writeQuery({ query, data }); 119 | } 120 | }, 121 | props: (props) => ({ 122 | deleteEvent: (event) => { 123 | return props.mutate({ 124 | variables: { id: event.id }, 125 | optimisticResponse: () => ({ 126 | deleteEvent: { 127 | ...event, __typename: 'Event', comments: { __typename: 'CommentConnection', items: [] } 128 | } 129 | }), 130 | }); 131 | } 132 | }) 133 | } 134 | ) 135 | )(AllEvents)); 136 | -------------------------------------------------------------------------------- /src/Components/DateTimePickerCustomInput.js: -------------------------------------------------------------------------------- 1 | import React, { Component } from "react"; 2 | import PropTypes from "prop-types"; 3 | 4 | export default class DateTimePickerCustomInput extends Component { 5 | 6 | render() { 7 | return ( 8 | 12 | ) 13 | } 14 | } 15 | 16 | DateTimePickerCustomInput.propTypes = { 17 | onClick: PropTypes.func, 18 | value: PropTypes.string, 19 | id: PropTypes.string, 20 | }; 21 | -------------------------------------------------------------------------------- /src/Components/EventComments.js: -------------------------------------------------------------------------------- 1 | import React, { Component } from "react"; 2 | import { graphql } from "react-apollo"; 3 | 4 | import moment from 'moment'; 5 | 6 | import QueryGetEvent from "../GraphQL/QueryGetEvent"; 7 | import SubsriptionEventComments from "../GraphQL/SubsriptionEventComments"; 8 | 9 | import NewComment from "./NewComment"; 10 | 11 | class EventComments extends Component { 12 | 13 | subscription; 14 | 15 | componentDidMount() { 16 | this.subscription = this.props.subscribeToComments(); 17 | } 18 | 19 | componentWillUnmount() { 20 | this.subscription(); 21 | } 22 | 23 | renderComment = (comment) => { 24 | return ( 25 |
26 |
27 |
28 |
29 | {comment.content} 30 |
31 |
{moment(comment.createdAt).format('LL, LT')}
32 |
33 |
34 | ); 35 | } 36 | 37 | render() { 38 | const { comments: { items }, eventId } = this.props; 39 | 40 | return ( 41 |
42 |
43 |
44 |

Comments

45 | {[].concat(items).sort((a, b) => a.createdAt.localeCompare(b.createdAt)).map(this.renderComment)} 46 | 47 |
48 |
49 |
50 | ); 51 | } 52 | 53 | } 54 | 55 | const EventCommentsWithData = graphql( 56 | QueryGetEvent, 57 | { 58 | options: ({ eventId: id }) => ({ 59 | fetchPolicy: 'cache-first', 60 | variables: { id } 61 | }), 62 | props: props => ({ 63 | comments: props.data.getEvent ? props.data.getEvent.comments : { items: [] }, 64 | subscribeToComments: () => props.data.subscribeToMore({ 65 | document: SubsriptionEventComments, 66 | variables: { 67 | eventId: props.ownProps.eventId, 68 | }, 69 | updateQuery: (prev, { subscriptionData: { data: { subscribeToEventComments } } }) => { 70 | const res = { 71 | ...prev, 72 | getEvent: { 73 | ...prev.getEvent, 74 | comments: { 75 | __typename: 'CommentConnections', 76 | ...prev.getEvent.comments, 77 | items: [ 78 | ...prev.getEvent.comments.items.filter(c => c.commentId !== subscribeToEventComments.commentId), 79 | subscribeToEventComments, 80 | ] 81 | } 82 | } 83 | }; 84 | 85 | return res; 86 | } 87 | }) 88 | }), 89 | }, 90 | )(EventComments); 91 | 92 | export default EventCommentsWithData; -------------------------------------------------------------------------------- /src/Components/NewComment.js: -------------------------------------------------------------------------------- 1 | import React, { Component } from "react"; 2 | import { graphql } from "react-apollo"; 3 | import { v4 as uuid } from "uuid"; 4 | 5 | import MutationCommentOnEvent from "../GraphQL/MutationCommentOnEvent"; 6 | import QueryGetEvent from "../GraphQL/QueryGetEvent"; 7 | import moment from "moment"; 8 | 9 | class NewComment extends Component { 10 | 11 | static defaultProps = { 12 | createComment: () => null, 13 | } 14 | 15 | static defaultState = { 16 | comment: { 17 | content: '', 18 | }, 19 | loading: false, 20 | }; 21 | 22 | state = NewComment.defaultState; 23 | 24 | handleSubmit = async (e) => { 25 | e.stopPropagation(); 26 | e.preventDefault(); 27 | const { comment } = this.state; 28 | const { eventId, createComment } = this.props; 29 | 30 | this.setState({ loading: true }); 31 | 32 | await createComment({ 33 | ...comment, 34 | eventId, 35 | createdAt: moment.utc().format(), 36 | }); 37 | 38 | this.setState(NewComment.defaultState); 39 | } 40 | 41 | handleChange = ({ target: { value: content } }) => { 42 | this.setState({ comment: { content } }); 43 | } 44 | 45 | render() { 46 | const { comment, loading } = this.state; 47 | return ( 48 |
49 |
50 | 51 |
52 | 57 |
58 | ); 59 | } 60 | } 61 | 62 | const NewCommentWithData = graphql( 63 | MutationCommentOnEvent, 64 | { 65 | options: props => ({ 66 | update: (proxy, { data: { commentOnEvent } }) => { 67 | const query = QueryGetEvent; 68 | const variables = { id: commentOnEvent.eventId }; 69 | const data = proxy.readQuery({ query, variables }); 70 | 71 | data.getEvent = { 72 | ...data.getEvent, 73 | comments: { 74 | ...data.getEvent.comments, 75 | items: [ 76 | ...data.getEvent.comments.items.filter(c => c.commentId !== commentOnEvent.commentId), 77 | commentOnEvent, 78 | ] 79 | } 80 | }; 81 | 82 | proxy.writeQuery({ query, data }); 83 | }, 84 | }), 85 | props: props => ({ 86 | createComment: (comment) => { 87 | return props.mutate({ 88 | variables: { ...comment }, 89 | optimisticResponse: { commentOnEvent: { ...comment, __typename: 'Comment', commentId: uuid() } }, 90 | }); 91 | } 92 | }) 93 | } 94 | )(NewComment); 95 | 96 | export default NewCommentWithData; -------------------------------------------------------------------------------- /src/Components/NewEvent.js: -------------------------------------------------------------------------------- 1 | import React, { Component } from "react"; 2 | import { Link } from "react-router-dom"; 3 | 4 | import { v4 as uuid } from "uuid"; 5 | import { graphql } from "react-apollo"; 6 | import QueryAllEvents from "../GraphQL/QueryAllEvents"; 7 | import QueryGetEvent from "../GraphQL/QueryGetEvent"; 8 | import MutationCreateEvent from "../GraphQL/MutationCreateEvent"; 9 | 10 | import DatePicker from 'react-datepicker'; 11 | import moment from 'moment'; 12 | 13 | import { nearest15min } from "../Utils"; 14 | import DateTimePickerCustomInput from "./DateTimePickerCustomInput"; 15 | 16 | class NewEvent extends Component { 17 | 18 | static defaultProps = { 19 | createEvent: () => null, 20 | } 21 | 22 | state = { 23 | event: { 24 | name: '', 25 | when: nearest15min().format(), 26 | where: '', 27 | description: '', 28 | } 29 | }; 30 | 31 | handleChange(field, { target: { value } }) { 32 | const { event } = this.state; 33 | 34 | event[field] = value; 35 | 36 | this.setState({ event }); 37 | } 38 | 39 | handleDateChange(field, value) { 40 | this.handleChange(field, { target: { value: value.format() } }); 41 | } 42 | 43 | handleSave = async (e) => { 44 | e.stopPropagation(); 45 | e.preventDefault(); 46 | 47 | const { createEvent, history } = this.props; 48 | const { event } = this.state; 49 | 50 | await createEvent({ ...event }); 51 | 52 | history.push('/'); 53 | } 54 | 55 | render() { 56 | const { event } = this.state; 57 | 58 | return ( 59 |
60 |

Create an event

61 |
62 |
63 | 64 | 65 |
66 |
67 | 68 | } 71 | id="when" 72 | selected={moment(event.when)} 73 | onChange={this.handleDateChange.bind(this, 'when')} 74 | peekNextMonth 75 | showMonthDropdown 76 | showYearDropdown 77 | dropdownMode="select" 78 | showTimeSelect 79 | timeFormat="hh:mm a" 80 | timeIntervals={15} 81 | dateFormat="LL LT" 82 | /> 83 |
84 |
85 | 86 | 87 |
88 |
89 | 90 | 92 |
93 |
94 | Cancel 95 |
96 | 97 |
98 |
99 |
100 | ); 101 | } 102 | 103 | } 104 | 105 | export default graphql( 106 | MutationCreateEvent, 107 | { 108 | props: (props) => ({ 109 | createEvent: (event) => { 110 | return props.mutate({ 111 | update: (proxy, { data: { createEvent } }) => { 112 | // Update QueryAllEvents 113 | const query = QueryAllEvents; 114 | const data = proxy.readQuery({ query }); 115 | 116 | data.listEvents.items = [...data.listEvents.items.filter(e => e.id !== createEvent.id), createEvent]; 117 | 118 | proxy.writeQuery({ query, data }); 119 | 120 | // Create cache entry for QueryGetEvent 121 | const query2 = QueryGetEvent; 122 | const variables = { id: createEvent.id }; 123 | const data2 = { getEvent: { ...createEvent } }; 124 | 125 | proxy.writeQuery({ query: query2, variables, data: data2 }); 126 | }, 127 | variables: event, 128 | optimisticResponse: () => ({ 129 | createEvent: { 130 | ...event, id: uuid(), __typename: 'Event', comments: { __typename: 'CommentConnection', items: [] } 131 | } 132 | }), 133 | }) 134 | } 135 | }) 136 | } 137 | )(NewEvent); 138 | -------------------------------------------------------------------------------- /src/Components/ViewEvent.js: -------------------------------------------------------------------------------- 1 | import React, { Component } from "react"; 2 | import { graphql } from "react-apollo"; 3 | import { Link } from "react-router-dom"; 4 | 5 | import moment from 'moment'; 6 | 7 | import QueryGetEvent from "../GraphQL/QueryGetEvent"; 8 | import EventComments from "./EventComments"; 9 | 10 | class ViewEvent extends Component { 11 | 12 | render() { 13 | const { event, loading } = this.props; 14 | 15 | return ( 16 |
17 | Back to events 18 |
19 |
20 | {event &&
21 |
{event.name}
22 |
{moment(event.when).format('LL')}
23 |
{moment(event.when).format('LT')}
24 |
{event.where}
25 |
{event.description}
26 |
27 | 28 |
29 |
} 30 |
31 |
32 |
33 | ); 34 | } 35 | 36 | } 37 | 38 | const ViewEventWithData = graphql( 39 | QueryGetEvent, 40 | { 41 | options: ({ match: { params: { id } } }) => ({ 42 | variables: { id }, 43 | fetchPolicy: 'cache-and-network', 44 | }), 45 | props: ({ data: { getEvent: event, loading} }) => ({ 46 | event, 47 | loading, 48 | }), 49 | }, 50 | )(ViewEvent); 51 | 52 | export default ViewEventWithData; -------------------------------------------------------------------------------- /src/GraphQL/MutationCommentOnEvent.js: -------------------------------------------------------------------------------- 1 | import gql from "graphql-tag"; 2 | 3 | export default gql(` 4 | mutation MutationCommentOnEvent($eventId: ID! $content: String! $createdAt: String!) { 5 | commentOnEvent( 6 | eventId: $eventId 7 | content: $content 8 | createdAt: $createdAt 9 | ) { 10 | eventId 11 | commentId 12 | content 13 | createdAt 14 | } 15 | }`); 16 | -------------------------------------------------------------------------------- /src/GraphQL/MutationCreateEvent.js: -------------------------------------------------------------------------------- 1 | import gql from "graphql-tag"; 2 | 3 | export default gql(` 4 | mutation($name: String! $when: String! $where: String! $description: String!) { 5 | createEvent( 6 | name: $name 7 | when: $when 8 | where: $where 9 | description: $description 10 | ) { 11 | id 12 | name 13 | where 14 | when 15 | description 16 | comments { 17 | items { 18 | commentId 19 | } 20 | } 21 | } 22 | }`); 23 | -------------------------------------------------------------------------------- /src/GraphQL/MutationDeleteEvent.js: -------------------------------------------------------------------------------- 1 | import gql from "graphql-tag"; 2 | 3 | export default gql(` 4 | mutation($id: ID!) { 5 | deleteEvent(id: $id) { 6 | id 7 | name 8 | where 9 | when 10 | description 11 | comments { 12 | items { 13 | commentId 14 | } 15 | } 16 | } 17 | }`); 18 | -------------------------------------------------------------------------------- /src/GraphQL/QueryAllEvents.js: -------------------------------------------------------------------------------- 1 | import gql from "graphql-tag"; 2 | 3 | export default gql(` 4 | query { 5 | listEvents(limit: 1000) { 6 | items { 7 | id 8 | name 9 | where 10 | when 11 | description 12 | comments { 13 | items { 14 | commentId 15 | } 16 | } 17 | } 18 | } 19 | }`); 20 | -------------------------------------------------------------------------------- /src/GraphQL/QueryGetEvent.js: -------------------------------------------------------------------------------- 1 | import gql from "graphql-tag"; 2 | 3 | export default gql(` 4 | query($id: ID!) { 5 | getEvent(id: $id) { 6 | id 7 | name 8 | where 9 | when 10 | description 11 | comments { 12 | __typename 13 | items { 14 | commentId 15 | content 16 | createdAt 17 | } 18 | } 19 | } 20 | }`); 21 | -------------------------------------------------------------------------------- /src/GraphQL/SubsriptionEventComments.js: -------------------------------------------------------------------------------- 1 | import gql from "graphql-tag"; 2 | 3 | export default gql(` 4 | subscription($eventId: String!) { 5 | subscribeToEventComments(eventId: $eventId) { 6 | eventId 7 | commentId 8 | content 9 | createdAt 10 | } 11 | }`); 12 | -------------------------------------------------------------------------------- /src/Utils.js: -------------------------------------------------------------------------------- 1 | import moment from 'moment'; 2 | 3 | export const nearestMinutes = (interval, someMoment) => { 4 | const roundedMinutes = Math.round(someMoment.clone().minute() / interval) * interval; 5 | return someMoment.clone().minute(roundedMinutes).second(0); 6 | } 7 | 8 | export const nearest15min = () => nearestMinutes(15, moment.utc()); 9 | -------------------------------------------------------------------------------- /src/aws-exports.js: -------------------------------------------------------------------------------- 1 | const awsmobile = { 2 | "aws_appsync_graphqlEndpoint": "https://xxxxxxxxxxxxxxxx.appsync-api.us-west-2.amazonaws.com/graphql", 3 | "aws_appsync_region": "us-west-2", 4 | "aws_appsync_authenticationType": "API_KEY", 5 | "aws_appsync_apiKey": "xxx-xxxxxxxxxxxx", 6 | }; 7 | 8 | export default awsmobile; -------------------------------------------------------------------------------- /src/index.css: -------------------------------------------------------------------------------- 1 | body { 2 | margin: 0; 3 | padding: 0; 4 | font-family: sans-serif; 5 | } 6 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ReactDOM from 'react-dom'; 3 | import './index.css'; 4 | import App from './App'; 5 | import registerServiceWorker from './registerServiceWorker'; 6 | 7 | ReactDOM.render(, document.getElementById('root')); 8 | registerServiceWorker(); 9 | -------------------------------------------------------------------------------- /src/registerServiceWorker.js: -------------------------------------------------------------------------------- 1 | // In production, we register a service worker to serve assets from local cache. 2 | 3 | // This lets the app load faster on subsequent visits in production, and gives 4 | // it offline capabilities. However, it also means that developers (and users) 5 | // will only see deployed updates on the "N+1" visit to a page, since previously 6 | // cached resources are updated in the background. 7 | 8 | // To learn more about the benefits of this model, read https://goo.gl/KwvDNy. 9 | // This link also includes instructions on opting out of this behavior. 10 | 11 | const isLocalhost = Boolean( 12 | window.location.hostname === 'localhost' || 13 | // [::1] is the IPv6 localhost address. 14 | window.location.hostname === '[::1]' || 15 | // 127.0.0.1/8 is considered localhost for IPv4. 16 | window.location.hostname.match( 17 | /^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/ 18 | ) 19 | ); 20 | 21 | export default function register() { 22 | if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) { 23 | // The URL constructor is available in all browsers that support SW. 24 | const publicUrl = new URL(process.env.PUBLIC_URL, window.location); 25 | if (publicUrl.origin !== window.location.origin) { 26 | // Our service worker won't work if PUBLIC_URL is on a different origin 27 | // from what our page is served on. This might happen if a CDN is used to 28 | // serve assets; see https://github.com/facebookincubator/create-react-app/issues/2374 29 | return; 30 | } 31 | 32 | window.addEventListener('load', () => { 33 | const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`; 34 | 35 | if (isLocalhost) { 36 | // This is running on localhost. Lets check if a service worker still exists or not. 37 | checkValidServiceWorker(swUrl); 38 | } else { 39 | // Is not local host. Just register service worker 40 | registerValidSW(swUrl); 41 | } 42 | }); 43 | } 44 | } 45 | 46 | function registerValidSW(swUrl) { 47 | navigator.serviceWorker 48 | .register(swUrl) 49 | .then(registration => { 50 | registration.onupdatefound = () => { 51 | const installingWorker = registration.installing; 52 | installingWorker.onstatechange = () => { 53 | if (installingWorker.state === 'installed') { 54 | if (navigator.serviceWorker.controller) { 55 | // At this point, the old content will have been purged and 56 | // the fresh content will have been added to the cache. 57 | // It's the perfect time to display a "New content is 58 | // available; please refresh." message in your web app. 59 | console.log('New content is available; please refresh.'); 60 | } else { 61 | // At this point, everything has been precached. 62 | // It's the perfect time to display a 63 | // "Content is cached for offline use." message. 64 | console.log('Content is cached for offline use.'); 65 | } 66 | } 67 | }; 68 | }; 69 | }) 70 | .catch(error => { 71 | console.error('Error during service worker registration:', error); 72 | }); 73 | } 74 | 75 | function checkValidServiceWorker(swUrl) { 76 | // Check if the service worker can be found. If it can't reload the page. 77 | fetch(swUrl) 78 | .then(response => { 79 | // Ensure service worker exists, and that we really are getting a JS file. 80 | if ( 81 | response.status === 404 || 82 | response.headers.get('content-type').indexOf('javascript') === -1 83 | ) { 84 | // No service worker found. Probably a different app. Reload the page. 85 | navigator.serviceWorker.ready.then(registration => { 86 | registration.unregister().then(() => { 87 | window.location.reload(); 88 | }); 89 | }); 90 | } else { 91 | // Service worker found. Proceed as normal. 92 | registerValidSW(swUrl); 93 | } 94 | }) 95 | .catch(() => { 96 | console.log( 97 | 'No internet connection found. App is running in offline mode.' 98 | ); 99 | }); 100 | } 101 | 102 | export function unregister() { 103 | if ('serviceWorker' in navigator) { 104 | navigator.serviceWorker.ready.then(registration => { 105 | registration.unregister(); 106 | }); 107 | } 108 | } 109 | --------------------------------------------------------------------------------