Skip to content

Commit bd62d0a

Browse files
christianalfoniSaraVieira
authored andcommitted
SortOptions: refactored to overmind (codesandbox#3183)
1 parent eb6baf7 commit bd62d0a

File tree

4 files changed

+109
-93
lines changed

4 files changed

+109
-93
lines changed

packages/app/src/app/pages/Dashboard/Content/Sandboxes/Filters/SortOptions/Option.js renamed to packages/app/src/app/pages/Dashboard/Content/Sandboxes/Filters/SortOptions/Option.tsx

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,23 @@
11
import React from 'react';
22
import Check from 'react-icons/lib/md/check';
3+
34
import { IconContainer, OptionContainer } from './elements';
45

5-
export default ({ name, field, currentField, setField }) => {
6+
interface Props {
7+
name: string;
8+
field: string;
9+
currentField: string;
10+
setField: (field: string) => void;
11+
}
12+
13+
export const Option: React.FC<Props> = ({
14+
name,
15+
field,
16+
currentField,
17+
setField,
18+
}) => {
619
const selected = field === currentField;
20+
721
return (
822
<OptionContainer onClick={() => setField(field)} selected={selected}>
923
<IconContainer>{selected && <Check />}</IconContainer> {name}

packages/app/src/app/pages/Dashboard/Content/Sandboxes/Filters/SortOptions/elements.js renamed to packages/app/src/app/pages/Dashboard/Content/Sandboxes/Filters/SortOptions/elements.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
import styled, { css } from 'styled-components';
21
import ArrowDown from 'react-icons/lib/md/arrow-downward';
2+
import styled, { css } from 'styled-components';
33

4-
export const OptionContainer = styled.a`
4+
export const OptionContainer = styled.a<{
5+
selected: boolean;
6+
}>`
57
transition: 0.3s ease color;
68
display: flex;
79
vertical-align: middle;
@@ -33,7 +35,9 @@ export const IconContainer = styled.div`
3335
margin-right: 0.5rem;
3436
`;
3537

36-
export const Container = styled.div`
38+
export const Container = styled.div<{
39+
hideOrder: boolean;
40+
}>`
3741
transition: 0.3s ease opacity;
3842
color: rgba(255, 255, 255, 0.6);
3943
font-size: 0.875rem;

packages/app/src/app/pages/Dashboard/Content/Sandboxes/Filters/SortOptions/index.js

Lines changed: 0 additions & 89 deletions
This file was deleted.
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import { Overlay as OverlayComponent } from 'app/components/Overlay';
2+
import { useOvermind } from 'app/overmind';
3+
import React from 'react';
4+
5+
import { Arrow, Container, OrderName, OverlayContainer } from './elements';
6+
import { Option } from './Option';
7+
8+
const FIELD_TO_NAME = {
9+
updatedAt: 'Last Modified',
10+
insertedAt: 'Last Created',
11+
title: 'Name',
12+
};
13+
14+
interface Props {
15+
hideOrder: boolean;
16+
}
17+
18+
export const SortOptions: React.FC<Props> = ({ hideOrder }) => {
19+
const { state, actions } = useOvermind();
20+
21+
function toggleSort(e) {
22+
e.preventDefault();
23+
const { orderBy } = state.dashboard;
24+
const { orderByChanged } = actions.dashboard;
25+
orderByChanged({
26+
orderBy: {
27+
order: orderBy.order === 'asc' ? 'desc' : 'asc',
28+
field: orderBy.field,
29+
},
30+
});
31+
}
32+
33+
function setField(field: string) {
34+
const { orderBy } = state.dashboard;
35+
const { orderByChanged } = actions.dashboard;
36+
orderByChanged({
37+
orderBy: {
38+
order: orderBy.order,
39+
field,
40+
},
41+
});
42+
}
43+
44+
const { field, order } = state.dashboard.orderBy;
45+
46+
const Overlay = () => (
47+
<OverlayContainer>
48+
<Option
49+
setField={setField}
50+
currentField={field}
51+
field="title"
52+
name={FIELD_TO_NAME.title}
53+
/>
54+
<Option
55+
setField={setField}
56+
currentField={field}
57+
field="insertedAt"
58+
name={FIELD_TO_NAME.insertedAt}
59+
/>
60+
<Option
61+
setField={setField}
62+
currentField={field}
63+
field="updatedAt"
64+
name={FIELD_TO_NAME.updatedAt}
65+
/>
66+
</OverlayContainer>
67+
);
68+
69+
return (
70+
<OverlayComponent event="Dashboard - Order By" content={Overlay}>
71+
{open => (
72+
<Container hideOrder={hideOrder}>
73+
Sort by <OrderName onClick={open}>{FIELD_TO_NAME[field]} </OrderName>
74+
<Arrow
75+
onClick={toggleSort}
76+
style={{
77+
transform: `rotate(${order === 'asc' ? -180 : 0}deg)`,
78+
fontSize: '.875rem',
79+
marginLeft: 4,
80+
marginBottom: 2,
81+
}}
82+
/>
83+
</Container>
84+
)}
85+
</OverlayComponent>
86+
);
87+
};

0 commit comments

Comments
 (0)