-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a9b2eb9
commit 1577eb8
Showing
7 changed files
with
107 additions
and
2 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
38 changes: 38 additions & 0 deletions
38
frontend/src/features/dashboard/app-setting-form/actions/update-setting.ts
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,38 @@ | ||
"use server"; | ||
import {revalidatePath} from "next/cache"; | ||
import {updateConfigs, AxiosError} from "@/dal"; | ||
import {convertFormDataActionToObject} from "@/lib/transformers"; | ||
import {APP_PATHS} from "@/lib/app-paths"; | ||
|
||
type FormState = { | ||
success: boolean; | ||
fieldErrors?: Record<string, string>; | ||
}; | ||
|
||
export async function updateSettingAction( | ||
formState: FormState, | ||
formData: FormData, | ||
): Promise<FormState> { | ||
try { | ||
const data = convertFormDataActionToObject(formData); | ||
if (typeof data.user_default_roles === "string") { | ||
data.user_default_roles = data.user_default_roles.split(","); | ||
} | ||
await updateConfigs(data); | ||
} catch (error) { | ||
if (error instanceof AxiosError && error.status === 400) { | ||
return { | ||
success: false, | ||
fieldErrors: error.response?.data.errors, | ||
}; | ||
} | ||
return { | ||
success: false, | ||
}; | ||
} | ||
|
||
revalidatePath(APP_PATHS.dashboard.settings); | ||
return { | ||
success: true, | ||
}; | ||
} |
38 changes: 38 additions & 0 deletions
38
frontend/src/features/dashboard/app-setting-form/components/app-setting-form.tsx
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,38 @@ | ||
"use client"; | ||
import {useFormState} from "react-dom"; | ||
import {Group, Stack, Textarea} from "@mantine/core"; | ||
import {FormButton} from "@/components/form-button"; | ||
import {updateSettingAction} from "../actions/update-setting"; | ||
|
||
type Props = { | ||
config: { | ||
userDefaultRoles: string[]; | ||
}; | ||
}; | ||
|
||
export function AppSettingForm({config}: Props) { | ||
const [state, dispatch] = useFormState(updateSettingAction, {success: false}); | ||
|
||
return ( | ||
<form action={dispatch}> | ||
<Stack> | ||
<Textarea | ||
name="user_default_roles" | ||
label="نقش پیشفرض کاربران" | ||
rows={4} | ||
defaultValue={config.userDefaultRoles} | ||
dir="ltr" | ||
styles={{ | ||
input: { | ||
textAlign: "left", | ||
}, | ||
}} | ||
error={state.fieldErrors?.user_default_roles || ""} | ||
/> | ||
<Group justify="flex-end" mt="md"> | ||
<FormButton>بروزرسانی</FormButton> | ||
</Group> | ||
</Stack> | ||
</form> | ||
); | ||
} |
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 @@ | ||
export {AppSettingForm} from "./components/app-setting-form"; |
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