Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Initial implementation of plugin which adds support for string[] #6

Draft
wants to merge 1 commit into
base: staging
Choose a base branch
from

Conversation

martintomas
Copy link
Collaborator

@martintomas martintomas commented Sep 7, 2023

Simple plugin which adds support for string[] db type. User's input provided via Administration is divided by , and individual parts of string are saved as elements of array. Vice versa, individual elements of array are joined together and presented to user.

Current implementation is using decorator around strapi.entityService that allows to update array column independently outside of Strapi supported types.

Provided implementation works at Strapi, but unfortunately does not work for import-export Strapi package when import version 1 is used. Based on this package documentation, this import should be already deprecated, but anyway csv and other stuff still use it. Its code does not use strapi.entityService (recommended by Strapi) and modifies data directly, therefore I have not figured way how to modify it without forking whole repo and tweaking code directly inside this package. For now decided to use JSON instead of array data type 馃槩

Maybe you find this interesting @davidsingal @tiagojsag :)

@martintomas martintomas self-assigned this Sep 7, 2023
@tiagojsag
Copy link
Contributor

I don't understand the first thing about strapi plugin development yet, so if it works, it works 馃憤 great stuff!

@martintomas
Copy link
Collaborator Author

I don't understand the first thing about strapi plugin development yet, so if it works, it works 馃憤 great stuff!

It works with Strapi itself, but does not work with other plugin which we need to use (import-export plugin) so I will not merge this in the end. Pushed it to repo just for others to see how strapi plugin can look like...

@@ -0,0 +1,45 @@
'use strict';

const _ = require("lodash");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const _ = require("lodash");
const { isArray, map, trim, unset } = require("lodash-es");

It's recommended to only import what you need instead of the complete library, and using this version of lodash will make it easier.
https://www.npmjs.com/package/lodash-es

@@ -14,6 +14,7 @@
"@strapi/plugin-i18n": "4.13.1",
"@strapi/plugin-users-permissions": "4.13.1",
"@strapi/strapi": "4.13.1",
"lodash": "^4.17.21",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should actually NOT be here, but inside the plugin itself

placeholder="Individual strings divided by commas"
label={name}
name={name}
onChange={(e) => handleValueChange(e.target.value)}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
onChange={(e) => handleValueChange(e.target.value)}
onChange={handleValueChange}

Comment on lines +17 to +20
const handleValueChange = (newValue) => {
setArrayOfStrings(newValue);
onChange({ target: { name, value: newValue, type: attribute.type } });
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const handleValueChange = (newValue) => {
setArrayOfStrings(newValue);
onChange({ target: { name, value: newValue, type: attribute.type } });
}
const handleValueChange = useCallback((e) => {
const newValue = e.target.value;
setArrayOfStrings(newValue);
onChange({ target: { name, value: newValue, type: attribute.type } });
}, []);

This form will avoid extra assignations in the renderization cycle loop of React

@@ -0,0 +1,41 @@
import React, { useState, useEffect } from 'react';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
import React, { useState, useEffect } from 'react';
import { useState } from 'react';

Remove no needed libs

Comment on lines +5 to +11
const Input = ({
attribute,
error,
name,
onChange,
value
}) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Types are missing, but vscode doesn't show any error. I'll take a look

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants