Skip to content

Commit

Permalink
用户状态字段
Browse files Browse the repository at this point in the history
  • Loading branch information
sobird committed Jan 12, 2024
1 parent 7bfe94f commit a1752a8
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 7 deletions.
2 changes: 1 addition & 1 deletion actions/user.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export async function createUserAction(
}

user.setRoles(data.roles);

revalidatePath('/dashboard/user');
redirect('/dashboard/user');
}

Expand Down
8 changes: 7 additions & 1 deletion app/dashboard/user/[id]/edit/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

import { NextPage } from 'next';
import { notFound } from 'next/navigation';
import React from 'react';
import UserForm from '@/app/dashboard/user/components/user-form';
import { updateUserAction } from '@/actions/user';
Expand Down Expand Up @@ -32,8 +33,13 @@ const UserEditPage: NextPage<UserEditPageProps> = async ({ params }) => {
}],
});

const { Roles, ...initialValues } = user?.toJSON() || {};
if (!user) {
notFound();
}

const { Roles, ...initialValues } = user.toJSON();
(initialValues as any).roles = Roles?.map((item) => { return item.id; });

return (
<UserForm action={updateUserAction} initialValues={initialValues} mode="update" />
);
Expand Down
10 changes: 9 additions & 1 deletion app/dashboard/user/components/user-form/form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import React from 'react';
import {
Row, Col, Form, Input, DatePicker,
Row, Col, Form, Input, DatePicker, Radio,
} from 'antd';
import dayjs from '@/utils/dayjs';
import {
Expand Down Expand Up @@ -80,6 +80,14 @@ const InternalUserForm: React.FC<WithFormProps> = ({ mode, data }) => {
<DebounceSelect options={roleOptions} mode="multiple" placeholder="请选择" />
</Form.Item>
</Col>
<Col span={12}>
<Form.Item label="状态" name="status" initialValue rules={[userFormRule]}>
<Radio.Group>
<Radio value>正常</Radio>
<Radio value={false}>禁用</Radio>
</Radio.Group>
</Form.Item>
</Col>

{mode !== 'create' && (
<>
Expand Down
11 changes: 10 additions & 1 deletion app/dashboard/user/table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
'use client';

import { FC } from 'react';
import { Button, Table, Modal } from 'antd';
import {
Button, Table, Modal, Switch,
} from 'antd';
import { useRouter, usePathname, useSearchParams } from 'next/navigation';
import Link from 'next/link';
import { UserModel, UserAttributes } from '@/models';
Expand Down Expand Up @@ -60,6 +62,13 @@ const UserTable:FC<UserTableProps> = ({ data }) => {
title="创建时间"
dataIndex="createdAt"
/>
<Table.Column
title="状态"
dataIndex="status"
render={(status) => {
return <Switch disabled checkedChildren="正常" unCheckedChildren="停用" defaultChecked={status} />;
}}
/>
<Table.Column<any>
title="操作"
dataIndex="actions"
Expand Down
2 changes: 2 additions & 0 deletions components/with-action-form/action-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ const ActionForm: React.FC<ActionFormProps> = ({
};
const [state, dispatch, pending] = useServerAction(action, initialState);

console.log('state', state);

useUpdate(() => {
if (state?.status === ActionStatus.INITIAL) {
return;
Expand Down
5 changes: 5 additions & 0 deletions lib/sequelize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,11 @@ export const sequelize = new Sequelize({
},
});

// sequelize.addHook('beforeDefine', (attributes) => {
// // todo
// console.log('attributes', attributes);
// });

/**
* 模型基类
*
Expand Down
6 changes: 6 additions & 0 deletions models/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ class User extends BaseModel<UserAttributes, UserCreationAttributes> {

public ip: CreationOptional<string>;

declare status: CreationOptional<boolean>;

declare createdBy: CreationOptional<number>;

declare updatedBy: CreationOptional<number>;
Expand Down Expand Up @@ -229,6 +231,10 @@ User.init(
defaultValue: 0,
comment: 'user last login ip',
},
status: {
type: DataTypes.BOOLEAN,
defaultValue: true,
},
createdBy: {
type: DataTypes.INTEGER,
},
Expand Down
2 changes: 1 addition & 1 deletion styles/theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const config: ThemeConfig = {
// disabled
// colorBgContainerDisabled: '#fff',
// colorTextDisabled: '#444',
// motion: false,
motion: false,

fontFamily: '',
fontSize: 14,
Expand Down
10 changes: 8 additions & 2 deletions zod/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ const ohtersZod = z.object({
nickname: z
.string()
.regex(/^[\d\w_\u4E00-\u9FFF]{2,20}$/, '长度在2~20位之间,包含中文、字母、数字、下划线')
.optional()
.or(z.literal('')),
realname: z
.string()
Expand All @@ -121,6 +122,7 @@ const ohtersZod = z.object({
message: '请输入正确的中文名',
},
)
.optional()
.or(z.literal('')),
mobile: z
.string()
Expand All @@ -132,9 +134,13 @@ const ohtersZod = z.object({
message: '请输入正确的手机号',
},
)
.optional()
.or(z.literal('')),
gender: z.enum(['male', 'female', 'unknown']),
roles: z.array(z.number()),
gender: z.enum(['male', 'female', 'unknown']).optional().or(z.literal('')),
roles: z.array(z.number()).optional(),
status: z.boolean({
required_error: '状态未选择',
}),
});

// 前台注册
Expand Down

0 comments on commit a1752a8

Please sign in to comment.