|
| 1 | +import Button from "@material-ui/core/Button" |
| 2 | +import Fade from "@material-ui/core/Fade" |
| 3 | +import InputAdornment from "@material-ui/core/InputAdornment" |
| 4 | +import Link from "@material-ui/core/Link" |
| 5 | +import Menu from "@material-ui/core/Menu" |
| 6 | +import MenuItem from "@material-ui/core/MenuItem" |
| 7 | +import { makeStyles } from "@material-ui/core/styles" |
| 8 | +import TextField from "@material-ui/core/TextField" |
| 9 | +import AddCircleOutline from "@material-ui/icons/AddCircleOutline" |
| 10 | +import SearchIcon from "@material-ui/icons/Search" |
1 | 11 | import { useMachine } from "@xstate/react"
|
2 |
| -import { FC } from "react" |
| 12 | +import { FormikErrors, useFormik } from "formik" |
| 13 | +import { FC, useState } from "react" |
| 14 | +import { Link as RouterLink } from "react-router-dom" |
| 15 | +import { CloseDropdown, OpenDropdown } from "../../components/DropdownArrows/DropdownArrows" |
| 16 | +import { Margins } from "../../components/Margins/Margins" |
| 17 | +import { Stack } from "../../components/Stack/Stack" |
| 18 | +import { getFormHelpers, onChangeTrimmed } from "../../util/formUtils" |
3 | 19 | import { workspacesMachine } from "../../xServices/workspaces/workspacesXService"
|
4 | 20 | import { WorkspacesPageView } from "./WorkspacesPageView"
|
5 | 21 |
|
| 22 | +interface FilterFormValues { |
| 23 | + query: string |
| 24 | +} |
| 25 | + |
| 26 | +const Language = { |
| 27 | + filterName: "Filters", |
| 28 | + createWorkspaceButton: "Create workspace", |
| 29 | + yourWorkspacesButton: "Your workspaces", |
| 30 | + allWorkspacesButton: "All workspaces", |
| 31 | +} |
| 32 | + |
| 33 | +export type FilterFormErrors = FormikErrors<FilterFormValues> |
| 34 | + |
6 | 35 | const WorkspacesPage: FC = () => {
|
7 |
| - const [workspacesState] = useMachine(workspacesMachine) |
| 36 | + const styles = useStyles() |
| 37 | + const [workspacesState, send] = useMachine(workspacesMachine) |
| 38 | + |
| 39 | + const form = useFormik<FilterFormValues>({ |
| 40 | + initialValues: { |
| 41 | + query: workspacesState.context.filter || "", |
| 42 | + }, |
| 43 | + onSubmit: (values) => { |
| 44 | + send({ |
| 45 | + type: "SET_FILTER", |
| 46 | + query: values.query, |
| 47 | + }) |
| 48 | + }, |
| 49 | + }) |
| 50 | + |
| 51 | + const getFieldHelpers = getFormHelpers<FilterFormValues>(form) |
| 52 | + |
| 53 | + const [anchorEl, setAnchorEl] = useState<null | HTMLElement>(null) |
| 54 | + |
| 55 | + const handleClick = (event: React.MouseEvent<HTMLButtonElement>) => { |
| 56 | + setAnchorEl(event.currentTarget) |
| 57 | + } |
| 58 | + |
| 59 | + const handleClose = () => { |
| 60 | + setAnchorEl(null) |
| 61 | + } |
| 62 | + |
| 63 | + const setYourWorkspaces = () => { |
| 64 | + void form.setFieldValue("query", "owner:me") |
| 65 | + void form.submitForm() |
| 66 | + handleClose() |
| 67 | + } |
| 68 | + |
| 69 | + const setAllWorkspaces = () => { |
| 70 | + void form.setFieldValue("query", "") |
| 71 | + void form.submitForm() |
| 72 | + handleClose() |
| 73 | + } |
8 | 74 |
|
9 | 75 | return (
|
10 |
| - <> |
11 |
| - <WorkspacesPageView |
12 |
| - loading={workspacesState.hasTag("loading")} |
13 |
| - workspaces={workspacesState.context.workspaces} |
14 |
| - error={workspacesState.context.getWorkspacesError} |
15 |
| - /> |
16 |
| - </> |
| 76 | + <Margins> |
| 77 | + <Stack direction="row" className={styles.workspacesHeaderContainer}> |
| 78 | + <Stack direction="column" className={styles.filterColumn}> |
| 79 | + <Stack direction="row" spacing={0} className={styles.filterContainer}> |
| 80 | + <Button |
| 81 | + aria-controls="filter-menu" |
| 82 | + aria-haspopup="true" |
| 83 | + onClick={handleClick} |
| 84 | + className={styles.buttonRoot} |
| 85 | + > |
| 86 | + {Language.filterName} {anchorEl ? <CloseDropdown /> : <OpenDropdown />} |
| 87 | + </Button> |
| 88 | + |
| 89 | + <form onSubmit={form.handleSubmit} className={styles.filterForm}> |
| 90 | + <TextField |
| 91 | + {...getFieldHelpers("query")} |
| 92 | + className={styles.textFieldRoot} |
| 93 | + onChange={onChangeTrimmed(form)} |
| 94 | + fullWidth |
| 95 | + variant="outlined" |
| 96 | + InputProps={{ |
| 97 | + startAdornment: ( |
| 98 | + <InputAdornment position="start"> |
| 99 | + <SearchIcon fontSize="small" /> |
| 100 | + </InputAdornment> |
| 101 | + ), |
| 102 | + }} |
| 103 | + /> |
| 104 | + </form> |
| 105 | + |
| 106 | + <Menu |
| 107 | + id="filter-menu" |
| 108 | + anchorEl={anchorEl} |
| 109 | + keepMounted |
| 110 | + open={Boolean(anchorEl)} |
| 111 | + onClose={handleClose} |
| 112 | + TransitionComponent={Fade} |
| 113 | + anchorOrigin={{ |
| 114 | + vertical: "bottom", |
| 115 | + horizontal: "left", |
| 116 | + }} |
| 117 | + transformOrigin={{ |
| 118 | + vertical: "top", |
| 119 | + horizontal: "left", |
| 120 | + }} |
| 121 | + > |
| 122 | + <MenuItem onClick={setYourWorkspaces}>{Language.yourWorkspacesButton}</MenuItem> |
| 123 | + <MenuItem onClick={setAllWorkspaces}>{Language.allWorkspacesButton}</MenuItem> |
| 124 | + </Menu> |
| 125 | + </Stack> |
| 126 | + </Stack> |
| 127 | + |
| 128 | + <Link underline="none" component={RouterLink} to="/workspaces/new"> |
| 129 | + <Button startIcon={<AddCircleOutline />} style={{ height: "44px" }}> |
| 130 | + {Language.createWorkspaceButton} |
| 131 | + </Button> |
| 132 | + </Link> |
| 133 | + </Stack> |
| 134 | + <WorkspacesPageView loading={workspacesState.hasTag("loading")} workspaces={workspacesState.context.workspaces} /> |
| 135 | + </Margins> |
17 | 136 | )
|
18 | 137 | }
|
19 | 138 |
|
| 139 | +const useStyles = makeStyles((theme) => ({ |
| 140 | + workspacesHeaderContainer: { |
| 141 | + marginTop: theme.spacing(3), |
| 142 | + marginBottom: theme.spacing(3), |
| 143 | + justifyContent: "space-between", |
| 144 | + }, |
| 145 | + filterColumn: { |
| 146 | + width: "60%", |
| 147 | + cursor: "text", |
| 148 | + }, |
| 149 | + filterContainer: { |
| 150 | + border: `1px solid ${theme.palette.divider}`, |
| 151 | + borderRadius: "6px", |
| 152 | + }, |
| 153 | + filterForm: { |
| 154 | + width: "100%", |
| 155 | + }, |
| 156 | + buttonRoot: { |
| 157 | + border: "none", |
| 158 | + borderRight: `1px solid ${theme.palette.divider}`, |
| 159 | + borderRadius: "6px 0px 0px 6px", |
| 160 | + }, |
| 161 | + textFieldRoot: { |
| 162 | + margin: "0px", |
| 163 | + "& fieldset": { |
| 164 | + border: "none", |
| 165 | + }, |
| 166 | + }, |
| 167 | +})) |
| 168 | + |
20 | 169 | export default WorkspacesPage
|
0 commit comments