Skip to content

Commit

Permalink
feat: edit contact form
Browse files Browse the repository at this point in the history
  • Loading branch information
matthieu-foucault committed Feb 15, 2022
1 parent aadbcec commit 2594ab7
Show file tree
Hide file tree
Showing 12 changed files with 310 additions and 60 deletions.
29 changes: 22 additions & 7 deletions app/components/Contact/ContactTableRow.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import Button from "@button-inc/bcgov-theme/Button";
import useCreateEditContactFormChange from "mutations/Contact/createEditContactFormChange";
import { useRouter } from "next/router";
import { getContactViewPageRoute } from "pageRoutes";
import { getContactFormPageRoute } from "pageRoutes";
import { useFragment, graphql } from "react-relay";
import { ContactTableRow_contact$key } from "__generated__/ContactTableRow_contact.graphql";
import { createEditContactFormChangeMutation$data } from "__generated__/createEditContactFormChangeMutation.graphql";

interface Props {
contact: ContactTableRow_contact$key;
}

const ContactTableRow: React.FC<Props> = ({ contact }) => {
const { id, fullName, fullPhone, position } = useFragment(
const { rowId, fullName, fullPhone, position } = useFragment(
graphql`
fragment ContactTableRow_contact on Contact {
id
rowId
fullName
fullPhone
position
Expand All @@ -22,9 +24,18 @@ const ContactTableRow: React.FC<Props> = ({ contact }) => {
);

const router = useRouter();
const [startContactRevision, isStartingContactRevision] =
useCreateEditContactFormChange();

const handleViewClick = () => {
router.push(getContactViewPageRoute(id));
const handleEditContact = () => {
startContactRevision({
variables: { contactRowId: rowId },
onCompleted: (response: createEditContactFormChangeMutation$data) => {
router.push(
getContactFormPageRoute(response.createFormChange.formChange.id)
);
},
});
};

return (
Expand All @@ -34,8 +45,12 @@ const ContactTableRow: React.FC<Props> = ({ contact }) => {
<td>{position}</td>
<td>
<div className="actions">
<Button size="small" onClick={handleViewClick}>
View
<Button
size="small"
disabled={isStartingContactRevision}
onClick={handleEditContact}
>
Edit
</Button>
</div>
</td>
Expand Down
34 changes: 0 additions & 34 deletions app/mutations/Contact/createContactFormChange.ts

This file was deleted.

36 changes: 36 additions & 0 deletions app/mutations/Contact/createEditContactFormChange.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { useMutation, graphql, Disposable, Environment } from "react-relay";
import { MutationConfig } from "relay-runtime";
import { createEditContactFormChangeMutation } from "__generated__/createEditContactFormChangeMutation.graphql";

export const mutation = graphql`
mutation createEditContactFormChangeMutation($contactRowId: Int!) {
createFormChange(
input: {
formDataSchemaName: "cif"
formDataTableName: "contact"
jsonSchemaName: "contact"
operation: "UPDATE"
changeReason: "Created from contact form"
formDataRecordId: $contactRowId
}
) {
formChange {
id
}
}
}
`;

export const useCreateEditContactFormChange = (
commitMutationFn?: (
environment: Environment,
config: MutationConfig<createEditContactFormChangeMutation>
) => Disposable
) => {
return useMutation<createEditContactFormChangeMutation>(
mutation,
commitMutationFn
);
};

export default useCreateEditContactFormChange;
33 changes: 33 additions & 0 deletions app/mutations/Contact/createNewContactFormChange.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { useMutation, graphql, Disposable, Environment } from "react-relay";
import { MutationConfig } from "relay-runtime";
import { createNewContactFormChangeMutation } from "__generated__/createNewContactFormChangeMutation.graphql";

export const mutation = graphql`
mutation createNewContactFormChangeMutation {
createFormChange(
input: {
formDataSchemaName: "cif"
formDataTableName: "contact"
jsonSchemaName: "contact"
operation: "INSERT"
changeReason: "Created from contact form"
}
) {
formChange {
id
}
}
}
`;

export const useCreateNewContactFormChange = (
commitMutationFn?: (
environment: Environment,
config: MutationConfig<createNewContactFormChangeMutation>
) => Disposable
) => {
return useMutation<createNewContactFormChangeMutation>(
mutation,
commitMutationFn
);
};
4 changes: 2 additions & 2 deletions app/pages/cif/contacts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { NoHeaderFilter, TextFilter } from "components/Table/Filters";
import Table from "components/Table";
import ContactTableRow from "components/Contact/ContactTableRow";
import { Button } from "@button-inc/bcgov-theme";
import useCreateContactFormChange from "mutations/Contact/createContactFormChange";
import { useCreateNewContactFormChange } from "mutations/Contact/createNewContactFormChange";
import { useRouter } from "next/router";
import { createContactFormChangeMutation$data } from "__generated__/createContactFormChangeMutation.graphql";
import { getContactFormPageRoute } from "pageRoutes";
Expand Down Expand Up @@ -56,7 +56,7 @@ const tableFilters = [
function Contacts({ preloadedQuery }: RelayProps<{}, contactsQuery>) {
const { session, allContacts } = usePreloadedQuery(pageQuery, preloadedQuery);
const router = useRouter();
const [addContact, isAddingContact] = useCreateContactFormChange();
const [addContact, isAddingContact] = useCreateNewContactFormChange();

const handleAddContact = () => {
addContact({
Expand Down
5 changes: 5 additions & 0 deletions app/postgraphile.tags.json5
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@
omit: ["create", "update", "delete"],
},
},
"cif.form_change": {
tags: {
omit: ["create"],
},
},
},
procedure: {
"cif.contact_full_name": {
Expand Down
25 changes: 16 additions & 9 deletions app/schema/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -17479,19 +17479,27 @@ type CreateCifUserPayload {
query: Query
}

"""All input for the create `FormChange` mutation."""
"""All input for the `createFormChange` mutation."""
input CreateFormChangeInput {
changeReason: String!
changeStatus: String

"""
An arbitrary string value with no semantic meaning. Will be included in the
payload verbatim. May be used to track mutations by the client.
"""
clientMutationId: String

"""The `FormChange` to be created by this mutation."""
formChange: FormChangeInput!
formDataRecordId: Int
formDataSchemaName: String!
formDataTableName: String!
jsonSchemaName: String!
newFormData: JSON
operation: String!
projectRevisionId: Int
validationErrors: JSON
}

"""The output of our create `FormChange` mutation."""
"""The output of our `createFormChange` mutation."""
type CreateFormChangePayload {
"""Reads a single `ChangeStatus` that is related to this `FormChange`."""
changeStatusByChangeStatus: ChangeStatus
Expand All @@ -17507,8 +17515,6 @@ type CreateFormChangePayload {
unchanged and unused. May be used by a client to track mutations.
"""
clientMutationId: String

"""The `FormChange` that was created by this mutation."""
formChange: FormChange

"""An edge for our `FormChange`. May be used by Relay 1."""
Expand Down Expand Up @@ -18903,6 +18909,7 @@ input FormChangeFilter {
validationErrors: JSONFilter
}

<<<<<<< chore/archived-at-refactor
"""An input for mutations affecting `FormChange`"""
input FormChangeInput {
"""The reason for the change"""
Expand Down Expand Up @@ -18952,6 +18959,8 @@ input FormChangeInput {
validationErrors: JSON
}

=======
>>>>>>> feat: edit contact form
"""
The type of change operation, defining the action taken when the form_change is committed.
"""
Expand Down Expand Up @@ -21972,8 +21981,6 @@ type Mutation {
"""
input: CreateCifUserInput!
): CreateCifUserPayload

"""Creates a single `FormChange`."""
createFormChange(
"""
The exclusive input argument for this mutation. An object type, make sure to see documentation for this object’s fields.
Expand Down
Loading

0 comments on commit 2594ab7

Please sign in to comment.