uuidv4#uuid TypeScript Examples

The following examples show how to use uuidv4#uuid. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: toast.tsx    From ecoleta with MIT License 6 votes vote down vote up
ToastProvider: React.FC = ({ children }) => {
  const [messages, setMessages] = useState<IToastMessage[]>([]);

  const addToast = useCallback(
    ({ title, type, description }: Omit<IToastMessage, 'id'>) => {
      const id = uuid();

      const toast = {
        id,
        type,
        title,
        description,
      };

      setMessages(oldMessages => [...oldMessages, toast]);
    },
    [],
  );

  const removeToast = useCallback((id: string) => {
    setMessages(oldMessages =>
      oldMessages.filter(message => message.id !== id),
    );
  }, []);

  return (
    <ToastContext.Provider value={{ addToast, removeToast }}>
      {children}
      <ToastContainer messages={messages} />
    </ToastContext.Provider>
  );
}
Example #2
Source File: todo.ts    From Riakuto-StartingReact-ja3.1 with Apache License 2.0 6 votes vote down vote up
todoSlice = createSlice({
  name: 'todo',
  initialState: { todoList: {}, doneList: {} } as TodoState,
  reducers: {
    taskCreated: (
      state,
      action: PayloadAction<Pick<Task, 'title' | 'deadline'>>,
    ) => {
      const id = uuid();
      const createdAt = new Date();
      state.todoList[id] = { ...action.payload, id, createdAt };
    },
    taskDone: (state, action: PayloadAction<string>) => {
      const id = action.payload;
      const task = state.todoList[id];

      if (task) {
        state.doneList[id] = { ...task };
        delete state.todoList[id];
      }
    },
    taskUpdated: (state, action: PayloadAction<Omit<Task, 'createdAt'>>) => {
      const { id, ...data } = action.payload;
      const task = state.todoList[id];

      if (task) state.todoList[id] = { ...task, ...data };
    },
  },
})
Example #3
Source File: toast.tsx    From GoBarber with MIT License 6 votes vote down vote up
ToastProvider: React.FC = ({ children }) => {
  const [messages, setMessages] = useState<ToastMessage[]>([]);

  const addToast = useCallback(
    ({ type, title, description }: Omit<ToastMessage, 'id'>) => {
      const id = uuid();

      const toast = {
        id,
        type,
        title,
        description,
      };

      setMessages((state) => [...state, toast]);
    },
    [],
  );

  const removeToast = useCallback((id: string) => {
    setMessages((state) => state.filter((message) => message.id !== id));
  }, []);

  return (
    <ToastContext.Provider value={{ addToast, removeToast }}>
      {children}
      <ToastContainer messages={messages} />
    </ToastContext.Provider>
  );
}
Example #4
Source File: FakeUsersRepository.ts    From GoBarber with MIT License 6 votes vote down vote up
public async create({
    name,
    email,
    password,
  }: ICreateUserDTO): Promise<User> {
    const user = new User();

    Object.assign(user, { id: uuid(), name, email, password });

    this.users.push(user);

    return user;
  }
Example #5
Source File: FakeUserTokensRepository.ts    From GoBarber with MIT License 6 votes vote down vote up
public async generate(user_id: string): Promise<UserToken> {
    const userToken = new UserToken();

    Object.assign(userToken, {
      id: uuid(),
      token: uuid(),
      user_id,
      created_at: new Date(),
      updated_at: new Date(),
    });

    this.usersTokens.push(userToken);

    return userToken;
  }
Example #6
Source File: FakeAppointmentsRepository.ts    From GoBarber with MIT License 6 votes vote down vote up
public async create({
    provider_id,
    user_id,
    date,
  }: ICreateAppointmentDTO): Promise<Appointment> {
    const appointment = new Appointment();

    Object.assign(appointment, { id: uuid(), date, provider_id, user_id });

    this.appointments.push(appointment);

    return appointment;
  }
