Nested filter in admin js

128 Views Asked by At

I've been using AdminJS for a while now and i faced a challenge that I'm unsure how to fix it.

I manage two collections, Users and Tasks. Each user in the Users collection contains several properties such as Unit, fullName, email, and phone_number. When accessing the Tasks page in AdminJs dashboard page, I see a table displaying data from the Tasks collection. However, the current filtering mechanism only operates based on user emails, and I'm seeking a way to filter tasks based on various user properties. Specifically, I want the ability to filter tasks based on the user's Unit, fullName, and potentially other properties.

Please tell me the way how to set the filter Tasks base on User's properties.

Here is my AdminJs codes:

const contentParent = {
  name: "Employees",
  icon: "User",
};

const adminOptions = {
  // We pass Employees to `resources`
  resources: [
    {
      resource: tasks,
      options: {
        listProperties: ["user", "date", "submitted"],
        // editProperties: ["reason_for_not_submiting", "submitted", "task"],
        filterProperties: ["user", "date", "submitted"],

        actions: {
          new: { isAccessible: false }, // Disable the create action
          delete: { isAccessible: false }, // Disable the delete action
        },
        parent: contentParent,
        sort: {
          sortBy: "date",
          direction: "desc",
        },
      },
    },
    {
      resource: Users,
      options: {
        actions: {
          new: {
            isAccessible: true,
            History: false,
            before: async (request) => {
              if (request.payload.password) {
                const salt = await bcrypt.genSalt(12);

                const hashedPassword = await bcrypt.hash(
                  request.payload.password,
                  salt
                );

                request.payload = {
                  ...request.payload,
                  password: hashedPassword,
                  created_at: new Date(),
                };
              }
              return request;
            },
          },
          delete: { isAccessible: false },
        },
        parent: contentParent,
        sort: {
          sortBy: "created_at",
          direction: "desc",
        },
      },
    },
  ],
  componentLoader,
};

const admin = new AdminJS({
  ...adminOptions,
  defaultTheme: light.id,
  availableThemes: [dark, light, noSidebar],
  branding: {
    companyName: "Tasks",
    withMadeWithLove: false,
  },
});

 const adminRouter = AdminJSExpress.buildRouter(
      admin
    );
    app.use(admin.options.rootPath, adminRouter);
0

There are 0 best solutions below