Skip to content

Commit

Permalink
client code changed
Browse files Browse the repository at this point in the history
  • Loading branch information
Apfirebolt committed Aug 6, 2022
1 parent db9a9b3 commit 4abc06d
Show file tree
Hide file tree
Showing 13 changed files with 172 additions and 569 deletions.
16 changes: 6 additions & 10 deletions client/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ import PrivateRoute from './components/PrivateRoute'
import Home from './pages/Home'
import Login from './pages/Login'
import Register from './pages/Register'
import NewTicket from './pages/NewTicket'
import Tickets from './pages/Tickets'
import Ticket from './pages/Ticket'
import NewPost from './pages/NewPost'
import Posts from './pages/Posts'

function App() {
return (
Expand All @@ -20,14 +19,11 @@ function App() {
<Route path='/' element={<Home />} />
<Route path='/login' element={<Login />} />
<Route path='/register' element={<Register />} />
<Route path='/new-ticket' element={<PrivateRoute />}>
<Route path='/new-ticket' element={<NewTicket />} />
<Route path='/new-post' element={<PrivateRoute />}>
<Route path='/new-post' element={<NewPost />} />
</Route>
<Route path='/tickets' element={<PrivateRoute />}>
<Route path='/tickets' element={<Tickets />} />
</Route>
<Route path='/ticket/:ticketId' element={<PrivateRoute />}>
<Route path='/ticket/:ticketId' element={<Ticket />} />
<Route path='/posts' element={<PrivateRoute />}>
<Route path='/posts' element={<Posts />} />
</Route>
</Routes>
</div>
Expand Down
6 changes: 2 additions & 4 deletions client/src/app/store.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { configureStore } from '@reduxjs/toolkit'
import authReducer from '../features/auth/authSlice'
import ticketReducer from '../features/tickets/ticketSlice'
import noteReducer from '../features/notes/noteSlice'
import blogReducer from '../features/blog/blogSlice'

export const store = configureStore({
reducer: {
auth: authReducer,
tickets: ticketReducer,
notes: noteReducer,
blog: blogReducer,
},
})
2 changes: 1 addition & 1 deletion client/src/components/Header.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function Header() {
return (
<header className='header'>
<div className='logo'>
<Link to='/'>Support Desk</Link>
<Link to='/'>Fast Blog</Link>
</div>
<ul>
{user ? (
Expand Down
36 changes: 36 additions & 0 deletions client/src/features/blog/blogService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import axios from 'axios'

const API_URL = 'http://localhost:8000/blog'

// Create new post
const createBlog = async (postData, token) => {
const config = {
headers: {
Authorization: `Bearer ${token}`,
},
}

const response = await axios.post(API_URL, postData, config)

return response.data
}

// Get user blog posts
const getPosts = async (token) => {
const config = {
headers: {
Authorization: `Bearer ${token}`,
},
}

const response = await axios.get(API_URL, config)
console.log('Response is ', response)
return response.data
}

const blogService = {
createBlog,
getPosts,
}

export default blogService
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
import { createSlice, createAsyncThunk } from '@reduxjs/toolkit'
import noteService from './noteService'
import blogService from './blogService'

const initialState = {
notes: [],
posts: [],
post: {},
isError: false,
isSuccess: false,
isLoading: false,
message: '',
}

// Get ticket notes
export const getNotes = createAsyncThunk(
'notes/getAll',
async (ticketId, thunkAPI) => {
// Create new blog
export const createPost = createAsyncThunk(
'blog/',
async (postData, thunkAPI) => {
try {
const token = thunkAPI.getState().auth.user.token
return await noteService.getNotes(ticketId, token)
return await blogService.createPost(postData, token)
} catch (error) {
const message =
(error.response &&
Expand All @@ -29,13 +30,13 @@ export const getNotes = createAsyncThunk(
}
)

// Create ticket note
export const createNote = createAsyncThunk(
'notes/create',
async ({ noteText, ticketId }, thunkAPI) => {
// Get user posts
export const getPosts = createAsyncThunk(
'blog/getAll',
async (_, thunkAPI) => {
try {
const token = thunkAPI.getState().auth.user.token
return await noteService.createNote(noteText, ticketId, token)
return await blogService.getPosts(token)
} catch (error) {
const message =
(error.response &&
Expand All @@ -49,42 +50,41 @@ export const createNote = createAsyncThunk(
}
)

export const noteSlice = createSlice({
name: 'note',
export const postSlice = createSlice({
name: 'post',
initialState,
reducers: {
reset: (state) => initialState,
},
extraReducers: (builder) => {
builder
.addCase(getNotes.pending, (state) => {
.addCase(createPost.pending, (state) => {
state.isLoading = true
})
.addCase(getNotes.fulfilled, (state, action) => {
state.isLoading = true
.addCase(createPost.fulfilled, (state) => {
state.isLoading = false
state.isSuccess = true
state.notes = action.payload
})
.addCase(getNotes.rejected, (state, action) => {
.addCase(createPost.rejected, (state, action) => {
state.isLoading = false
state.isError = true
state.message = action.payload
})
.addCase(createNote.pending, (state) => {
.addCase(getPosts.pending, (state) => {
state.isLoading = true
})
.addCase(createNote.fulfilled, (state, action) => {
.addCase(getPosts.fulfilled, (state, action) => {
state.isLoading = false
state.isSuccess = true
state.notes.push(action.payload)
state.posts = action.payload
})
.addCase(createNote.rejected, (state, action) => {
.addCase(getPosts.rejected, (state, action) => {
state.isLoading = false
state.isError = true
state.message = action.payload
})
},
})

export const { reset } = noteSlice.actions
export default noteSlice.reducer
export const { reset } = postSlice.actions
export default postSlice.reducer
42 changes: 0 additions & 42 deletions client/src/features/notes/noteService.js

This file was deleted.

68 changes: 0 additions & 68 deletions client/src/features/tickets/ticketService.js

This file was deleted.

Loading

0 comments on commit 4abc06d

Please sign in to comment.