Example #7
Source File: toast.tsx    From vagasExplorer with MIT License 6 votes vote down vote up
ToastProvider: React.FC = ({ children }) => {
  const [messages, setMessages] = useState<ToastMessage[]>([]);
  const addToast = useCallback(
    ({ type, title, description }: Omit<ToastMessage, 'id'>) => {
      const id = uuid();

      const toast = {
        id,
        type,
        title,
        description,
      };

      setMessages((oldMessages) => [...oldMessages, toast]);
    },
    [],
  );

  const removeToast = useCallback((id: string) => {
    setMessages((state) => state.filter((message) => message.id !== id));
  }, []);

  return (
    <ToastContext.Provider value={{ addToast, removeToast }}>
      {children}
      <ToastContainer messages={messages} />
    </ToastContext.Provider>
  );
}
Example #8
Source File: toast.tsx    From rocketredis with MIT License 6 votes vote down vote up
ToastProvider: React.FC = ({ children }) => {
  const [messages, setMessages] = useState<ToastMessage[]>([])

  const removeToast = useCallback(id => {
    setMessages(state => state.filter(message => message.id !== id))
  }, [])

  const addToast = useCallback<ToastShow>(message => {
    const id = uuid()

    setMessages(state => [...state, { ...message, id }])

    return id
  }, [])

  return (
    <ToastContext.Provider value={{ addToast, removeToast }}>
      <ToastContainer toasts={messages} />
      {children}
    </ToastContext.Provider>
  )
}
Example #9
Source File: toastModal.tsx    From front-entenda-direito with GNU General Public License v3.0 6 votes vote down vote up
ToastModalProvider: React.FC = ({ children }) => {
  const [messages, setMessages] = useState<ToastMessage[]>([]);

  const addModal = useCallback(
    ({ type, title, description }: Omit<ToastMessage, 'id'>) => {
      const id = uuid();

      const toast = {
        id,
        type,
        title,
        description,
      };

      setMessages((oldMessages) => [...oldMessages, toast]);
    },
    [],
  );

  const removeModal = useCallback((id: string) => {
    setMessages((state) => state.filter((message) => message.id !== id));
  }, []);

  return (
    <ToastContext.Provider value={{ addModal, removeModal }}>
      {children}
      <ToastContainerModal messages={messages} />
    </ToastContext.Provider>
  );
}
Example #10
Source File: controller.ts    From serverless-dynamodb-api-boilerplate with MIT License 6 votes vote down vote up
createController = async (
  event: APIGatewayProxyEvent,
  documentClient: DynamoDB.DocumentClient
) => {
  // Parse string to verified request schema model
  const input: Thing = JSON.parse(event.body);
  const item = {
    id: uuid(),
    status: input.status || 'OK',
    date_added: (+new Date()).toString(),
    name: input.name,
  };
  const params = ThingModel.put(item);

  await documentClient.put(params).promise();

  return item;
}
Example #11
Source File: FakeUsersRepository.ts    From hotseat-api with MIT License 6 votes vote down vote up
public async create(userData: ICreateUserDTO): Promise<User> {
    const user = Object.assign(new User(), {
      id: uuid(),
      ...userData,
    });

    this.users.push(user);

    return user;
  }
Example #12
Source File: FakeRecoverPasswordRequestsRepository.ts    From hotseat-api with MIT License 6 votes vote down vote up
public async create(user_id: string): Promise<RecoverPasswordRequest> {
    const recoverPasswordRequest = Object.assign(new RecoverPasswordRequest(), {
      user_id,
      token: uuid(),
      expires_at: addHours(Date.now(), RESET_PASSWORD_REQUEST_EXPIRES_IN_HOURS),
      created_at: new Date(),
      updated_at: new Date(),
    });

    this.requests.push(recoverPasswordRequest);

    return recoverPasswordRequest;
  }
Example #13
Source File: FakeAppointmentsRepository.ts    From hotseat-api with MIT License 6 votes vote down vote up
public async create({
    provider_id,
    customer_id,
    type,
    date,
  }: ICreateAppointmentDTO): Promise<Appointment> {
    const appointment = Object.assign(new Appointment(), {
      id: uuid(),
      provider_id,
      customer_id,
      type,
      date,
    });

    this.appointments.push(appointment);

    return appointment;
  }
