Skip to content
This repository was archived by the owner on Apr 17, 2023. It is now read-only.

Commit 3cf03a3

Browse files
Add uniforms to starter app (#417)
1 parent 043c6a9 commit 3cf03a3

6 files changed

Lines changed: 74 additions & 83 deletions

File tree

client/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@
3131
"react-router-dom": "5.1.2",
3232
"react-scripts": "3.4.1",
3333
"subscriptions-transport-ws": "0.9.16",
34-
"typescript": "3.8.3"
34+
"typescript": "3.8.3",
35+
"uniforms-ionic": "^0.0.11",
36+
"uniforms": "3.0.0-alpha.3",
37+
"uniforms-bridge-simple-schema-2": "3.0.0-alpha.3"
3538
},
3639
"scripts": {
3740
"start": "npm run build && cap serve",

client/src/forms/TaskForm.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import React from 'react';
2+
import {AutoForm, TextField, LongTextField, HiddenField, SubmitField, ErrorsField} from 'uniforms-ionic';
3+
import {schema} from './schema';
4+
5+
export function TaskForm({model, handleSubmit}) {
6+
return (
7+
<AutoForm
8+
schema={schema}
9+
onSubmit={handleSubmit}
10+
model={model}
11+
>
12+
<ErrorsField />
13+
<TextField name="title" />
14+
<LongTextField name="description" />
15+
<HiddenField name="status" value="OPEN" />
16+
<HiddenField name="version" value={model.version ?? 1} />
17+
<SubmitField />
18+
</AutoForm>
19+
)
20+
}

client/src/forms/schema.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import SimpleSchema from 'simpl-schema';
2+
import SimpleSchema2Bridge from 'uniforms-bridge-simple-schema-2';
3+
import { LongTextField } from 'uniforms-ionic';
4+
5+
const s = new SimpleSchema({
6+
title: {
7+
type: String,
8+
uniforms: {
9+
required: true
10+
}
11+
},
12+
13+
description: {
14+
type: String,
15+
uniforms: {
16+
component: LongTextField,
17+
}
18+
},
19+
20+
status: {
21+
type: String,
22+
allowedValues: ['OPEN', 'ASSIGNED', 'COMPLETE']
23+
},
24+
25+
version: {
26+
type: Number
27+
}
28+
});
29+
30+
export const schema = new SimpleSchema2Bridge(s);

client/src/pages/AddTaskPage.tsx

Lines changed: 8 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,14 @@
1-
import React, { useState, SyntheticEvent } from 'react'
1+
import React, { useState } from 'react'
22
import { RouteComponentProps } from 'react-router-dom'
3-
import {
4-
IonContent,
5-
IonButton,
6-
IonItem,
7-
IonLabel,
8-
IonInput,
9-
IonToast,
10-
} from '@ionic/react';
3+
import { IonContent, IonToast } from '@ionic/react';
114
import { useOfflineMutation } from 'react-offix-hooks';
125
import { mutationOptions } from '../helpers';
136
import { Header } from '../components/Header';
147
import { createTask } from '../graphql/mutations/createTask';
8+
import { TaskForm } from '../forms/TaskForm';
159

1610
export const AddTaskPage: React.FC<RouteComponentProps> = ({ history, match }) => {
1711

18-
const [title, setTitle] = useState<string>('');
19-
const [description, setDescription] = useState<string>('');
2012
const [ showToast, setShowToast ] = useState<boolean>(false);
2113
const [ errorMessage, setErrorMessage ] = useState<string>('');
2214

@@ -32,51 +24,19 @@ export const AddTaskPage: React.FC<RouteComponentProps> = ({ history, match }) =
3224
setShowToast(true);
3325
};
3426

35-
const submit = (event: SyntheticEvent) => {
36-
event.preventDefault();
37-
const variables = {
38-
input: {
39-
title,
40-
description,
41-
status: 'OPEN',
42-
version: 1
43-
},
44-
};
45-
27+
const submit = (model: any) => {
4628
createTaskMutation({
47-
variables
29+
variables: {input: {...model}}
4830
})
49-
.then(() => history.push('/'))
50-
.catch(handleError);
31+
.then(() => history.push('/'))
32+
.catch(handleError);
5133
};
5234

5335
return (
5436
<>
5537
<Header title="Add task" backHref="/tasks" match={match} />
5638
<IonContent>
57-
<form onSubmit={submit} style={{ padding: '0 16px' }}>
58-
<IonItem>
59-
<IonLabel color="primary" position="floating">Title</IonLabel>
60-
<IonInput
61-
type="text"
62-
required
63-
name="title"
64-
value={title}
65-
onInput={(e:any) => setTitle(e.target.value)}
66-
/>
67-
</IonItem>
68-
<IonItem>
69-
<IonLabel color="primary" position="floating">Description</IonLabel>
70-
<IonInput
71-
type="text"
72-
required
73-
name="description"
74-
value={description}
75-
onInput={(e:any) => setDescription(e.target.value)}
76-
/>
77-
</IonItem>
78-
<IonButton className="submit-btn" expand="block" type="submit">Create</IonButton>
79-
</form>
39+
<TaskForm handleSubmit={submit} model={{}} />
8040
<IonToast
8141
isOpen={showToast}
8242
onDidDismiss={() => setShowToast(false)}

client/src/pages/UpdateTaskPage.tsx

Lines changed: 11 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
import React, { useState, SyntheticEvent } from 'react'
1+
import React, { useState } from 'react'
22
import { RouteComponentProps } from 'react-router-dom'
33
import {
44
IonContent,
55
IonCard,
6-
IonButton,
7-
IonItem,
86
IonLabel,
9-
IonInput,
107
IonCardHeader,
118
IonCardContent,
129
IonNote,
@@ -22,13 +19,11 @@ import { mutationOptions } from '../helpers';
2219
import { IUpdateMatchParams } from '../declarations';
2320
import { updateTask } from '../graphql/mutations/updateTask';
2421
import { findTasks } from '../graphql/queries/findTasks';
22+
import { TaskForm } from '../forms/TaskForm';
2523

2624
export const UpdateTaskPage: React.FC<RouteComponentProps<IUpdateMatchParams>> = ({ history, match }) => {
2725

2826
const { id } = match.params;
29-
30-
const [title, setTitle] = useState<string>(null!);
31-
const [description, setDescription] = useState<string>(null!);
3227
const [ showToast, setShowToast ] = useState<boolean>(false);
3328
const [ errorMessage, setErrorMessage ] = useState<string>('');
3429
const { loading, error, data } = useQuery(findTasks, {
@@ -46,27 +41,20 @@ export const UpdateTaskPage: React.FC<RouteComponentProps<IUpdateMatchParams>> =
4641
history.push('/');
4742
return;
4843
}
44+
console.log(error);
4945
setErrorMessage(error.message);
5046
setShowToast(true);
5147
}
5248

53-
const submit = (event: SyntheticEvent) => {
54-
event.preventDefault();
55-
const task = data.findTasks;
56-
delete task.__typename;
57-
const variables = {
58-
input: {
59-
...task,
60-
title: title || task.title,
61-
description: description || task.description,
62-
}
63-
};
64-
49+
const submit = (model: any) => {
50+
// remove `__typename` property without
51+
// deleting from the model object (as this may be a state reference)
52+
const { __typename, ...no__typename } = model;
6553
updateTaskMutation({
66-
variables
54+
variables: { input: no__typename }
6755
})
68-
.then(() => history.push('/'))
69-
.catch(handleError);
56+
.then(() => history.push('/'))
57+
.catch(handleError);
7058
}
7159

7260
if (error) return <pre>{JSON.stringify(error)}</pre>;
@@ -99,17 +87,7 @@ export const UpdateTaskPage: React.FC<RouteComponentProps<IUpdateMatchParams>> =
9987
</IonLabel>
10088
</IonCardContent>
10189
</IonCard>
102-
<form onSubmit={submit} style={{ padding: '0 16px' }}>
103-
<IonItem>
104-
<IonLabel color="primary" position="floating">Title</IonLabel>
105-
<IonInput type="text" name="title" onInput={(e: any) => setTitle(e.target.value)} value={task.title} />
106-
</IonItem>
107-
<IonItem>
108-
<IonLabel color="primary" position="floating">Description</IonLabel>
109-
<IonInput type="text" name="description" onInput={(e: any) => setDescription(e.target.value)} value={task.description} />
110-
</IonItem>
111-
<IonButton className="submit-btn" expand="block" type="submit">Update</IonButton>
112-
</form>
90+
<TaskForm handleSubmit={submit} model={task} />
11391
<IonToast
11492
isOpen={showToast}
11593
onDidDismiss={() => setShowToast(false)}

server/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
"express-session": "1.17.0",
3636
"graphql-tag": "2.10.3",
3737
"keycloak-connect": "9.0.2",
38-
"keycloak-connect-graphql": "0.4.0",
38+
"keycloak-connect-graphql": "0.5.0",
3939
"@graphback/keycloak-authz": "0.12.0"
4040
}
4141
}

0 commit comments

Comments
 (0)