Skip to content

Commit

Permalink
chore(admin): convert core utils to TS (strapi#18551)
Browse files Browse the repository at this point in the history
  • Loading branch information
gu-stav authored Oct 24, 2023
1 parent 6d4aa21 commit 374327c
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 23 deletions.
3 changes: 2 additions & 1 deletion packages/core/admin/admin/src/StrapiApp.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import { Providers } from './components/Providers';
import { HOOKS, INJECTION_ZONES } from './constants';
import { customFields, Plugin, Reducers } from './core/apis';
import { configureStore } from './core/store/configure';
import { basename, createHook } from './core/utils';
import { basename } from './core/utils/basename';
import { createHook } from './core/utils/createHook';
import favicon from './favicon.png';
import languageNativeNames from './translations/languageNativeNames';

Expand Down
3 changes: 0 additions & 3 deletions packages/core/admin/admin/src/core/utils/basename.js

This file was deleted.

1 change: 1 addition & 0 deletions packages/core/admin/admin/src/core/utils/basename.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const basename = (process.env.ADMIN_PATH ?? '').replace(window.location.origin, '');
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
// TODO: refacto this file to avoid eslint issues
/* eslint-disable no-unused-vars */
/* eslint-disable no-await-in-loop */
/* eslint-disable no-restricted-syntax */
const createHook = () => {
const _handlers = [];

type Handler = (...args: any[]) => any;

export const createHook = () => {
const _handlers: Handler[] = [];

return {
register(fn) {
register(fn: Handler) {
_handlers.push(fn);
},
delete(handler) {
delete(handler: Handler) {
_handlers.splice(_handlers.indexOf(handler), 1);
},
runWaterfall(args, store) {
runWaterfall<T>(args: T, store?: any) {
return _handlers.reduce((acc, fn) => fn(acc, store), args);
},
async runWaterfallAsync(args, store) {
async runWaterfallAsync<T>(args: T, store?: any) {
let result = args;

for (const fn of _handlers) {
Expand All @@ -24,10 +25,10 @@ const createHook = () => {

return result;
},
runSeries(...args) {
runSeries<T extends any[]>(...args: T) {
return _handlers.map((fn) => fn(...args));
},
async runSeriesAsync(...args) {
async runSeriesAsync<T extends any[]>(...args: T) {
const result = [];

for (const fn of _handlers) {
Expand All @@ -36,7 +37,7 @@ const createHook = () => {

return result;
},
runParallel(...args) {
runParallel<T extends any[]>(...args: T) {
return Promise.all(
_handlers.map((fn) => {
return fn(...args);
Expand All @@ -45,5 +46,3 @@ const createHook = () => {
},
};
};

export default createHook;
2 changes: 0 additions & 2 deletions packages/core/admin/admin/src/core/utils/index.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import createHook from '../createHook';
import { createHook } from '../createHook';

describe('ADMIN | core | utils | createHook', () => {
let hooksContainer;
let hooksContainer: ReturnType<typeof createHook>;

beforeEach(() => {
hooksContainer = createHook();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import React from 'react';
import PropTypes from 'prop-types';
import { useIntl } from 'react-intl';

import basename from '../../../../../../core/utils/basename';
import { basename } from '../../../../../../core/utils/basename';

import MagicLinkWrapper from './MagicLinkWrapper';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import React from 'react';
import PropTypes from 'prop-types';
import { useIntl } from 'react-intl';

import basename from '../../../../../../../../admin/src/core/utils/basename';
import { basename } from '../../../../../../../../admin/src/core/utils/basename';
import MagicLinkWrapper from '../../../../../../../../admin/src/pages/SettingsPage/pages/Users/components/MagicLink/MagicLinkWrapper';

// FIXME replace with parts compo when ready
Expand Down

0 comments on commit 374327c

Please sign in to comment.