Commit 08c07225 authored by Цой Данил's avatar Цой Данил 💬

Added stores for photos and users + added privateroute

parent 0917317f
......@@ -3,6 +3,7 @@
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<link href="https://fonts.googleapis.com/css?family=Kanit:100,100italic,200,200italic,300,300italic,regular,italic,500,500italic,600,600italic,700,700italic,800,800italic,900,900italic" rel="stylesheet" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite + React + TS</title>
</head>
......
#root {
max-width: 1280px;
margin: 0 auto;
padding: 2rem;
text-align: center;
}
.logo {
height: 6em;
padding: 1.5em;
will-change: filter;
transition: filter 300ms;
}
.logo:hover {
filter: drop-shadow(0 0 2em #646cffaa);
}
.logo.react:hover {
filter: drop-shadow(0 0 2em #61dafbaa);
}
@keyframes logo-spin {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
@media (prefers-reduced-motion: no-preference) {
a:nth-of-type(2) .logo {
animation: logo-spin infinite 20s linear;
}
}
.card {
padding: 2em;
}
.read-the-docs {
color: #888;
}
......@@ -25,45 +25,18 @@ a:hover {
body {
margin: 0;
display: flex;
place-items: center;
min-width: 320px;
font-family: 'Kanit', sans-serif;
background-position:center top;
min-height: 100vh;
height: 100%;
background: url('../src/assets/background.jpg');
}
h1 {
font-size: 3.2em;
line-height: 1.1;
button{
font-family: 'Kanit', sans-serif;
}
button {
border-radius: 8px;
border: 1px solid transparent;
padding: 0.6em 1.2em;
font-size: 1em;
font-weight: 500;
font-family: inherit;
background-color: #1a1a1a;
cursor: pointer;
transition: border-color 0.25s;
}
button:hover {
border-color: #646cff;
}
button:focus,
button:focus-visible {
outline: 4px auto -webkit-focus-ring-color;
}
@media (prefers-color-scheme: light) {
:root {
color: #213547;
background-color: #ffffff;
}
a:hover {
color: #747bff;
}
button {
background-color: #f9f9f9;
}
.active{
color: black !important;
border-bottom: 2px solid black;
}
\ No newline at end of file
import React from 'react'
import ReactDOM from 'react-dom/client'
import App from './App'
import './index.css'
import { Provider } from 'react-redux'
import { BrowserRouter } from 'react-router-dom'
import store from './store/store'
ReactDOM.createRoot(document.getElementById('root') as HTMLElement).render(
<React.StrictMode>
<Provider store={store}>
<BrowserRouter>
<App />
</React.StrictMode>,
</BrowserRouter>
</Provider>
)
import { createAsyncThunk } from "@reduxjs/toolkit"
import { AppDispatch, AppState } from "./store"
export const createAppAsyncThunk = createAsyncThunk.withTypes<{
state: AppState
dispatch: AppDispatch
rejectValue: string
}>()
\ No newline at end of file
import IPhoto from "../../interfaces/IPhoto";
import IUser from "../../interfaces/IUser";
export default interface IPhotosState{
photos: IPhoto[]
photosByUser: IPhoto[]
targetedUser: IUser
loadingPhotos: boolean
}
\ No newline at end of file
import { createSlice } from "@reduxjs/toolkit"
import { photosApi } from "../../api/photosApi"
import { createAppAsyncThunk } from "../createAppAsyncThunk"
import IPhotosState from "./IPhotosState"
import IPhoto from "../../interfaces/IPhoto"
import IUser from "../../interfaces/IUser"
const namespace: string = 'photos'
export const getAllPhotos = createAppAsyncThunk(
`${namespace}/getAllPhotos`,
async () => {
return await photosApi.getAllPhotos()
}
)
export const addPhoto = createAppAsyncThunk(
`${namespace}/addPhoto`,
async (photoDto: FormData) => {
return await photosApi.addPhoto(photoDto)
}
)
export const getPhotosByUserId = createAppAsyncThunk(
`${namespace}/getPhotosByUserId`,
async (id: string) => {
return await photosApi.getPhotosByUserId(id)
}
)
export const deletePhotoById = createAppAsyncThunk(
`${namespace}/deletePhotoById`,
async (id: string) => {
return await photosApi.deletePhotoById(id)
}
)
const initialState: IPhotosState = {
photos: [],
photosByUser: [],
targetedUser: {} as IUser,
loadingPhotos: false
}
export const photosSlice = createSlice({
name: namespace,
initialState: initialState,
reducers: {
setTargetedUser(state, action){
state.targetedUser = action.payload
}
},
extraReducers: (builder) => {
builder
.addCase(getAllPhotos.pending, (state) => {
state.loadingPhotos = true
})
.addCase(getAllPhotos.rejected, (state) => {
state.loadingPhotos = false
})
.addCase(getAllPhotos.fulfilled, (state, action) => {
state.loadingPhotos = false
if (action.payload.result) state.photos = action.payload.result
})
.addCase(getPhotosByUserId.pending, (state) => {
state.loadingPhotos = true
})
.addCase(getPhotosByUserId.rejected, (state) => {
state.loadingPhotos = false
})
.addCase(getPhotosByUserId.fulfilled, (state, action) => {
state.loadingPhotos = false
if (action.payload.result) state.photosByUser = action.payload.result
})
.addCase(addPhoto.pending, (state) => {
state.loadingPhotos = true
})
.addCase(addPhoto.rejected, (state) => {
state.loadingPhotos = false
})
.addCase(addPhoto.fulfilled, (state, action) => {
state.loadingPhotos = false
if (action.payload.result) state.photos.push(action.payload.result)
})
.addCase(deletePhotoById.pending, (state) => {
state.loadingPhotos = true
})
.addCase(deletePhotoById.rejected, (state) => {
state.loadingPhotos = false
})
.addCase(deletePhotoById.fulfilled, (state, action) => {
state.loadingPhotos = false
if (action.payload.result){
state.photosByUser = state.photosByUser.filter((photo: IPhoto) => {
return photo._id !== action.payload.result?._id
})
}
})
}
})
export const {setTargetedUser} = photosSlice.actions
\ No newline at end of file
import { configureStore, ThunkAction, Action } from "@reduxjs/toolkit"
import { useDispatch } from 'react-redux'
import { usersSlice } from "./user/user.slice";
import { photosSlice } from "./photos/photos.slice";
const localStorageMiddleware = ({ getState }: any) => {
return (next: any) => (action: any) => {
const result = next(action);
localStorage.setItem('applicationState', JSON.stringify(getState()));
return result;
};
};
const reHydrateStore = () => {
if (localStorage.getItem('applicationState') !== null) {
return JSON.parse(localStorage.getItem('applicationState') || '');
}
};
const makeStore = () => {
return configureStore({
reducer:{
users: usersSlice.reducer,
photos: photosSlice.reducer
},
preloadedState: reHydrateStore(),
middleware: (mw) => mw().concat(localStorageMiddleware)
})
}
const store = makeStore()
export type AppDispatch = typeof store.dispatch;
export type AppStore = ReturnType<typeof makeStore>;
export type AppState = ReturnType<AppStore["getState"]>;
export type AppThunk<ReturnType = void> = ThunkAction<
ReturnType,
AppState,
unknown,
Action
>;
export const useAppDispatch: () => AppDispatch = useDispatch;
export default store
\ No newline at end of file
import IUserGetDto from "../../interfaces/IUserGetDto";
export default interface IUsersState {
user: IUserGetDto | null
isAuth: boolean
loadingUser: boolean
messageUser: string
}
\ No newline at end of file
import { createSlice } from "@reduxjs/toolkit"
import { EStatuses } from "../../enum/EStatuses"
import IUser from "../../interfaces/IUser"
import { createAppAsyncThunk } from "../createAppAsyncThunk"
import IUserCreateDto from "../../interfaces/IUserCreateDto"
import { usersApi } from "../../api/usersApi"
import IUsersState from "./IUserState"
const namespace: string = 'users'
export const loginUser = createAppAsyncThunk(
`${namespace}/loginUser`,
async (user: IUserCreateDto) => {
return usersApi.loginUser(user)
}
)
export const createUser = createAppAsyncThunk(
`${namespace}/createUser`,
async (user: IUserCreateDto) => {
return await usersApi.createUser(user)
}
)
export const checkToken = createAppAsyncThunk(
`${namespace}/checkToken`,
async () => {
return usersApi.checkToken()
}
)
const initialState: IUsersState = {
user: {} as IUser,
isAuth: false,
loadingUser: false,
messageUser: ''
}
export const usersSlice = createSlice({
name: namespace,
initialState: initialState,
reducers: {
setToInitState(state){
state.isAuth = false
state.user = {} as IUser
state.loadingUser = false
state.messageUser = ''
},
hideMessage(state){
state.messageUser = ''
}
},
extraReducers: (builder) => {
builder
.addCase(loginUser.pending, (state) => {
state.loadingUser = true
})
.addCase(loginUser.rejected, (state) => {
state.loadingUser = false
})
.addCase(loginUser.fulfilled, (state, action) => {
state.loadingUser = false
const user = action.payload.result
state.user = user
if (action.payload.status === EStatuses.FAILURE){
state.messageUser = action.payload.message
}
if (user){
localStorage.setItem('token', user.token)
state.isAuth = true
}
})
.addCase(createUser.pending, (state) => {
state.loadingUser = true
})
.addCase(createUser.rejected, (state) => {
state.loadingUser = false
})
.addCase(createUser.fulfilled, (state, action) => {
state.loadingUser = false
const user = action.payload.result
state.user = user
if (action.payload.status === EStatuses.FAILURE){
state.messageUser = action.payload.message
}
if (user){
localStorage.setItem('token', user.token)
state.isAuth = true
}
})
.addCase(checkToken.pending, (state) => {
state.loadingUser = true
})
.addCase(checkToken.rejected, (state) => {
state.loadingUser = false
})
.addCase(checkToken.fulfilled, (state, action) => {
state.loadingUser = false
const user = action.payload.result
state.user = user
if (user) {
state.isAuth = true
} else {
state.isAuth = false
}
})
}
})
export const {setToInitState, hideMessage} = usersSlice.actions
\ No newline at end of file
import { useEffect } from "react"
import { shallowEqual, useSelector } from "react-redux"
import { Navigate, Outlet, useLocation } from "react-router-dom"
import { AppDispatch, AppState, useAppDispatch } from "../store/store"
import { checkToken } from "../store/user/user.slice"
const PrivateRoute: React.FunctionComponent = ():React.ReactElement => {
const {isAuth} = useSelector((state: AppState) => state.users, shallowEqual)
const location = useLocation()
const dispatch: AppDispatch = useAppDispatch()
useEffect(() => {
dispatch(checkToken())
}, [])
return(
isAuth
?
<Outlet/>
:
<Navigate to='/' replace state={{from: location}} />
)
}
export default PrivateRoute
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment