Guest User

Axios interceptor error

a guest
Sep 27th, 2023
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.34 KB | None | 0 0
  1. import Button from "$ui/Button";
  2. import { userInfo } from "$utils/user";
  3. import { faker } from "@faker-js/faker";
  4. import { RequestCookie } from "next/dist/compiled/@edge-runtime/cookies";
  5. import { cookies } from "next/headers";
  6.  
  7. const ProfileForm = async () => {
  8.  
  9. const userData = await userInfo();
  10.  
  11. async function create(formData: FormData) {
  12. "use server";
  13. const d = userData;
  14.  
  15. const data: { [key: string]: string | number | boolean | null } =
  16. Object.fromEntries(
  17. Array.from(formData.entries()).map(([key, value]) => {
  18. // Remove leading '$' character from keys
  19. const cleanedKey = key.replace(/^\$/, "");
  20.  
  21. // Parse values to the appropriate types
  22. let cleanedValue: string | number | boolean | null = value as string;
  23. if (!isNaN(Number(value))) {
  24. cleanedValue = Number(value);
  25. } else if (value === "true" || value === "false") {
  26. cleanedValue = value === "true";
  27. }
  28.  
  29. return [cleanedKey, cleanedValue];
  30. })
  31. );
  32.  
  33. const keysToExclude = ["ACTION_REF_1", "ACTION_1:1", "ACTION_1:0"];
  34. const updatedFields = Object.fromEntries(
  35. Object.entries(data).filter(([key, value]) => {
  36. if (keysToExclude.includes(key)) {
  37. return false;
  38. }
  39. return d?.client[key] !== value;
  40. })
  41. );
  42.  
  43. console.log(updatedFields);
  44. }
  45.  
  46. return (
  47. <div>
  48. <h1 className="text-xl sm:text-center font-semibold capitalize text-secondary">
  49. Personal Information
  50. </h1>
  51. <div className="flex sm:flex-col items-center gap-6 py-6">
  52. <img
  53. className="w-36 h-36 object-cover rounded-full"
  54. src={
  55. userData?.data?.client?.picture ||
  56. faker.image.url({ width: 160, height: 160 })
  57. }
  58. alt="john doe"
  59. />
  60. <button>Update your profile pic</button>
  61. </div>
  62. <form action={create} className="p-4 sm:p-0 space-y-6">
  63. {userData &&
  64. Object.entries(userData?.client).map(
  65. ([name, value], i) =>
  66. name !== "picture" &&
  67. name !== "id" &&
  68. name !== "is_active" &&
  69. name !== "average_rating" && (
  70. <div
  71. key={i}
  72. className="grid xl:grid-cols-12 lg:grid-cols-12 gap-1 xl:gap-4 lg:gap-4"
  73. >
  74. <span className="col-span-2 sm:col-span-full font-semibold capitalize text-secondary">
  75. {name.replace(/_/g, " ")}
  76. </span>
  77. <input
  78. disabled={
  79. name === "email" ||
  80. name === "is_active" ||
  81. name === "average_rating"
  82. }
  83. type="text"
  84. name={name}
  85. defaultValue={value as string}
  86. className="col-span-10 sm:col-span-full shadow border border-primary border-opacity-30 bg-gray-100 text-secondary px-4 py-2 rounded-lg outline-orange-600"
  87. />
  88. </div>
  89. )
  90. )}
  91. <div className="flex justify-end pt-6">
  92. <Button className="sm:w-full">Update Profile</Button>
  93. </div>
  94. </form>
  95. </div>
  96. );
  97. };
  98. export default ProfileForm;
  99.  
Advertisement
Add Comment
Please, Sign In to add comment