-
Notifications
You must be signed in to change notification settings - Fork 2
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
tidy: release add Team Editor feature #3580
Changes from 49 commits
a4d60bf
f7e87fb
5f38c8a
d2f9c59
0a566a1
edc221b
fe7ce77
12df25c
2975369
3240488
50c0a9c
f6d7f25
b419666
5d189eb
c5f31a8
0f368f5
4a27c5c
3f81d7b
fc91022
4ec6cb6
e616c02
237a0ee
29624c1
522e2d7
37c8a3f
b6a12d2
2e1fedc
8a3459d
126d6d4
a5dfb84
a78a86d
a4904bb
0a1aee5
034d4d4
45ef05c
752fb93
99b38c6
55c558b
1c5651f
096f824
c54b7c7
0cfbd19
0cb25f9
06547b7
600125e
25e53a8
91f6de5
e7007a1
1799a78
eafac04
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,13 +24,15 @@ import { optimisticallyUpdateMembersTable } from "./lib/optimisticallyUpdateMemb | |
export const AddNewEditorModal = ({ | ||
showModal, | ||
setShowModal, | ||
setShowToast, | ||
setShowSuccessToast, | ||
setShowErrorToast, | ||
}: AddNewEditorModalProps) => { | ||
const [showUserAlreadyExistsError, setShowUserAlreadyExistsError] = | ||
useState<boolean>(false); | ||
|
||
const clearErrors = () => { | ||
setShowUserAlreadyExistsError(false); | ||
setShowErrorToast(false); | ||
}; | ||
|
||
const handleSubmit = async ( | ||
|
@@ -39,7 +41,7 @@ export const AddNewEditorModal = ({ | |
) => { | ||
const { teamId, teamSlug } = useStore.getState(); | ||
|
||
const newUserId = await createAndAddUserToTeam( | ||
const createUserResult = await createAndAddUserToTeam( | ||
values.email, | ||
values.firstName, | ||
values.lastName, | ||
|
@@ -49,16 +51,19 @@ export const AddNewEditorModal = ({ | |
if (isUserAlreadyExistsError(err.message)) { | ||
setShowUserAlreadyExistsError(true); | ||
} | ||
if (err.message === "Unable to create user") { | ||
setShowErrorToast(true); | ||
} | ||
Comment on lines
+54
to
+56
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure if we'll ever reach this due to the error handling already in |
||
console.error(err); | ||
}); | ||
|
||
if (!newUserId) { | ||
if (!createUserResult) { | ||
return; | ||
} | ||
clearErrors(); | ||
optimisticallyUpdateMembersTable(values, newUserId); | ||
optimisticallyUpdateMembersTable(values, createUserResult.id); | ||
setShowModal(false); | ||
setShowToast(true); | ||
setShowSuccessToast(true); | ||
resetForm({ values }); | ||
}; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,6 @@ import TableCell from "@mui/material/TableCell"; | |
import TableContainer from "@mui/material/TableContainer"; | ||
import TableHead from "@mui/material/TableHead"; | ||
import TableRow from "@mui/material/TableRow"; | ||
import { hasFeatureFlag } from "lib/featureFlags"; | ||
import { AddButton } from "pages/Team"; | ||
import React, { useState } from "react"; | ||
|
||
|
@@ -20,17 +19,29 @@ export const MembersTable = ({ | |
showAddMemberButton, | ||
}: MembersTableProps) => { | ||
const [showModal, setShowModal] = useState(false); | ||
const [showToast, setShowToast] = useState(false); | ||
const [showSuccessToast, setShowSuccessToast] = useState(false); | ||
const [showErrorToast, setShowErrorToast] = useState(false); | ||
|
||
const handleCloseToast = ( | ||
const handleCloseSuccessToast = ( | ||
_event?: React.SyntheticEvent | Event, | ||
reason?: string, | ||
) => { | ||
if (reason === "clickaway") { | ||
return; | ||
} | ||
|
||
setShowToast(false); | ||
setShowSuccessToast(false); | ||
}; | ||
|
||
const handleCloseErrorToast = ( | ||
_event?: React.SyntheticEvent | Event, | ||
reason?: string, | ||
) => { | ||
if (reason === "clickaway") { | ||
return; | ||
} | ||
|
||
setShowErrorToast(false); | ||
Comment on lines
+36
to
+44
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Leaving this as duplicated for now and will revisit with a useToast hook 🤞 |
||
}; | ||
|
||
const roleLabels: Record<string, string> = { | ||
|
@@ -102,7 +113,7 @@ export const MembersTable = ({ | |
<TableCell>{member.email}</TableCell> | ||
</StyledTableRow> | ||
))} | ||
{showAddMemberButton && hasFeatureFlag("ADD_NEW_EDITOR") && ( | ||
{showAddMemberButton && ( | ||
<TableRow> | ||
<TableCell colSpan={3}> | ||
<AddButton onClick={() => setShowModal(true)}> | ||
|
@@ -115,23 +126,39 @@ export const MembersTable = ({ | |
</Table> | ||
{showAddMemberButton && ( | ||
<Snackbar | ||
open={showToast} | ||
open={showSuccessToast} | ||
autoHideDuration={6000} | ||
onClose={handleCloseToast} | ||
onClose={handleCloseSuccessToast} | ||
> | ||
<Alert | ||
onClose={handleCloseToast} | ||
onClose={handleCloseSuccessToast} | ||
severity="success" | ||
sx={{ width: "100%" }} | ||
> | ||
Successfully added a user | ||
</Alert> | ||
</Snackbar> | ||
)} | ||
{showAddMemberButton && ( | ||
<Snackbar | ||
open={showErrorToast} | ||
autoHideDuration={6000} | ||
onClose={handleCloseErrorToast} | ||
> | ||
<Alert | ||
onClose={handleCloseErrorToast} | ||
severity="error" | ||
sx={{ width: "100%" }} | ||
> | ||
Failed to add new user, please try again | ||
</Alert> | ||
</Snackbar> | ||
)} | ||
</TableContainer> | ||
{showModal && ( | ||
<AddNewEditorModal | ||
setShowToast={setShowToast} | ||
setShowSuccessToast={setShowSuccessToast} | ||
setShowErrorToast={setShowErrorToast} | ||
showModal={showModal} | ||
setShowModal={setShowModal} | ||
/> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,12 @@ | ||
import { gql } from "@apollo/client"; | ||
import { FetchResult, gql } from "@apollo/client"; | ||
import { GET_USERS_FOR_TEAM_QUERY } from "routes/teamMembers"; | ||
|
||
import { client } from "../../../../../lib/graphql"; | ||
|
||
type CreateAndAddUserResponse = FetchResult<{ | ||
insert_users_one: { id: number; __typename: "users" }; | ||
}>; | ||
|
||
export const createAndAddUserToTeam = async ( | ||
email: string, | ||
firstName: string, | ||
|
@@ -11,7 +15,7 @@ export const createAndAddUserToTeam = async ( | |
teamSlug: string, | ||
) => { | ||
// NB: the user is hard-coded with the 'teamEditor' role for now | ||
const response = (await client.mutate({ | ||
const response: CreateAndAddUserResponse = await client.mutate({ | ||
mutation: gql` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FYI: |
||
mutation CreateAndAddUserToTeam( | ||
$email: String! | ||
|
@@ -40,6 +44,9 @@ export const createAndAddUserToTeam = async ( | |
refetchQueries: [ | ||
{ query: GET_USERS_FOR_TEAM_QUERY, variables: { teamSlug } }, | ||
], | ||
})) as any; | ||
return response.data.insert_users_one; | ||
}); | ||
if (response.data) { | ||
return response.data.insert_users_one; | ||
} | ||
throw new Error("Unable to create user"); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { screen } from "@testing-library/react"; | ||
import { FullStore, useStore } from "pages/FlowEditor/lib/store"; | ||
import { vi } from "vitest"; | ||
|
||
import { setupTeamMembersScreen } from "./helpers/setupTeamMembersScreen"; | ||
import { userTriesToAddNewEditor } from "./helpers/userTriesToAddNewEditor"; | ||
import { mockTeamMembersData } from "./mocks/mockTeamMembersData"; | ||
import { alreadyExistingUser } from "./mocks/mockUsers"; | ||
|
||
let initialState: FullStore; | ||
vi.mock( | ||
"pages/FlowEditor/components/Team/queries/createAndAddUserToTeam.tsx", | ||
() => ({ | ||
createAndAddUserToTeam: vi.fn().mockRejectedValue({ | ||
message: "Unable to create user", | ||
}), | ||
}), | ||
); | ||
|
||
describe("when a user fills in the 'add a new editor' form correctly but there is a server-side error", () => { | ||
afterAll(() => useStore.setState(initialState)); | ||
beforeEach(async () => { | ||
useStore.setState({ | ||
teamMembers: [...mockTeamMembersData, alreadyExistingUser], | ||
}); | ||
|
||
const { user } = await setupTeamMembersScreen(); | ||
await userTriesToAddNewEditor(user); | ||
}); | ||
|
||
it("shows an appropriate error message", async () => { | ||
expect( | ||
await screen.findByText(/Failed to add new user, please try again/), | ||
).toBeInTheDocument(); | ||
}); | ||
}); |
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the sort of thing I hope to get rid of with a
useToast
hook in another PR 😁