-1
import React from 'react'
import { Input, Form, Button } from 'antd';
import { LockTwoTone, MailTwoTone, EyeTwoTone, EyeInvisibleOutlined } from '@ant-design/icons';
import { login } from '@/services/Api.Login/index'
import { getProfile } from '@/services/Api.User';
 
const LoginForm = () => {

    const onFinish = async (values) => {
 
        const { email, role, userName, organisationId } = await getProfile(values);
        
        localStorage.setItem('email', email);     
        localStorage.setItem('role', role); 
        localStorage.setItem('userName', userName); 
        localStorage.setItem('organisationId', organisationId); 

        // var items = [ 'email', 'role', 'userName', 'organisationId' ]
        
    };

    return (null)
}

export default LoginForm

wangdev87
  • 8,611
  • 3
  • 8
  • 31

2 Answers2

1
    const onFinish = async (values) => {
 
        const result = await getProfile(values);
        const items = [ 'email', 'role', 'userName', 'organisationId' ]
        items.forEach(key => {
          localStorage.setItem(key, result[key]);
        });
        
    };
wangdev87
  • 8,611
  • 3
  • 8
  • 31
0

Instead of this

const { email, role, userName, organisationId } = await getProfile(values);

You can store it in an array

const arr = await getProfile(values);

Then use this what you have in the commentary as foundation for the loop

var items = [ 'email', 'role', 'userName', 'organisationId' ]

Loop through it

items.forEach(key => {
        localStorage.setIteam(key, arr[key]);
});
Aalexander
  • 4,987
  • 3
  • 11
  • 34