- Notifications
You must be signed in to change notification settings - Fork928
feat: Workspaces filtering#1972
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
d7c1318
5cb62d5
09011d9
11836ea
568e3a6
f1409b2
f8e09e6
424e933
5dbf5b4
2912d03
9ea5c14
93a7d3c
79e71f1
5f1ae06
6b2999d
57fe5db
0a5d972
4aea7a0
0dd5552
5d0d9bb
59e7d5e
912b65c
30a17e9
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.
Uh oh!
There was an error while loading.Please reload this page.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -137,17 +137,14 @@ func (api *API) workspaces(rw http.ResponseWriter, r *http.Request) { | ||
// Empty strings mean no filter | ||
orgFilter := r.URL.Query().Get("organization_id") | ||
ownerFilter := r.URL.Query().Get("owner") | ||
nameFilter := r.URL.Query().Get("name") | ||
filter := database.GetWorkspacesWithFilterParams{Deleted: false} | ||
if orgFilter != "" { | ||
orgID, err := uuid.Parse(orgFilter) | ||
if err == nil { | ||
f0ssel marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
filter.OrganizationID = orgID | ||
} | ||
} | ||
if ownerFilter == "me" { | ||
filter.OwnerID = apiKey.UserID | ||
@@ -160,15 +157,15 @@ func (api *API) workspaces(rw http.ResponseWriter, r *http.Request) { | ||
Username: ownerFilter, | ||
Email: ownerFilter, | ||
}) | ||
if err == nil { | ||
filter.OwnerID = user.ID | ||
} | ||
} else { | ||
filter.OwnerID = userID | ||
} | ||
} | ||
if nameFilter != "" { | ||
filter.Name = nameFilter | ||
} | ||
workspaces, err := api.Database.GetWorkspacesWithFilter(r.Context(), filter) | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -198,9 +198,10 @@ func (c *Client) PutExtendWorkspace(ctx context.Context, id uuid.UUID, req PutEx | ||
} | ||
type WorkspaceFilter struct { | ||
OrganizationID uuid.UUID `json:"organization_id,omitempty"` | ||
// Owner can be a user_id (uuid), "me", or a username | ||
Owner string `json:"owner,omitempty"` | ||
Name string `json:"name,omitempty"` | ||
f0ssel marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
} | ||
// asRequestOption returns a function that can be used in (*Client).Request. | ||
@@ -214,6 +215,9 @@ func (f WorkspaceFilter) asRequestOption() requestOption { | ||
if f.Owner != "" { | ||
q.Set("owner", f.Owner) | ||
} | ||
if f.Name != "" { | ||
q.Set("name", f.Name) | ||
} | ||
r.URL.RawQuery = q.Encode() | ||
} | ||
} | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -118,10 +118,10 @@ describe("api.ts", () => { | ||
it.each<[TypesGen.WorkspaceFilter | undefined, string]>([ | ||
[undefined, "/api/v2/workspaces"], | ||
[{organization_id: "1",owner: "" }, "/api/v2/workspaces?organization_id=1"], | ||
[{organization_id: "",owner: "1" }, "/api/v2/workspaces?owner=1"], | ||
[{organization_id: "1",owner: "me" }, "/api/v2/workspaces?organization_id=1&owner=me"], | ||
f0ssel marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
])(`getWorkspacesURL(%p) returns %p`, (filter, expected) => { | ||
expect(getWorkspacesURL(filter)).toBe(expected) | ||
}) | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,169 @@ | ||
import Button from "@material-ui/core/Button" | ||
import Fade from "@material-ui/core/Fade" | ||
import InputAdornment from "@material-ui/core/InputAdornment" | ||
import Link from "@material-ui/core/Link" | ||
import Menu from "@material-ui/core/Menu" | ||
import MenuItem from "@material-ui/core/MenuItem" | ||
import { makeStyles } from "@material-ui/core/styles" | ||
import TextField from "@material-ui/core/TextField" | ||
import AddCircleOutline from "@material-ui/icons/AddCircleOutline" | ||
import SearchIcon from "@material-ui/icons/Search" | ||
import { useMachine } from "@xstate/react" | ||
import { FormikErrors, useFormik } from "formik" | ||
import { FC, useState } from "react" | ||
import { Link as RouterLink } from "react-router-dom" | ||
import { CloseDropdown, OpenDropdown } from "../../components/DropdownArrows/DropdownArrows" | ||
import { Margins } from "../../components/Margins/Margins" | ||
import { Stack } from "../../components/Stack/Stack" | ||
import { getFormHelpers, onChangeTrimmed } from "../../util/formUtils" | ||
import { workspacesMachine } from "../../xServices/workspaces/workspacesXService" | ||
import { WorkspacesPageView } from "./WorkspacesPageView" | ||
interface FilterFormValues { | ||
query: string | ||
} | ||
const Language = { | ||
filterName: "Filters", | ||
createWorkspaceButton: "Create workspace", | ||
yourWorkspacesButton: "Your workspaces", | ||
allWorkspacesButton: "All workspaces", | ||
} | ||
export type FilterFormErrors = FormikErrors<FilterFormValues> | ||
const WorkspacesPage: FC = () => { | ||
const styles = useStyles() | ||
const [workspacesState, send] = useMachine(workspacesMachine) | ||
const form = useFormik<FilterFormValues>({ | ||
initialValues: { | ||
query: workspacesState.context.filter || "", | ||
}, | ||
onSubmit: (values) => { | ||
send({ | ||
type: "SET_FILTER", | ||
query: values.query, | ||
}) | ||
}, | ||
}) | ||
const getFieldHelpers = getFormHelpers<FilterFormValues>(form) | ||
const [anchorEl, setAnchorEl] = useState<null | HTMLElement>(null) | ||
const handleClick = (event: React.MouseEvent<HTMLButtonElement>) => { | ||
setAnchorEl(event.currentTarget) | ||
} | ||
const handleClose = () => { | ||
setAnchorEl(null) | ||
} | ||
const setYourWorkspaces = () => { | ||
void form.setFieldValue("query", "owner:me") | ||
void form.submitForm() | ||
handleClose() | ||
} | ||
const setAllWorkspaces = () => { | ||
void form.setFieldValue("query", "") | ||
void form.submitForm() | ||
handleClose() | ||
} | ||
return ( | ||
<Margins> | ||
<Stack direction="row" className={styles.workspacesHeaderContainer}> | ||
<Stack direction="column" className={styles.filterColumn}> | ||
<Stack direction="row" spacing={0} className={styles.filterContainer}> | ||
<Button | ||
aria-controls="filter-menu" | ||
aria-haspopup="true" | ||
onClick={handleClick} | ||
className={styles.buttonRoot} | ||
> | ||
{Language.filterName} {anchorEl ? <CloseDropdown /> : <OpenDropdown />} | ||
</Button> | ||
<form onSubmit={form.handleSubmit} className={styles.filterForm}> | ||
<TextField | ||
{...getFieldHelpers("query")} | ||
className={styles.textFieldRoot} | ||
onChange={onChangeTrimmed(form)} | ||
fullWidth | ||
variant="outlined" | ||
InputProps={{ | ||
startAdornment: ( | ||
<InputAdornment position="start"> | ||
<SearchIcon fontSize="small" /> | ||
</InputAdornment> | ||
), | ||
}} | ||
/> | ||
</form> | ||
<Menu | ||
id="filter-menu" | ||
anchorEl={anchorEl} | ||
keepMounted | ||
open={Boolean(anchorEl)} | ||
onClose={handleClose} | ||
TransitionComponent={Fade} | ||
anchorOrigin={{ | ||
vertical: "bottom", | ||
horizontal: "left", | ||
}} | ||
transformOrigin={{ | ||
vertical: "top", | ||
horizontal: "left", | ||
}} | ||
> | ||
<MenuItem onClick={setYourWorkspaces}>{Language.yourWorkspacesButton}</MenuItem> | ||
<MenuItem onClick={setAllWorkspaces}>{Language.allWorkspacesButton}</MenuItem> | ||
</Menu> | ||
</Stack> | ||
</Stack> | ||
<Link underline="none" component={RouterLink} to="/workspaces/new"> | ||
<Button startIcon={<AddCircleOutline />} style={{ height: "44px" }}> | ||
{Language.createWorkspaceButton} | ||
</Button> | ||
</Link> | ||
</Stack> | ||
<WorkspacesPageView loading={workspacesState.hasTag("loading")} workspaces={workspacesState.context.workspaces} /> | ||
</Margins> | ||
) | ||
} | ||
const useStyles = makeStyles((theme) => ({ | ||
workspacesHeaderContainer: { | ||
marginTop: theme.spacing(3), | ||
marginBottom: theme.spacing(3), | ||
justifyContent: "space-between", | ||
f0ssel marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
}, | ||
filterColumn: { | ||
width: "60%", | ||
cursor: "text", | ||
}, | ||
filterContainer: { | ||
border: `1px solid ${theme.palette.divider}`, | ||
borderRadius: "6px", | ||
}, | ||
filterForm: { | ||
width: "100%", | ||
}, | ||
buttonRoot: { | ||
border: "none", | ||
borderRight: `1px solid ${theme.palette.divider}`, | ||
borderRadius: "6px 0px 0px 6px", | ||
}, | ||
textFieldRoot: { | ||
margin: "0px", | ||
"& fieldset": { | ||
border: "none", | ||
}, | ||
}, | ||
})) | ||
export default WorkspacesPage |
Uh oh!
There was an error while loading.Please reload this page.