Skip to content

Commit

Permalink
Merge pull request #219 from ita-social-projects/#186connectWithSWR
Browse files Browse the repository at this point in the history
#186connect with swr
  • Loading branch information
ddianaoo authored Sep 23, 2023
2 parents de24efa + cdcfab9 commit a1afb1b
Show file tree
Hide file tree
Showing 11 changed files with 139 additions and 61 deletions.
26 changes: 26 additions & 0 deletions FrontEnd/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions FrontEnd/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"react-router-dom": "^6.4.1",
"react-router-hash-link": "^2.4.3",
"react-scripts": "5.0.1",
"swr": "^2.2.2",
"validator": "^13.11.0",
"web-vitals": "^2.1.4"
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import css from './MultipleSelectChip.module.css';
import Box from '@mui/material/Box';
import Chip from '@mui/material/Chip';
import FormControl from '@mui/material/FormControl';
import InputLabel from '@mui/material/InputLabel';
import MenuItem from '@mui/material/MenuItem';
import OutlinedInput from '@mui/material/OutlinedInput';
import Select from '@mui/material/Select';
import { useTheme } from '@mui/material/styles';


const ITEM_HEIGHT = 48;
const ITEM_PADDING_TOP = 8;
const MenuProps = {
PaperProps: {
style: {
maxHeight: ITEM_HEIGHT * 4.5 + ITEM_PADDING_TOP,
width: 250,
},
},
};

function getStyles(name, selectedOptions, theme) {
return {
fontWeight:
selectedOptions.indexOf(name) === -1
? theme.typography.fontWeightRegular
: theme.typography.fontWeightMedium,
};
}

export default function OneSelectChip(props) {
const theme = useTheme();
return (
<div className={css['fields__column']}>
<div className={css['fields__label']}>
{props.requredField && <label className={css['fields__label--required']}>*</label>}
<label
className={`${css['fields__label--text']} ${!props.requredField && css['fields__field--notrequired']}`}
>
{props.label}
</label>
</div>
<FormControl sx={{ width: props.selectedWidth ? props.selectedWidth : 257}} size='small' >
<InputLabel id="demo-multiple-chip-label" className={css['choose__placeholder']}>{props.defaultValue}</InputLabel>
<Select
name={props.name}
labelId="demo-multiple-chip-label"
id="demo-multiple-chip"
value={props.value}
onChange={props.updateHandler}
input={<OutlinedInput id="select-multiple-chip" label={props.defaultValue} />}
renderValue={(selected) => (
<Box sx={{ display: 'flex', flexWrap: 'wrap', gap: 0.5 }}>
<Chip
label={selected}
sx={{color: '#1F9A7C', backgroundColor: 'white', border: '1px solid #1F9A7C'}}/>
</Box>
)}
MenuProps={MenuProps}
>
{props.options.map((option) => (
<MenuItem
key={option.key}
value={option.value}
style={{
...getStyles(option.value, props.value, theme),
fontFamily: 'Inter, sans-serif',
fontSize: '14px',
fontWeight: 400,
lineHeight: '20px',
letterSpacing: '-0.01em',
backgroundColor: props.value.includes(option.name) && '#A3AAB0',
}}
>
{option.value}
</MenuItem>
))}
</Select>
</FormControl>
{props.requredField &&
<div className={css['error-message']}>
{props.error}
</div>
}
</div>
);
}
51 changes: 19 additions & 32 deletions FrontEnd/src/components/ProfilePage/FormComponents/GeneralInfo.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import css from './FormComponents.module.css';
import { useState, useEffect } from 'react';
import useSWR from 'swr';

import CheckBoxField from './FormFields/CheckBoxField';
import FullField from './FormFields/FullField';
import HalfFormField from './FormFields/HalfFormField';
import ImageField from './FormFields/ImageField';
import MultipleSelectChip from './FormFields/MultipleSelectChip';
import OneSelectChip from './FormFields/OneSelectChip';
import TextField from './FormFields/TextField';