Example #14
Source File: toast.tsx    From gobarber-web with MIT License 6 votes vote down vote up
ToastProvider: React.FC = ({ children }) => {
  const [messages, setMessages] = useState<ToastMessage[]>([]);

  const addToast = useCallback(
    ({ type, title, description }: Omit<ToastMessage, 'id'>) => {
      const id = uuid();

      const toast = {
        id,
        type,
        title,
        description,
      };

      setMessages(state => [...state, toast]);
    },
    [],
  );
  const removeToast = useCallback((id: string) => {
    setMessages(state => state.filter(message => message.id !== id));
  }, []);

  return (
    <ToastContext.Provider value={{ addToast, removeToast }}>
      {children}
      <ToastContainer messages={messages} />
    </ToastContext.Provider>
  );
}
Example #15
Source File: FakeUsersRepository.ts    From gobarber-api with MIT License 6 votes vote down vote up
public async create({
    name,
    email,
    password,
  }: ICraeteUserDTO): Promise<User> {
    const user = new User();

    Object.assign(user, { id: uuid(), name, email, password });

    this.users.push(user);

    return user;
  }
Example #16
Source File: FakeUserTokensRepository.ts    From gobarber-api with MIT License 6 votes vote down vote up
public async generate(user_id: string): Promise<UserToken> {
    const userToken = new UserToken();

    Object.assign(userToken, {
      id: uuid(),
      token: uuid(),
      user_id,
      created_at: new Date(),
      updated_at: new Date(),
    });

    this.userTokens.push(userToken);

    return userToken;
  }
Example #17
Source File: FakeAppointmentsRepository.ts    From gobarber-api with MIT License 6 votes vote down vote up
public async create({
    provider_id,
    user_id,
    date,
  }: ICraeteAppointmentDTO): Promise<Appointment> {
    const appointment = new Appointment();

    Object.assign(appointment, { id: uuid(), date, provider_id, user_id });

    this.appointments.push(appointment);

    return appointment;
  }
Example #18
Source File: toast.tsx    From front-entenda-direito with GNU General Public License v3.0 5 votes vote down vote up
ToastProvider: React.FC = ({ children }) => {
  const [messages, setMessages] = useState<ToastMessage[]>([]);
  const [processView, setProcessView] = useState(false);
  const [dictionaryView, setDictionaryView] = useState(false);

  const addToast = useCallback(
    ({ type, title, description }: Omit<ToastMessage, 'id'>) => {
      const id = uuid();

      const toast = {
        id,
        type,
        title,
        description,
      };

      setMessages((oldMessages) => [...oldMessages, toast]);
    },
    [],
  );

  const removeToast = useCallback((id: string) => {
    setMessages((state) => state.filter((message) => message.id !== id));
  }, []);

  const getProcess = useCallback(() => {
    return processView;
  }, [processView]);

  const setProcess = useCallback(() => {
    setProcessView(!processView);
  }, [processView]);

  const getDictionary = useCallback(() => {
    return dictionaryView;
  }, [dictionaryView]);

  const setDictionary = useCallback(() => {
    setDictionaryView(!dictionaryView);
  }, [dictionaryView]);

  return (
    <ToastContext.Provider
      value={{
        addToast,
        removeToast,
        getProcess,
        setProcess,
        getDictionary,
        setDictionary,
      }}
    >
      {children}
      <ToastContainer messages={messages} />
    </ToastContext.Provider>
  );
}
Example #19
Source File: Transaction.ts    From rocketseat-gostack-11-desafios with MIT License 5 votes vote down vote up
constructor({ title, value, type }: Omit<Transaction, 'id'>) {
    this.id = uuid();
    this.title = title;
    this.value = value;
    this.type = type;
  }
