mirror of
https://github.com/myfatemi04/wheelshare-frontend.git
synced 2025-04-21 11:20:17 -04:00
Add days of week picker and binary encoding
This commit is contained in:
parent
12c0f9d32f
commit
3fc96ca3d4
|
@ -1,4 +1,5 @@
|
||||||
import { useState } from 'react';
|
import { useState } from 'react';
|
||||||
|
import { green, lightgrey } from './colors';
|
||||||
import latlongdist, { R_miles } from './latlongdist';
|
import latlongdist, { R_miles } from './latlongdist';
|
||||||
import UIButton from './UIButton';
|
import UIButton from './UIButton';
|
||||||
import UIPlacesAutocomplete from './UIPlacesAutocomplete';
|
import UIPlacesAutocomplete from './UIPlacesAutocomplete';
|
||||||
|
@ -8,9 +9,6 @@ import usePlace from './usePlace';
|
||||||
import useThrottle from './useThrottle';
|
import useThrottle from './useThrottle';
|
||||||
import useToggle from './useToggle';
|
import useToggle from './useToggle';
|
||||||
|
|
||||||
const green = '#60f760';
|
|
||||||
const lightgrey = '#e0e0e0';
|
|
||||||
|
|
||||||
export type IEvent = {
|
export type IEvent = {
|
||||||
id: number;
|
id: number;
|
||||||
name: string;
|
name: string;
|
||||||
|
@ -18,6 +16,8 @@ export type IEvent = {
|
||||||
formattedAddress: string;
|
formattedAddress: string;
|
||||||
startTime: string;
|
startTime: string;
|
||||||
endTime: string;
|
endTime: string;
|
||||||
|
latitude: number;
|
||||||
|
longitude: number;
|
||||||
};
|
};
|
||||||
|
|
||||||
function formatStartAndEndTime(
|
function formatStartAndEndTime(
|
||||||
|
@ -211,8 +211,8 @@ function People({ event, placeId }: { event: IEvent; placeId: string }) {
|
||||||
// eslint-disable-next-line
|
// eslint-disable-next-line
|
||||||
const [people, setPeople] = useState(dummyPeopleData);
|
const [people, setPeople] = useState(dummyPeopleData);
|
||||||
const placeDetails = usePlace(placeId);
|
const placeDetails = usePlace(placeId);
|
||||||
const myLatitude = 10;
|
const locationLongitude = event.latitude;
|
||||||
const myLongitude = 10;
|
const locationLatitude = event.longitude;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div style={{ display: 'flex', flexDirection: 'column' }}>
|
<div style={{ display: 'flex', flexDirection: 'column' }}>
|
||||||
|
@ -220,28 +220,28 @@ function People({ event, placeId }: { event: IEvent; placeId: string }) {
|
||||||
{people.map(({ name, latitude, longitude, id }) => {
|
{people.map(({ name, latitude, longitude, id }) => {
|
||||||
let extraDistance = null;
|
let extraDistance = null;
|
||||||
if (placeDetails != null) {
|
if (placeDetails != null) {
|
||||||
const locationLatitude = placeDetails.latitude;
|
const myLatitude = placeDetails.latitude;
|
||||||
const locationLongitude = placeDetails.longitude;
|
const myLongitude = placeDetails.longitude;
|
||||||
const meToThem = latlongdist(
|
const meToThem = latlongdist(
|
||||||
latitude,
|
latitude,
|
||||||
longitude,
|
longitude,
|
||||||
myLatitude,
|
locationLongitude,
|
||||||
myLongitude,
|
locationLatitude,
|
||||||
R_miles
|
R_miles
|
||||||
);
|
);
|
||||||
const themToLocation = latlongdist(
|
const themToLocation = latlongdist(
|
||||||
latitude,
|
latitude,
|
||||||
longitude,
|
longitude,
|
||||||
locationLatitude,
|
myLatitude,
|
||||||
locationLongitude,
|
myLongitude,
|
||||||
R_miles
|
R_miles
|
||||||
);
|
);
|
||||||
const totalWithThem = meToThem + themToLocation;
|
const totalWithThem = meToThem + themToLocation;
|
||||||
const totalWithoutThem = latlongdist(
|
const totalWithoutThem = latlongdist(
|
||||||
|
locationLongitude,
|
||||||
|
locationLatitude,
|
||||||
myLatitude,
|
myLatitude,
|
||||||
myLongitude,
|
myLongitude,
|
||||||
locationLatitude,
|
|
||||||
locationLongitude,
|
|
||||||
R_miles
|
R_miles
|
||||||
);
|
);
|
||||||
extraDistance = totalWithThem - totalWithoutThem;
|
extraDistance = totalWithThem - totalWithoutThem;
|
||||||
|
|
|
@ -1,14 +1,81 @@
|
||||||
import { useCallback, useState } from 'react';
|
import { Dispatch, SetStateAction, useCallback, useState } from 'react';
|
||||||
import { post } from './api';
|
import { post } from './api';
|
||||||
|
import { toggleBit } from './bits';
|
||||||
|
import { green, lightgrey } from './colors';
|
||||||
import { IGroup } from './Group';
|
import { IGroup } from './Group';
|
||||||
import UIButton from './UIButton';
|
import UIButton from './UIButton';
|
||||||
import UIDatetimeInput from './UIDatetimeInput';
|
import UIDatetimeInput from './UIDatetimeInput';
|
||||||
import UIPlacesAutocomplete from './UIPlacesAutocomplete';
|
import UIPlacesAutocomplete from './UIPlacesAutocomplete';
|
||||||
import UISecondaryBox from './UISecondaryBox';
|
import UISecondaryBox from './UISecondaryBox';
|
||||||
import UITextInput from './UITextInput';
|
import UITextInput from './UITextInput';
|
||||||
|
import useToggle from './useToggle';
|
||||||
|
|
||||||
const noop = () => {};
|
const noop = () => {};
|
||||||
|
|
||||||
|
const DAY_NAMES = [
|
||||||
|
'Sunday',
|
||||||
|
'Monday',
|
||||||
|
'Tuesday',
|
||||||
|
'Wednesday',
|
||||||
|
'Thursday',
|
||||||
|
'Friday',
|
||||||
|
'Saturday',
|
||||||
|
];
|
||||||
|
|
||||||
|
function DaysOfWeekSelector({
|
||||||
|
daysOfWeek,
|
||||||
|
update,
|
||||||
|
}: {
|
||||||
|
daysOfWeek: number;
|
||||||
|
update: Dispatch<SetStateAction<number>>;
|
||||||
|
}) {
|
||||||
|
const toggleDayOfWeek = useCallback(
|
||||||
|
function (idx: 1 | 2 | 3 | 4 | 5 | 6 | 7) {
|
||||||
|
update((daysOfWeek) => toggleBit(daysOfWeek, idx));
|
||||||
|
},
|
||||||
|
[update]
|
||||||
|
);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div
|
||||||
|
style={{
|
||||||
|
display: 'flex',
|
||||||
|
flexDirection: 'row',
|
||||||
|
margin: '1rem auto',
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{DAY_NAMES.map((name, idx) => {
|
||||||
|
const mask = 0b1000_0000 >> (idx + 1);
|
||||||
|
const active = (daysOfWeek & mask) !== 0;
|
||||||
|
return (
|
||||||
|
<div
|
||||||
|
style={{
|
||||||
|
borderRadius: '100%',
|
||||||
|
cursor: 'pointer',
|
||||||
|
backgroundColor: active ? green : lightgrey,
|
||||||
|
color: active ? 'white' : 'black',
|
||||||
|
userSelect: 'none',
|
||||||
|
width: '2em',
|
||||||
|
height: '2em',
|
||||||
|
margin: '0.5rem',
|
||||||
|
display: 'flex',
|
||||||
|
flexDirection: 'row',
|
||||||
|
alignItems: 'center',
|
||||||
|
justifyContent: 'center',
|
||||||
|
}}
|
||||||
|
onClick={() =>
|
||||||
|
// @ts-ignore
|
||||||
|
toggleDayOfWeek(idx + 1)
|
||||||
|
}
|
||||||
|
>
|
||||||
|
{name.charAt(0)}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
})}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
export default function EventCreator({ group }: { group: IGroup }) {
|
export default function EventCreator({ group }: { group: IGroup }) {
|
||||||
const [name, setName] = useState('');
|
const [name, setName] = useState('');
|
||||||
const [startTime, setStartTime] = useState<Date | null>(null);
|
const [startTime, setStartTime] = useState<Date | null>(null);
|
||||||
|
@ -17,6 +84,9 @@ export default function EventCreator({ group }: { group: IGroup }) {
|
||||||
const [creating, setCreating] = useState(false);
|
const [creating, setCreating] = useState(false);
|
||||||
const [createdEventId, setCreatedEventId] = useState(-1);
|
const [createdEventId, setCreatedEventId] = useState(-1);
|
||||||
|
|
||||||
|
const [recurring, toggleRecurring] = useToggle(false);
|
||||||
|
const [daysOfWeek, setDaysOfWeek] = useState(0);
|
||||||
|
|
||||||
const buttonEnabled =
|
const buttonEnabled =
|
||||||
name.length > 0 &&
|
name.length > 0 &&
|
||||||
startTime != null &&
|
startTime != null &&
|
||||||
|
@ -63,6 +133,19 @@ export default function EventCreator({ group }: { group: IGroup }) {
|
||||||
setPlaceId(placeId);
|
setPlaceId(placeId);
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
|
<UIButton
|
||||||
|
onClick={toggleRecurring}
|
||||||
|
style={{
|
||||||
|
backgroundColor: recurring ? green : lightgrey,
|
||||||
|
color: recurring ? 'white' : 'black',
|
||||||
|
transition: 'color 0.2s, background-color 0.2s',
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
Recurring event
|
||||||
|
</UIButton>
|
||||||
|
{recurring && (
|
||||||
|
<DaysOfWeekSelector daysOfWeek={daysOfWeek} update={setDaysOfWeek} />
|
||||||
|
)}
|
||||||
{createdEventId === -1 ? (
|
{createdEventId === -1 ? (
|
||||||
<UIButton
|
<UIButton
|
||||||
onClick={buttonEnabled ? createEvent : noop}
|
onClick={buttonEnabled ? createEvent : noop}
|
||||||
|
|
23
src/components/NewUI/bits.ts
Normal file
23
src/components/NewUI/bits.ts
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
export function setBit(n: number, idx: number, active: boolean) {
|
||||||
|
if (idx < 1 || idx > 7 || !isFinite(idx)) {
|
||||||
|
throw new Error('invalid idx. idx must be from 1 - 7.');
|
||||||
|
}
|
||||||
|
|
||||||
|
const mask = 0b1000_0000 >> idx;
|
||||||
|
|
||||||
|
if (active) {
|
||||||
|
return n | mask;
|
||||||
|
} else {
|
||||||
|
return n & ~mask;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function toggleBit(n: number, idx: number) {
|
||||||
|
if (idx < 1 || idx > 7 || !isFinite(idx)) {
|
||||||
|
throw new Error('invalid idx. idx must be from 1 - 7.');
|
||||||
|
}
|
||||||
|
|
||||||
|
const mask = 0b1000_0000 >> idx;
|
||||||
|
|
||||||
|
return n ^ mask;
|
||||||
|
}
|
2
src/components/NewUI/colors.ts
Normal file
2
src/components/NewUI/colors.ts
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
export const green = '#60f760';
|
||||||
|
export const lightgrey = '#e0e0e0';
|
Loading…
Reference in New Issue
Block a user