const LABELS = {
Expand Down Expand Up @@ -36,34 +38,6 @@ const CATEGORIES = [
{ name: "Спеції" },
];

const REGIONS = [
{ name: "Київська область" },
{ name: "Вінницька область" },
{ name: "Волинська область" },
{ name: "Дніпропетровська область" },
{ name: "Донецька область" },
{ name: "Житомирська область" },
{ name: "Закарпатська область" },
{ name: "Запорізька область" },
{ name: "Івано-Франківська область" },
{ name: "Кіровоградська область" },
{ name: "Крим" },
{ name: "Луганська область" },
{ name: "Львівська область" },
{ name: "Миколаївська область" },
{ name: "Одеська область" },
{ name: "Полтавська область" },
{ name: "Рівненська область" },
{ name: "Сумська область" },
{ name: "Тернопільська область" },
{ name: "Харківська область" },
{ name: "Херсонська область" },
{ name: "Хмельницька область" },
{ name: "Черкаська область" },
{ name: "Чернівецька область" },
{ name: "Чернігівська область" },
];

const ACTIVITIES = [
{ name: "Виробництво" },
{ name: "Роздрібна мережа" },
Expand All @@ -90,13 +64,17 @@ const ERRORS = {
const TEXT_AREA_MAX_LENGTH = 1000;
const IMAGE_SIZE = 50 * 1024 * 1024;

const fetcher = (...args) => fetch(...args).then(res => res.json());

const GeneralInfo = (props) => {
const [user, setUser] = useState(props.user);
const [formStateErr, setFormStateErr] = useState(ERRORS);
const [imageBannerError, setImageBannerError] = useState(null);
const [imageLogoError, setImageLogoError] = useState(null);
const [edrpouError, setEdrpouError] = useState(null);

const { data: fetchedRegions, error, isLoading } = useSWR('http://127.0.0.1:8000/api/regions/', fetcher);

useEffect(() => {
props.currentFormNameHandler(props.curForm);
}, []);
Expand Down Expand Up @@ -131,6 +109,13 @@ const GeneralInfo = (props) => {
});
};

const onUpdateOneSelectField = e => {
setUser((prevState) => {
const selectedRegion = fetchedRegions.find((el) => el.value === e.target.value);
return { ...prevState, [e.target.name]: selectedRegion.key };
});
};

const onUpdateEdrpouField = e => {
if (e.target.value && e.target.value.length !== 8) {
setEdrpouError('ЄДРПОУ має містити 8 символів');
Expand Down Expand Up @@ -248,15 +233,17 @@ const GeneralInfo = (props) => {
value={user.edrpou}
error={edrpouError}
/>
<MultipleSelectChip
{!isLoading &&
<OneSelectChip
name='regions'
options={REGIONS}
options={fetchedRegions}
label={LABELS.regions}
updateHandler={onUpdateSelectField}
updateHandler={onUpdateOneSelectField}
requredField={false}
value={user.regions}
defaultValue="Оберіть"
value={fetchedRegions.find((el) => el.key === user.regions)?.value ?? ''}
/>
}
</div>
<div className={css['fields-groups']}>
<MultipleSelectChip
Expand Down
3 changes: 2 additions & 1 deletion FrontEnd/src/components/ProfilePage/ProfilePage.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import ProfileContent from './ProfilePageComponents/ProfileContent';
import { useState } from 'react';
import BreadCrumbs from '../BreadCrumbs/BreadCrumbs';


const USER = {
'email': 'ex@gmail.com',
'companyEmail': '',
Expand All @@ -21,7 +22,7 @@ const USER = {
'edrpou': '',
'activities': [],
'categories': [],
'regions': [],
'regions': '',
'bannerImage': '',
'logo': '',
'slogan': '',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import css from './ProfileContent.module.css';
import { Link, NavLink, Route, Routes, Navigate } from 'react-router-dom';
import axios from 'axios';
import AdditionalInfo from '../FormComponents/AdditionalInfo';
import ContactsInfo from '../FormComponents/ContactsInfo';
import DeleteProfilePage from '../FormComponents/DeleteProfileComponent/DeleteProfilePage';
Expand Down Expand Up @@ -72,23 +71,12 @@ const ProfileContent = (props) => {

<Routes>
<Route path="/delete" element={<DeleteProfilePage user={props.user} currentFormNameHandler={props.currentFormNameHandler} curForm={FORM_NAMES[6]} />} />

{/* {INFOLINKS.map((el, index) => (
<Route
path={el.link}
Component={() => <el.element user={props.user} onUpdate={props.onUpdate} currentFormNameHandler={props.currentFormNameHandler} curForm={FORM_NAMES[index]}/>}
key={el.title}
/>
))} */}

<Route path="/user-info" element={<UserInfo user={props.user} onUpdate={props.onUpdate} currentFormNameHandler={props.currentFormNameHandler} curForm={FORM_NAMES[0]} />} />
<Route path="/general-info" element={<GeneralInfo user={props.user} onUpdate={props.onUpdate} currentFormNameHandler={props.currentFormNameHandler} curForm={FORM_NAMES[1]} />} />
<Route path="/contacts" element={<ContactsInfo user={props.user} onUpdate={props.onUpdate} currentFormNameHandler={props.currentFormNameHandler} curForm={FORM_NAMES[2]} />} />
<Route path="/products-service-info" element={<ProductServiceInfo user={props.user} onUpdate={props.onUpdate} currentFormNameHandler={props.currentFormNameHandler} curForm={FORM_NAMES[3]} />} />
<Route path="/additional-info" element={<AdditionalInfo user={props.user} onUpdate={props.onUpdate} currentFormNameHandler={props.currentFormNameHandler} curForm={FORM_NAMES[4]} />} />
<Route path="/startup" element={<StartupInfo user={props.user} onUpdate={props.onUpdate} currentFormNameHandler={props.currentFormNameHandler} curForm={FORM_NAMES[5]} />} />


</Routes>
</div>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { useEffect, useState } from "react";
import { useForm } from "react-hook-form";
import axios from 'axios';
import EyeInvisible from "../../../../authorization/EyeInvisible";
import EyeVisible from "../../../../authorization/EyeVisible";
import styles from "./SignUpFormContent.module.css";
Expand Down
1 change: 0 additions & 1 deletion FrontEnd/src/components/authorization/LoginContent.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { useForm } from "react-hook-form";
import { useState, useEffect} from "react";
import axios from 'axios';
import validator from "validator";
import EyeVisible from "./EyeVisible";
import EyeInvisible from "./EyeInvisible";
Expand Down
11 changes: 0 additions & 11 deletions FrontEnd/src/components/landing-page/companies/Companies.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,6 @@
import axios from 'axios';
import './Companies.css';
import {ProductCard} from "./companies-product-cards/CompaniesProductCards";

async function list_of_companies() {
const companiesList = axios.get('http://localhost:8000/api/profiles/')

.then(function (response) {
return response.data;
})
.catch(error => {console.log(error)});
return companiesList;
}
console.log(list_of_companies());

const MainCompanies = () => {
const companyDataList = [
Expand Down
4 changes: 2 additions & 2 deletions forum/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,13 @@
CORS_ALLOW_ALL_ORIGINS = False

CORS_ORIGIN_WHITELIST = [
'http://localhost',
'http://localhost:3000',
'http://127.0.0.1',
'http://0.0.0.0',
]

CORS_ALLOWED_ORIGINS = [
'http://localhost',
'http://localhost:3000',
'http://127.0.0.1',
'http://0.0.0.0',
]
Expand Down
2 changes: 1 addition & 1 deletion profiles/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,4 @@ def validate(self, attrs):
class RegionSerializer(serializers.Serializer):

def to_representation(self, obj):
return {obj[0]: obj[1]}
return {'key':obj[0], 'value': obj[1]}

0 comments on commit a1afb1b

Please sign in to comment.