Example #20
Source File: CreateAppointmentService.spec.ts    From hotseat-api with MIT License 4 votes vote down vote up
describe('Create Appointment', () => {
  beforeEach(() => {
    cacheProvider = new FakeCacheProvider();

    appointmentsRepository = new FakeAppointmentsRepository();

    notificationsRepository = new FakeNotificationsRepository();

    createAppointment = new CreateAppointmentService(
      appointmentsRepository,
      notificationsRepository,
      cacheProvider,
    );
  });

  it('should create an appointment', async () => {
    jest
      .spyOn(Date, 'now')
      .mockImplementationOnce(() => new Date(2020, 1, 1, 14, 0, 0).getTime());

    const appointment = await createAppointment.execute({
      provider_id: 'meanless provider id',
      customer_id: 'meanless customer id',
      type: APPOINTMENT_TYPES[2],
      date: new Date(2020, 1, 1, 15, 0, 0),
    });

    expect(appointment).toHaveProperty('id');
  });

  it('should not create two appointments with the same date', async () => {
    jest
      .spyOn(Date, 'now')
      .mockImplementationOnce(() => new Date(2020, 1, 1, 14, 0, 0).getTime());

    const appointmentData = Object.assign(new Appointment(), {
      provider_id: 'meanless provider id',
      customer_id: 'meanless customer id',
      type: APPOINTMENT_TYPES[1],
      date: new Date(2020, 1, 1, 15, 0, 0),
    });

    await createAppointment.execute(appointmentData);

    await expect(
      createAppointment.execute(appointmentData),
    ).rejects.toBeInstanceOf(AppError);
  });

  it('should not allow a provider to book an appointment with himself', async () => {
    jest
      .spyOn(Date, 'now')
      .mockImplementationOnce(() => new Date(2020, 1, 1, 14, 0, 0).getTime());

    const providerId = uuid();

    await expect(
      createAppointment.execute({
        provider_id: providerId,
        customer_id: providerId,
        date: new Date(2020, 1, 1, 15, 0, 0),
        type: APPOINTMENT_TYPES[1],
      }),
    ).rejects.toBeInstanceOf(AppError);
  });

  it('should not allow to book an appointment in past dates', async () => {
    jest
      .spyOn(Date, 'now')
      .mockImplementationOnce(() => new Date(2020, 1, 1, 14, 0, 0).getTime());

    await expect(
      createAppointment.execute({
        provider_id: 'meanless provider id',
        customer_id: 'meanless customer id',
        date: new Date(2020, 1, 1, 13, 0, 0),
        type: APPOINTMENT_TYPES[2],
      }),
    ).rejects.toBeInstanceOf(AppError);
  });

  it('should not allow to book an appointment before or after the business hours range', async () => {
    jest
      .spyOn(Date, 'now')
      .mockImplementationOnce(() => new Date(2020, 0, 1, 14, 0, 0).getTime());

    await expect(
      createAppointment.execute({
        customer_id: 'meanless customer id',
        provider_id: 'meanless provider id',
        date: new Date(2020, 1, 1, BUSINESS_START_HOUR - 1, 0, 0),
        type: APPOINTMENT_TYPES[1],
      }),
    ).rejects.toBeInstanceOf(AppError);

    await expect(
      createAppointment.execute({
        customer_id: 'meanless customer id',
        provider_id: 'meanless provider id',
        date: new Date(2020, 1, 1, BUSINESS_LIMIT_HOUR + 1, 0, 0),
        type: APPOINTMENT_TYPES[0],
      }),
    ).rejects.toBeInstanceOf(AppError);
  });

  it('should send a notification to the provider after creating an appointment', async () => {
    jest
      .spyOn(Date, 'now')
      .mockImplementationOnce(() => new Date(2020, 0, 1, 14, 0, 0).getTime());

    const createNotification = jest.spyOn(notificationsRepository, 'create');

    await createAppointment.execute({
      customer_id: 'meanless customer id',
      provider_id: 'meanless provider id',
      date: new Date(2020, 1, 1, BUSINESS_LIMIT_HOUR, 0, 0),
      type: APPOINTMENT_TYPES[2],
    });

    expect(createNotification).toHaveBeenCalled();
  });
});