Merge branch 'dev' into main

This commit is contained in:
Ben Grant 2021-03-11 01:33:22 +11:00
commit 9a34d5c6eb
18 changed files with 336 additions and 47 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View file

@ -6,12 +6,16 @@
@font-face { @font-face {
font-family: 'Samurai Bob'; font-family: 'Samurai Bob';
src: url('fonts/samuraibob.ttf') format('truetype'); src: url('fonts/samuraibob.woff2') format('woff2'),
url('fonts/samuraibob.woff') format('woff');
font-weight: 400; font-weight: 400;
font-style: normal;
} }
@font-face { @font-face {
font-family: 'Molot'; font-family: 'Molot';
src: url('fonts/molot.otf') format('opentype'); src: url('fonts/molot.woff2') format('woff2'),
url('fonts/molot.woff') format('woff');
font-weight: 400; font-weight: 400;
font-style: normal;
} }

View file

@ -6,6 +6,7 @@ import {
} from 'react-router-dom'; } from 'react-router-dom';
import { ThemeProvider, Global } from '@emotion/react'; import { ThemeProvider, Global } from '@emotion/react';
import { Settings } from 'components';
import { import {
Home, Home,
Event, Event,
@ -22,7 +23,7 @@ const App = () => {
return ( return (
<BrowserRouter> <BrowserRouter>
<ThemeProvider theme={theme[isDark ? 'dark' : 'light']}> <ThemeProvider theme={theme[isDark ? 'dark' : 'light']}>
{process.env.NODE_ENV !== 'production' && <button onClick={() => setIsDark(!isDark)} style={{ position: 'absolute', top: 0, left: 0 }}>{isDark ? 'dark' : 'light'}</button>} {process.env.NODE_ENV !== 'production' && <button onClick={() => setIsDark(!isDark)} style={{ position: 'absolute', top: 0, left: 0, zIndex: 1000 }}>{isDark ? 'dark' : 'light'}</button>}
<Global <Global
styles={theme => ({ styles={theme => ({
html: { html: {
@ -63,6 +64,8 @@ const App = () => {
<Route path="/" component={Home} exact /> <Route path="/" component={Home} exact />
<Route path="/:id" component={Event} exact /> <Route path="/:id" component={Event} exact />
</Switch> </Switch>
<Settings />
</ThemeProvider> </ThemeProvider>
</BrowserRouter> </BrowserRouter>
); );

View file

@ -2,8 +2,11 @@ import { useState, useEffect, useRef } from 'react';
import dayjs from 'dayjs'; import dayjs from 'dayjs';
import isToday from 'dayjs/plugin/isToday'; import isToday from 'dayjs/plugin/isToday';
import localeData from 'dayjs/plugin/localeData'; import localeData from 'dayjs/plugin/localeData';
import updateLocale from 'dayjs/plugin/updateLocale';
import { Button } from 'components'; import { Button } from 'components';
import { useSettingsStore } from 'stores';
import { import {
Wrapper, Wrapper,
StyledLabel, StyledLabel,
@ -17,12 +20,13 @@ import {
dayjs.extend(isToday); dayjs.extend(isToday);
dayjs.extend(localeData); dayjs.extend(localeData);
dayjs.extend(updateLocale);
const calculateMonth = (month, year) => { const calculateMonth = (month, year, weekStart) => {
const date = dayjs().month(month).year(year); const date = dayjs().month(month).year(year);
const daysInMonth = date.daysInMonth(); const daysInMonth = date.daysInMonth();
const daysBefore = date.date(1).day(); const daysBefore = date.date(1).day() - weekStart;
const daysAfter = 6 - date.date(daysInMonth).day(); const daysAfter = 6 - date.date(daysInMonth).day() + weekStart;
let dates = []; let dates = [];
let curDate = date.date(1).subtract(daysBefore, 'day'); let curDate = date.date(1).subtract(daysBefore, 'day');
@ -49,7 +53,9 @@ const CalendarField = ({
register, register,
...props ...props
}) => { }) => {
const [dates, setDates] = useState(calculateMonth(dayjs().month(), dayjs().year())); const weekStart = useSettingsStore(state => state.weekStart);
const [dates, setDates] = useState(calculateMonth(dayjs().month(), dayjs().year(), weekStart));
const [month, setMonth] = useState(dayjs().month()); const [month, setMonth] = useState(dayjs().month());
const [year, setYear] = useState(dayjs().year()); const [year, setYear] = useState(dayjs().year());
@ -70,8 +76,14 @@ const CalendarField = ({
}; };
useEffect(() => { useEffect(() => {
setDates(calculateMonth(month, year)); if (weekStart !== dayjs.Ls.en.weekStart) {
}, [month, year]); dayjs.updateLocale('en', {
weekStart: weekStart,
weekdaysShort: weekStart ? 'Mon_Tue_Wed_Thu_Fri_Sat_Sun'.split('_') : 'Sun_Mon_Tue_Wed_Thu_Fri_Sat'.split('_'),
});
}
setDates(calculateMonth(month, year, weekStart));
}, [weekStart, month, year]);
return ( return (
<Wrapper> <Wrapper>

View file

@ -0,0 +1,57 @@
import { useState } from 'react';
import { useTheme } from '@emotion/react';
import { ToggleField } from 'components';
import { useSettingsStore } from 'stores';
import {
OpenButton,
Modal,
Heading,
Cover,
} from './settingsStyle';
const Settings = () => {
const theme = useTheme();
const store = useSettingsStore();
const [isOpen, setIsOpen] = useState(false);
return (
<>
<OpenButton
isOpen={isOpen}
tabIndex="1"
type="button"
onClick={() => setIsOpen(!isOpen)} title="Options"
>
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke={theme.text} strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><circle cx="12" cy="12" r="3"></circle><path d="M19.4 15a1.65 1.65 0 0 0 .33 1.82l.06.06a2 2 0 0 1 0 2.83 2 2 0 0 1-2.83 0l-.06-.06a1.65 1.65 0 0 0-1.82-.33 1.65 1.65 0 0 0-1 1.51V21a2 2 0 0 1-2 2 2 2 0 0 1-2-2v-.09A1.65 1.65 0 0 0 9 19.4a1.65 1.65 0 0 0-1.82.33l-.06.06a2 2 0 0 1-2.83 0 2 2 0 0 1 0-2.83l.06-.06a1.65 1.65 0 0 0 .33-1.82 1.65 1.65 0 0 0-1.51-1H3a2 2 0 0 1-2-2 2 2 0 0 1 2-2h.09A1.65 1.65 0 0 0 4.6 9a1.65 1.65 0 0 0-.33-1.82l-.06-.06a2 2 0 0 1 0-2.83 2 2 0 0 1 2.83 0l.06.06a1.65 1.65 0 0 0 1.82.33H9a1.65 1.65 0 0 0 1-1.51V3a2 2 0 0 1 2-2 2 2 0 0 1 2 2v.09a1.65 1.65 0 0 0 1 1.51 1.65 1.65 0 0 0 1.82-.33l.06-.06a2 2 0 0 1 2.83 0 2 2 0 0 1 0 2.83l-.06.06a1.65 1.65 0 0 0-.33 1.82V9a1.65 1.65 0 0 0 1.51 1H21a2 2 0 0 1 2 2 2 2 0 0 1-2 2h-.09a1.65 1.65 0 0 0-1.51 1z"></path></svg>
</OpenButton>
<Cover isOpen={isOpen} onClick={() => setIsOpen(false)} />
<Modal isOpen={isOpen}>
<Heading>Options</Heading>
<ToggleField
label="Week starts on"
name="weekStart"
id="weekStart"
options={['Sunday', 'Monday']}
value={store.weekStart === 1 ? 'Monday' : 'Sunday'}
onChange={value => store.setWeekStart(value === 'Monday' ? 1 : 0)}
/>
<ToggleField
label="Time format"
name="timeFormat"
id="timeFormat"
options={['12h', '24h']}
value={store.timeFormat}
onChange={value => store.setTimeFormat(value)}
/>
</Modal>
</>
);
};
export default Settings;

View file

@ -0,0 +1,79 @@
import styled from '@emotion/styled';
export const OpenButton = styled.button`
border: 0;
background: none;
height: 50px;
width: 50px;
cursor: pointer;
color: inherit;
display: flex;
align-items: center;
justify-content: center;
position: absolute;
top: 12px;
right: 12px;
z-index: 200;
border-radius: 100%;
transition: background-color .15s;
transition: transform .15s;
padding: 0;
&:focus {
outline: 0;
}
&:focus-visible {
background-color: ${props => props.theme.text}22;
}
${props => props.isOpen && `
transform: rotate(-45deg);
`}
`;
export const Cover = styled.div`
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
z-index: 100;
display: none;
${props => props.isOpen && `
display: block;
`}
`;
export const Modal = styled.div`
position: absolute;
top: 70px;
right: 12px;
background-color: ${props => props.theme.background};
border: 1px solid ${props => props.theme.primaryBackground};
z-index: 150;
padding: 10px 18px;
border-radius: 3px;
width: 250px;
box-sizing: border-box;
max-width: calc(100% - 20px);
box-shadow: 0 3px 6px 0 rgba(0,0,0,.3);
pointer-events: none;
opacity: 0;
transform: translateY(-10px);
transition: opacity .15s, transform .15s;
${props => props.isOpen && `
pointer-events: all;
opacity: 1;
transform: translateY(0);
`}
`;
export const Heading = styled.span`
font-size: 1.5rem;
display: block;
margin: 6px 0;
line-height: 1em;
`;

View file

@ -1,5 +1,7 @@
import { useState, useEffect, useRef } from 'react'; import { useState, useEffect, useRef } from 'react';
import { useSettingsStore } from 'stores';
import { import {
Wrapper, Wrapper,
StyledLabel, StyledLabel,
@ -9,7 +11,8 @@ import {
Selected, Selected,
} from './timeRangeFieldStyle'; } from './timeRangeFieldStyle';
const times = [ const times = {
'12h': [
'12am', '12am',
'1am', '1am',
'2am', '2am',
@ -35,7 +38,35 @@ const times = [
'10pm', '10pm',
'11pm', '11pm',
'12am', '12am',
]; ],
'24h': [
'00',
'01',
'02',
'03',
'04',
'05',
'06',
'07',
'08',
'09',
'10',
'11',
'12',
'13',
'14',
'15',
'16',
'17',
'18',
'19',
'20',
'21',
'22',
'23',
'0',
],
};
const TimeRangeField = ({ const TimeRangeField = ({
label, label,
@ -44,6 +75,8 @@ const TimeRangeField = ({
register, register,
...props ...props
}) => { }) => {
const timeFormat = useSettingsStore(state => state.timeFormat);
const [start, setStart] = useState(9); const [start, setStart] = useState(9);
const [end, setEnd] = useState(17); const [end, setEnd] = useState(17);
@ -90,7 +123,7 @@ const TimeRangeField = ({
{start > end && <Selected start={start > end ? 0 : start} end={end} />} {start > end && <Selected start={start > end ? 0 : start} end={end} />}
<Handle <Handle
value={start} value={start}
label={times[start]} label={times[timeFormat][start]}
onMouseDown={() => { onMouseDown={() => {
document.addEventListener('mousemove', handleMouseMove); document.addEventListener('mousemove', handleMouseMove);
isStartMoving.current = true; isStartMoving.current = true;
@ -112,7 +145,7 @@ const TimeRangeField = ({
/> />
<Handle <Handle
value={end} value={end}
label={times[end]} label={times[timeFormat][end]}
onMouseDown={() => { onMouseDown={() => {
document.addEventListener('mousemove', handleMouseMove); document.addEventListener('mousemove', handleMouseMove);
isEndMoving.current = true; isEndMoving.current = true;

View file

@ -0,0 +1,40 @@
import {
Wrapper,
ToggleContainer,
StyledLabel,
Option,
HiddenInput,
LabelButton,
} from './toggleFieldStyle';
const ToggleField = ({
label,
id,
name,
options = [],
value,
onChange,
...props
}) => (
<Wrapper>
{label && <StyledLabel>{label}</StyledLabel>}
<ToggleContainer>
{options.map(option =>
<Option key={option}>
<HiddenInput
type="radio"
name={name}
value={option}
id={`${name}-${option}`}
checked={value === option}
onChange={() => onChange(option)}
/>
<LabelButton htmlFor={`${name}-${option}`}>{option}</LabelButton>
</Option>
)}
</ToggleContainer>
</Wrapper>
);
export default ToggleField;

View file

@ -0,0 +1,43 @@
import styled from '@emotion/styled';
export const Wrapper = styled.div`
margin: 10px 0;
`;
export const ToggleContainer = styled.div`
display: flex;
border: 1px solid ${props => props.theme.primary};
border-radius: 3px;
overflow: hidden;
`;
export const StyledLabel = styled.label`
display: block;
padding-bottom: 4px;
font-size: .9rem;
`;
export const Option = styled.div`
flex: 1;
`;
export const HiddenInput = styled.input`
height: 0;
width: 0;
position: absolute;
right: -1000px;
top: 0;
&:checked + label {
color: ${props => props.theme.background};
background-color: ${props => props.theme.primary};
}
`;
export const LabelButton = styled.label`
padding: 6px;
display: block;
text-align: center;
cursor: pointer;
user-select: none;
`;

View file

@ -2,6 +2,7 @@ export { default as TextField } from './TextField/TextField';
export { default as SelectField } from './SelectField/SelectField'; export { default as SelectField } from './SelectField/SelectField';
export { default as CalendarField } from './CalendarField/CalendarField'; export { default as CalendarField } from './CalendarField/CalendarField';
export { default as TimeRangeField } from './TimeRangeField/TimeRangeField'; export { default as TimeRangeField } from './TimeRangeField/TimeRangeField';
export { default as ToggleField } from './ToggleField/ToggleField';
export { default as Button } from './Button/Button'; export { default as Button } from './Button/Button';
export { default as Legend } from './Legend/Legend'; export { default as Legend } from './Legend/Legend';
@ -11,3 +12,4 @@ export { default as Error } from './Error/Error';
export { default as Center } from './Center/Center'; export { default as Center } from './Center/Center';
export { default as Donate } from './Donate/Donate'; export { default as Donate } from './Donate/Donate';
export { default as Settings } from './Settings/Settings';

View file

@ -34,6 +34,7 @@ import {
} from './eventStyle'; } from './eventStyle';
import api from 'services'; import api from 'services';
import { useSettingsStore } from 'stores';
import logo from 'res/logo.svg'; import logo from 'res/logo.svg';
import timezones from 'res/timezones.json'; import timezones from 'res/timezones.json';
@ -43,6 +44,8 @@ dayjs.extend(timezone);
dayjs.extend(customParseFormat); dayjs.extend(customParseFormat);
const Event = (props) => { const Event = (props) => {
const timeFormat = useSettingsStore(state => state.timeFormat);
const { register, handleSubmit } = useForm(); const { register, handleSubmit } = useForm();
const { id } = props.match.params; const { id } = props.match.params;
const [timezone, setTimezone] = useState(Intl.DateTimeFormat().resolvedOptions().timeZone); const [timezone, setTimezone] = useState(Intl.DateTimeFormat().resolvedOptions().timeZone);
@ -152,17 +155,17 @@ const Event = (props) => {
if (allTimes.length - 1 === i) return [ if (allTimes.length - 1 === i) return [
...labels, ...labels,
{ label: '', time }, { label: '', time },
{ label: dayjs(time, 'HHmm').add(1, 'hour').format('h A'), time: null } { label: dayjs(time, 'HHmm').add(1, 'hour').format(timeFormat === '12h' ? 'h A' : 'HH'), time: null }
]; ];
if (allTimes.length - 1 > i && parseInt(allTimes[i+1].substring(0, 2))-1 > parseInt(time.substring(0, 2))) return [ if (allTimes.length - 1 > i && parseInt(allTimes[i+1].substring(0, 2))-1 > parseInt(time.substring(0, 2))) return [
...labels, ...labels,
{ label: '', time }, { label: '', time },
{ label: dayjs(time, 'HHmm').add(1, 'hour').format('h A'), time: 'space' }, { label: dayjs(time, 'HHmm').add(1, 'hour').format(timeFormat === '12h' ? 'h A' : 'HH'), time: 'space' },
{ label: '', time: 'space' }, { label: '', time: 'space' },
{ label: '', time: 'space' }, { label: '', time: 'space' },
]; ];
if (time.substring(2) !== '00') return [...labels, { label: '', time }]; if (time.substring(2) !== '00') return [...labels, { label: '', time }];
return [...labels, { label: dayjs(time, 'HHmm').format('h A'), time }]; return [...labels, { label: dayjs(time, 'HHmm').format(timeFormat === '12h' ? 'h A' : 'HH'), time }];
}, [])); }, []));
setDates(times.reduce((allDates, time) => { setDates(times.reduce((allDates, time) => {
@ -172,7 +175,7 @@ const Event = (props) => {
return [...allDates, date]; return [...allDates, date];
}, [])); }, []));
} }
}, [times]); }, [times, timeFormat]);
useEffect(() => { useEffect(() => {
const fetchUser = async () => { const fetchUser = async () => {

View file

@ -136,7 +136,7 @@ const Home = () => {
<Center> <Center>
<Logo src={logo} alt="" /> <Logo src={logo} alt="" />
</Center> </Center>
<TitleSmall>Create a</TitleSmall> <TitleSmall>CREATE A</TitleSmall>
<TitleLarge>CRAB FIT</TitleLarge> <TitleLarge>CRAB FIT</TitleLarge>
<Links> <Links>
<a href="#about">About</a> / <a href="#donate">Donate</a> <a href="#about">About</a> / <a href="#donate">Donate</a>

View file

@ -0,0 +1,13 @@
import create from 'zustand';
import { persist } from 'zustand/middleware';
export const useSettingsStore = create(persist(
set => ({
weekStart: 0,
timeFormat: '12h',
setWeekStart: weekStart => set({ weekStart }),
setTimeFormat: timeFormat => set({ timeFormat }),
}),
{ name: 'crabfit-settings' },
));