Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add initial loading states to the app #8

Merged
merged 2 commits into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
}
},
"rules": {
"object-shorthand": "error",
"import/order": [
"error",
{
Expand Down
10 changes: 8 additions & 2 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# .github/workflows/lint.yml

name: Lint # name of the action (displayed in the github interface)
name: Code health checks # name of the action (displayed in the github interface)

on: # event list
pull_request: # on a pull request to each of these branches
Expand All @@ -26,5 +26,11 @@ jobs: # list of things to do
- name: Install Dependencies
run: yarn install

- name: Type Checking
run: yarn typecheck

- name: Code Linting
run: yarn eslint:check
run: yarn eslint:check

- name: Code Formatting
run: yarn prettier:check
30 changes: 18 additions & 12 deletions src/components/calendar/CalendarCell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,29 +31,34 @@ const StyledCalendarDayCellButton = styled('button')(() => ({
'&:last-of-type': {
borderRight: '1px solid',
},
'&[data-prev-month="true"]': {
'&[data-prev-month="true"]:not([disabled])': {
cursor: 'w-resize',
},
'&[data-next-month="true"]': {
'&[data-next-month="true"]:not([disabled])': {
cursor: 'e-resize',
},
'&[data-prev-month="true"], &[data-next-month="true"]': {
backgroundColor: 'white',
'&:hover': {
'&:not([disabled]):hover': {
backgroundColor: '#f5f5f4',
},
},
'&[data-active="true"]': {
cursor: 'pointer',
backgroundColor: '#f5f5f4',
'&:hover': {
backgroundColor: '#e7e5e4',
'&:not([disabled])': {
cursor: 'pointer',
'&:hover': {
backgroundColor: '#e7e5e4',
},
},
},
'&[data-current="true"]': {
backgroundColor: '#e7e5e4',
'&:hover': {
backgroundColor: '#d6d3d1',
'&:not([disabled])': {
cursor: 'pointer',
'&:hover': {
backgroundColor: '#d6d3d1',
},
},
},
}));
Expand Down Expand Up @@ -87,7 +92,9 @@ export default function CalendarCell({
onClick,
rangeStatus,
}: Props) {
const { setCalendarEvents } = React.useContext(CalendarEventsContext);
const { removeCalendarEvent, fetchingCalendarEvents } = React.useContext(
CalendarEventsContext
);
const [active, setActive] = React.useState(false);
const [current, setCurrent] = React.useState(false);
const [eventIdBeingDeleted, setEventIdBeingDeleted] = React.useState<
Expand Down Expand Up @@ -129,9 +136,7 @@ export default function CalendarCell({

try {
await deleteCalendarEvent(calendarEventId);
setCalendarEvents((prevCalendarEvents) =>
prevCalendarEvents.filter((event) => event.id !== calendarEventId)
);
removeCalendarEvent(calendarEventId);
} catch (error) {
console.error(error);
} finally {
Expand All @@ -146,6 +151,7 @@ export default function CalendarCell({
data-next-month={rangeStatus === 'above-range'}
data-current={current}
onClick={handleClick}
disabled={fetchingCalendarEvents}
>
<StyledCalendarDayCellButtonHeader>
<Typography level="body-sm" fontWeight={current ? 900 : 400}>
Expand Down
28 changes: 28 additions & 0 deletions src/components/calendar/CalendarHeader.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { CalendarEventsContext } from '@context';
import { NavigateBefore, NavigateNext } from '@mui/icons-material';
import { Typography, styled, IconButton } from '@mui/joy';
import { AnimatePresence, motion } from 'framer-motion';
Expand All @@ -10,6 +11,7 @@ const StyledCalendarHeader = styled('div')(({ theme }) => ({
border: '1px solid',
borderRadius: theme.radius.sm,
alignItems: 'center',
position: 'relative',
}));

const StyledCalendarActiveMonthContainer = styled('div')({
Expand All @@ -30,6 +32,19 @@ const StyledIconButton = styled(IconButton)(({ theme }) => ({
},
}));

const StyledLoadingOverlay = styled(Typography)(() => ({
position: 'absolute',
top: 0,
left: 0,
right: 0,
bottom: 0,
margin: 'auto',
backgroundColor: 'rgba(255, 255, 255, 0.8)',
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
}));

type ButtonProps = {
disabled: boolean;
'aria-label': string;
Expand All @@ -52,6 +67,8 @@ export default function CalendarHeader({
onNavigateBack,
onNavigateForward,
}: Props) {
const { fetchingCalendarEvents } = React.useContext(CalendarEventsContext);

return (
<StyledCalendarHeader>
<StyledCalendarActiveMonthContainer>
Expand Down Expand Up @@ -84,6 +101,17 @@ export default function CalendarHeader({
</StyledIconButton>
</StyledCalendarNavigationContainer>
</StyledCalendarActiveMonthContainer>

{fetchingCalendarEvents && (
<StyledLoadingOverlay>
<motion.div
animate={{ opacity: [0, 1, 0] }}
transition={{ duration: 1.5, repeat: Infinity }}
>
Fetching calendar events, please wait...
</motion.div>
</StyledLoadingOverlay>
)}
</StyledCalendarHeader>
);
}
7 changes: 2 additions & 5 deletions src/components/calendar/DayHabitModalDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type Props = {

export default function DayHabitModalDialog({ open, onClose, date }: Props) {
const { habits } = React.useContext(HabitsContext);
const { setCalendarEvents } = React.useContext(CalendarEventsContext);
const { addCalendarEvent } = React.useContext(CalendarEventsContext);
const [submitting, setSubmitting] = React.useState(false);
const [selectedBadHabit, setSelectedBadHabit] = React.useState<number | null>(
null
Expand All @@ -49,10 +49,7 @@ export default function DayHabitModalDialog({ open, onClose, date }: Props) {
date,
selectedBadHabit as number
);
setCalendarEvents((prevCalendarEvents) => [
...prevCalendarEvents,
newCalendarEvent,
]);
addCalendarEvent(newCalendarEvent);
} catch (error) {
console.error(error);
} finally {
Expand Down
11 changes: 8 additions & 3 deletions src/components/habit/add-habit/AddHabitDialogButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,17 @@ import {
} from '@mui/joy';
import React, { FormEventHandler } from 'react';

export default function AddHabitDialogButton() {
type Props = {
disabled?: boolean;
};

export default function AddHabitDialogButton({ disabled = false }: Props) {
const [open, setOpen] = React.useState(false);
const [habitName, setHabitName] = React.useState('');
const [habitDescription, setHabitDescription] = React.useState('');
const [habitTrait, setHabitTrait] = React.useState<'good' | 'bad' | ''>('');
const [addingHabit, setAddingHabit] = React.useState(false);
const { setHabits } = React.useContext(HabitsContext);
const { addHabit } = React.useContext(HabitsContext);

const handleDialogOpen = () => {
setOpen(true);
Expand All @@ -48,7 +52,7 @@ export default function AddHabitDialogButton() {
setHabitName('');
setHabitDescription('');
setHabitTrait('');
setHabits((prevHabits) => [...prevHabits, newHabit]);
addHabit(newHabit);
} catch (error) {
console.error(error);
} finally {
Expand Down Expand Up @@ -80,6 +84,7 @@ export default function AddHabitDialogButton() {
variant="solid"
startDecorator={<AddRounded />}
onClick={handleDialogOpen}
disabled={disabled}
>
Add habit
</Button>
Expand Down
23 changes: 6 additions & 17 deletions src/components/habit/edit-habit/EditHabitDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@ export default function EditHabitDialog({
const [description, setDescription] = React.useState('');
const [trait, setTrait] = React.useState<'good' | 'bad' | ''>('');
const [isUpdating, setIsUpdating] = React.useState(false);
const { setHabits } = React.useContext(HabitsContext);
const { setCalendarEvents } = React.useContext(CalendarEventsContext);
const habitsContext = React.useContext(HabitsContext);
const { updateHabitInsideCalendarEvents } = React.useContext(
CalendarEventsContext
);

React.useEffect(() => {
setIsOpen(open);
Expand Down Expand Up @@ -84,21 +86,8 @@ export default function EditHabitDialog({
description,
trait: trait as 'good' | 'bad',
});
setHabits((prevHabits) =>
prevHabits.map((prevHabit) =>
prevHabit.id === habit.id ? updatedHabit : prevHabit
)
);
setCalendarEvents((prevCalendarEvents) =>
prevCalendarEvents.map((prevCalendarEvent) =>
prevCalendarEvent.habit.id === habit.id
? {
...prevCalendarEvent,
habit: updatedHabit,
}
: prevCalendarEvent
)
);
habitsContext.updateHabit(updatedHabit);
updateHabitInsideCalendarEvents(updatedHabit);
} catch (error) {
console.error(error);
} finally {
Expand Down
7 changes: 2 additions & 5 deletions src/components/habit/view-habit/HabitItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,14 @@ type HabitItemProps = {

export default function HabitItem({ habit, onEdit }: HabitItemProps) {
const [isBeingDeleted, setIsBeingDeleted] = React.useState(false);
const { setHabits } = React.useContext(HabitsContext);
const { removeHabit } = React.useContext(HabitsContext);

const handleDeleteHabit = async () => {
setIsBeingDeleted(true);

try {
await deleteHabit(habit.id);
await new Promise((resolve) => setTimeout(resolve, 1000));
setHabits((prevHabits) =>
prevHabits.filter((prevHabit) => prevHabit.id !== habit.id)
);
removeHabit(habit.id);
} catch (error) {
console.error(error);
} finally {
Expand Down
18 changes: 15 additions & 3 deletions src/components/habit/view-habit/ViewAllHabitsModalButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import ViewListRoundedIcon from '@mui/icons-material/ViewListRounded';
import {
Box,
Button,
CircularProgress,
DialogContent,
DialogTitle,
List,
Expand All @@ -27,7 +28,11 @@ const StyledPlaceholderContainer = styled(Box)(({ theme }) => ({
margin: `${theme.spacing(1)} auto 0`,
}));

export default function ViewAllHabitsModalButton() {
type Props = {
loading?: boolean;
};

export default function ViewAllHabitsModalButton({ loading = false }: Props) {
const { habits } = React.useContext(HabitsContext);
const [open, setOpen] = React.useState(false);
const [isEditingHabit, setIsEditingHabit] = React.useState(false);
Expand Down Expand Up @@ -56,10 +61,17 @@ export default function ViewAllHabitsModalButton() {
<Button
color="neutral"
variant="soft"
disabled={loading}
onClick={handleOpen}
startDecorator={<ViewListRoundedIcon />}
startDecorator={
loading ? (
<CircularProgress size="sm" variant="soft" color="neutral" />
) : (
<ViewListRoundedIcon />
)
}
>
View All Habits
{loading ? 'Fetching habits...' : 'View All Habits'}
</Button>
<Modal open={open} onClose={handleClose}>
<ModalDialog>
Expand Down
7 changes: 5 additions & 2 deletions src/components/header/Header.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { AddHabitDialogButton, ViewAllHabitsModalButton } from '@components';
import { HabitsContext } from '@context';
import { AccountCircleOutlined } from '@mui/icons-material';
import { IconButton } from '@mui/joy';
import { styled } from '@mui/joy/styles';
Expand Down Expand Up @@ -29,12 +30,14 @@ const StyledAppHeaderContent = styled('div')(() => ({
}));

export default function Header() {
const { fetchingHabits } = React.useContext(HabitsContext);

return (
<StyledAppHeader>
<StyledAppHeaderContent>
<StyledButtonsContainer>
<AddHabitDialogButton />
<ViewAllHabitsModalButton />
<AddHabitDialogButton disabled={fetchingHabits} />
<ViewAllHabitsModalButton loading={fetchingHabits} />
</StyledButtonsContainer>
<IconButton>
<AccountCircleOutlined />
Expand Down
Loading
Loading