-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor the code and enhance sending requests to the server .
- Loading branch information
1 parent
39f339d
commit 7811abd
Showing
22 changed files
with
283 additions
and
565 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { store } from "../../store/store"; | ||
import { toast } from "react-toastify"; | ||
import { customFetch } from "../../utils/customFetch"; | ||
|
||
export const sendPostRequest = async ( | ||
endPoint, | ||
data, | ||
successMessage, | ||
failedMessage, | ||
setIsChanged, | ||
actions | ||
) => { | ||
try { | ||
await customFetch.post(endPoint, data, { | ||
headers: { | ||
Authorization: `Bearer ${store.getState().userReducers.token}`, | ||
}, | ||
}); | ||
|
||
setIsChanged(true); | ||
actions.resetForm(); | ||
toast.success(successMessage); | ||
} catch (error) { | ||
console.log(error); | ||
toast.error(failedMessage); | ||
} | ||
}; | ||
|
||
export const sendPatchRequest = async ( | ||
endPoint, | ||
data, | ||
confirmText, | ||
successMessage, | ||
failedMessage, | ||
setIsChanged | ||
) => { | ||
const isConfirmed = window.confirm(confirmText); | ||
if (!isConfirmed) return; | ||
|
||
try { | ||
await customFetch.patch(endPoint, data, { | ||
headers: { | ||
Authorization: `Bearer ${store.getState().userReducers.token}`, | ||
}, | ||
}); | ||
|
||
setIsChanged(true); | ||
toast.success(successMessage); | ||
} catch (error) { | ||
console.log(error.message); | ||
toast.error(failedMessage); | ||
} | ||
}; | ||
|
||
export const sendDeleteRequest = async ( | ||
confirmText, | ||
endPoint, | ||
successMessage, | ||
failedMessage, | ||
setIsChanged | ||
) => { | ||
const isConfirmed = window.confirm(confirmText); | ||
if (!isConfirmed) return; | ||
|
||
try { | ||
await customFetch.delete(endPoint, { | ||
headers: { | ||
Authorization: `Bearer ${store.getState().userReducers.token}`, | ||
}, | ||
}); | ||
|
||
setIsChanged(true); | ||
toast.success(successMessage); | ||
} catch (error) { | ||
console.log(error.message); | ||
toast.error(failedMessage); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
const ActionButton = ({ btnType, onClick, btnText }) => { | ||
const colorClass = | ||
btnType === "danger" | ||
? "bg-red-500 hover:bg-red-800" | ||
: btnType === "mainBlue" | ||
? "bg-blue-500 hover:bg-blue-800" | ||
: ""; | ||
return ( | ||
<button | ||
className={`${colorClass} text-white font-bold py-2 px-4 rounded-[10px] focus:outline-none focus:shadow-outline`} | ||
onClick={onClick} | ||
> | ||
{btnText} | ||
</button> | ||
); | ||
}; | ||
|
||
export default ActionButton; |
Oops, something went wrong.