Skip to content

Commit

Permalink
Merge pull request #243 from Boye95/dev
Browse files Browse the repository at this point in the history
refactor: populate url.js with urls & use in all API slices
  • Loading branch information
Boye95 committed Jul 30, 2023
2 parents a9b5eda + 556fdff commit 762f3dc
Show file tree
Hide file tree
Showing 8 changed files with 166 additions and 53 deletions.
4 changes: 3 additions & 1 deletion src/app/constants/api.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { fetchBaseQuery } from '@reduxjs/toolkit/query/react';

import url from '../url';

// set the base query
export const baseQuery = fetchBaseQuery({
baseUrl: 'https://api.thealtcamp.com',
baseUrl: url.BASE_URL,
prepareHeaders: (headers, { getState }) => {
const token = getState().user.user.token;
// console.log(token)
Expand Down
33 changes: 17 additions & 16 deletions src/app/slices/apiSlices/accountSlice.js
Original file line number Diff line number Diff line change
@@ -1,68 +1,69 @@
import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react';
import { baseQuery } from '../../constants/api';

import url from '../../url';

export const accountSlice = createApi({
reducerPath: 'accountApi',
baseQuery,
// tagTypes:["Auth"],
endpoints: (builder) => ({
updateProfilePicture: builder.mutation({
query: (body) => ({
url: '/accounts/profile-picture',
url: url.UPDATE_PROFILE_PICTURE_URL,
method: 'PUT',
body,
}),
}),
updateBio: builder.mutation({
query: (body) => ({
url: '/accounts/bio',
url: url.UPDATE_BIO_URL,
method: 'PUT',
body,
}),
}),
getAllAccounts: builder.query({
query: ({ accountType, searchTerm, page, limit = 16 }) => ({
url:
searchTerm && accountType
? `/accounts?category=${accountType}&searchTerm=${searchTerm}&isPaginated=true&page=${page}&limit=${limit}`
: !accountType && searchTerm
? `/accounts?searchTerm=${searchTerm}&isPaginated=true&page=${page}&limit=${limit}`
: accountType && !searchTerm
? `/accounts?category=${accountType}&isPaginated=true&page=${page}&limit=${limit}`
: `/accounts?&isPaginated=true&page=${page}&limit=${limit}`,
// url: `/accounts?category=${accountType}&searchTerm=${searchTerm}&isPaginated=true&page=${page}&limit=${limit}`,
url: url.GET_ALL_ACCOUNTS_URL(
url,
accountType,
searchTerm,
true,
page,
limit
),
method: 'GET',
}),
}),
getSearchedAccounts: builder.query({
query: (searchTerm) => ({
url: `/accounts?searchTerm=${searchTerm}&isPaginated=true`,
url: url.GET_SEARCHED_ACCOUNTS_URL(url, searchTerm, true),
method: 'GET',
}),
}),
getAccountById: builder.query({
query: (accountId) => ({
url: `/accounts/${accountId}`,
url: url.GET_ACCOUNT_BY_ID_URL(url, accountId),
method: 'GET',
}),
}),
updateDetails: builder.mutation({
query: (body) => ({
url: '/accounts',
url: url.UPDATE_DETAILS_URL,
method: 'PUT',
body,
}),
}),
startVerifyEmail: builder.mutation({
query: (body) => ({
url: '/auth/start-email-verification',
url: url.START_VERIFY_EMAIL_URL,
method: 'POST',
body,
}),
}),
verifyEmail: builder.mutation({
query: (body) => ({
url: 'auth/verify-email',
url: url.VERIFY_EMAIL_URL,
method: 'POST',
body,
}),
Expand Down
8 changes: 5 additions & 3 deletions src/app/slices/apiSlices/authSlice.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react';

import url from '../../url';

export const authSlice = createApi({
reducerPath: 'authApi',
baseQuery: fetchBaseQuery({ baseUrl: 'https://api.thealtcamp.com' }),
baseQuery: fetchBaseQuery({ baseUrl: url.BASE_URL }),
endpoints: (builder) => ({
login: builder.mutation({
query: (body) => ({
url: '/auth/login',
url: url.LOGIN_URL,
method: 'POST',
body,
}),
}),
register: builder.mutation({
query: (body) => ({
url: '/auth/register',
url: url.REGISTER_URL,
method: 'POST',
body,
}),
Expand Down
12 changes: 7 additions & 5 deletions src/app/slices/apiSlices/bookmarkSlice.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,44 @@
import { createApi } from '@reduxjs/toolkit/query/react';
import { baseQuery } from '../../constants/api';

import url from '../../url';

export const bookmarkSlice = createApi({
reducerPath: 'bookmarkApi',
baseQuery,
tagTypes: ['Bookmark'],
endpoints: (builder) => ({
getAllBookmarks: builder.query({
query: ({ page, limit = 10 }) => ({
url: `/bookmarks?isPaginated=true&page=${page}&limit=${limit}`,
url: url.GET_ALL_BOOKMARKS_URL(url, page, true, limit),
method: 'GET',
}),
providesTags: ['Bookmark'],
}),
getBookmarkById: builder.query({
query: (bookmarkId) => ({
url: `/bookmarks/${bookmarkId}`,
url: url.GET_BOOKMARK_BY_ID_URL(url, bookmarkId),
method: 'GET',
}),
}),
createBookmark: builder.mutation({
query: (body) => ({
url: '/bookmarks',
url: url.CREATE_BOOKMARK_URL,
method: 'POST',
body,
}),
invalidatesTags: ['Bookmark'],
}),
updateBookmark: builder.mutation({
query: ({ bookmarkId, ...body }) => ({
url: `/bookmarks/${bookmarkId}`,
url: url.UPDATE_BOOKMARK_URL(url, bookmarkId),
method: 'PATCH',
body,
}),
}),
deleteBookmark: builder.mutation({
query: (bookmarkId) => ({
url: `/bookmarks/${bookmarkId}`,
url: url.DELETE_BOOKMARK_URL(url, bookmarkId),
method: 'DELETE',
}),
invalidatesTags: ['Bookmark'],
Expand Down
26 changes: 14 additions & 12 deletions src/app/slices/apiSlices/communitySlice.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,58 +2,60 @@ import { createApi } from '@reduxjs/toolkit/query/react';

import { baseQuery } from '../../constants/api';

import url from '../../url';

export const communitySlice = createApi({
reducerPath: 'communityApi',
baseQuery,
tagTypes: ['Questions', 'Answers'],
endpoints: (builder) => ({
getAllQuestions: builder.query({
query: ({ page, limit = 10 }) => ({
url: `/questions?isPaginated=true&page=${page}&limit=${limit}`,
url: url.GET_ALL_QUESTIONS_URL(url, page, true, limit),
method: 'GET',
}),
providesTags: ['Questions'],
}),
getQuestionById: builder.query({
query: (id) => ({
url: `/questions/${id}`,
url: url.GET_QUESTION_BY_ID_URL(url, id),
method: 'GET',
}),
providesTags: ['Questions'],
}),
createQuestion: builder.mutation({
query: (body) => ({
url: '/questions',
url: url.CREATE_QUESTION_URL,
method: 'POST',
body,
}),
invalidatesTags: ['Questions'],
}),
updateQuestion: builder.mutation({
query: ({ id, body }) => ({
url: `/questions/${id}`,
url: url.UPDATE_QUESTION_URL(url, id),
method: 'PATCH',
body,
}),
invalidatesTags: ['Questions'],
}),
deleteQuestion: builder.mutation({
query: (id) => ({
url: `/questions/${id}`,
url: url.DELETE_QUESTION_URL(url, id),
method: 'DELETE',
}),
invalidatesTags: ['Questions'],
}),
upvoteQuestion: builder.mutation({
query: (id) => ({
url: `/questions/${id}/upvote`,
url: url.UPVOTE_QUESTION_URL(url, id),
method: 'PATCH',
}),
invalidatesTags: ['Questions'],
}),
downvoteQuestion: builder.mutation({
query: (id) => ({
url: `/questions/${id}/downvote`,
url: url.DOWNVOTE_QUESTION_URL(url, id),
method: 'PATCH',
}),
invalidatesTags: ['Questions'],
Expand All @@ -62,37 +64,37 @@ export const communitySlice = createApi({
// Answers
getAnswers: builder.query({
query: (questionId) => ({
url: `/answers?questionId=${questionId}`,
url: url.GET_ALL_ANSWERS_URL(url, questionId),
method: 'GET',
}),
providesTags: ['Answers'],
}),
createAnswer: builder.mutation({
query: (body) => ({
url: `/answers`,
url: url.CREATE_ANSWER_URL,
method: 'POST',
body,
}),
invalidatesTags: ['Answers', 'Questions'],
}),
updateAnswer: builder.mutation({
query: ({ answerId, body }) => ({
url: `/answers/${answerId}`,
url: url.UPDATE_ANSWER_URL(url, answerId),
method: 'PATCH',
body,
}),
invalidatesTags: ['Answers'],
}),
upvoteAnswer: builder.mutation({
query: (answerId) => ({
url: `/answers/upvote/${answerId}`,
url: url.UPVOTE_ANSWER_URL(url, answerId),
method: 'PATCH',
}),
invalidatesTags: ['Answers'],
}),
downvoteAnswer: builder.mutation({
query: (answerId) => ({
url: `/answers/downvote/${answerId}`,
url: url.DOWNVOTE_ANSWER_URL(url, answerId),
method: 'PATCH',
}),
invalidatesTags: ['Answers'],
Expand Down
16 changes: 9 additions & 7 deletions src/app/slices/apiSlices/feedSlice.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,58 +2,60 @@ import { createApi } from '@reduxjs/toolkit/query/react';

import { baseQuery } from '../../constants/api';

import url from '../../url';

export const feedSlice = createApi({
reducerPath: 'feedApi',
baseQuery,
tagTypes: ['Posts', 'Post', 'Comments'],
endpoints: (builder) => ({
getAllPosts: builder.query({
query: ({ page, limit = 15 }) => ({
url: `/posts?isPaginated=true&page=${page}&limit=${limit}`,
url: url.GET_ALL_POSTS_URL(url, true, page, limit),
method: 'GET',
}),
providesTags: ['Posts'],
}),
getPostById: builder.query({
query: (id) => ({
url: `/posts/${id}`,
url: url.GET_POST_BY_ID(url, id),
method: 'GET',
}),
providesTags: ['Post'],
}),
createPost: builder.mutation({
query: (body) => ({
url: '/posts',
url: url.CREATE_POST,
method: 'POST',
body,
}),
invalidatesTags: ['Posts'],
}),
likePost: builder.mutation({
query: (id) => ({
url: `/posts/${id}/upvote`,
url: url.LIKE_POST(url, id),
method: 'PATCH',
}),
invalidatesTags: ['Post'],
}),
getAllComments: builder.query({
query: (postId) => ({
url: `/comments?postId=${postId}`,
url: url.GET_ALL_COMMENTS_URL(url, postId),
method: 'GET',
}),
providesTags: ['Comments'],
}),
createComment: builder.mutation({
query: (body) => ({
url: `/comments`,
url: url.CREATE_COMMENT_URL,
method: 'POST',
body,
}),
invalidatesTags: ['Comments', 'Post', 'Posts'],
}),
likeComment: builder.mutation({
query: (id) => ({
url: `/comments/${id}/upvote`,
url: url.LIKE_COMMENT_URL(url, id),
method: 'PATCH',
}),
invalidatesTags: ['Comments'],
Expand Down
6 changes: 3 additions & 3 deletions src/app/slices/apiSlices/forgotPasswordSlice.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,21 @@ export const forgotPasswordSlice = createApi({
// forgot password flow
forgotPassword: builder.mutation({
query: (body) => ({
url: '/accounts/forgot-password',
url: url.FORGOT_PASSWORD_URL,
method: 'POST',
body,
}),
}),
verifyForgotPasswordOtp: builder.mutation({
query: (body) => ({
url: '/accounts/verify-password-reset-otp',
url: url.VERIFY_FORGOT_PASSWORD_OTP_URL,
method: 'POST',
body,
}),
}),
resetPassword: builder.mutation({
query: (body) => ({
url: '/accounts/reset-password',
url: url.RESET_PASSWORD_URL,
method: 'POST',
body,
}),
Expand Down
Loading

0 comments on commit 762f3dc

Please sign in to comment.