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

377/ui categories from db #385

Merged
merged 3 commits into from
Nov 7, 2023
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
48 changes: 25 additions & 23 deletions client/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import * as React from 'react';
import { BrowserRouter } from 'react-router-dom';
import { Grid, ThemeProvider, StyledEngineProvider } from '@mui/material';

Expand All @@ -17,6 +16,7 @@ import { QueryClient, QueryClientProvider } from 'react-query';
import { ReactQueryDevtools } from 'react-query/devtools';

import './App.css';
import { SettingsProvider } from './providers/SettingsProvider';

const queryClient = new QueryClient();

Expand All @@ -28,29 +28,31 @@ function App(): JSX.Element {
<ReactQueryDevtools />
<ErrorBoundary fallback={GeneralError}>
<UserProvider>
<ModalProvider>
<BrowserRouter
getUserConfirmation={() => {
// intentionally left empty callback to block the default browser prompt.
}}
>
<Grid
className="App"
display="flex"
flexDirection="column"
height="100vh"
width="100vw"
justifyContent="space-between"
<SettingsProvider>
<ModalProvider>
<BrowserRouter
getUserConfirmation={() => {
// intentionally left empty callback to block the default browser prompt.
}}
>
<Header />
<ErrorBoundary fallback={NotFound}>
<Main />
</ErrorBoundary>
<Footer />
<Modal />
</Grid>
</BrowserRouter>
</ModalProvider>
<Grid
className="App"
display="flex"
flexDirection="column"
height="100vh"
width="100vw"
justifyContent="space-between"
>
<Header />
<ErrorBoundary fallback={NotFound}>
<Main />
</ErrorBoundary>
<Footer />
<Modal />
</Grid>
</BrowserRouter>
</ModalProvider>
</SettingsProvider>
</UserProvider>
</ErrorBoundary>
</QueryClientProvider>
Expand Down
18 changes: 9 additions & 9 deletions client/src/components/Users/Auth/SignUpCitizen/StepThree.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import React, { useState } from 'react';
import { useContext, useState } from 'react';
import { Box, Chip, Button, Grid, Typography } from '@mui/material';

import { interests } from './constants/interests';

import { useStyles } from './styles/styles';
import { SettingsContext } from '../../../../providers/SettingsProvider';

type TStepThreeProps = {
initData: { interests: string[] };
Expand All @@ -15,19 +14,20 @@ export default function StepThree({ initData, handleBack, handleNext }: TStepThr
const { classes } = useStyles();

const [formData, setFormData] = useState<{ interests: string[] }>(initData);
const { orgCategories } = useContext(SettingsContext);

const makeChips = () => {
return interests.map((interest) => {
// TODO: toggle chip style when interest is chosen
const isSelected = formData.interests.includes(interest);
console.log('orgCategories', orgCategories);
return orgCategories().map((interest) => {
const isSelected = formData.interests.includes(interest.name);
return (
<Chip
key={interest}
key={interest.id}
className={`${classes.chip} ${isSelected && classes.selectedChip}`}
label={interest}
label={interest.name}
sx={{ fontSize: '16px' }}
variant="outlined"
onClick={() => toggleInterest(interest)}
onClick={() => toggleInterest(interest.name)}
/>
);
});
Expand Down
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With this data now, being fetched from the backend, this is no longer needed.

This file was deleted.

25 changes: 0 additions & 25 deletions client/src/components/Users/Auth/SignUpCitizen/validation-rules.ts
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Our new implementations of form validation don't used this anymore

This file was deleted.

65 changes: 65 additions & 0 deletions client/src/providers/SettingsProvider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { ReactNode, createContext, useEffect, useRef } from 'react';
import { Category } from '../types';
import { APP_API_BASE_URL } from '../configs';

export interface Settings {
categories: () => Category[];
setCategories: (categories: Category[]) => void;
orgCategories: () => Category[];
}

interface Props {
children: ReactNode;
}

export const SettingsContext = createContext<Settings>({
categories: () => [],
setCategories: () => {},
orgCategories: () => [],
});

/**
* SettingsProvider will load the categories from the API and store them in memory.
*
* To re-load the categories, call fetchCategories().
* @param children
* @returns
*/
export const SettingsProvider = ({ children }: Props) => {
const _categoriesRef = useRef<Category[]>([]);

useEffect(() => {
const _fetchCategories = async () => {
await fetchCategories();
};

_fetchCategories().catch(() => console.log('Error initializing SettingsProvider'));
}, []);

const setCategories = (_categories: Category[]) => {
_categoriesRef.current = [..._categories];
};

const categories = () => [..._categoriesRef.current];

const orgCategories = () => {
return [..._categoriesRef.current.filter((category) => category.applies_to_organizations)];
};
const fetchCategories = async () => {
try {
const res = await fetch(`${APP_API_BASE_URL}/categories`);
const response = (await res.json()) satisfies Category[];
if (res.ok) {
_categoriesRef.current = [...response];
}
} catch (error) {
console.log('Error fetching Categorie', error);
}
};

return (
<SettingsContext.Provider value={{ categories, setCategories, orgCategories }}>
{children}
</SettingsContext.Provider>
);
};
Loading