Using ESLint and Prettier with React

How to setup ESLint and Prettier for your React apps Using ESLint and Prettier in a TypeScript Project eslint-config-ionic How to use ESLint with TypeScript

RCF2047 mutipart form decode for php

Sometime, there are error on the php backend when uploading the file. When I check the form data and it was encoded with RCF2047.

=?utf-8?B?L2RhdGEvdXNlci8wL2NvbS5mdXp6eXBhd3Muc25zL2ZpbGVzL+iMuOS5i+eIqsK356S+5Yy6L0ltYWdlcy8yMDIwMDUwODIyNTExMjMzODMuanBlZw==?=

This is because the file path contains non-US ACSII character, so the request programe will try to encode it first. There is no way to extract file name from the raw data. The easy way to solve it is using iconv_mime_decode function to decode the file name first.

echo iconv_mime_decode('=?utf-8?B?L2RhdGEvdXNlci8wL2NvbS5mdXp6eXBhd3Muc25zL2ZpbGVzL+iMuOS5i+eIqsK356S+5Yy6L0ltYWdlcy8yMDIwMDUwODIyNTExMjMzODMuanBlZw==?=', 0);

 The filename will output correctly.

TS2345 error using react-redux connect

TL;DR

install @type/react-redux@6.0.6

npm i --save-dev @type/react-redux@6.0.6

Then it will be ready to go.

What cause the problem

When trying to connect state with connect function shown belown.

export interface ILoginProps extends StateProps, DispatchProps {}
const Login: React.FC<ILoginProps> = (props) => { /** codes omitted **/ };

const mapStateToProps = ({ authentication }: IRootState) => ({
    isAuthenticated: authentication.isAuthenticated
});
const mapDispatchToProps = { loginUser };
type StateProps = ReturnType<typeof mapStateToProps>;
type DispatchProps = typeof mapDispatchToProps;

export default connect(
    mapStateToProps,
    mapDispatchToProps
)(Login);

The newer @type/react-redux@7.1.7 is likely to cause problem because it is trying to resolve the dispatch function into different form.

// from
const loginUser = (username:string, password:string, rememberMe:boolean) => async (dispatch, getState) => Promise<Void>
// to
const loginUser = (username:string, password:string, rememberMe:boolean) => Promise<Void>

This will cause this extend ILoginProps does not match with type expected with connect() function.

Solution

Therefore, the easy is to downgrade by install the package below. The other way is to manually write the prop interface.

export interface ILoginProps extends StateProps {
    loginUser: Function
}

After more search, I found this answer. The document also has more information. This is another way to solve the problem.

export interface ILoginProps extends PropsConnected {}
const mapStateToProps = ({ authentication }: IRootState) => ({
    isAuthenticated: authentication.isAuthenticated
});
const mapDispatchToProps = { loginUser };

const connector =  connect(
    mapStateToProps,
    mapDispatchToProps
);
type PropsConnected = ConnectedProps<typeof connector>;

export default connector(Login);

Spring Boot Autowiring Multiple implementation for Interface

Autowiring is a convenient feature for development. Developers do not require to handle object creation, the application can help decide the object/s that a service or controller required to work.

When specifying a class, sometimes it is necessary to have an interface so you can make different implementation. However, it might confuse the application since it cannot choose the correct bean. There are ways to solve the problem.

  1. Primary annotation.

    If you have multiple implementations, you can choose the one that is primarily used. Then apply @Primary, the application will automate wire all related interface to this implementation.

public interface A {
	void a();
}

@Service
@Primary
public class B implements A {
	//...
}

@Service
public class C implements A {
	//...
}
So the application will link it to class B.
  1. Qualifier annotation

    If you need to choose between different implementation, you should use @Qualifier.

public interface A {
	void a();
}

@Service
@Qualifier("firstService")
public class B implements A {
	//...
}

@Service
@Qualifier("secondService")
public class C implements A {
	//...
}
If you need implementation C, you can specify with `@Qualifier`.
@Component
public static class FirstManager {

    private final A a;

    @Autowired // inject FirstServiceImpl
    public FirstManager(@Qualifier("secondService") A a) {
        this.a = a;
    }
}

Reference: link

Testing in Spring Boot with JPA

Application needs to be tested. Here are some notes to test spring boot application.