npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ra-order-buttons

v1.0.0

Published

Adds up and down buttons to reorder records

Downloads

8

Readme

ra-order-buttons

This addon provides a component which renders up and down buttons to reorder records in react-admin. It connects with your Data Provider by dispatching a move action. All the magic is done in the backend so a custom logic is needed to make it work correctly. Check the backend section below to learn how to adapt your API.

Installation

npm install --save ra-order-buttons
# or
yarn add ra-order-buttons

Usage

You should pass as source to <OrderButtons /> the attribute you are using to order the records.

import React from 'react';
import { List, Datagrid, TextField } from 'react-admin';
import OrderButtons from 'ra-order-buttons';

export const ProductList = props => (
  <List {...props}>
    <Datagrid>
      <TextField source="id" />
      <TextField source="name" />
      <OrderButtons source="ordering" />
    </Datagrid>
  </List>
);

In your Data Provider you will need to handle the new request type move and map it to the appropriate API call. It is called as follow:

dataProvider.move('products', {
  id: 123, // the record id
  direction: 'up', // which button was clicked
  filter: { name: 'foo' }, // the filters used in the listing
});

Implementing the backend

When user clicks in one of the two buttons we have to switch the order value between the clicked record and the immediately above (move up) or below record (move down). The example below was based on the logic behind Joomla's Administration page which has the same functionality. You can check the original code here.

<?php

$dsn = 'mysql:host=127.0.0.1;dbname=mydb';
$pdo = new PDO($dsn, 'root', 'secret');

$id         = intval($_GET['id']);
$direction  = $_GET['direction'];
$filter     = json_decode($_GET['filter']);

$record = $pdo->query('SELECT ordering FROM products WHERE id = ' . $id);
$ordering = $record->fetchColumn();

$conditions = [];

if ($filter->q) {
    $conditions[] = 'name LIKE :search';
}

if ($direction === 'up') {
    $conditions[] = 'ordering < :ordering';
} else {
    $conditions[] = 'ordering > :ordering';
}

$sql = 'SELECT * FROM products';
$sql .= ' WHERE ' . implode(' AND ', $conditions);
$sql .= ' ORDER BY ordering ' . ($direction === 'up' ? 'DESC' : 'ASC');
$sql .= ' LIMIT 1';

$stmt = $pdo->prepare($sql);
$stmt->bindValue(':ordering', $ordering);

if ($filter->q) {
    $stmt->bindValue(':search', "%{$filter->q}%");
}

$stmt->execute();
$result = $stmt->fetchObject();

if ($result) {
    $pdo->exec('UPDATE products
        SET ordering = ' . $ordering . '
        WHERE id = ' . $result->id);

    $pdo->exec('UPDATE products
        SET ordering = ' . $result->ordering . '
        WHERE id = ' . $id);
}

header('Content-Type: application/json');
echo json_encode(['data' => null]);

License

MIT