Skip to content

Commit

Permalink
Fix error when update user and reset password (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
haiphucnguyen authored Jan 4, 2025
1 parent 5e6a1d0 commit cba91d7
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 27 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"@radix-ui/react-toggle-group": "^1.1.1",
"@radix-ui/react-tooltip": "^1.1.6",
"@tailwindcss/typography": "^0.5.15",
"@tanstack/react-query": "^5.62.11",
"@tanstack/react-query": "^5.62.15",
"@tanstack/react-table": "^8.20.6",
"@tiptap/core": "^2.11.0",
"@tiptap/extension-link": "^2.11.0",
Expand Down Expand Up @@ -83,7 +83,7 @@
"devDependencies": {
"@eslint/compat": "^1.2.4",
"@eslint/js": "^9.17.0",
"@types/node": "^22.10.4",
"@types/node": "^22.10.5",
"@types/react": "^19.0.2",
"@types/react-dom": "^19.0.2",
"@typescript-eslint/eslint-plugin": "^8.19.0",
Expand Down
40 changes: 20 additions & 20 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion scripts/init_environments.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ while true; do
done

# Append to the .env.local file
echo "NEXT_PUBLIC_BACK_END_URL=\"$backend_server\"" >> "$output_file"
cat <<EOL >> "$output_file"
NEXT_PUBLIC_BASE_URL="$backend_server"
BACK_END_URL="$backend_server"
EOL


# Run npx auth and append its output to .env
Expand Down
4 changes: 3 additions & 1 deletion src/app/account/reset/finish/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
} from "@/components/ui/form";
import { Input } from "@/components/ui/input";
import { passwordReset } from "@/lib/actions/users.action";
import { useError } from "@/providers/error-provider";

const schema = z
.object({
Expand All @@ -44,6 +45,7 @@ function ActivationContent() {

const [status, setStatus] = useState("loading"); // 'loading', 'success', 'error'
const [errorMessage, setErrorMessage] = useState("");
const { setError } = useError();

const form = useForm<FormData>({
resolver: zodResolver(schema),
Expand Down Expand Up @@ -77,7 +79,7 @@ function ActivationContent() {
if (status === "submitted" && keyParam) {
const activateUser = async (key: string) => {
try {
await passwordReset(key, form.getValues("password"));
await passwordReset(key, form.getValues("password"), setError);
setStatus("success");
} catch (error) {
setErrorMessage(
Expand Down
19 changes: 17 additions & 2 deletions src/components/users/user-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ import {
} from "@/components/ui/ext-form";
import { Form } from "@/components/ui/form";
import { Separator } from "@/components/ui/separator";
import { createUser, findUserById } from "@/lib/actions/users.action";
import {
createUser,
findUserById,
updateUser,
} from "@/lib/actions/users.action";
import { obfuscate } from "@/lib/endecode";
import { useError } from "@/providers/error-provider";
import { UserDTO, UserDTOSchema } from "@/types/users";
Expand Down Expand Up @@ -57,10 +61,21 @@ export const UserForm = ({ userId }: { userId?: number }) => {
}, [userId, reset]);

async function onSubmit(data: UserDTO) {
const savedUser = await createUser(data, setError);
const savedUser = data.id
? await updateUser(prepareFormData(data), setError)
: await createUser(data, setError);
router.push(`/portal/users/${obfuscate(savedUser.id)}`);
}

function prepareFormData(data: UserDTO): FormData {
const formData = new FormData();
const userJsonBlob = new Blob([JSON.stringify(data)], {
type: "application/json",
});
formData.append("userDTO", userJsonBlob);
return formData;
}

const isEdit = !!user;
const title = isEdit
? `Edit User ${user?.firstName} ${user?.lastName}`
Expand Down
6 changes: 5 additions & 1 deletion src/lib/actions/commons.action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ export const fetchData = async <TData, TResponse>(

try {
const apiUrl =
securityMode === SecurityMode.CLIENT_SECURE ? BASE_URL : BACK_END_URL;
securityMode === SecurityMode.CLIENT_SECURE ||
securityMode === SecurityMode.NOT_SECURE
? BASE_URL
: BACK_END_URL;

const response = await fetch(`${apiUrl}${url}`, options);

if (response.ok) {
Expand Down

0 comments on commit cba91d7

Please sign in to comment.