Skip to content

Commit 97ea1e4

Browse files
committed
Add sweetalert to replace default JS confirm when editing task
1 parent 25dd071 commit 97ea1e4

File tree

3 files changed

+20
-12
lines changed

3 files changed

+20
-12
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
"popper.js": "^1.14.7",
1212
"react": "^16.8.4",
1313
"react-dom": "^16.8.4",
14-
"react-scripts": "2.1.8"
14+
"react-scripts": "2.1.8",
15+
"sweetalert2": "^8.8.7"
1516
},
1617
"scripts": {
1718
"start": "react-scripts start",

src/components/tasks/index.js

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React, { Component } from 'react';
22
import DefaultLayout from '../../layouts/Default';
33
import Task from './task';
44
import TaskAdd from './task/add.js';
5+
import Swal from 'sweetalert2/dist/sweetalert2.js';
56
import './css/index.css';
67

78
class Index extends Component {
@@ -49,20 +50,25 @@ class Index extends Component {
4950
* TODO: Use Bootstrap alerts instead of default JS window.prompt
5051
* @param {number} taskId
5152
*/
52-
editTask(taskId) {
53-
const name = window.prompt('Enter task name');
53+
async editTask(taskId) {
54+
const tasks = this.state.tasks;
5455

55-
/** User cancels prompt */
56-
if(name === null) return;
56+
const {value: taskName} = await Swal.fire({
57+
title: 'Edit task',
58+
input: 'text',
59+
inputValue: tasks[taskId].name,
60+
showCancelButton: true,
61+
inputValidator: (value) => {
62+
if (!value) {
63+
return 'Task name cannot be empty!'
64+
}
65+
}
66+
});
5767

58-
/** User sets name as empty */
59-
if(name.trim() === '') {
60-
alert('Task name cannot be empty');
61-
return;
62-
}
68+
/** User clicks cancel button */
69+
if(typeof(taskName) === 'undefined') return;
6370

64-
const tasks = this.state.tasks;
65-
tasks[taskId].name = name;
71+
tasks[taskId].name = taskName;
6672
this.setTasks(tasks);
6773
}
6874

src/layouts/Default.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React, { Component } from 'react';
22
import 'bootstrap/dist/css/bootstrap.css';
33
import 'bootstrap/dist/js/bootstrap.js';
4+
import 'sweetalert2/dist/sweetalert2.css';
45
import Header from './Header';
56

67
class Default extends Component {

0 commit comments

Comments
 (0)