-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathAddPetPage.tsx
More file actions
59 lines (54 loc) · 1.83 KB
/
AddPetPage.tsx
File metadata and controls
59 lines (54 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import * as React from "react";
import { graphql, QueryProps, MutationFunc } from "react-apollo";
import { withRouter, RouteComponentProps } from "react-router-dom";
import * as AddPetMutationGql from "./AddPetMutation.graphql";
import * as OwnerQueryGql from "../../owner/OwnerQuery.graphql";
import { GetAddPetFormDataQuery, AddPetInput, AddPetMutation, PetData } from "../../types";
import PetForm from "../PetForm";
import withAddPetFormDataLoader from "./withAddPetFormDataLoader";
type AddPetPageOwnProps = RouteComponentProps<{}> & {
data: QueryProps & GetAddPetFormDataQuery;
};
type AddPetPageProps = AddPetPageOwnProps & {
mutate: MutationFunc<AddPetMutation>;
};
const emptyPet = (): PetData => ({
name: "",
birthDate: "",
type: -1
});
const AddPetPage = ({ mutate, history, data }: AddPetPageProps) =>
<div>
<PetForm
formTitle={`Add Pet for ${data.owner.firstName} ${data.owner.lastName}`}
initialPet={emptyPet()}
pettypes={data.pettypes}
onFormSubmit={(pet: PetData) => {
const input: AddPetInput = {
name: pet.name,
birthDate: pet.birthDate,
typeId: pet.type,
ownerId: data.owner.id
};
return mutate({
variables: { input },
refetchQueries: [
{
query: OwnerQueryGql,
variables: {
ownerId: data.owner.id
}
}
]
})
.then(() => {
history.push(`/owners/${data.owner.id}`);
})
.catch(error => {
console.log("there was an error sending the query", error);
return Promise.reject(`Could not save owner: ${error}`);
});
}}
/>
</div>;
export default withAddPetFormDataLoader(graphql<AddPetMutation, AddPetPageOwnProps>(AddPetMutationGql)(AddPetPage));